Skip to content Skip to sidebar Skip to footer

How I Make My Python Code Delete The Pills From My Pac Man Game?

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

Solution 1:

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

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]

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()

Post a Comment for "How I Make My Python Code Delete The Pills From My Pac Man Game?"