-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanes.py
344 lines (298 loc) · 11.6 KB
/
lanes.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
Lanes module for Froggo
Author: Lucy Beck
Date: January 2, 2021
"""
from kivy.graphics import *
from models import *
from constants import *
from PIL import Image
import os
class Lane(object):
"""
Parent class for a lane.
"""
# Attribute _width: the width of the window to animate in
# Invariant: _width is a float > 0
#
# Attribute _tiles: a list of tiles for each lane
# Invariant: _tile is a list of kivy.graphics Rectangles
#
# Attribute _objs: a list of all of the objects in the lanes
# Invariant: _objs is a list of kivy.graphics Rectangles
#
# Attribute _speed: the speed of the objects in the lanes
# Invariant: _speed is a number (int or float)
#
# Attribute _buffer: how far (in grid squares) that any image must be
# offscreen before it is time to wrap it back around
# Invariant: _buffer is an int
#
# Attribute _animator: A coroutine for performing an animation
# Invariant: _animator is a generator-based coroutine (or None)
#
def __init__(self,width,leveldict,imagespath,pos):
"""
Initializes the lanes.
Parameter width: The width of the window to animate in
Precondition : width is a number (int or float) > 0
Parameter leveldict: A dictionary containing level information
Precondition: leveldict is a dictionary
Parameter imagespath: The path to the Images folder
Precondition: imagespath is a valid path
Parameter pos: the position in the leveldict['lanes'] list
Precondition: pos is an int
"""
self._width = width
dict = leveldict['lanes'][pos]
image = dict['type'] + '.png'
self._tiles = []
for col in range(leveldict['size'][0]):
x = col*GRID_SIZE
y = pos*GRID_SIZE
tile = Rectangle(source=image, size=(GRID_SIZE, GRID_SIZE), pos=(x,y))
self._tiles.append(tile)
self._objs = []
if 'objects' in dict:
for dict2 in dict['objects']:
x = dict2['position']*GRID_SIZE
y = pos*GRID_SIZE
if dict2['type'] == 'turtle_east':
obstacle = Turtle('east',x,y)
elif dict2['type'] == 'turtle_west':
obstacle = Turtle('west',x,y)
else:
image = dict2['type'] + '.png'
im = Image.open(os.path.join(imagespath,image))
multiplier = GRID_SIZE / im.size[1]
width = im.size[0] * multiplier
if 'speed' in dict and dict['speed']<0:
rotated = im.rotate(180)
rotated.save(imagespath+'/temp'+image)
image = 'temp'+image
obstacle = Rectangle(source=image, size=(width,GRID_SIZE), pos=(x,y))
self._objs.append(obstacle)
if 'speed' in dict:
self._speed = dict['speed']
else:
self._speed = None
self._buffer = leveldict['offscreen']
self._animator = None
def update(self,dt):
"""
Updates the game objects each frame.
Parameter dt: The time in seconds since last update
Precondition: dt is a number (int or float)
"""
for obj in self._objs:
if isinstance(obj,Turtle):
obj.x += dt*self._speed
if self._speed > 0 and obj.x > self._width:
d = obj.x - (self._width + self._buffer*2*GRID_SIZE)
obj.x = -self._buffer*GRID_SIZE + d
elif self._speed < 0 and obj.x < -self._buffer*GRID_SIZE:
d = obj.x - (-self._buffer*GRID_SIZE)
obj.x = self._width + self._buffer*GRID_SIZE + d
obj.update(dt)
else:
obj.pos = (obj.pos[0]+dt*self._speed, obj.pos[1])
if self._speed > 0 and obj.pos[0] > self._width:
d = obj.pos[0] - (self._width+(self._buffer*2)*GRID_SIZE)
obj.pos = (-self._buffer*GRID_SIZE + d, obj.pos[1])
elif self._speed < 0 and obj.pos[0] < -self._buffer*GRID_SIZE:
d = obj.pos[0] - (-self._buffer*GRID_SIZE)
obj.pos = (self._width+self._buffer*GRID_SIZE + d, obj.pos[1])
def draw(self,canvas):
"""
Draws the game objects to the canvas.
Parameter canvas: The root object used for drawing by a Widget
Precondition: canvas is a root object used for drawing by a Widget
"""
for tile in self._tiles:
canvas.add(tile)
for obj in self._objs:
if isinstance(obj,Turtle):
obj.draw(canvas)
else:
canvas.add(obj)
def collides(self,obj1,obj2):
"""
Returns True if obj1 and obj2 collide and False otherwise
Parameter obj1: The first object
Precondition: obj1 is a kivy graphics rectangle
Parameter obj2: The second object
Precondition: obj2 is a tuple in the form of ((x,y),(width,height),(hitbox))
"""
obj1x = obj1.pos[0]
obj1y = obj1.pos[1]
obj1w = obj1.size[0]
obj1h = obj1.size[1]
obj2x = obj2[0][0]
obj2y = obj2[0][1]
obj2w = obj2[1][0]
obj2h = obj2[1][1]
left = obj2[2][0]
top = obj2[2][1]
right = obj2[2][2]
bottom = obj2[2][3]
return (obj1x < obj2x+obj2w-right) and (obj1x+obj1w > obj2x+left) and \
(obj1y < obj2y+obj2h-top) and (obj1y+obj1h > obj2y+bottom)
def contains(self,obj1,obj2):
"""
Returns True if obj1 contains the center of obj2 and False otherwise
Parameter obj1: The first object
Precondition: obj1 is a kivy graphics rectangle or Turtle
Parameter obj2: The second object
Precondition: obj2 is a tuple in the form of ((x,y),(width,height),(hitbox))
"""
if isinstance(obj1,Turtle):
obj1x = obj1.x
obj1y = obj1.y
obj1w = obj1.w
obj1h = obj1.h
else:
obj1x = obj1.pos[0]
obj1y = obj1.pos[1]
obj1w = obj1.size[0]
obj1h = obj1.size[1]
obj2x = obj2[0][0] + GRID_SIZE/2
obj2y = obj2[0][1] + GRID_SIZE/2
return (obj2x < obj1x+obj1w) and (obj2x > obj1x) and \
(obj2y < obj1y+obj1h) and (obj2y > obj1y)
class Grass(Lane):
"""
A class representing a 'safe' grass area.
"""
pass
class Road(Lane):
"""
A class representing a roadway with cars.
"""
def roadCollision(self,frog):
"""
Returns True if the frog collides with a hedge.
Parameter frog: the frog
Precondition: frog is a Frog object
"""
tuple = ((frog.x,frog.y),(frog.w,frog.h),(frog.hitbox))
for obj in self._objs:
if self.collides(obj,tuple):
return True
class Water(Lane):
"""
A class representing a waterway with logs.
"""
def logContains(self,frog,dt):
"""
Returns True if the log contains the frog.
Parameter frog: the frog
Precondition: frog is a Frog object
Parameter dt: The time in seconds since last update
Precondition: dt is a number (int or float)
"""
tuple = ((frog.x,frog.y),(frog.w,frog.h),(frog.hitbox))
for obj in self._objs:
if isinstance(obj,Turtle):
if self.contains(obj,tuple) and obj.frame < 8:
frog.x += dt*self._speed
return True
elif self.contains(obj,tuple):
frog.x += dt*self._speed
return True
def waterCollision(self,frog):
"""
Returns True if the frog collides with water.
Parameter frog: the frog
Precondition: frog is a Frog object
"""
tuple = ((frog.x,frog.y),(frog.w,frog.h),(frog.hitbox))
for tile in self._tiles:
if self.collides(tile,tuple):
return True
def flyCollision(self,frog):
"""
Returns True if the frog collides with a fly.
Parameter frog: the frog
Precondition: frog is a Frog object
"""
tuple = ((frog.x,frog.y),(frog.w,frog.h),(frog.hitbox))
for obj in self._objs:
if not isinstance(obj,Turtle):
pos = obj.source.find('Images')
source = obj.source[pos+7:]
if source == 'fly.png' and self.collides(obj,tuple):
self._objs.remove(obj)
return True
class Hedge(Lane):
"""
A class representing the exit hedge.
"""
def getNumExits(self):
"""
Returns the number of exits in the lane.
"""
numExits = 0
for obj in self._objs:
pos = obj.source.find('Images')
source = obj.source[pos+7:]
if source == 'exit.png':
numExits += 1
return numExits
def hedgeCollision(self,frog,safeFrogs):
"""
Returns True if the frog collides with a hedge or safe frog and False
otherwise.
Parameter frog: the frog
Precondition: frog is a Frog object
Parameter safeFrogs: the safe frogs
Precondition: safeFrogs is a list of of kivy.graphics Rectangles
"""
tuple = tuple = ((frog.x,frog.y+GRID_SIZE),(frog.w,frog.h),(frog.hitbox))
for safeFrog in safeFrogs:
if self.collides(safeFrog,tuple):
return True
for obj in self._objs:
pos = obj.source.find('Images')
source = obj.source[pos+7:]
if source == 'exit.png' or source == 'open.png':
if self.contains(obj,tuple):
return False
for tile in self._tiles:
if self.collides(tile,tuple):
return True
def frogSafe(self,frog):
"""
Returns True if the frog is safe.
Parameter frog: the frog
Precondition: frog is a Frog object
"""
tuple = ((frog.x,frog.y),(frog.w,frog.h),(frog.hitbox))
for obj in self._objs:
pos = obj.source.find('Images')
source = obj.source[pos+7:]
if source == 'exit.png' and self.contains(obj,tuple):
frog.x = obj.pos[0]
frog.y = obj.pos[1]
return True
def enterFromNorth(self,frog):
"""
Returns True if the frog enters hedge from North.
Parameter frog: the frog
Precondition: frog is a Frog object
"""
tuple = ((frog.x,frog.y-GRID_SIZE),(frog.w,frog.h),(frog.hitbox))
for tile in self._tiles:
if self.collides(tile,tuple):
return True
def enterFromSide(self,frog):
"""
Returns True if the frog enters hedge from the East or West.
Parameter frog: the frog
Precondition: frog is a Frog object
"""
tuple = ((frog.x,frog.y),(frog.w,frog.h),(frog.hitbox))
for obj in self._objs:
pos = obj.source.find('Images')
source = obj.source[pos+7:]
if source == 'open.png' and self.contains(obj,tuple):
return True