-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
273 lines (223 loc) · 7.93 KB
/
game.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
import pygame
from pygame.locals import Color
import os
# Window Size, Fonts, and Images
WIN_WIDTH = 800
WIN_HEIGHT = 600
pygame.font.init()
STAT_FONT = pygame.font.SysFont("comicsans", 50)
END_FONT = pygame.font.SysFont("comicsans", 80)
SHIP_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs", "ship.png")), (64, 64))
SHOT_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs", "shot.png")), (32, 32))
ENEMY_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs", "enemy.png")), (32, 32))
BG_IMG = pygame.transform.scale(pygame.image.load(os.path.join("imgs", "galaxy.jpg")), (WIN_WIDTH, WIN_HEIGHT))
# User controlled space ship
class Ship:
def __init__(self, x, y):
self.x = x
self.y = y
self.movements = [] # Holds user input (left is -5 and right is 5)
self.img = SHIP_IMG
def move(self, change):
self.movements.append(change)
def release(self, change):
self.movements.remove(change)
# Move the space ship
def update(self):
if len(self.movements) != 0:
self.x += self.movements[-1]
if self.x < 0:
self.x = 0
elif self.x > 750:
self.x = 750
def draw(self, win):
win.blit(self.img, (self.x, self.y))
# Shot which is launched by the Ship
class Shot:
def __init__(self):
self.x = 0
self.y = 0
self.speed = 5
self.state = "ready"
self.img = SHOT_IMG
# Stores x and y of launch
def launch(self, x, y):
if self.state == "ready":
self.x = x
self.y = y
self.state = "launching"
# Move the shot
def update(self):
if self.state == "launching":
self.y -= self.speed
if (self.y < 0):
self.state = "ready"
def draw(self, win):
if self.state == "launching":
win.blit(self.img, (self.x, self.y))
# Get the mask of the current location to detect collisions
def get_mask(self):
return pygame.mask.from_surface(self.img)
class Enemy:
def __init__(self, x, y):
self.x = x
self.y = y
self.updatesCounter = 1 # Number of past updates since y change
self.updatesNeeded = 110 # Number of updates until y change
self.state = "ready"
self.img = ENEMY_IMG
# Move enemy and check if it won
def update(self):
if self.state == "ready":
# Occassionaly move enemy right based on counter
if self.updatesCounter % 30 == 0:
self.x += 50
# Reached number of updates to move y
if self.updatesCounter > self.updatesNeeded:
self.y += 50
self.x -= 150
self.updatesCounter = 1
else:
self.updatesCounter = self.updatesCounter + 1
# Enemy wins if it passes the height of the window
if (self.y >= WIN_HEIGHT):
self.state = "won"
# Check if enemy is shot, otherwise
# Move enemy and check if it won
def checkCollisionandUpdate(self, shot):
if self.state == "ready":
# Check Collision
shot_mask = shot.get_mask()
enemy_mask = pygame.mask.from_surface(self.img)
offset = (self.x - shot.x, self.y - round(shot.y))
overlap = shot_mask.overlap(enemy_mask, offset)
if overlap:
self.state = "hit"
shot.state = "ready"
return 1 # increment score
# Move enemy and check if it won
if self.updatesCounter % 30 == 0:
self.x += 50
if self.updatesCounter > self.updatesNeeded:
self.y += 50
self.x -= 150
self.updatesCounter = 1
else:
self.updatesCounter = self.updatesCounter + 1
# Enemy wins if it passes the height of the window
if (self.y >= WIN_HEIGHT):
self.state = "won"
return 0 # no hit
def draw(self, win):
win.blit(self.img, (self.x, self.y))
# Draw each element on the screen during the game
def draw_windows(win, ship, shot, enemies, score):
win.blit(BG_IMG, (0, 0))
# Draw score
text = STAT_FONT.render("Score: " + str(score), 1, (255, 255, 255))
win.blit(text, (WIN_WIDTH - 10 -text.get_width(), 10))
# Draw ship
ship.draw(win)
# Draw shot
shot.draw(win)
# Draw each enemy
for enemy in enemies:
enemy.draw(win)
pygame.display.update()
def draw_end_screen(win, score):
win.blit(BG_IMG, (0, 0))
# Draw end screen
score_text = END_FONT.render('Score: ' + str(score), 1, (255, 255, 255))
win.blit(score_text, (WIN_WIDTH/2 - score_text.get_width()/2, 150))
exit_text = STAT_FONT.render("Click to Exit", 1, (255, 255, 255))
win.blit(exit_text, (WIN_WIDTH/2 - exit_text.get_width()/2, 240))
pygame.display.update()
# Wait for Exit
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
run = False
def main():
# Create ship and shot
ship = Ship(400,480)
shot = Shot()
# Create each enemy starting at (275, 50)
num_of_enemies = 10
enemies = []
enemy_x = 275
enemy_y = 50
while(num_of_enemies > 0):
# Check if new row is needed to store enemies
if enemy_x > 575:
enemy_x = 275
enemy_y += 50
#Create enemy
enemies.append(Enemy(enemy_x, enemy_y))
num_of_enemies = num_of_enemies - 1
# Increment position of next enemy
enemy_x += 50
# Start game
pygame.init()
win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
clock = pygame.time.Clock()
run = True
score = 0
while run:
clock.tick(30)
# Check key presses and releases
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
# Move ship left
if event.key == pygame.K_LEFT:
ship.move(-5)
# Move ship right
if event.key == pygame.K_RIGHT:
ship.move(5)
# Fire a shot
if event.key == pygame.K_SPACE:
shot.launch(ship.x, ship.y)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
ship.release(-5)
if event.key == pygame.K_RIGHT:
ship.release(5)
# Update each element
ship.update()
shot.update()
for enemy in enemies:
# Check if a enemy is hit by a bullet
if (shot.state == "launching"):
score += enemy.checkCollisionandUpdate(shot)
# Check if a enemy has reached the end
else:
enemy.update()
if enemy.state == "won":
run = False
if enemy.state == "hit":
enemies.remove(enemy)
# If all enemies are gone then create more
if len(enemies) == 0:
num_of_enemies = 10
while(num_of_enemies > 0):
# Check if new row is needed to store enemies
if enemy_x > 575:
enemy_x = 275
enemy_y += 50
#Create enemy
enemies.append(Enemy(enemy_x, enemy_y))
num_of_enemies = num_of_enemies - 1
# Increment position of next enemy
enemy_x += 50
# Draw each element
draw_windows(win, ship, shot, enemies, score)
# End of game
draw_end_screen(win, score)
pygame.quit()
quit()
main()