-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.py
282 lines (233 loc) · 9.98 KB
/
objects.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
import config
from config import *
#images
bullet_image_filename = 'bullet1.png'
box_image_filename = 'pictures/jellyfish.gif'
hamburger_image_filename = 'Hamburger.gif'
#sounds
jellyfish_sound_filename = 'sounds/jellyfish.ogg'
stage1, stage2, stage3 = range(3)
bullet_speed = 200
bullet_damage = 30
number_of_bullets = 3
enemy_speed = 30
enemy_blood = 120
enemy_damage = 90
defender_blood = 270
detect_radius = 400
max_fire_speed = 2000 #ms
import pygame
import vector2
import math_extended
import GIFImage
from pygame.locals import *
from sys import exit
from random import *
from math import *
from math_extended import *
class Bullet(pygame.sprite.Sprite):
def __init__(self, group, screen, pos, direction):
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.speed = bullet_speed
#damage system
self.damage = bullet_damage
self.direction = direction
self.pos = pos
self.angle = angle_of_vector2(Vector2(0, -1), self.direction)
if direction[0] > 0:
self.angle *= -1
self.image = pygame.image.load(bullet_image_filename).convert_alpha()
self.image = pygame.transform.rotozoom(self.image, degrees(self.angle), 0.1)
self.rect = self.image.get_rect()
self.rect.center = Vector2.as_tuple(self.pos)
self.clock = pygame.time.Clock()
group.add(self)
def update(self):
self.position()
self.rect.center = (self.pos.x, self.pos.y)
self.checkBounds()
def position(self):
time_passed = self.clock.tick()/1000.0
self.pos += self.direction*self.speed*time_passed
def checkBounds(self):
if self.pos.y < 0 or self.pos.y > self.screen.get_height() or \
self.pos.x < 0 or self.pos.x > self.screen.get_width():
self.kill()
class Food(pygame.sprite.Sprite):
def __init__(self, group, screen):
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.image = pygame.image.load(hamburger_image_filename).convert_alpha()
self.image = pygame.transform.scale(self.image, (70, 70))
self.rect = self.image.get_rect()
self.rect.center = (randint(100, SCREEN_SIZE[0]), randint(200, SCREEN_SIZE[1]))
self.pos = (self.rect.center[0]-35, self.rect.center[1]-35)
self.clock = pygame.time.Clock()
self.time = 0
group.add(self)
def update(self):
self.checkTime()
def checkTime(self):
self.time += self.clock.tick()/1000.0
if self.time > 10:
self.kill()
class Enemy(pygame.sprite.Sprite):
def __init__(self, group, screen):
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.image=GIFImage.GIFImage(box_image_filename, 192)
#self.image.set_alpha(120)
self.size = self.image.get_size()
self.rect = self.image.get_rect()
self.rect.center = (randint(self.rect.width,self.screen.get_width()-self.rect.width), -50)
self.speed = enemy_speed
#damage system
self.damage = enemy_damage
self.blood = enemy_blood
self.get_damage = 0
self.direction = Vector2(randint(-10, 10), randint(1, 10))
self.direction.normalise()
self.pos = Vector2(self.rect.center)
self.clock = pygame.time.Clock()
self.sound = pygame.mixer.Sound(jellyfish_sound_filename)
self.sound.play()
group.add(self)
def update(self):
self.image.render(self.screen, Vector2.as_tuple(Vector2(self.rect.center)-Vector2(self.size)*0.5))
self.position()
self.rect.center = (self.pos.x, self.pos.y)
self.checkBounds()
self.checkBlood()
def position(self):
time_passed = self.clock.tick()/1000.0
self.pos += self.direction*self.speed*time_passed
def checkBounds(self):
if self.pos.y > self.screen.get_height():
self.kill()
e = pygame.event.Event(USEREVENT+3, {'code': 1})
pygame.event.post(e)
print ('you lose!')
elif self.pos.x +self.rect.width/2 > self.screen.get_width() or self.pos.x -self.rect.width/2 < 0:
self.direction.x *=-1
def checkBlood(self):
self.blood -= self.get_damage
self.get_damage = 0
if self.blood <= 0:
self.kill()
class Friend(pygame.sprite.Sprite):
def __init__(self, screen, imagelist, soundlist):
pygame.sprite.Sprite.__init__(self)
self.imagelist = imagelist
self.screen = screen
self.image = GIFImage.GIFImage(self.imagelist[0])
self.size = self.image.get_size()
self.rect = self.image.get_rect()
self.rect.center = (0, 0)
self.bulletgroup = pygame.sprite.Group()
self.currentstate = stage1
self.nextstate = stage1
self.radius = detect_radius
self.clock = pygame.time.Clock()
#sum_of_time to check the time passed
self.sum_of_time = 0
#damage system
self.blood = defender_blood
self.get_damage = 0
self.get_blood = 0
self.startingsound = pygame.mixer.Sound(soundlist[0])
self.stoppingsound = pygame.mixer.Sound(soundlist[2])
self.startingsound.play()
def update(self):
self.image.render(self.screen, Vector2.as_tuple(Vector2(self.rect.center)-Vector2(self.size)*0.5))
self.checkBlood()
self.checkStage()
def checkBlood(self):
self.blood -= self.get_damage
if self.blood <= 0:
self.stoppingsound.play()
self.kill()
self.get_damage = 0
# print self.get_blood
self.blood += self.get_blood
if self.blood > defender_blood:
self.blood = defender_blood
self.get_blood = 0
def checkStage(self):
if self.currentstate == stage1:
if self.blood > 2*defender_blood/3:
self.nextstate = stage1
elif self.blood > defender_blood/3 and self.blood <= 2*defender_blood/3:
self.nextstate = stage2
self.image = GIFImage.GIFImage(self.imagelist[1])
elif self.blood > 0 and self.blood <= defender_blood/3:
self.nextstate = stage3
self.image = GIFImage.GIFImage(self.imagelist[2])
elif self.currentstate == stage2:
if self.blood > 2*defender_blood/3:
self.nextstate = stage1
self.image = GIFImage.GIFImage(self.imagelist[0])
elif self.blood > defender_blood/3 and self.blood <= 2*defender_blood/3:
self.nextstate = stage2
elif self.blood > 0 and self.blood <= defender_blood/3:
self.nextstate = stage3
self.image = GIFImage.GIFImage(self.imagelist[2])
elif self.currentstate == stage3:
if self.blood > 2*defender_blood/3:
self.nextstate = stage1
self.image = GIFImage.GIFImage(self.imagelist[0])
elif self.blood > defender_blood/3 and self.blood <= 2*defender_blood/3:
self.nextstate = stage2
self.image = GIFImage.GIFImage(self.imagelist[1])
elif self.blood > 0 and self.blood <= defender_blood/3:
self.nextstate = stage3
self.currentstate = self.nextstate
class Defender(Friend):
def __init__(self, screen, imagelist, soundlist):
Friend.__init__(self, screen, imagelist, soundlist)
def fire(self, allbox):
#sum_of_time to control fire speed
self.sum_of_time += self.clock.tick()
#if time passed bigger than max_fire_speed then fire
if self.sum_of_time > max_fire_speed:
self.sum_of_time = 0
boxlist = pygame.sprite.spritecollide(self, allbox, False, pygame.sprite.collide_circle)
# if find enemy and fired bullets are less than 3
if len(boxlist) and len(self.bulletgroup) < number_of_bullets:
#find the nearest enemy
length=detect_radius + max(self.size)
for box in boxlist:
tempvector = Vector2.from_points(self.rect.center, box.rect.center)
if length > tempvector.get_length():
length=tempvector.get_length()
nearestbox=box
#caculate the fire direction
if nearestbox:
vec1 = Vector2.from_points(nearestbox.rect.center, self.rect.center)
vec2 = nearestbox.direction.copy()
dist = vec1.get_length()
angle = angle_of_vector2(vec1, vec2)
s = cos(angle)*dist
v1=bullet_speed
v2=enemy_speed
root=roots2(v2**2-v1**2, -2*s*(v2**2), (dist*v2)**2)
dest = Vector2(nearestbox.rect.center) + nearestbox.direction*nearest_zero(root)
# set the property of bullets(direction, start position)
direction=Vector2.from_points(self.rect.center, dest)
direction.normalise()
pos = Vector2(self.rect.center)
self.bullet = Bullet(self.bulletgroup, self.screen, pos, direction)
class Producer(Friend):
def __init__(self, screen, imagelist, soundlist):
Friend.__init__(self, screen, imagelist, soundlist)
self.bloodup_speed = 10
self.support_group = pygame.sprite.Group()
def make_HP(self):
time_passed = self.clock.tick()/1000.0
added_blood = self.bloodup_speed*time_passed
self.get_blood = added_blood / 10.0
for element in self.support_group:
element.get_blood += added_blood
def update(self):
Friend.update(self)
self.make_HP()