如何让我的 python 代码从我的 pac man 游戏中删除药丸?

Posted

技术标签:

【中文标题】如何让我的 python 代码从我的 pac man 游戏中删除药丸?【英文标题】:How I make my python code delete the pills from my pac man game? 【发布时间】:2021-03-19 21:51:06 【问题描述】:

我的 Pac-Man 代码使所有细胞切换,所以当我通过 Pac-Man 药丸时,他不吃药,只是忽略它们并改变位置。 我希望药丸在他通过时消失,做一个计数器并将分数显示在屏幕上,但我不知道该怎么做,因为它们会切换位置。

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】:

您必须为“空”字段定义一个数字。下面我将 -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 代码从我的 pac man 游戏中删除药丸?的主要内容,如果未能解决你的问题,请参考以下文章

如何让我的列表读取某些值?

如何从我的 Flask python 应用程序查询数据库?

让我的 discord.js 机器人从我的计算机中的一组图像中发送一张随机图片

如何从我的应用程序中打开网页?

如何从我的 Facebook 应用程序访问我的客户帐户?

如何让我的Python密码破解器更高效的运行?