我如何让我的 python 代码从我的 pac man 游戏中删除药丸? [英] How I make my python code delete the pills from my pac man game?

查看:67
本文介绍了我如何让我的 python 代码从我的 pac man 游戏中删除药丸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My code for Pac-Man make all the cells switch, so when I pass Pac-Man through the pills, he doesn't eat it, just ignore them and change position. I want the pills to disappear when he pass through them, to make a counter and put the score on the screen, but I don't know how to do it as they switch positions.

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 作为玩家访问的字段.

You have to define a number for the "empty" fields. In the following I use -1 for the fields visited by the player.

更改方法trocar_celulas.用 -1 替换为 0 的字段

Change the method trocar_celulas. Replace a field that is 0 with -1

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 的字段:

You must allow the player to move to fields that are 0 and -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)


完整示例:


Complete example:

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 游戏中删除药丸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆