-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghost.py
More file actions
48 lines (44 loc) · 1.7 KB
/
ghost.py
File metadata and controls
48 lines (44 loc) · 1.7 KB
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
import pygame
RED = (255, 0, 0)
class ghost:
def __init__(self, x, y, speed, radius, walls, screen, cell_size, gw, gh):
self.x = x
self.y = y
self.xdir = 0
self.ydir = 0
self.modes = ['scatter', 'chase', 'frightened']
self.color = RED
self.speed = speed
self.radius = radius
self.walls = walls
self.screen = screen
self.cell_size = cell_size
self.gw = gw
self.gh = gh
self.cx = (self.x * cell_size) + radius
self.cy = (self.y * cell_size) + radius
self.txdir = 0
self.tydir = 0
self.inmotion = False
def update(self):
if self.inmotion:
self.cx += self.txdir * self.speed
self.cy += self.tydir * self.speed
pygame.draw.circle(self.screen, RED, (self.cx, self.cy), self.radius)
# checking if motion from one square to other is over
if (self.cx == ((self.x + self.txdir) * self.cell_size) + self.radius) and (
self.cy == ((self.y + self.tydir) * self.cell_size) + self.radius):
self.x += self.txdir
self.y += self.tydir
# wrapping around grid edges
self.x %= self.gw
self.y %= self.gh
self.cx = (self.x * self.cell_size) + self.radius
self.cy = (self.y * self.cell_size) + self.radius
if (self.xdir != 0) or (self.ydir != 0):
self.txdir = self.xdir
self.tydir = self.ydir
else:
self.inmotion = False
else:
pygame.draw.circle(self.screen, RED, (self.cx, self.cy), self.radius)