-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
302 lines (262 loc) · 8.15 KB
/
main.py
File metadata and controls
302 lines (262 loc) · 8.15 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
import pygame, random
from pygame.locals import *
# from pygame.locals import (
# K_UP,
# K_DOWN,
# K_LEFT,
# K_RIGHT,
# KEYDOWN,
# QUIT,
# MOUSEBUTTONDOWN,
# )
global shoot_time
shoot_time = 1
print('Click on the other window to play')
pygame.init()
pygame.mixer.init()
pygame.font.init()
clock = pygame.time.Clock()
bang = pygame.sprite.Group()
NME = pygame.sprite.Group()
music = pygame.mixer.music.load("shot.mp3")
# ms=pygame.mixer.music.load("bg.mp3")
# pygame.mixer.music.load(sound)
score = 0
display_width = 1200
display_height = 600
X = 300
Y = 500
var1 = 200
var2 = 500
clr = (204,255,255)
levelcount = 1
SCREEN_WIDTH = display_width
SCREEN_HEIGHT = display_height
screen = pygame.display.set_mode((display_width,display_height))
bullet = pygame.image.load('bul.png').convert_alpha()
alive = False
start = True
while start == True:
for event in pygame.event.get():
print(event)
if event.type == MOUSEBUTTONDOWN:
start = False
alive = True
global textsurface
textsurface= ""
screen.fill((255,229,209))
# pygame.mixer.music.play(-1)
myfont = pygame.font.SysFont('Comic Sans MS', 20)
textsurface = myfont.render('The Helicopter Game', True, (0, 0, 0))
screen.blit(textsurface,(40,150))
pygame.display.update()
clock.tick(30)
def death():
screen.fill((150,150,150))
pygame.display.update()
print('You died')
myfont = pygame.font.SysFont('Comic Sans MS', 130)
textsurface = myfont.render('Game Over.', True, (0, 0, 0))
screen.blit(textsurface,(0,0))
print('Score: {}'.format(score))
def win1():
print('Level 1 completed')
global levelcount
global var1
global var2
global clr
global score
global alive
myfont = pygame.font.SysFont('Comic Sans MS', 100)
textsurface = myfont.render('Level 1 completed', True, (0, 0, 0))
screen.blit(textsurface,(0,0))
for sprite in enemies:
sprite.kill()
pygame.display.flip()
pygame.event.pump()
pygame.time.wait(3000)
levelcount = 2
var1 = 200
var2 = 300
clr = (153, 204, 255)
alive = True
score = 0
def win2():
global levelcount, var1, var2, clr, score, alive
for sprite in enemies:
sprite.kill()
myfont = pygame.font.SysFont('Comic Sans MS', 100)
textsurface = myfont.render('Level 2 completed', True, (0, 0, 0))
screen.blit(textsurface,(0,0))
for sprite in enemies:
sprite.kill()
pygame.display.flip()
pygame.event.pump()
pygame.time.wait(3000)
levelcount = 3
var1 = 100
var2 = 200
clr = (102, 178, 255)
alive = True
score = 0
def win3():
global levelcount, var1, var2, clr, score, alive
for sprite in enemies:
sprite.kill()
myfont = pygame.font.SysFont('Comic Sans MS', 100)
textsurface = myfont.render('You beat the game!', True, (0, 0, 0))
screen.blit(textsurface,(200, 400))
pygame.display.flip()
pygame.event.pump()
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super(Player, self).__init__()
self.surf = pygame.image.load("Heli 1.png").convert_alpha()
self.rect = self.surf.get_rect(topleft = (x, y))
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
if pressed_keys[K_SPACE]:
#pygame.mixer.Sound.play(sound)
global shoot_time
if shoot_time > 30:
shoot_time = 0
bullet2 = Projectile(self.rect.x+100, self.rect.y+70, bullet)
bang.add(bullet2)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
class Projectile(pygame.sprite.Sprite):
def __init__(self, x, y, image):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x - 50
self.rect.y = y - 60
def update(self):
if self.rect.x < SCREEN_WIDTH :
self.rect.x += 6
def updateWindow():
screen.blit(textsurface, (0,0))
bang.update()
bang.draw(screen)
NME.update()
NME.draw(screen)
pygame.display.update()
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super(Enemy, self).__init__()
self.surf = pygame.image.load("Rocket 1.png").convert_alpha()
self.rect = self.surf.get_rect(
center=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(10, 15) # speed of enemies
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
global score
score += 1
self.kill()
class Bomb(pygame.sprite.Sprite):
def __init__(self):
super(Bomb, self).__init__()
self.surf = pygame.image.load("Plane 1.png").convert_alpha()
self.rect = self.surf.get_rect(
center=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(5, 10)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
global score
score += 2
self.kill()
class Blimp(pygame.sprite.Sprite):
def __init__(self):
super(Blimp, self).__init__()
self.surf = pygame.image.load("Blimp.png").convert_alpha()
self.rect = self.surf.get_rect(
center=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(4, 8)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
global score
score += 3
self.kill()
ADDENEMY = pygame.USEREVENT + 1
print(var1, var2, "adfadfdfasdf-------")
pygame.time.set_timer(ADDENEMY, random.randrange(var1, var2))
player = Player(display_width // 2, display_height // 2)
bang2 = Projectile(100,100,bullet)
enemies = pygame.sprite.Group()
bomb = pygame.sprite.Group()
blimp = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
while alive == True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
alive = False
elif event.type == ADDENEMY:
new_opponent = random.choice([Enemy(), Bomb(), Blimp()])
enemies.add(new_opponent)
all_sprites.add(new_opponent)
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
enemies.update()
screen.fill(clr)
for entity in all_sprites:
screen.blit(entity.surf, entity.rect)
if pygame.sprite.spritecollideany(player, enemies):
player.kill()
alive = False
death()
if pygame.sprite.groupcollide(enemies, bang, True, True):
pygame.mixer.music.play()
hit = pygame.sprite.groupcollide(enemies, bang, True, True)
all_sprites.remove(hit)
pygame.display.update()
if levelcount == 1:
if score > 30:
alive = False
win1()
pygame.time.set_timer(ADDENEMY, random.randrange(var1, var2))
if levelcount == 2:
if score > 50:
alive = False
win2()
pygame.time.set_timer(ADDENEMY, random.randrange(var1, var2))
if levelcount == 3:
if score > 100:
alive = False
win3()
pygame.time.set_timer(ADDENEMY, random.randrange(var1, var2))
screen.blit(player.surf, player.rect)
pygame.display.flip()
pygame.display.update()
shoot_time += 1
updateWindow()
clock.tick(80)