我给吃豆人的代码让所有的细胞都转换了,所以当我把药片递给吃豆人时,他不吃,只是忽略它们,改变位置。 我希望药片在他穿过药片时消失,制作一个计数器并将分数显示在屏幕上,但我不知道如何在药片切换位置时做到这一点
import pygame
import random
from pygame.constants import *
pygame.init()
tela = pygame.display.set_mode((1000, 560))
class Tabuleiro:
def __init__(self, tela, x, y):
self.x = x
self.y = y
self.tela = tela
self.maze = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 3, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1],
[1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1],
[1, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
def show(self):
for col in range(20):
for lin in range(11):
if self.maze[lin][col] == 0:
self.tela.fill((255, 255, 255), rect=[self.x + col * 50 + 15, self.y + lin * 50 + 15, 7, 7])
if self.maze[lin][col] == 1:
self.tela.fill((0, 117, 176), rect=[self.x + col * 50, self.y + lin * 50, 50, 50])
if self.maze[lin][col] == 2:
self.tela.fill((255, 255, 0), rect=[self.x + col * 50 + 12, self.y + lin * 50 + 12, 25, 25])
if self.maze[lin][col] == 3:
self.tela.fill((255, 0, 0), rect=[self.x + col * 50 + 12, self.y + lin * 50 + 12, 25, 25])
def enumerar(self, number):
for row, rowlist in enumerate(self.maze):
for col, cell in enumerate(rowlist):
if cell == number:
return row, col
return None, None
def validar_col(self, row, col):
if row == None or col == None:
return False
if 0 <= row < len(self.maze) and 0 <= col < len(self.maze[row]):
return True
return False
def numero(self, row, col, number):
if self.validar_col(row, col):
return self.maze[row][col] == number
return False
def trocar_celulas(self, row1, col1, row2, col2):
if self.validar_col(row1, col1) and self.validar_col(row2, col2):
self.maze[row1][col1], self.maze[row2][col2] = self.maze[row2][col2], self.maze[row1][col1]
maze = Tabuleiro(tela, 10, 10)
clock = pygame.time.Clock()
next_move_time = 0
jogo = True
while jogo:
row, col = maze.enumerar(2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
jogo = False
if pygame.key.get_pressed()[K_LEFT]:
if maze.numero(row, col - 1, 0):
maze.trocar_celulas(row, col - 1, row, col)
if pygame.key.get_pressed()[K_RIGHT]:
if maze.numero(row, col + 1, 0):
maze.trocar_celulas(row, col + 1, row, col)
if pygame.key.get_pressed()[K_UP]:
if maze.numero(row - 1, col, 0):
maze.trocar_celulas(row, col, row - 1, col)
if pygame.key.get_pressed()[K_DOWN]:
if maze.numero(row + 1, col, 0):
maze.trocar_celulas(row + 1, col, row, col)
tela.fill(0)
clock.tick(7)
maze.show()
pygame.display.update()
我又卡住了,谢谢你
必须为“空”字段定义一个数字。在下面的例子中,我使用-1表示玩家访问的字段
更改方法
trocar_celulas
。将0字段替换为-1您必须允许玩家移动到0和-1字段:
完整示例:
相关问题 更多 >
编程相关推荐