-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghostScroll.py
107 lines (100 loc) · 3.55 KB
/
ghostScroll.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from cmu_112_graphics import *
from pacman import *
# animation framework attained from
# http://www.cs.cmu.edu/~112/notes/notes-animations-part1.html
# Ghost class defines characteristics of ghosts, draws ghosts,
# and defines movement
# specifically for sidescroll to avoid bugs with Pac-Man and ghosts not being
# on the same screen
class Ghost(object):
def __init__(self):
self.speed=5
self.x,self.y=0,0
self.radius=10
self.color=None
self.dx,self.dy=0,-1*self.speed
self.currDirection="down"
self.direction=self.currDirection
self.legalDirections=["up","right","down","left"]
self.distances=set()
def dirSetter(self):
if self.currDirection=="right":
self.speedSetter(self.speed,0)
elif self.currDirection=="left":
self.speedSetter(-1*self.speed,0)
elif self.currDirection=="up":
self.speedSetter(0,-1*self.speed)
elif self.currDirection=="down":
self.speedSetter(0,self.speed)
def speedSetter(self,dx,dy):
self.dx=dx
self.dy=dy
def shortestGhost(self,ghosts):
for ghost in ghosts:
distance=((self.x-ghost.x)**2+(self.y-ghost.y)**2)**0.5
self.distances.add(distance)
minDist=min(self.distances)
for ghost in ghosts:
if ((self.x-ghost.x)**2+(self.y-ghost.y)**2)**0.5==minDist:
return (ghost.x,ghost.y)
def moveGhost(self,ghosts,a,b): #x,y are Ghost's positions
x,y=self.shortestGhost(ghosts)
if x==self.x and y==self.y:
x,y=a,b
if self.currDirection in self.legalDirections:
if y<self.y:
if x<self.x:
if self.x-x<self.y-y:
self.currDirection="up"
else:
self.currDirection="left"
else:
if x-self.x<self.y-y:
self.currDirection="up"
else:
self.currDirection="right"
else:
if x<self.x:
if self.x-x<y-self.y:
self.currDirection="down"
else:
self.currDirection="left"
else:
if x-self.x<y-self.y:
self.currDirection="down"
else:
self.currDirection="right"
else:
if self.currDirection=="left" or self.currDirection=="right":
if y<self.y:
self.currDirection="up"
else:
self.currDirection="down"
elif self.currDirection=="up" or self.currDirection=="down":
if x<self.x:
self.currDirection="left"
else:
self.currDirection="right"
self.dirSetter()
if self.currDirection in self.legalDirections:
self.x+=self.dx
self.y+=self.dy
def drawGhost(self,canvas):
canvas.create_oval(self.x-self.radius,self.y-self.radius,\
self.x+self.radius,self.y+self.radius,fill=self.color)
class Inky(Ghost):
def __init__(self):
super().__init__()
self.color="aqua"
class Pinky(Ghost):
def __init__(self):
super().__init__()
self.color="pink"
class Blinky(Ghost):
def __init__(self):
super().__init__()
self.color="red"
class Clyde(Ghost):
def __init__(self):
super().__init__()
self.color="orange"