-
Notifications
You must be signed in to change notification settings - Fork 0
/
space_invaders.py
228 lines (192 loc) · 6.66 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
224
225
226
227
228
import pygame
from pygame import mixer
import math
import random
# CUSTOM LOOK FOR ERRORS DISPLAY
import pretty_errors
pretty_errors.configure(
separator_character='*',
line_color=pretty_errors.BRIGHT_RED + '-> ' + pretty_errors.default_config.line_color,
lines_before=5,
lines_after=5,
code_color=pretty_errors.BLUE,
filename_color=pretty_errors.BRIGHT_BLACK,
function_color=pretty_errors.BLACK,
line_number_color=pretty_errors.BRIGHT_GREEN + 'Line: '
)
from actors.player import Player
from actors.bullets import Bullets
from actors.enemies import Enemies
from actors.player_upgrades import PlayerUpgrades
# Initialize the Pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((1920, 1100))
'''
Keeping all media rendering outside the Main Game Loop
so rendering of images/sounds does not need to be re-rendered
at every 'game surface' update
'''
# Background image
background = pygame.image.load('./images/background.png')
# Background Sound
mixer.music.load("./sounds/background.wav")
mixer.music.set_volume(0.6)
mixer.music.play(-1)
# App Name and Icon
pygame.display.set_caption("Space Invaders Clone")
icon = pygame.image.load('./images/player_spaceship_sprite.png')
pygame.display.set_icon(icon)
# Player
playerImg = pygame.image.load('./images/player_spaceship_sprite.png')
playerX = 930
playerY = 1020
playerX_change = 0
# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 10
# Creates initial enemies
for i in range(num_of_enemies):
enemyImg.append(pygame.image.load('./images/enemy_spaceship_sprite.png'))
# Random coordinates for enemy spawning positions
enemyX.append(random.randint(0, 1856)) # 1856 = 1920 (game surface) - 64 (enemy ship width)
enemyY.append(random.randint(50, 400))
enemyX_change.append(4)
enemyY_change.append(40)
# Bullet
# Ready - bullet is hidden and ready to be fired
# Fire - The bullet is currently moving
bulletImg = pygame.image.load('./images/laser_bullet.png')
bulletX = 0
bulletY = 980
bulletX_change = 0
bulletY_change = 20
bullet_state = "ready"
# Score
score_value = 0
font = pygame.font.Font('./font/scifi.otf', 32)
textX = 10
textY = 10
# Game Over
over_font = pygame.font.Font('./font/scifi.otf', 64)
# Game Score section
def show_score(x, y):
score = font.render("Score : " + str(score_value), True, (255, 255, 0))
# blit means to draw the object on screen (game surface)
screen.blit(score, (x, y))
# Game Over text
def game_over_text():
over_text = over_font.render("GAME OVER!", True, (255, 0, 0))
screen.blit(over_text, (700, 500))
# Player - initial placement of player
def player(x, y):
screen.blit(playerImg, (x, y))
# Enemy - initial placement of all the enemies
def enemy(x, y, i):
screen.blit(enemyImg[i], (x, y))
# Bullet
# Fire - The bullet is currently moving
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 26, y + 10))
# Taking Enemy Ship and Bullet coordinates
# and if they are closer then 27px then they 'collided'
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt(math.pow(enemyX - bulletX, 2) + (math.pow(enemyY - bulletY, 2)))
if distance < 27:
return True
else:
return False
# Main Game Loop - keeps the game running
# all events in here
running = True
while running:
# RGB = Red, Green, Blue
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
# Controls & Quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed
if event.type == pygame.KEYDOWN:
# check whether its right arrow / left arrow / space bar
if event.key == pygame.K_LEFT:
playerX_change = -2
if event.key == pygame.K_RIGHT:
playerX_change = 2
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bulletSound = mixer.Sound("./sounds/laser.wav")
bulletSound.set_volume(0.3)
bulletSound.play()
# Get the current x coordinate of the spaceship
bulletX = playerX
fire_bullet(bulletX, bulletY)
# Stop movement of ship with KEYUP as arrows are released
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# Keep Player Spaceship from going off screen
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 1856: # 1920 (game surface) - 64 (player ship width)
playerX = 1856
# Enemy Movement
for i in range(num_of_enemies):
# Game Over
if enemyY[i] > 972: # Enemy Ship reaches Player Ship
for j in range(num_of_enemies):
enemyY[j] = 2000
# ADD
# GAME OVER SOUND EFFECT !!!!!!!!!!!!!!!!
game_over_text()
break
# ADD
# RESTART GAME | QUIT buttons !!!!!!!!!!!!!!!!!!!!
# Keep Enemy Spaceship from going off screen AND lower it when it hits boundary
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 2
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 1856: # 1920 (game surface) - 64 (player ship width)
enemyX_change[i] = -2
enemyY[i] += enemyY_change[i]
# Collision of bullet and enemy
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
explosionSound = mixer.Sound("./sounds/explosion.wav")
explosionSound.set_volume(0.3)
explosionSound.play()
bulletY = 480
bullet_state = "ready"
score_value += 1
# Next generation enemies speed up
if enemyX_change[i] > 0:
enemyX_change[i] += 1
else:
enemyX_change[i] -= 1
#num_of_enemies += 1 !!!!!!!!!!!!!!!!!!!!!!!!!
enemyX[i] = random.randint(0, 1856)
enemyY[i] = random.randint(50, 150)
# ADD:
# check for enemies overlapping and space them out !!!!!!!!!!!!!!!!!!!!!!
enemy(enemyX[i], enemyY[i], i)
# Bullet Movement
if bulletY <= 0:
bulletY = 1000
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
show_score(textX, textY)
# Continuous updating of game window
pygame.display.update()