-
Notifications
You must be signed in to change notification settings - Fork 2
/
AI.py
422 lines (339 loc) · 15 KB
/
AI.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import random, copy, os, pygame, sys, tiledtmxloader
from pygame.locals import *
DEGREES_IN_CIRCLE = 360
class Obstacle:
'''
In order to initialize the AI object you must give in the
position the AI will start in, size of the AI's image,
the image that represents the AI, and the damage that
the obstacle is capable of dealing.
Both the size and pos paramaters must be ordered pairs (tuples).
e.g. (0,0), (50,50), etc
'''
def __init__(self,pos,size,image):
self.image = image
self.facing = 'right'
self.xPos = pos[0]
self.yPos = pos[1]
self.width = size[0]
self.height = size[1]
# We obtain collision boundaries with this method
def get_rect(self):
return pygame.Rect((self.xPos, self.yPos, self.width, self.height))
def set_rect(self, rect):
self.xPos = rect[0]
self.yPos = rect[1]
self.width = rect[2]
self.height = rect[3]
def getPosition(self):
return (int(self.xPos), int(self.yPos))
def get_sprite(self):
return tiledtmxloader.helperspygame.SpriteLayer.Sprite(self.image, self.get_rect())
# Is the obstacle touching a particular object?
def isTouching(self, x, y, endYRange):
'''
Here, we are fundamentally checking to see
whether or not the obstacle is in a certain
rectangular boundary (x, y, width, height) of
the target object.
'''
'''
The first check checks whether or not the object
is within the horizontal boundaries of the obstacle itself.
'''
if (int(x) in range(int(self.xPos), int(self.xPos + self.width))):
'''
Here, we check for whether or not the object's bottom is at
the same level as (or below) the obstacle's top y coordinate.
'''
if (int(endYRange) >= int(self.yPos)):
return True
return False
# Stationary obstacles
class stationaryObstacle(Obstacle):
def __init__(self,pos,size,image):
return Obstacle.__init__(self, pos, size, image)
class spikes(stationaryObstacle):
def __init__(self,pos,size,image):
self.collidedHit = False
return stationaryObstacle.__init__(self, pos, size, image)
def spikeBump(self, obj):
'''
We want to ensure that the first time the obstacle is hit
by another object, it emits a "clang" sound. But after that,
we want it so that as long as the object is touching the spikes,
the spikes make no sound.
'''
if self.isTouching(obj.x + obj.width, obj.y, obj.y + obj.height) or self.isTouching(obj.x, obj.y, obj.y + obj.height):
if (not self.collidedHit):
soundObj = pygame.mixer.Sound('Sounds/Spikes.wav')
soundObj.play()
self.collidedHit = True
else:
if (self.collidedHit):
self.collidedHit = False
# It's a log.
class treeLog(stationaryObstacle):
def __init__(self,pos,size,image):
return stationaryObstacle.__init__(self, pos, size, image)
def collidedHardWith(self, obj):
if (self.isTouching(obj.x + obj.width, obj.y, obj.y + obj.height)):
'''
This checks that the most front position
of the object is less than or equal to the
x position of the log (indicating a
collision at the left side.)
* This works - it has been tested
'''
if (int(obj.x + obj.width) <= int(self.xPos + (self.width/10))):
return True
return False
# Obstacles that are set into motion when triggered
class triggeredObstacle(Obstacle):
def __init__(self,pos,size,image):
self.triggerDelay = 0
self.timeToChangeFrame = 0
self.timeSinceLastFrame = 0
self.haveSetFrameRate = True
self.animateToSecondFrame = True
return Obstacle.__init__(self, pos, size, image)
def move(self, xIncrement, yIncrement):
self.xPos += xIncrement
self.yPos += yIncrement
def setFrameRate(self, framerate):
# We only want to set the frame rate once
if (self.haveSetFrameRate):
self.timeToChangeFrame = framerate
self.haveSetFrameRate = False
class bananaPeel(triggeredObstacle):
def __init__(self,pos,size,image):
self.xSpeed = -2.5
self.ySpeed = -2.5
self.haveSetSpeeds = False
self.rotation = 0
self.slippedOn = False
# Properties of time concerned with the last slip with this object
self.slipTimeCounter = 0
self.slipRiseTime = 50
self.slipAlpha = 255
# Properties of gravity that dictate the banana peel's triggered movement
self.gravityForce = 1
self.gravityXCarry = -1
return triggeredObstacle.__init__(self, pos, size, image)
def setHoriAndVertRiseSpeeds(self, hori, vert):
if (not self.haveSetSpeeds):
self.xSpeed = hori
self.ySpeed = vert
self.haveSetSpeeds = True
'''
The banana peel will rotate as it is reaching its peak height (to
create the effect that it's been slipped on.
'''
# Returns the value for the fade-out of the banana peel
def doFadeOutBananaPeel(self, alphaDecrement):
if self.slipAlpha > 0 and self.slipTimeCounter >= self.slipRiseTime:
self.slipAlpha += alphaDecrement
return self.getBananaPeelFadeAmount()
def getBananaPeelFadeAmount(self):
return self.slipAlpha
'''
When an object (like you for example) slips on a banana peel,
the banana peel will fly a certain direction (defualt right now
is backwards), and rotate 90 degrees while it's reaching its max
height. Then, it will maintain its rotation once it has reached
that height, and simply be under the influence of gravity.
'''
def slipRotate(self, gameFloor, rotateIncrementInit, rotateIncrementFall):
if (self.slipTimeCounter < self.slipRiseTime and self.slippedOn):
self.rotation += rotateIncrementInit
return (self.rotation)
else:
# Did we already hit the floor?
if (self.yPos >= gameFloor):
self.rotation = 0
else:
self.rotation += rotateIncrementFall
return (self.rotation)
def doBananaPeelGravity(self, obj, floor, downAccel):
if self.yPos < floor:
self.move(self.gravityXCarry, 0)
# Settings on the gravity, so we have acceleration
self.gravityForce += (self.gravityForce * downAccel)
self.move(0, self.gravityForce)
def doBananaPeelAction(self, obj, gameFloor, gravAccel, counterIncrement, winWidth):
# The first time the body actually slips on the banana peel
if (self.isTouching(obj.x, obj.y, obj.y + obj.height) and self.slippedOn == False):
self.slippedOn = True
if (self.slippedOn and self.slipTimeCounter < self.slipRiseTime):
self.slipTimeCounter += counterIncrement
self.move(self.xSpeed, self.ySpeed)
else:
# The point where the banana peel reaches its maximum height
if (self.slipTimeCounter >= self.slipRiseTime):
self.doBananaPeelGravity(obj, gameFloor, gravAccel)
if (self.yPos >= gameFloor):
self.slippedOn = False
self.slipTimeCounter = 0
self.gravityForce = 1
class mud(triggeredObstacle):
def __init__(self, pos, size, image):
return triggeredObstacle.__init__(self, pos, size, image)
def animateToSecond(self):
if (self.timeSinceLastFrame == self.timeToChangeFrame):
self.timeSinceLastFrame = 0
self.animateToSecondFrame = (not self.animateToSecondFrame)
else:
self.timeSinceLastFrame += 1
return self.animateToSecondFrame
def doMudAction(self, speed):
return self.animateToSecond()
class coconut(triggeredObstacle):
def __init__(self, pos, size, image):
return triggeredObstacle.__init__(self, pos, size, image)
class sandCastle(triggeredObstacle):
def __init__(self, pos, size, image):
return triggeredObstacle.__init__(self, pos, size, image)
# Obstacles capable of moving on their own
class movingObstacle(Obstacle):
def __init__(self,pos,size,image):
self.speed = 0
return Obstacle.__init__(self, pos, size, image)
# Accepts a surface image to flip. "hori" and "vert" are booleans.
def reflectOff(self, display, image, hori, vert):
pygame.transform.flip(image, hori, vert)
def move(self, xIncrement, yIncrement):
self.xPos += xIncrement
self.yPos += yIncrement
def setSpeed(self, speedAmount):
self.speed = speedAmount
class giantRock(movingObstacle):
def __init__(self,pos,size,image,moveMode):
self.giantRockMoveMode = moveMode
self.gravityForce = 1
self.rotation = 0
self.timeToChangeFrame = 20
self.timeSinceLastFrame = 1
self.frameNumber = 0
return movingObstacle.__init__(self, pos, size, image)
'''
Rotate the soccer ball every certain amount of degree specified,
depending on what direction the soccer ball is currently moving.
'''
def giantRockRotate(self, rotateIncrement):
if self.giantRockMoveMode == 'left':
self.rotation += rotateIncrement
if self.rotation >= DEGREES_IN_CIRCLE:
self.rotation = 0
elif self.giantRockMoveMode == 'right':
self.rotation -= rotateIncrement
if self.rotation <= -DEGREES_IN_CIRCLE:
self.rotation = 0
return self.rotation
def animateToNext(self, sectionOfTime, timeToChangeFrame):
if (self.timeSinceLastFrame % sectionOfTime == 0):
self.frameNumber = (self.timeSinceLastFrame / sectionOfTime - 1)
if (self.timeSinceLastFrame >= timeToChangeFrame):
self.timeSinceLastFrame = 1
else:
self.timeSinceLastFrame += 1
else:
self.timeSinceLastFrame += 1
return self.frameNumber
def doGiantRockPhysics(self, obj, floor, downAccel):
if self.yPos < floor:
self.move(0, self.speed)
# Settings on the gravity, so we have acceleration.
self.gravityForce += (self.gravityForce * downAccel)
self.move(0, self.gravityForce)
else:
self.gravityForce = 1
def doGiantRockAction(self, obj, gameFloor, gravAccel, winWidth):
# Soccer ball physics includes the gravity aspect of the ball.
self.doGiantRockPhysics(obj, gameFloor, gravAccel)
# Testing for when to switch direction of the ball.
if self.xPos <= 0:
self.giantRockMoveMode = 'right'
elif self.giantRockMoveMode == 'left' and self.isTouching(obj.x + obj.width, obj.y, obj.y + obj.height):
self.giantRockMoveMode = 'right'
elif self.xPos >= winWidth:
self.giantRockMoveMode = 'left'
elif self.giantRockMoveMode == 'right' and self.isTouching(obj.x, obj.y, obj.y + obj.height):
self.giantRockMoveMode = 'left'
# Move the soccer ball in accordance to the current direction it has on now.
if self.giantRockMoveMode == 'right':
self.move(self.speed, 0)
elif self.giantRockMoveMode == 'left':
self.move(-self.speed, 0)
class tidalWave(movingObstacle):
def __init__(self,pos,size,image):
return movingObstacle.__init__(self, pos, size, image)
'''
It is convenient to define an animal sub-class for the class of
moving obstacles, since animals are usually animated.
'''
class animal(movingObstacle):
def __init__(self,pos,size,image):
self.timeToChangeFrame = 0
self.timeSinceLastFrame = 0
self.haveSetFrameRate = True
return movingObstacle.__init__(self, pos, size, image)
def setFrameRate(self, framerate):
# We only want to set the frame rate once
if (self.haveSetFrameRate):
self.timeToChangeFrame = framerate
self.haveSetFrameRate = False
class bird(animal):
def __init__(self,pos,size,image):
self.animateToSecondFrame = True
return animal.__init__(self, pos, size, image)
def animateToSecond(self):
if (self.timeSinceLastFrame == self.timeToChangeFrame):
self.timeSinceLastFrame = 0
self.animateToSecondFrame = (not self.animateToSecondFrame)
else:
self.timeSinceLastFrame += 1
return self.animateToSecondFrame
def doBirdAction(self, speed):
self.move(-speed, 0)
return self.animateToSecond()
class crocodile(animal):
def __init__(self,pos,size,image):
return movingObstacle.__init__(self, pos, size, image)
class spider(animal):
def __init__(self,pos,size,image):
self.animateToSecondFrame = True
self.webStringX = pos[0] + (size[0]/2)
self.webStringY = pos[1] - 50
self.webStringWidth = 2
self.webStringHeight = 50
return animal.__init__(self, pos, size, image)
def animateToSecond(self):
if (self.timeSinceLastFrame == self.timeToChangeFrame):
self.timeSinceLastFrame = 0
self.animateToSecondFrame = (not self.animateToSecondFrame)
else:
self.timeSinceLastFrame += 1
return self.animateToSecondFrame
def setWebStringRect(self, x, y, width, height):
self.webStringX = x
self.webStringY = y
self.webStringWidth = width
self.webStringHeight = height
def getWebStringRect(self):
return (self.webStringX, self.webStringY, self.webStringWidth, self.webStringHeight)
def doSpiderAction(self, speed):
return self.animateToSecond()
class snake(animal):
def __init__(self,pos,size,image):
self.animateToSecondFrame = True
return animal.__init__(self, pos, size, image)
def animateToSecond(self):
if (self.timeSinceLastFrame == self.timeToChangeFrame):
self.timeSinceLastFrame = 0
self.animateToSecondFrame = (not self.animateToSecondFrame)
else:
self.timeSinceLastFrame += 1
return self.animateToSecondFrame
def doSnakeAction(self, speed):
self.move(-speed, 0)
return self.animateToSecond()