-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathspace invaders.py
223 lines (177 loc) · 6.28 KB
/
space invaders.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
# -----------------------------------------------------------------------------
#
# Space Invaders
# Language - Python
# Modules - pygame, sys, time
#
# Controls - Left and Right Keys to Move, Space to shoot
#
# By - Jatin Kumar Mandav
#
# Website - https://jatinmandav.wordpress.com
#
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
# Twitter - @jatinmandav
#
# -----------------------------------------------------------------------------
import pygame
import sys
import time
# -------------- Initialization ------------
pygame.init()
width = 700
height = 500
display = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
pygame.display.set_caption("Space Invaders")
ship_width = 40
ship_height = 30
# -------------- Colours -----------------
background = (74, 35, 90)
white = (244, 246, 247)
yellow = (241, 196, 15)
orange = (186, 74, 0)
green = (35, 155, 86)
white1 = (253, 254, 254)
dark_gray = (23, 32, 42)
# -------------- Space-Ship Class --------------
class SpaceShip:
def __init__(self, x, y, w, h, colour):
self.x = x
self.y = y
self.w = w
self.h = h
self.colour = colour
def draw(self):
pygame.draw.rect(display, yellow, (self.x + self.w/2 - 8, self.y - 10, 16, 10))
pygame.draw.rect(display, self.colour, (self.x, self.y, self.w, self.h))
pygame.draw.rect(display, dark_gray, (self.x + 5, self.y + 6, 10, self.h - 10))
pygame.draw.rect(display, dark_gray, (self.x + self.w - 15, self.y + 6, 10, self.h - 10))
# ----------------- Bullet Class -------------
class Bullet:
def __init__(self, x, y):
self.x = x
self.y = y
self.d = 10
self.speed = -5
def draw(self):
pygame.draw.ellipse(display, orange, (self.x, self.y, self.d, self.d))
def move(self):
self.y += self.speed
def hit(self, x, y, d):
if x < self.x < x + d:
if y + d > self.y > y:
return True
# ------------------ Alien Class ---------------
class Alien:
def __init__(self, x, y, d):
self.x = x
self.y = y
self.d = d
self.x_dir = 1
self.speed = 3
def draw(self):
pygame.draw.ellipse(display, green, (self.x, self.y, self.d, self.d))
pygame.draw.ellipse(display, dark_gray, (self.x + 10, self.y + self.d/3, 8, 8), 2)
pygame.draw.ellipse(display, dark_gray, (self.x + self.d - 20, self.y + self.d/3, 8, 8), 2)
pygame.draw.rect(display, dark_gray, (self.x, self.y+self.d-20, 50, 7))
def move(self):
self.x += self.x_dir*self.speed
def shift_down(self):
self.y += self.d
# ------------------- Saved ------------------
def saved():
font = pygame.font.SysFont("Wide Latin", 22)
font_large = pygame.font.SysFont("Wide Latin", 43)
text2 = font_large.render("Congratulations!", True, white1)
text = font.render("You Prevented the Alien Invasion!", True, white1)
display.blit(text2, (60, height/2))
display.blit(text, (45, height/2 + 100))
pygame.display.update()
time.sleep(3)
# -------------------- Death ----------------
def GameOver():
font = pygame.font.SysFont("Chiller", 50)
font_large = pygame.font.SysFont("Chiller", 100)
text2 = font_large.render("Game Over!", True, white1)
text = font.render("You Could not Prevent the Alien Invasion!", True, white1)
display.blit(text2, (180, height/2-50))
display.blit(text, (45, height/2 + 100))
# --------------------- The Game ------------------
def game():
invasion = False
ship = SpaceShip(width/2-ship_width/2, height-ship_height - 10, ship_width, ship_height, white)
bullets = []
num_bullet = 0
for i in range(num_bullet):
i = Bullet(width/2 - 5, height - ship_height - 20)
bullets.append(i)
x_move = 0
aliens = []
num_aliens = 8
d = 50
for i in range(num_aliens):
i = Alien((i+1)*d + i*20, d+20, d)
aliens.append(i)
while not invasion:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.key == pygame.K_RIGHT:
x_move = 5
if event.key == pygame.K_LEFT:
x_move = -5
if event.key == pygame.K_SPACE:
num_bullet += 1
i = Bullet(ship.x + ship_width/2 - 5, ship.y)
bullets.append(i)
if event.type == pygame.KEYUP:
x_move = 0
display.fill(background)
for i in range(num_bullet):
bullets[i].draw()
bullets[i].move()
for alien in list(aliens):
alien.draw()
alien.move()
for item in list(bullets):
if item.hit(alien.x, alien.y, alien.d):
bullets.remove(item)
num_bullet -= 1
aliens.remove(alien)
num_aliens -= 1
if num_aliens == 0:
saved()
invasion = True
for i in range(num_aliens):
if aliens[i].x + d >= width:
for j in range(num_aliens):
aliens[j].x_dir = -1
aliens[j].shift_down()
if aliens[i].x <= 0:
for j in range(num_aliens):
aliens[j].x_dir = 1
aliens[j].shift_down()
try:
if aliens[0].y + d > height:
GameOver()
pygame.display.update()
time.sleep(3)
invasion = True
except Exception as e:
pass
ship.x += x_move
if ship.x < 0:
ship.x -= x_move
if ship.x + ship_width > width:
ship.x -= x_move
ship.draw()
pygame.display.update()
clock.tick(60)
# ----------------- Calling the Game Function ---------------------
game()