-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
225 lines (174 loc) · 6 KB
/
index.html
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
<html>
<head>
<title>Brython Test - Space Invaders</title>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/brython@3.9.5/brython_stdlib.js">
</script>
</head>
<body onload="brython()">
<script type="text/python">
import pygame # type: ignore
import random
import time
import math
from pygame import mixer
# Initialize pygame engine
pygame.init()
# Set the window size
size = width, height = 1024, 768
# size = width, height = 800, 600
# Create the screen (window)
screen = pygame.display.set_mode(size)
# Background image https://www.freepik.com/free-photos-vectors/background by rawpwww.freepik.com
if width == 800:
background = pygame.image.load("background-902x600.jpg")
else:
background = pygame.image.load("background-1154x768.jpg")
# Background sound
mixer.music.load("background.wav")
mixer.music.play(-1)
# Set the clock speed delta
clock = pygame.time.Clock()
score_value = 0
# font = pygame.font.Font('freesansbold.ttf', 32)
font = pygame.font.Font('Space.ttf', 28)
textX = 10
textY = 10
# Game Over text
over_font = pygame.font.Font('Space.ttf', 64)
def show_score(x, y):
score = font.render("Score: " + str(score_value), True, (255, 255, 255))
screen.blit(score, (x, y))
def game_over_text():
over_text = font.render("GAME OVER! Hit % - " + str(int(hit_average)), True, (255, 255, 255))
screen.blit(over_text, (int((width * .25) + 96), int((height * .35) + 64)))
shots_fired = 0
# Quit event
# looking for the escape key pressed or the X close window button clicked
def quitting(event):
escape = event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
x_button = event.type == pygame.QUIT
return x_button or escape
# Set title and icon (image made by Freepik <https://www.flaticon.com/authors/freepik>, Downloaded from www.flaticon.com)
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("ufo.png")
pygame.display.set_icon(icon)
# Set playerImg (image made by Freepik <https://www.flaticon.com>, Downloaded from www.flaticon.com)
playerImg = pygame.image.load("player.png")
playerX = int((width / 2) - 32)
playerY = int(height * 0.85)
playerX_change = 0
# Set enemyImg <https://icons8.com/icons/set/planet-globe"> UFO icon by icons8.com
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6
for i in range(num_of_enemies):
enemyImg.append(pygame.image.load("enemy.png"))
enemyX.append(random.randint(0, (height - 64)))
enemyY.append(random.randint(0, (int(height * 0.10))))
enemyX_change.append(4)
enemyY_change.append(40)
# Bullet Icon made by Those Icons https://www.flaticon.com/authors/those-icons> from www.flaticon.com
# State of 'ready' is waiting
# State of 'fired' is in motion
bulletImg = pygame.image.load("bullet.png")
bulletX = 0
bulletY = int(height * 0.85)
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
# Display player
def player(x, y):
screen.blit(playerImg, (x, y))
# Display enemy
def enemy(x, y, i):
screen.blit(enemyImg[i], (x, y))
# Fire the bullet
def fire_bullet(x, y):
global bullet_state
bullet_state = "fired"
screen.blit(bulletImg, (x + 16, y + 10))
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
running = True
while running:
for event in pygame.event.get():
if quitting(event):
running = False
# Move the player on the X-axis with the arrow keys
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -6
if event.key == pygame.K_RIGHT:
playerX_change = 6
# Fire the bullet with the space key
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_sound = mixer.Sound("laser.wav")
bullet_sound.play()
bulletX = playerX
fire_bullet(bulletX, bulletY)
shots_fired += 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or pygame.K_LEFT:
playerX_change = 0
# Set RGB values for background
screen.fill((0, 0, 0))
# Load bg image
screen.blit(background, (0, 0))
# Player movement
playerX += playerX_change
# Keep the players X-axis from going off the screen
if playerX <= 0:
playerX = 0
elif playerX >= width - 64:
playerX = width - 64
# Keep the enemies X-axis from going off the screen
for i in range(num_of_enemies):
# Game over
if enemyY[i] > playerY - 25: # Use playerY in production
for j in range(num_of_enemies):
enemyY[j] = 2000
hit_average = score_value / shots_fired
game_over_text()
break
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 4
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= width - 64:
enemyX_change[i] = -4
enemyY[i] += enemyY_change[i]
# Test for collsion
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
explosion_sound = mixer.Sound("explosion.wav")
explosion_sound.play()
bulletY = int(height * 0.85)
bullet_state = "ready"
score_value += 1
enemyX[i] = random.randint(0, (height - 64))
enemyY[i] = random.randint(0, 150)
enemy(enemyX[i], enemyY[i], i)
# Bullet movement
if bulletY <= 0:
bullet_state = "ready"
bulletY = int(height * 0.85)
if bullet_state == "fired":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
show_score(textX, textY)
# Update the screen
pygame.display.update()
clock.tick(60)
</body>
</html>