Pessoal, tenho uma matriz de dimensão 64x64 que mapea as posições de peixes e de um tubarão( presas e predador). Todas as modificações feitas nesta matriz serão refletidas na tela em tempo de execução. Utilizei o conceito de tilemap pra isso, assim como utilizei com sdl e c, onde tinha uma matriz de inteiros(representando tipos de tile) e uma matriz de rect, só que lá eu não fazia modificações na matriz em tempo de execução. Agora estou utilizando python pygame e fazendo as modificações na matriz(no caso agora matriz de strings), porém as modificações ñ estão aparecendo na tela. Desconfio que está acontecendo algo errado na hora do blit das rects de cada tile(ja tentei com imagens e agora to usando cores) mas ñ funciona. Segue abaixo o código:
import sys, pygame
import random
NUM_CELULAS = 64
AZUL_CLARO = (127,255,212)
AZUL = (0,0,255)
VERDE = (0,255,0)
class Tela(object):
def __init__(self):
#pega tamanho da tela automaticamente
#self.displayInfo = pygame.display.Info()
#self.tela = pygame.display.set_mode((self.displayInfo.current_w,self.displayInfo.current_h))
self.larguraTela = 768
self.alturaTela = 640
self.tela = pygame.display.set_mode((self.larguraTela,self.alturaTela))
#calcula largura e altura ideal da rect do peixe e do tubarao conforme dimensao da tela
self.larguraRect = self.larguraTela//NUM_CELULAS
self.alturaRect = self.alturaTela//NUM_CELULAS
print("larg x altura")
print(self.larguraRect)
print(self.alturaRect)
pygame.display.set_caption('Presa-predador')
def blitarJogo(self,matrizLogica):
for i in range(NUM_CELULAS):
for j in range(NUM_CELULAS):
if matrizLogica[i][j] == '0':
pygame.draw.rect(self.tela,AZUL_CLARO,(j*self.larguraRect,i*self.alturaRect,self.larguraRect,self.alturaRect))
elif matrizLogica[i][j] == '1':
pygame.draw.rect(self.tela,VERDE,(j*self.larguraRect,i*self.alturaRect,self.larguraRect,self.alturaRect))
elif matrizLogica[i][j] == '2':
pygame.draw.rect(self.tela,AZUL,(j*self.larguraRect,i*self.alturaRect,self.larguraRect,self.alturaRect))
pygame.display.update()
class Game(object):
def moverAgentes(self,matrizLogica):
for i in range(NUM_CELULAS):
for j in range(NUM_CELULAS):
if matrizLogica[i][j] == '1' or matrizLogica[i][j] == '2':
eixo = random.choice('xy')
sit = random.choice('+-')
print("entrei")
if eixo == 'x' and sit == '+':
if j < NUM_CELULAS - 1:
if matrizLogica[i][j+1] == 0 :
matrizLogica[i][j+1] = matrizLogica[i][j]
matrizLogica[i][j] = 0
elif eixo == 'x' and sit == '-':
if j > 0:
if matrizLogica[i][j-1] == 0 :
matrizLogica[i][j-1] = matrizLogica[i][j]
matrizLogica[i][j] = 0
elif eixo == 'y' and sit == '+':
if i < NUM_CELULAS - 1:
if matrizLogica[i+1][j] == 0 :
matrizLogica[i+1][j] = matrizLogica[i][j]
matrizLogica[i][j] = 0
elif eixo == 'y' and sit == '-':
if i > 0:
if matrizLogica[i-1][j] == 0 :
matrizLogica[i-1][j] = matrizLogica[i][j]
matrizLogica[i][j] = 0
def main():
#inicilizando pygame
pygame.init()
#criando tela
t1 = Tela()
g1 = Game()
#matriz logica
arquivo = open('mapa2.txt','r')
conteudoArquivo = arquivo.read()
linhas = conteudoArquivo.split('\n')
matrizLogica = []
for linha in linhas:
splitEspaco = linha.split(' ')
matrizLogica.append(splitEspaco)
loop = 1
while(loop):
for evento in pygame.event.get():
if evento.type == pygame.QUIT:
loop = 0
if evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_s:
print("apertou s")
g1.moverAgentes(matrizLogica)
t1.blitarJogo(matrizLogica)
pygame.display.update()
pygame.quit()
quit()
if name == ‘main’:
main()
