-
Notifications
You must be signed in to change notification settings - Fork 1
/
zombie.py
240 lines (190 loc) · 8.01 KB
/
zombie.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import random
import abc
from PIL import Image, ImageTk
""" Contains class implementations for Zombie Types"""
class Zombie():
__inplay = True
def __init__(self, canvas):
self.x = 0
self.y = 0
self.canvas = canvas
self.width = int(self.canvas.cget("width"))
self.height = int(self.canvas.cget("height"))
self.framecounter = 0
@abc.abstractmethod
def draw(self):
"""draw each type of zombie slightly differently"""
pass
@abc.abstractmethod
def movement(self):
"""implements different movement patterns for different zombie types"""
pass
def getCanvas():
return self.canvas
@staticmethod
def getInPlay():
return Zombie.__inplay
@staticmethod
def setInPlay(inplay):
# this needs to change inplay for all instances
Zombie.__inplay = inplay
class BasicZombie(Zombie):
def __init__(self):
self.max_speed = 5
def draw(self):
# spawn in random positon, adjust size slightly
self.circle = self.canvas.create_oval(
250, 250, 200, 200, fill = "gray")
def movement(self):
# needs to follow user
pass
class LargeZombie(Zombie):
def __init__(self, canvas):
super(LargeZombie, self).__init__(canvas)
self.max_speed = 1
self.xbias = random.randint(-1, 1)
self.ybias = random.randint(-1, 1)
self.resetBias = False
#load sprite images
self.PILimg1 = Image.open("media/zombie1.png")
self.PILimg1 = self.PILimg1.resize((64, 84))
self.PILimg2 = Image.open("media/zombie2.png")
self.PILimg2 = self.PILimg2.resize((84, 104))
self.PILimg3 = Image.open("media/zombie3.png")
self.PILimg3 = self.PILimg3.resize((84, 104))
def draw(self):
# spawn in random position??
# self.circle = self.canvas.create_oval(
# 250, 250, 200, 200, fill = "brown")
#picking zombie sprite
num = random.randint(1,3)
if num == 1:
self.tkimg = ImageTk.PhotoImage(self.PILimg1)
elif num == 2:
self.tkimg = ImageTk.PhotoImage(self.PILimg2)
else:
self.tkimg = ImageTk.PhotoImage(self.PILimg3)
self.img = self.canvas.create_image((random.randint(45,325),random.randint(45,425)),image=self.tkimg)
def movement(self):
if Zombie.getInPlay():
self.x = random.randint(-1*self.max_speed, self.max_speed)
self.y = random.randint(-1*self.max_speed, self.max_speed)
if random.randint(1,4) < 4: # adding movement bias to make zombies better
self.x = self.xbias * self.max_speed
self.y = self.ybias * self.max_speed
#print( "self x: " + str(self.x) + " self y: " + str(self.y))
coords = self.canvas.coords(self.img)
if coords[0] < 10 and self.x < 0:
self.x = 0
self.resetBias = True
elif coords[0] > (self.width - 30) and self.x > 0:
self.x = 0
self.resetBias = True
if coords[1] < 10 and self.y < 0:
self.y =0
self.resetBias = True
elif coords[1] > (self.height - 30) and self.y > 0:
self.y = 0
self.resetBias = True
if self.resetBias:
self.xbias = random.randint(-1, 1)
self.ybias = random.randint(-1, 1)
self.resetBias = False
print("resetting bias")
# either return x and y or pass in canvas during init
self.canvas.move(self.img, self.x, self.y)
# new random input
self.canvas.after(100, self.movement)
class RunningZombie(Zombie):
def __init__(self, canvas, hero):
super(RunningZombie, self).__init__(canvas)
self.max_speed = 7
self.player = hero
#load sprite images
self.PILimg1 = Image.open("media/angry_zombie.png")
self.PILimg1 = self.PILimg1.resize((32, 42))
self.PILimg2 = Image.open("media/angry_zombie.png")
self.PILimg2 = self.PILimg2.resize((32, 42))
self.PILimg3 = Image.open("media/angry_zombie.png")
self.PILimg3 = self.PILimg3.resize((32, 42))
def draw(self):
#picking zombie sprite
num = random.randint(1,3)
if num == 1:
self.tkimg = ImageTk.PhotoImage(self.PILimg1)
elif num == 2:
self.tkimg = ImageTk.PhotoImage(self.PILimg2)
else:
self.tkimg = ImageTk.PhotoImage(self.PILimg3)
self.img = self.canvas.create_image((random.randint(45,325),random.randint(45,425)),image=self.tkimg)
def movement(self):
# needs to follow user positions
if Zombie.getInPlay():
# determine x and y based on player position
player_pos = self.canvas.coords(self.player.getSprite())
zombie_pos = self.canvas.coords(self.img)
self.x = (zombie_pos[0] - player_pos[0])*-1
self.y = (zombie_pos[1] - player_pos[1])*-1
if (abs(self.x) > self.max_speed):
if(self.x > 0): self.x = self.max_speed
elif(self.x < 0): self.x = self.max_speed*-1
if (abs(self.y) > self.max_speed):
if(self.y > 0): self.y = self.max_speed
elif(self.y < 0): self.y = self.max_speed*-1
# then move
self.canvas.move(self.img, self.x, self.y)
# new random input
self.canvas.after(100, self.movement)
class RandomZombie(Zombie):
def __init__(self, canvas):
super(RandomZombie, self).__init__(canvas)
self.max_speed = 5
self.xbias = random.randint(-1, 1)
self.ybias = random.randint(-1, 1)
self.resetBias = False
#load sprite images
self.PILimg1 = Image.open("media/zombie1.png")
self.PILimg1 = self.PILimg1.resize((32, 42))
self.PILimg2 = Image.open("media/zombie2.png")
self.PILimg2 = self.PILimg2.resize((32, 42))
self.PILimg3 = Image.open("media/zombie3.png")
self.PILimg3 = self.PILimg3.resize((32, 42))
def draw(self):
#picking zombie sprite
num = random.randint(1,3)
if num == 1:
self.tkimg = ImageTk.PhotoImage(self.PILimg1)
elif num == 2:
self.tkimg = ImageTk.PhotoImage(self.PILimg2)
else:
self.tkimg = ImageTk.PhotoImage(self.PILimg3)
self.img = self.canvas.create_image((random.randint(45,325),random.randint(45,425)),image=self.tkimg)
def movement(self):
if Zombie.getInPlay():
self.x = random.randint(-1*self.max_speed, self.max_speed)
self.y = random.randint(-1*self.max_speed, self.max_speed)
if random.randint(1,4) < 4: # adding movement bias to make zombies better
self.x = self.xbias * self.max_speed
self.y = self.ybias * self.max_speed
coords = self.canvas.coords(self.img)
if coords[0] < 10 and self.x < 0:
self.x = 0
self.resetBias = True
elif coords[0] > (self.width - 30) and self.x > 0:
self.x = 0
self.resetBias = True
if coords[1] < 10 and self.y < 0:
self.y =0
self.resetBias = True
elif coords[1] > (self.height - 30) and self.y > 0:
self.y = 0
self.resetBias = True
if self.resetBias:
self.xbias = random.randint(-1, 1)
self.ybias = random.randint(-1, 1)
self.resetBias = False
# either return x and y or pass in canvas during init
# self.canvas.move(self.circle, self.x, self.y)
self.canvas.move(self.img, self.x, self.y)
# new random input
self.canvas.after(100, self.movement)