-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.py
260 lines (206 loc) · 9.43 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
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 getPosition(self):
return (self.xPos, 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, endRangeX, endRangeY) of
the target object.
'''
if (int(x) in range(int(self.xPos), int(self.xPos + self.width))):
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
class treeLog(stationaryObstacle):
def __init__(self,pos,size,image):
return stationaryObstacle.__init__(self, pos, size, image)
# Obstacles that are set into motion when triggered
class triggeredObstacle(Obstacle):
def __init__(self,pos,size,image):
self.triggerDelay = 0
return Obstacle.__init__(self, pos, size, image)
def move(self, xIncrement, yIncrement):
self.xPos += xIncrement
self.yPos += yIncrement
class bananaPeel(triggeredObstacle):
def __init__(self,pos,size,image):
self.xSpeed = 2.5
self.ySpeed = 2.5
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)
'''
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, winWidth):
# The first time the body actually slips on the banana peel
if (self.isTouching(obj.x - 12, obj.y, obj.x + obj.width - 3, obj.y + obj.height) and self.slippedOn == False):
self.slippedOn = True
if (self.slippedOn and self.slipTimeCounter < self.slipRiseTime):
self.slipTimeCounter += 1
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 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 soccerBall(movingObstacle):
def __init__(self,pos,size,image,moveMode):
self.soccerMoveMode = moveMode
self.gravityForce = 1
self.rotation = 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 soccerBallRotate(self, rotateIncrement):
if self.soccerMoveMode == 'left':
self.rotation += rotateIncrement
if self.rotation == DEGREES_IN_CIRCLE:
self.rotation = 0
elif self.soccerMoveMode == 'right':
self.rotation -= rotateIncrement
if self.rotation == DEGREES_IN_CIRCLE:
self.rotation = 0
return self.rotation
def doSoccerBallPhysics(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 doSoccerBallAction(self, obj, gameFloor, gravAccel, winWidth):
# Soccer ball physics includes the gravity aspect of the ball.
self.doSoccerBallPhysics(obj, gameFloor, gravAccel)
# Testing for when to switch direction of the ball.
if self.xPos == 0:
self.soccerMoveMode = 'right'
elif self.soccerMoveMode == 'left' and self.isTouching(obj.x + obj.width, obj.y, obj.y + obj.height):
self.soccerMoveMode = 'right'
elif self.xPos == winWidth:
self.soccerMoveMode = 'left'
elif self.soccerMoveMode == 'right' and self.isTouching(obj.x, obj.y, obj.y + obj.height):
self.soccerMoveMode = 'left'
# Move the soccer ball in accordance to the current direction it has on now.
if self.soccerMoveMode == 'right':
self.move(self.speed, 0)
elif self.soccerMoveMode == 'left':
self.move(-self.speed, 0)
class bird(movingObstacle):
def __init__(self,pos,size,image):
return movingObstacle.__init__(self, pos, size, image)
class crocodile(movingObstacle):
def __init__(self,pos,size,image):
return movingObstacle.__init__(self, pos, size, image)