-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
203 lines (158 loc) · 5 KB
/
application.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
import random
import math
import pygame
from pygame import mixer
from time import sleep
import easygui
easygui.msgbox("Get a score of 30 to WIN. Click OK to begin game. Designed by: jeffzy15", title="Notification")
# Initialise the pygame
pygame.init()
pygame.font.init()
# Create the Window
screen = pygame.display.set_mode((800, 600))
# Background
background = pygame.image.load("images/background.png")
# Background Music
mixer.music.load("audio/background.wav")
mixer.music.play(-100)
# Title and Icon
pygame.display.set_caption("Return of the Aliens")
icon = pygame.image.load("images/ufo.png")
pygame.display.set_icon(icon)
# Player
Player_Image = pygame.image.load("images/player.png")
PlayerX = 370
PlayerY = 480
Player_X_Change = 0
# Enemy
Enemy_Image = []
EnemyX = []
EnemyY = []
Enemy_X_Change = []
Enemy_Y_Change = []
num_of_enemies = 5
for i in range(num_of_enemies):
Enemy_Image.append(pygame.image.load("images/enemy.png"))
EnemyX.append(random.randint(0, 735))
EnemyY.append(random.randint(50, 150))
Enemy_X_Change.append(4)
Enemy_Y_Change.append(40)
# Bullet
# Ready - You cannot see the bullet
# Fire - You can see the bullet
Bullet_Image = pygame.image.load("images/bullet.png")
BulletX = 0
BulletY = 480
Bullet_X_Change = 0
Bullet_Y_Change = 40
Bullet_State = "Ready"
# Score
score_value = 0
font = pygame.font.Font("freesansbold.ttf", 32)
TextX = 10
TextY = 10
# Game Over Text
over_font = pygame.font.Font("freesansbold.ttf", 64)
win_font = pygame.font.Font("freesansbold.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 = over_font.render("GAME OVER", True, (255, 255, 255))
screen.blit(over_text, (200, 250))
def game_win_text():
winning_text = win_font.render("YOU WIN!", True, (255, 255, 255))
screen.blit(winning_text, (200, 250))
def player(x, y):
screen.blit(Player_Image, (x, y))
def enemy(x, y, i):
screen.blit(Enemy_Image[i], (x, y))
def fire_bullet(x, y):
global Bullet_State
Bullet_State = "Fire"
screen.blit(Bullet_Image, (x + 16, y + 10))
def is_Collision(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
# Game Loop
running = True
while running:
# RGB = Red, Green, Blue
screen.fill((0, 0, 0))
# Background image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether its left or right
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
Player_X_Change = -6
if event.key == pygame.K_RIGHT:
Player_X_Change = 6
if event.key == pygame.K_SPACE and Bullet_State == "Ready":
bullet_sound = mixer.Sound("audio/laser.wav")
bullet_sound.play()
if Bullet_State == "Ready":
# Get the x coordinate of the spaceship
BulletX = PlayerX
fire_bullet(BulletX, BulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
Player_X_Change = 0
# Checking for boundaries, ensuring it doesn't go out of boundaries
PlayerX += Player_X_Change
# Player Movement
if PlayerX <= 0:
PlayerX = 0
elif PlayerX >= 736:
PlayerX = 736
win = False
if score_value == 30:
win = True
if win:
game_win_text()
pygame.display.update()
pygame.time.delay(5000)
quit()
# Enemy Movement
for i in range(num_of_enemies):
# Game Over
if EnemyY[i] > 440:
for j in range(num_of_enemies):
EnemyY[j] = 2000
game_over_text()
pygame.display.update()
pygame.time.delay(5000)
quit()
EnemyX[i] += Enemy_X_Change[i]
if EnemyX[i] <= 0:
Enemy_X_Change[i] = 3
EnemyY[i] += Enemy_Y_Change[i]
elif EnemyX[i] >= 736:
Enemy_X_Change[i] = -3
EnemyY[i] += Enemy_Y_Change[i]
# Collision
collision = is_Collision(EnemyX[i],EnemyY[i],BulletX,BulletY)
if collision:
explosion_sound = mixer.Sound("audio/explosion.wav")
explosion_sound.play()
BulletY = 480
Bullet_State = "Ready"
score_value += 1
EnemyX[i] = random.randint(0, 735)
EnemyY[i] = random.randint(50, 150)
enemy(EnemyX[i], EnemyY[i], i)
# Bullet Movement
if BulletY <= 0:
BulletY = 480
Bullet_State = "Ready"
if Bullet_State == "Fire":
fire_bullet(BulletX, BulletY)
BulletY -= Bullet_Y_Change
player(PlayerX, PlayerY)
show_score(TextX, TextY)
pygame.display.update()