This repository has been archived by the owner on May 3, 2024. It is now read-only.
/
main.py
254 lines (201 loc) · 6.21 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
import pygame
import sys
from enum import Enum
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 1000
# -----------------------------
# CLASSES
class Actions(Enum):
LEFT = 1
RIGHT = 2
FIRE = 3
STOP = 4
class Ship:
x = SCREEN_WIDTH / 2
y = SCREEN_HEIGHT - 100
vx = 10
width = 60
height = 32
fireLock = 0
pic = pygame.image.load("./assets/images/ship.png")
def __init__(self):
pass
def draw(self, display):
pos = (self.x - self.width / 2, self.y - self.height / 2, self.width, self.height)
# pygame.draw.rect(display, (255, 0, 0), pos)
display.blit(self.pic, pos)
def is_open_fire(self):
if self.fireLock == 0:
return True
else:
return False
class Enemy:
x = 0
y = 0
width = 48
height = 32
vx = 20
vy = 80
score = 30
moveCnt = 50
isHitWall = False
isMoveY = False
pic = pygame.image.load("./assets/images/InvaderA1.png")
def __init__(self):
pass
def __init__(self, x0, y0, cnt):
self.x = x0
self.y = y0
self.moveCnt = cnt
def update(self):
if self.moveCnt == 0:
self.moveCnt = 50
# if hit wall
if (self.vx > 0 and self.x > SCREEN_WIDTH - self.width / 2) or (self.vx < 0 and self.x < 0 + self.width):
self.isHitWall = True
self.x = self.x + self.vx
self.moveCnt = self.moveCnt - 1
def move_y(self):
self.vx = 0 - self.vx
self.y = self.y + self.vy
self.isHitWall = False
def draw(self, display):
pos = (self.x - self.width / 2, self.y - self.height / 2, self.width, self.height)
# pygame.draw.rect(display, (0, 255, 0), pos)
display.blit(self.pic, pos)
def is_hit(self, x, y):
if (x >= self.x - self.width / 2) and (x <= self.x + self.width / 2) and (y >= self.y - self.width / 2) and (y <= self.y + self.width / 2):
return True
else:
return False
class Bullet:
x = 0
y = 0
vy = 0
width = 6
height = 17
pic = pygame.image.load("./assets/images/bullet.png")
def __init__(self):
pass
def __init__(self, x0, y0, vel):
self.x = x0
self.y = y0
self.vy = vel
def update(self):
self.y = self.y + self.vy
def draw(self, display):
pos = (self.x - self.width / 2, self.y - self.height / 2, self.width, self.height)
# pygame.draw.rect(display, (200, 200, 200), pos)
display.blit(self.pic, pos)
def is_out_of_screen(self):
if self.y < 0 or self.y > 1000:
return True
else:
return False
# -----------------------------
# GLOBAL VARIABLES
action = Actions.STOP
score = 0
ship = Ship()
enemies = []
bullets = []
for i in range(5):
for j in range(3):
enemy = Enemy(100 * i + 100, 80 * j + 300, 30)
enemies.append(enemy)
if __name__ == "__main__":
# -----------------------------
# initialize pygame
pygame.init()
# load assets
font = pygame.font.SysFont("comicsansms", 36)
bg = pygame.image.load("./assets/images/bg1.jpg")
soundFire = pygame.mixer.Sound("./assets/sounds/ShipBullet.wav")
soundDestroyed = pygame.mixer.Sound("./assets/sounds/InvaderHit.wav")
# -----------------------------
# show display
gameDisplay = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Space Invader')
clock = pygame.time.Clock()
# -----------------------------
# the main loop of the game
crashed = False
while not crashed:
# -----------------------------
# 1. handling events
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
crashed = True
# keys = pygame.key.get_pressed()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
action = Actions.LEFT
elif event.key == pygame.K_RIGHT:
action = Actions.RIGHT
elif event.key == pygame.K_SPACE:
action = Actions.FIRE
else:
action = Actions.STOP
# -----------------------------
# 2. update states
# update the ship
if action == Actions.LEFT:
ship.x = ship.x - ship.vx
elif action == Actions.RIGHT:
ship.x = ship.x + ship.vx
elif action == Actions.FIRE:
if ship.is_open_fire():
soundFire.play()
bullet = Bullet(ship.x, ship.y, -10)
bullets.append(bullet)
ship.fireLock = 50
if ship.fireLock != 0:
ship.fireLock = ship.fireLock - 1
# update all bullets
for i in bullets:
i.update()
# hit any enemy?
for j in enemies:
if j.is_hit(i.x, i.y):
# destroy enemy
score = score + j.score
enemies.remove(j)
soundDestroyed.play()
# destroy bullet
bullets.remove(i)
# out of screen?
if i.is_out_of_screen():
# destroy bullet
bullets.remove(i)
print(bullets)
# update all enemies
for i in enemies:
i.update()
for i in enemies:
if i.isHitWall:
for j in enemies:
j.move_y()
# win? lose?
# -----------------------------
# 3. draw everything
# background
gameDisplay.blit(bg, (0, 0))
# score
text = font.render("Score: {:d}".format(score), True, (0, 128, 0))
gameDisplay.blit(text, (0, 0)) #(0 + text.get_width(), 0 + text.get_height()))
# draw the ship
ship.draw(gameDisplay)
# draw bullets
for i in bullets:
i.draw(gameDisplay)
# draw enemies
for i in enemies:
i.draw(gameDisplay)
# update display
pygame.display.update()
clock.tick(60)
# -----------------------------
# quit game
pygame.quit()
sys.exit()