Find the last position of the matrix

0

I'm making a game in pygame and I need to know the last position of the array for the movement of enemies.

It has to be according to the size of the array, ie the last column and the first column of the array has to collide with the map edges.

This is my code:

import pygame
from pygame.locals import *
from sys import exit
from random import randint
#define os parametros do canhao
def Canhao(C, x, y):
  R = []
  for i in range(len(C)):
    a = C[i][0] + x
    b = C[i][1] + y
    R.append((a, b))
  return(tuple(R))
#variaveis
T = []
C = ((0,15), (4,5), (12,5), (15,0), (18,5), (26,5), (30,15))
XC = 460
YC = 400
CorFundo = (0, 0, 0)
lar = 30
esp = 5
alt = 30
xp = 330
yp = 380
movx, movy = 5,5
direcao = 1
y = 0
#criando a matriz
j = 0
while j < 5:
  i = 0
  while i < 12:
    o = (10+i*(lar+esp), 10+j*(alt+esp), (0, 180-j*20, 255-j*20), (0, 0, 127))
    T.append(o)
    i = i + 1
  j = j + 1
TiroAtivo = False
#iniciando
pygame.init()
pygame.font.init()
tela = pygame.display.set_mode((700, 500))
clock = pygame.time.Clock()
pygame.display.set_caption('Space invaders')
#loop infinito
fim = False
while not fim:
  pygame.display.flip()
  tela.fill((0,0,0))
#movimentacao## usar o len(T) para testar se a movimentacao chegou no fim da tela
  if direcao == 1:
      movx += 5
  if movx == 280:
    direcao = 2
    movx -= 5
    movy += 5
  if direcao == 2:
    movx -= 5
  if movx < 0:
    direcao = 1
    movy += 5
    movx += 5
#desenha o canhao
  pygame.draw.polygon(tela, (50,130,50), Canhao(C, XC, YC), 0)
#desenha os inimigos
  for i in range(len(T)):
    pygame.draw.rect(tela, T[i][2], (movx+T[i][0], movy+T[i][1], lar, alt), 0)
    pygame.draw.rect(tela, T[i][3], (movx+T[i][0], movy+T[i][1], lar, alt), 2) 
#colisao
  for i in range(len(T)):
    if movx+T[i][0] <= xp <= movx+T[i][0]+lar and movy+T[i][1] <= yp <= movy+T[i][1]+alt:
      del(T[i])
      xp = 0
##      yp = 0
      TiroAtivo = False
      break
  if yp < 5:
    TiroAtivo = False
#desenha o tiro
  if TiroAtivo:
    pygame.draw.circle(tela, (127, 90, 90), (xp, int(yp)), 3, 0)
    pygame.draw.circle(tela, (127, 90, 90), (xp, int(yp)+3), 3, 0)
    pygame.draw.circle(tela, (127, 90, 90), (xp, int(yp)+6), 3, 0)
    pygame.draw.circle(tela, (127, 90, 90), (xp, int(yp)+9), 3, 0)
    yp = yp - 10
#movimentacao do canhao
  Teclas = pygame.key.get_pressed()
  if Teclas[K_LEFT]:
    XC = XC - 10
  if Teclas[K_RIGHT]:
    XC = XC + 10
  if Teclas[K_SPACE] and TiroAtivo == False:
    xp = XC + C[3][0]
    yp = 380;
    TiroAtivo = True
#tela de game over
  if len(T) == 0:
    # definindo o texto
    fonte_Arial = pygame.font.SysFont("Arial", 60)
    texto = fonte_Arial.render("Game over!",1,(255,255,255))
    # copiando o texto para a superfície
    tela.blit(texto, [200, 150])
  time_passed = clock.tick(30)
  for event in pygame.event.get():
    if event.type == QUIT:
      fim = True
pygame.display.quit()

I'm using a variable to move that is movx and movy , only I want to move according to the first and last column of the array.

For example, if the last column is destroyed, the penultimate becomes the last and the movement continues to the end of the screen.

    
asked by anonymous 24.10.2018 / 21:21

1 answer

0

Your maximum right-hand drive number is currently set at 280; You need to calculate this value dynamically using the rightmost enemy as the base.

700 is the width of the screen, so change the line

if movx == 280:

by

if movx > (700 - max(inimigo[0] for inimigo in T) - lar):
    
24.10.2018 / 21:41