-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceinvaders.py
168 lines (140 loc) · 4.47 KB
/
spaceinvaders.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
from PPlay.window import *
from PPlay.sprite import *
from PPlay.gameimage import *
from PPlay.keyboard import *
from PPlay.collision import *
from variables import janela, janH, janW, ms, key, backgroundStart, backgroundOptions, backgroundMenu
from variables import start, options, exit, menu, state
from variables import astro, velAstro, velFire, fire, velShip, ship
from variables import shootFireTime, canShootFire, x, y
def shipsMatrix(x, y):
ships = []
for i in range(x):
line = []
for j in range(y):
ship = Sprite("assets/shipm.png")
ship.x = 20 + i * ship.width * 1.5
ship.y = 20 + j * ship.height * 1.5
line.append(ship)
ships.append(line)
return ships
def designShips(ships):
for line in ships:
for ship in line:
ship.draw()
def shootFire(fire, fires):
fire = fire
fire.set_position(astro.x + 45, astro.y - 10)
fires.append(fire)
return fires
def designFires(fires):
for fire in fires:
fire.y -= velFire * janela.delta_time()
fire.draw()
for fire in fires:
if fire.y < 0:
fires.remove(fire)
def pageStart():
global state, shootFireTime, canShootFire, velShip, ship, fire
ships = shipsMatrix(x, y)
fires = []
hitR = False
hitL = False
while state:
if ms.is_over_object(menu):
if ms.is_button_pressed(1):
canShootFire = 0
fires = []
ships = []
astro.set_position(janW/2 - astro.width/2, janH-astro.height)
return
# movimentação dos ships
for line in ships:
for ship in line:
ship.x += velShip * janela.delta_time()
if ships and ships[0]:
if ships[0][0].x <= 0:
hitL = True
velShip *= -1
if ships and ships[-1]:
if ships[-1][-1].x >= janela.width - ship.width:
velShip *= -1
hitR = True
if hitR or hitL:
for line in ships:
for ship in line:
ship.y += 10
hitL = False
hitR = False
# movimentação do astro
if astro.x >= 0:
if key.key_pressed("LEFT"):
astro.x -= velAstro * janela.delta_time()
if astro.x <= janW - astro.width:
if key.key_pressed("RIGHT"):
astro.x += velAstro * janela.delta_time()
# atirar fire e não atirar mais de um fire por vez
if key.key_pressed("UP"):
if canShootFire == 0:
shootFire(fire, fires)
canShootFire += 1
# recharge to shoot again
if canShootFire != 0:
canShootFire += janela.delta_time()
if canShootFire >= shootFireTime:
canShootFire = 0
# Detect collisions between fires and ships
for fire in fires:
for line in ships:
for ship in line:
if fire.collided(ship):
line.remove(ship)
fires.remove(fire)
break
else:
continue
break
# Remove any empty lines in ships
ships = [line for line in ships if line]
# Check for collision with astro
for line in ships:
for ship in line:
if ship.collided(astro):
return
backgroundStart.draw()
designFires(fires)
designShips(ships)
astro.draw()
menu.draw()
janela.set_title("Start")
janela.update()
def pageMenu():
global state
while state:
if ms.is_over_object(start):
if ms.is_button_pressed(1):
pageStart()
if ms.is_over_object(options):
if ms.is_button_pressed(1):
pageOptions()
if ms.is_over_object(exit):
if ms.is_button_pressed(1):
state = False
backgroundMenu.draw()
start.draw()
options.draw()
exit.draw()
janela.set_title("Menu")
janela.update()
def pageOptions():
global state, velAstro, velFire
while state:
if ms.is_over_object(menu):
if ms.is_button_pressed(1):
pageMenu()
backgroundOptions.draw()
menu.draw()
janela.set_title("Options")
janela.update()
while state:
pageMenu()