-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacman.py
89 lines (76 loc) · 2.58 KB
/
pacman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pygame
from pygame.locals import *
from vector import Vector2
from constants import *
from entity import Entity
from sprites import PacmanSprites
import time
class Pacman(Entity):
def __init__(self, node):
Entity.__init__(self, node)
self.name = PACMAN
self.color = YELLOW
self.direction = LEFT
self.setBetweenNodes(LEFT)
self.alive = True
self.sprites = PacmanSprites(self)
self.timer = 0
def reset(self):
Entity.reset(self)
self.direction = LEFT
self.setBetweenNodes(LEFT)
self.alive = True
self.image = self.sprites.getStartImage()
self.sprites.reset()
def die(self):
self.alive = False
self.direction = STOP
def update(self, dt):
self.sprites.update(dt)
# print("updating timer")
self.timer += 1
if self.timer >= 120:
super().updateSpeedFromBrainSignals()
self.timer = 0
self.position += self.directions[self.direction] * self.speed * dt
direction = self.getValidKey()
if self.overshotTarget():
self.node = self.target
if self.node.neighbors[PORTAL] is not None:
self.node = self.node.neighbors[PORTAL]
self.target = self.getNewTarget(direction)
if self.target is not self.node:
self.direction = direction
else:
self.target = self.getNewTarget(self.direction)
if self.target is self.node:
self.direction = STOP
self.setPosition()
else:
if self.oppositeDirection(direction):
self.reverseDirection()
def getValidKey(self):
key_pressed = pygame.key.get_pressed()
if key_pressed[K_UP]:
return UP
if key_pressed[K_DOWN]:
return DOWN
if key_pressed[K_LEFT]:
return LEFT
if key_pressed[K_RIGHT]:
return RIGHT
return STOP
def eatPellets(self, pelletList):
for pellet in pelletList:
if self.collideCheck(pellet):
return pellet
return None
def collideGhost(self, ghost):
return self.collideCheck(ghost)
def collideCheck(self, other):
d = self.position - other.position
dSquared = d.magnitudeSquared()
rSquared = (self.collideRadius + other.collideRadius) ** 2
if dSquared <= rSquared:
return True
return False