-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRabbit.py
More file actions
347 lines (260 loc) · 8.4 KB
/
Rabbit.py
File metadata and controls
347 lines (260 loc) · 8.4 KB
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
#!/usr/bin/python
import pygame
import Resources
import random
from Animation import *
from pygame.locals import *
from Explosion import *
from Carrot import *
class Rabbit():
pygame.mixer.pre_init(44100, -16, 2, 1024)
pygame.mixer.init()
def __init__(self, id = -1, name = "", color = (255, 255, 255), objectList = [], spriteList = [], fake = False, x = 0, y = 0):
self.objectList = objectList
self.spriteList = spriteList
self.rabbitList = []
self.fake = fake
self.color = color
self.rect = pygame.Rect(x, y, 43, 32)
self.rabbitAnim = Animation("rabbit", 30)
self.rabbitAnim.updateColor(self.color)
self.rabbitAnim.setFrameRange(1, 8);
self.sprite = pygame.sprite.RenderPlain(self.rabbitAnim)
self.rabbitAnim.stopAnim()
self.screen = pygame.display.get_surface()
self.area = self.screen.get_rect()
self.area.h += 500
self.area.y -= 550
self.area.w -= 200
self.floorLevel = self.screen.get_height() - self.rect.h
self.explosion = Explosion()
self.movingLeft = False
self.movingRight = False
self.movingUp = False
self.movingDown = False
self.isJumping = False
self.isOnBlock = False
self.direction = "left"
self.collide = False
self.touched = False
self.touchDelay = 0
self.id = id
self.name = name
self.jumpSound = pygame.mixer.Sound("resources/sound/jump.wav")
self.splashSound = pygame.mixer.Sound("resources/sound/splash.wav")
self.carrotSound = pygame.mixer.Sound("resources/sound/carrot.wav")
self.jumpSound.set_volume(float(Resources.getOptionValue("sound"))/100)
self.splashSound.set_volume(float(Resources.getOptionValue("sound"))/100)
self.carrotSound.set_volume(float(Resources.getOptionValue("sound"))/100)
self.velocity = 5
self.gravity = 0.6
self.movePos = [0,0.01]
self.replaceRabbit()
self.points = 0
self.carrots = 0
self.currentFriction = self.getFloorFriction()
self.thrownCarrots = []
self.blood = Resources.getOptionValue("blood")
def __str__(self):
print "Rabbit ", self.id, ": ", self.name
def update(self):
if not self.fake:
if self.touched:
self.touchDelayCalculation()
else:
if self.movingLeft == True:
self.movePos[0] = -self.velocity
if self.movingRight == True:
self.movePos[0] = self.velocity
if self.movePos[1] is not 0:
self.movePos[1] += self.gravity
self.rect.y += self.movePos[1]
if self.rect.y > self.floorLevel:
self.rect.y = self.floorLevel
self.movePos[1] = 0
self.isJumping = False
if self.movePos[1] < 0:
self.movingUp = True
self.movingDown = False
elif self.movePos[1] > 0:
self.movingUp = False
self.movingDown = True
else:
self.movingUp = False
self.movingDown = False
self.checkForCollision()
newpos = self.rect.move(self.movePos)
if self.area.contains(newpos) and not self.collide:
self.rect = newpos
if self.isJumping:
self.rabbitAnim.setFrameRange(9, 15)
if self.movingUp:
self.rabbitAnim.stopAnim()
self.rabbitAnim.setCurrentFrame(9)
else:
self.rabbitAnim.playAnim(False)
else:
self.rabbitAnim.setFrameRange(1, 8)
if not self.movingLeft and not self.movingRight:
self.rabbitAnim.stopAnim()
self.rabbitAnim.rewind()
else:
self.rabbitAnim.playAnim()
self.rabbitAnim.getRect().x = self.rect.x
self.rabbitAnim.getRect().y = self.rect.y - 16
tmpThrownCarrots = []
for c in self.thrownCarrots:
end = c.update()
if not end:
tmpThrownCarrots.append(c)
self.thrownCarrots = tmpThrownCarrots
self.sprite.update()
self.sprite.draw(self.screen)
self.rabbitAnim.update()
self.explosion.update()
pygame.event.pump()
def collisionDetection(self, obj, rabbit = False):
if self.movingLeft:
if (self.rect.x < (obj.rect.x + obj.rect.w)) and (obj.rect.x < self.rect.x):
if (self.rect.y + self.rect.h) > (obj.rect.y + 1):
if self.rect.y < (obj.rect.y + obj.rect.h):
self.movePos[0] = 0
if self.movingRight:
if (obj.rect.x < (self.rect.x + self.rect.w)) and (obj.rect.x > self.rect.x):
if (self.rect.y + self.rect.h) > (obj.rect.y + 1):
if self.rect.y < (obj.rect.y + obj.rect.h):
self.movePos[0] = 0
if self.movingUp:
if (self.rect.y <= (obj.rect.y + obj.rect.h)) and (obj.rect.y <= self.rect.y):
if (self.rect.x + self.rect.w) > (obj.rect.x + 3):
if self.rect.x < (obj.rect.x + obj.rect.w - 5):
self.movePos[1] = 0.01
if ((self.rect.y + self.rect.h) >= obj.rect.y) and (self.rect.y <= obj.rect.y):
if (self.rect.x + self.rect.w) > (obj.rect.x + 3):
if self.rect.x < (obj.rect.x + obj.rect.w - 5):
if self.movePos[1] >= 0 and not self.isOnBlock:
self.isJumping = False
if rabbit:
self.jump(5)
if self.blood == 1:
obj.explosion = Explosion(obj.rect.x, obj.rect.y, obj.color)
obj.explosion.startExplosion()
self.splashSound.play()
obj.replaceRabbit()
self.points += 1
elif obj.getType() == "boing":
self.jump(12.7)
else:
self.rect.y = obj.rect.y - self.rect.h
self.movePos[1] = 0
self.isOnBlock = True
if self.isJumping:
self.isOnBlock = False
if self.isOnBlock:
if (self.rect.x > (obj.rect.x + obj.rect.w)) or ((self.rect.x + self.rect.w) < obj.rect.x):
self.isOnBlock = False
self.movePos[1] = 0.01
def checkForCollision(self):
if not self.movingLeft and not self.movingRight and self.movePos[1] == 0 and self.velocity == 0 and not self.isJumping:
return
for obj in self.objectList:
if obj.getType() == "carrot" and obj.isInBlock(self.rect.x + self.rect.w/2, self.rect.y + self.rect.h -5):
self.carrotSound.play()
self.carrots += 1
self.objectList.remove(obj)
self.spriteList.remove(obj)
if obj.getType() is not "carrot":
self.collisionDetection(obj, False)
for rabbit in self.rabbitList:
self.collisionDetection(rabbit, True)
def jump(self, velocity = 8.1):
if not self.isJumping:
self.jumpSound.play()
self.movePos[1] = -velocity
self.isJumping = True
def moveLeftStart(self):
if self.rabbitAnim.getFlip():
self.rabbitAnim.flipAnim()
self.rabbitAnim.playAnim()
self.movingLeft = True
self.movingRight = False
self.direction = "left"
def moveLeftStop(self):
if not self.movingRight:
self.rabbitAnim.stopAnim()
self.rabbitAnim.rewind()
self.movingLeft = False
self.movePos[0] = 0
def moveRightStart(self):
if not self.rabbitAnim.getFlip():
self.rabbitAnim.flipAnim()
self.rabbitAnim.playAnim()
self.movingRight = True
self.movingLeft = False
self.direction = "right"
def moveRightStop(self):
if not self.movingLeft:
self.rabbitAnim.stopAnim()
self.rabbitAnim.rewind()
self.movingRight = False
self.movePos[0] = 0
def appendRabbit(self, rabbit):
self.rabbitList.append(rabbit)
def replaceRabbit(self):
self.touched = False
while True:
randObj = self.objectList[random.randint(1, len(self.objectList)) - 1]
if self.isFloor(randObj):
break
self.rect.topleft = (randObj.rect.x, randObj.rect.y - randObj.rect.h)
def isInBlock(self, x, y):
for obj in self.objectList:
if obj.isInBlock(x, y):
return True
return False
def isFloor(self, object):
if self.isInBlock(object.getX() + 5, object.getY() - 5):
return False
return True
def getFloorFriction(self):
return 0.7
def touch(self):
self.touched = True
self.touchDelay = 180
TOUCH = USEREVENT + 1
touchevent = pygame.event.Event(TOUCH)
pygame.event.post(touchevent)
def touchDelayCalculation(self):
self.touchDelay -= 1
if(self.touchDelay == 0):
self.touched = False
UNTOUCH = USEREVENT + 2
untouchevent = pygame.event.Event(UNTOUCH)
pygame.event.post(untouchevent)
def isTouched(self):
return self.touched
def throwCarrot(self):
if(self.carrots > 0):
if(self.direction == "right"):
self.thrownCarrots.append(Carrot(self.direction, self.rect.x + 10, self.rect.y, self.objectList, self.rabbitList))
else:
self.thrownCarrots.append(Carrot(self.direction, self.rect.x, self.rect.y, self.objectList, self.rabbitList))
self.carrots -= 1
def updateColor(self, color):
self.rabbitAnim.updateColor(color)
def getId(self):
return self.id
def getName(self):
return self.name
def getPoints(self):
return self.points
def getCarrots(self):
return self.carrots
def getAnim(self):
return self.rabbitAnim
def setId(self, id):
self.id = id
def setName(self, name):
self.name = name
def setPoints(self, points):
self.points = points