Question: Please help with the pacman code in python as I need pacman to have an ai code where he plays automatically, collects the dots, runs

Please help with the pacman code in python as I need pacman to have an ai code where he plays automatically, collects the dots, runs away from the ghost, when he gets the pill pacman chases afte the ghost.
Here is the pacman code:
from GameObject import GameObject
from GameDefs import Pos
from GameDefs import SpriteType
from GameDefs import Direction
from GameDefs import globals
class PacMan(GameObject):
def __init__(self, p):
super().__init__(p, SpriteType.PACMAN)
def move(self):
if False:
# Try moving one to the right
newPos = Pos(self.position.x+1, self.position.y)
# check if there is a wall in the direction of the movement
if globals.game.check_position(newPos)== SpriteType.WALL:
return Direction.DOWN
return Direction.RIGHT
Game.py
from GameObject import GameObject
from GameDefs import globals
from GameDefs import SpriteType
from GameDefs import Pos
from Pill import Pill
from PacMan import PacMan
from Ghost import Ghost
class Game:
gameTime =0
addWalls = True
def __init__(self):
# Initialize game elements
self.pacman = PacMan(Pos(15,15))
self.ghost = Ghost(Pos(1,1))
self.pill = Pill(Pos(15,3))
self.score =0
self.grid =[[None for x in range(globals.gameSize)] for y in range(globals.gameSize)]
globals.pacman = self.pacman
globals.ghost = self.ghost
globals.pill = self.pill
for i in range(globals.gameSize):
GameObject(Pos(i,0), SpriteType.WALL)
GameObject(Pos(i, globals.gameSize -1), SpriteType.WALL)
GameObject(Pos(0, i), SpriteType.WALL)
GameObject(Pos(globals.gameSize -1, i), SpriteType.WALL)
if Game.addWalls:
for i in range(globals.gameSize //2-3):
GameObject(Pos(i + globals.gameSize //4+2, globals.gameSize //4), SpriteType.WALL)
GameObject(Pos(i + globals.gameSize //4+2, globals.gameSize *3//4), SpriteType.WALL)
GameObject(Pos(globals.gameSize //4, i + globals.gameSize //4+2), SpriteType.WALL)
GameObject(Pos(globals.gameSize *3//4, i + globals.gameSize //4+2), SpriteType.WALL)
for i in range(globals.gameSize):
for j in range(globals.gameSize):
if GameObject.checkCollisions(Pos(i, j))== SpriteType.EMPTY:
self.grid[i][j]= GameObject(Pos(i, j), SpriteType.DOT)
def update(self):
Game.gameTime +=1
if self.grid[self.pacman.position.x][self.pacman.position.y]!= None:
self.grid[self.pacman.position.x][self.pacman.position.y].hide()
self.grid[self.pacman.position.x][self.pacman.position.y]= None
self.score +=1
# Update game elements
self.pacman.update()
self.ghost.update()
self.pill.update()
if not self.pill.isActive() and self.pacman.checkCollision(self.pill):
self.pill.eaten()
if self.pacman.checkCollision(self.ghost):
if self.pill.isActive():
self.ghost.hide()
self.score +=100
else:
print("Score: "+ str(self.score))
return True
if Game.gameTime >=1000:
return True
return False
@staticmethod
def check_position(p):
for obj in GameObject.gameObjects:
if obj.position.x == p.x and obj.position.y == p.y:
return obj.type
return SpriteType.EMPTY
GameDefs:
from enum import Enum, Flag
import types
globals = types.SimpleNamespace()
globals.gameSize =32
class SpriteType(Enum):
EMPTY =0
PACMAN =1
GHOST =2
PILL =3
WALL =4
DOT =5
class Direction(Flag):
UP =1
RIGHT =2
DOWN =4
LEFT =8
NONE =0
class Pos:
def __init__(self, x, y):
self.x = x
self.y = y
Please help with the pacman code in python as I

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!