-
Notifications
You must be signed in to change notification settings - Fork 0
/
Butterfly.py
211 lines (153 loc) · 5.52 KB
/
Butterfly.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
#!/usr/bin/python
import pygame
import Resources
import random
import math
from Animation import *
from pygame.locals import *
from Explosion import *
from random import randint
class Butterfly():
def __init__(self, x, y, color = (255, 255, 255), objectList = [], spriteList = []):
self.objectList = objectList
self.spriteList = spriteList
self.rect = pygame.Rect(x, y, 15, 15)
self.screen = pygame.display.get_surface()
self.butterflyAnim = Animation("butterfly", 8)
self.butterflyAnim.updateColor(color)
self.butterflyAnim.setFrameRange(1, 8);
self.butterflyAnim.setCurrentFrame(randint(1, 8))
self.sprite = pygame.sprite.RenderPlain(self.butterflyAnim)
self.butterflyAnim.playAnim()
self.area = self.screen.get_rect()
self.color = color
self.floorLevel = self.screen.get_height() - self.rect.h
self.movingLeft = False
self.movingRight = False
self.movingUp = False
self.movingDown = False
self.degree = randint(0, 360)
self.speed = 4
self.detlaUpdate = 0
self.collide = False
self.movePos = [0,0.01]
def update(self):
if(self.detlaUpdate == 1):
self.detlaUpdate = 0
limitBottom = self.degree - 20
limitTop = self.degree + 20
self.degree = randint(limitBottom, limitTop)
self.degree = self.degree % 360
if(randint(0, 1) == 0):
if(self.speed < 6):
self.speed += 1
else:
if(self.speed > 2):
self.speed -= 1
self.movePos[0] = math.cos(math.radians(self.degree)) * self.speed
self.movePos[1] = math.sin(math.radians(self.degree)) * self.speed
#self.movePos[0] = randint(-8, 8)
#self.movePos[1] = randint(-8, 8)
self.checkForCollision()
newpos = self.rect.move(self.movePos)
if self.area.contains(newpos) and not self.collide:
self.rect = newpos
self.butterflyAnim.getRect().x = self.rect.x
self.butterflyAnim.getRect().y = self.rect.y
if self.degree >= 90 and self.degree <= 240:
self.movingLeft = True
self.movingRight = False
else:
self.movingLeft = False
self.movingRight = True
if self.degree >= 0 and self.degree <= 180:
self.movingDown = True
self.movingUp = False
else:
self.movingDown = False
self.movingUp = True
else:
self.detlaUpdate += 1
self.butterflyAnim.update()
self.sprite.update()
self.sprite.draw(self.screen)
pygame.event.pump()
def collisionDetection(self, obj, rabbit = False):
#FLEE BEHAVIOR !!!
objCenterX = obj.rect.x + obj.rect.w/2
objCenterY = obj.rect.y + obj.rect.h/2
butterflyCenterX = self.rect.x + self.rect.w/2
butterflyCenterY = self.rect.y + self.rect.h/2
dist = math.fabs(math.sqrt(((objCenterX - butterflyCenterX) * (objCenterX - butterflyCenterX)) + ((objCenterY - butterflyCenterY) * (objCenterY - butterflyCenterY))))
if dist < 50:
distX = math.fabs(objCenterX - butterflyCenterX)
angle = math.degrees(math.acos(distX/dist))
if (objCenterX <= butterflyCenterX) and (objCenterY <= butterflyCenterY):
angle = 240 - angle
elif (objCenterX > butterflyCenterX) and (objCenterY <= butterflyCenterY):
angle = 360 - angle
elif (objCenterX <= butterflyCenterX) and (objCenterY > butterflyCenterY):
angle = 180 - angle
angle += 180
angle = angle % 360
self.degree = int(angle)
#FLEE BEHAVIOR !!!
# 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] = 5
# 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] = -5
# 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] = 5
# 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:
# self.rect.y = obj.rect.y - self.rect.h
# self.movePos[1] = -5
def screenCollisionDetection(self):
butterflyCenterX = self.rect.x + self.rect.w/2
butterflyCenterY = self.rect.y + self.rect.h/2
distBorder = butterflyCenterX
if distBorder < 10:
self.degree = 0
if (butterflyCenterY) < distBorder:
distBorder = butterflyCenterY
if distBorder < 10:
self.degree = 90
if (self.screen.get_rect().w - butterflyCenterX) < distBorder:
distBorder = self.screen.get_rect().w - butterflyCenterX
if distBorder < 10:
self.degree = 180
if (self.screen.get_rect().h - butterflyCenterY) < distBorder:
distBorder = self.screen.get_rect().h - butterflyCenterY
if distBorder < 10:
self.degree = 240
def checkForCollision(self):
#if not self.movingLeft and not self.movingRight and self.movePos[1] == 0 and self.velocity == 0:
# return
for obj in self.objectList:
if obj.getType() is not "carrot":
self.collisionDetection(obj, False)
self.screenCollisionDetection()
#for rabbit in self.rabbitList:
# self.collisionDetection(rabbit, True)
def moveLeftStart(self):
self.movingLeft = True
self.movingRight = False
def moveLeftStop(self):
self.movingLeft = False
self.movePos[0] = 0
def moveRightStart(self):
self.movingRight = True
self.movingLeft = False
def moveRightStop(self):
self.movingRight = False
self.movePos[0] = 0
def getAnim(self):
return self.butterflyAnim