-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
280 lines (237 loc) · 9.26 KB
/
main.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
import pygame
import sys
import random
from missile import Missile
from explosion import Explosion
from launcher import Launcher
import math
# width = 480
# height = 480
pygame.init()
pygame.mouse.set_cursor(*pygame.cursors.diamond)
INFO = pygame.display.Info()
WIDTH = int(INFO.current_h * 0.5)
HEIGHT = int(INFO.current_h * 0.5)
GROUND_HEIGHT = int(HEIGHT / 15)
SHELTER_HEIGHT = int(HEIGHT / 10)
s = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Missile Command')
clock = pygame.time.Clock()
level = 0
half = WIDTH/18
shelter_positions = [WIDTH/9 - half, 2*WIDTH/9 - half, 3*WIDTH/9 - half, 4*WIDTH/9 - half, 5*WIDTH/9 - half,
6*WIDTH/9 - half, 7*WIDTH/9 - half, 8*WIDTH/9 - half, 9*WIDTH/9 - half]
enemy_missiles = []
points = 0
player_missiles = []
explosion_list = []
shelter = [True, True, True, True, True, True]
launcher_list = [Launcher(0), Launcher(1), Launcher(2)]
#launcher_positions = [30, WIDTH / 2, WIDTH - 30]
launcher_positions = [WIDTH/9 - half, 5*WIDTH/9 - half, 9*WIDTH/9 - half]
colors_list = [pygame.Color(0, 255, 0), pygame.Color(255, 0, 0), pygame.Color(255, 255, 0),
pygame.Color(0, 255, 255), pygame.Color(255, 0, 255), pygame.Color(255, 255, 255),
pygame.Color(0, 0, 255)]
def draw():
s.fill((0, 0, 0))
w = HEIGHT - SHELTER_HEIGHT
pygame.draw.rect(s, pygame.Color(255, 255, 0), (0, HEIGHT - GROUND_HEIGHT, WIDTH, GROUND_HEIGHT))
for ss in range(len(launcher_list)):
pygame.draw.polygon(s, pygame.Color(255, 255, 0),
[(ss * WIDTH / 2 - SHELTER_HEIGHT/2 + GROUND_HEIGHT - (GROUND_HEIGHT * ss), w), (ss * WIDTH / 2 + SHELTER_HEIGHT/2 + GROUND_HEIGHT - (GROUND_HEIGHT * ss), w),
(ss * WIDTH / 2 + SHELTER_HEIGHT + GROUND_HEIGHT - (GROUND_HEIGHT * ss), HEIGHT), (ss * WIDTH / 2 - SHELTER_HEIGHT + GROUND_HEIGHT - (GROUND_HEIGHT * ss), HEIGHT)])
counter = launcher_list[ss].ammo
number = 1
while counter > 0:
for j in range(number):
pygame.draw.ellipse(s, (0, 0, 255), (launcher_positions[ss] - ((number - 2 * j) * WIDTH/100) + WIDTH/200, HEIGHT - SHELTER_HEIGHT + ((number-1) * WIDTH/100), WIDTH/100, WIDTH/100))
counter = counter-1
if counter == 0:
break
number = number+1
launcher_pos = 1
for i in range(len(shelter)):
if shelter[i]:
pygame.draw.rect(s, pygame.Color(0, 0, 255), ((i+launcher_pos) * WIDTH/9 + SHELTER_HEIGHT/4, HEIGHT - GROUND_HEIGHT*1.2, SHELTER_HEIGHT/2, SHELTER_HEIGHT/3))
if (i + 1) % 3 == 0:
launcher_pos = launcher_pos+1
for p in player_missiles:
pygame.draw.line(s, pygame.Color(0, 255, 0), (p.start_x, p.start_y), (p.current_x, p.current_y), 1)
pygame.draw.ellipse(s, random.choice(colors_list),
(p.current_x-1.5, p.current_y-1.5, 4, 4), 0)
col = random.choice(colors_list)
pygame.draw.line(s, col, (p.end_x-5, p.end_y-5),
(p.end_x+5, p.end_y+5), 1)
pygame.draw.line(s, col, (p.end_x+5, p.end_y-5),
(p.end_x-5, p.end_y+5), 1)
p.move()
for p in enemy_missiles:
pygame.draw.line(s, pygame.Color(255, 0, 0), (p.start_x, p.start_y), (p.current_x, p.current_y), 1)
pygame.draw.ellipse(s, random.choice(colors_list),
(p.current_x-1.5, p.current_y-1.5, 4, 4), 0)
p.move()
for w in explosion_list:
if w.expires:
w.frame -= 2
if w.frame == 0:
explosion_list.remove(w)
del w
continue
elif w.frame == 1500:
w.expires = True
else:
w.frame += 1
pygame.draw.ellipse(s, random.choice(colors_list),
(w.poz_x-w.frame/60, w.poz_y-w.frame/60, w.frame/30, w.frame/30), 0)
pygame.display.update()
def designate_launcher(x, y):
minimum_x = 10
y1 = HEIGHT - 50
dy = y-y1
minimum = 100000
if launcher_list[0].ammo > 0:
x1 = launcher_positions[0]
dx = x1-x
temp = math.sqrt(dx*dx + dy*dy)
if temp < minimum:
minimum = temp
minimum_x = x1
if launcher_list[1].ammo > 0:
x1 = launcher_positions[1]
dx = x1-x
temp = math.sqrt(dx*dx + dy*dy)
if temp < minimum:
minimum = temp
minimum_x = x1
if launcher_list[2].ammo > 0:
x1 = launcher_positions[2]
dx = x1-x
temp = math.sqrt(dx*dx + dy*dy)
if temp < minimum:
minimum = temp
minimum_x = x1
return minimum_x
def launch_rocket(x, y):
if y > HEIGHT-SHELTER_HEIGHT*1.4:
return
launcher_position = designate_launcher(x, y)
if launcher_position == launcher_positions[0]:
launcher_list[0].ammo -= 1
elif launcher_position == launcher_positions[1]:
launcher_list[1].ammo -= 1
elif launcher_position == launcher_positions[2]:
launcher_list[2].ammo -= 1
else:
return
player_missiles.append(Missile(launcher_position, HEIGHT - SHELTER_HEIGHT, x, y, 0.2, 0))
def middle_point(x, y, wx, wy, r):
p = ((math.pow((x - wx), 2) // math.pow(r+1, 2)) +
(math.pow((y - wy), 2) // math.pow(r+1, 2)))
return p
def collision():
for p in player_missiles:
if p.current_y-p.end_y < 0.1:
temp = Explosion(p.current_x, p.current_y)
explosion_list.append(temp)
player_missiles.remove(p)
del p
continue
for w in explosion_list:
if middle_point(p.current_x, p.current_y, w.poz_x, w.poz_y, w.frame / 60) < 1:
temp = Explosion(p.current_x, p.current_y)
explosion_list.append(temp)
player_missiles.remove(p)
del p
break
for p in enemy_missiles:
if p.current_y-p.end_y > -0.1:
temp = Explosion(p.current_x, p.current_y)
explosion_list.append(temp)
if p.end_x == shelter_positions[0]:
launcher_list[0].ammo = 0
elif p.end_x == shelter_positions[1]:
shelter[0] = False
elif p.end_x == shelter_positions[2]:
shelter[1] = False
elif p.end_x == shelter_positions[3]:
shelter[2] = False
elif p.end_x == shelter_positions[4]:
launcher_list[1].ammo = 0
elif p.end_x == shelter_positions[5]:
shelter[3] = False
elif p.end_x == shelter_positions[6]:
shelter[4] = False
elif p.end_x == shelter_positions[7]:
shelter[5] = False
elif p.end_x == shelter_positions[8]:
launcher_list[2].ammo = 0
lose()
enemy_missiles.remove(p)
del p
continue
for w in explosion_list:
if middle_point(p.current_x, p.current_y, w.poz_x, w.poz_y, w.frame / 60) < 1:
temp = Explosion(p.current_x, p.current_y)
explosion_list.append(temp)
enemy_missiles.remove(p)
del p
break
def new_level():
if not enemy_missiles:
launcher_list[0].ammo = 10
launcher_list[1].ammo = 10
launcher_list[2].ammo = 10
explosion_list.clear()
global points
points += 1
for i in range(10):
temp = Missile(random.randrange(WIDTH), 0,
random.choice(shelter_positions), HEIGHT - GROUND_HEIGHT, 0.04, random.randrange(4000))
enemy_missiles.append(temp)
def lose():
for i in shelter:
if i:
return
del explosion_list[:]
del player_missiles[:]
del enemy_missiles[:]
global points
draw()
large_text = pygame.font.SysFont("consolas", int(HEIGHT * 0.035))
text_surface = large_text.render("Survived waves : " + str(points) +
" Press space to start again", True, (255, 0, 0))
text_rect = text_surface.get_rect()
text_rect.center = ((WIDTH / 2), (HEIGHT / 2))
s.blit(text_surface, text_rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
launcher_list[0].ammo = 10
launcher_list[1].ammo = 10
launcher_list[2].ammo = 10
shelter[0] = True
shelter[1] = True
shelter[2] = True
shelter[3] = True
shelter[4] = True
shelter[5] = True
points = 0
main()
def main():
while True:
collision()
draw()
new_level()
for e in pygame.event.get():
if e.type == pygame.QUIT:
sys.exit(0)
elif e.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
launch_rocket(x, y)
#clock.tick(100)
main()