Find the last matrix position

Asked

Viewed 85 times

0

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

It has to be according to the size of the matrix, that is, the last column and the first column of the matrix has to collide with the edges of the map.

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 for drive which is the movx and movy, only that I want to move according to the first and last column of the matrix.

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

1 answer

0


Currently your maximum right drive number is fixed at 280; you need to calculate this value dynamically using the right-most enemy as the base.

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

if movx == 280:

for

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.