-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarrot.py
111 lines (81 loc) · 2.38 KB
/
Carrot.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
#!/usr/bin/python
import pygame
import Resources
from Animation import *
from pygame.locals import *
class Carrot():
def __init__(self, direction, posX, posY, objectList = [], rabbitList = []):
self.objectList = objectList
self.rabbitList = rabbitList
self.carrotAnim = Animation("carrot", 24)
self.carrotAnim.setFrameRange(1, 12);
self.rect = pygame.Rect(posX, posY, 25, 25)
self.carrotAnim.setRect(self.rect)
self.carrotAnim.playAnim()
self.sprite = pygame.sprite.RenderPlain(self.carrotAnim)
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.smoked = False
self.countDown = 60
self.moveX = posX
self.direction = direction
if self.direction == "left":
self.carrotAnim.flipAnim()
def update(self):
if self.smoked:
self.carrotAnim.setRect(self.rect)
self.countDown -= 1
if self.countDown == 0:
return True
else:
if self.direction == "right":
self.moveX += 6
else:
self.moveX -= 6
self.rect.x = self.moveX
self.carrotAnim.setRect(self.rect)
self.checkForCollision()
if self.rect.x > self.area.w + 10:
return True
elif (self.rect.x + self.rect.w) < self.area.x - 10:
return True
self.sprite.update()
self.sprite.draw(self.screen)
self.carrotAnim.update()
pygame.event.pump()
return False
def collisionDetection(self, obj, rabbit = False):
if rabbit:
if (self.rect.y + self.rect.h) > obj.rect.y and self.rect.y < (obj.rect.y + obj.rect.h):
if self.direction == "right":
if (self.rect.x + self.rect.w) > obj.rect.x:
obj.touch()
self.smoke()
else:
if self.rect.x < (obj.rect.x + obj.rect.w):
obj.touch()
self.smoke()
else:
if obj.isInBlock(self.rect.x + self.rect.w/2, self.rect.y + self.rect.h/2):
self.smoke()
def checkForCollision(self):
for obj in self.objectList:
self.collisionDetection(obj)
for rabbit in self.rabbitList:
self.collisionDetection(rabbit, True)
def getAnim(self):
return self.carrotAnim
def smoke(self):
self.smoked = True
self.rect.x -= 25
self.rect.y -= 25
self.rect.w = 75
self.rect.h = 75
self.carrotAnim = Animation("carrot_smoke", 25)
self.carrotAnim.setFrameRange(1, 25);
self.carrotAnim.setRect(self.rect)
self.sprite = pygame.sprite.RenderPlain(self.carrotAnim)
self.carrotAnim.playAnim(False)