gpt4 book ai didi

python - 我如何让我的 python 代码从我的吃 bean 人游戏中删除药丸?

转载 作者:行者123 更新时间:2023-12-04 15:11:00 26 4
gpt4 key购买 nike

我的吃 bean 人代码让所有的细胞都切换,所以当我把药丸递给吃 bean 人时,他不会吃药,只是忽略它们并改变位置。我希望药丸在他经过时消失,进行计数并将分数显示在屏幕上,但当它们交换位置时我不知道该怎么做。

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。用 -1 替换为 0 的字段

class Tabuleiro:
# [...]

def trocar_celulas(self, row1, col1, row2, col2):
if self.validar_col(row1, col1) and self.validar_col(row2, col2):
if self.maze[row1][col1] == 0:
self.maze[row1][col1] = -1
if self.maze[row2][col2] == 0:
self.maze[row2][col2] = -1
self.maze[row1][col1], self.maze[row2][col2] = self.maze[row2][col2], self.maze[row1][col1]

您必须允许玩家移动到 0 和 -1 的字段:

jogo = True
while jogo:
# [...]

if pygame.key.get_pressed()[K_LEFT]:
if maze.numero(row, col - 1, 0) or maze.numero(row, col - 1, -1):
maze.trocar_celulas(row, col - 1, row, col)
if pygame.key.get_pressed()[K_RIGHT]:
if maze.numero(row, col + 1, 0) or maze.numero(row, col + 1, -1):
maze.trocar_celulas(row, col + 1, row, col)
if pygame.key.get_pressed()[K_UP]:
if maze.numero(row - 1, col, 0) or maze.numero(row - 1, col, -1):
maze.trocar_celulas(row - 1, col, row, col)
if pygame.key.get_pressed()[K_DOWN]:
if maze.numero(row + 1, col, 0) or maze.numero(row + 1, col, -1):
maze.trocar_celulas(row + 1, col, row, col)

完整示例:

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):
if self.maze[row1][col1] == 0:
self.maze[row1][col1] = -1
if self.maze[row2][col2] == 0:
self.maze[row2][col2] = -1
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) or maze.numero(row, col - 1, -1):
maze.trocar_celulas(row, col - 1, row, col)
if pygame.key.get_pressed()[K_RIGHT]:
if maze.numero(row, col + 1, 0) or maze.numero(row, col + 1, -1):
maze.trocar_celulas(row, col + 1, row, col)
if pygame.key.get_pressed()[K_UP]:
if maze.numero(row - 1, col, 0) or maze.numero(row - 1, col, -1):
maze.trocar_celulas(row - 1, col, row, col)
if pygame.key.get_pressed()[K_DOWN]:
if maze.numero(row + 1, col, 0) or maze.numero(row + 1, col, -1):
maze.trocar_celulas(row + 1, col, row, col)


tela.fill(0)
clock.tick(7)
maze.show()
pygame.display.update()

关于python - 我如何让我的 python 代码从我的吃 bean 人游戏中删除药丸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65201227/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com