-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
176 lines (137 loc) · 4.06 KB
/
app.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
import pygame
import math
from pygame import mixer
# initialize pygame
pygame.init()
# Create screen
screenX = 800
screenY = 600
screen = pygame.display.set_mode((screenX, screenY))
# Make Icon
title = pygame.display.set_caption("Space Demon")
icon = pygame.image.load("spaceship.png")
pygame.display.set_icon(icon)
# Background
background = pygame.image.load("space.png")
# Player Icon
player_img = pygame.image.load("spaceship.png")
playerX = 360
playerY = 460
playerX_change = 0
playerY_change = 0
# Enemy Icon
enemy_img = pygame.image.load("pngfuel.com.png")
enemyX = 360
enemyY = 50
enemyX_change = 1.5
# Bullet icon
bullet_img = pygame.image.load("bullet.png")
bulletX = 0
bulletY = 0
bulletY_change = 7
bullet_state = "ready"
# Explosion icon
explosion = pygame.image.load("explosion.png")
# Enemy Health
enemy_hp = 100
enemy_max = 100
font = pygame.font.Font("freesansbold.ttf", 16)
# Game Over Text
game_over_font = pygame.font.Font("freesansbold.ttf", 64)
def game_over():
game_over_text = game_over_font.render("GAME OVER", True, (255, 0, 0))
screen.blit(game_over_text, (200, 250))
def show_hp():
eHp = font.render(f"HP : {str(enemy_hp)}/{str(enemy_max)}", True, (255, 255, 255))
screen.blit(eHp, (10, 10))
def player(x, y):
screen.blit(player_img, (x, y))
def enemy(x, y):
screen.blit(enemy_img, (x, y))
def fire(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bullet_img, (x + 8, y + 5))
def isCollision(enemyX, enemyY, bulletX, bulletY):
x_axis = enemyX - bulletX
y_axis = enemyY - bulletY
distance = math.sqrt((math.pow(x_axis, 2)) + (math.pow(y_axis, 2)))
if distance < 57:
return True
return False
def show_explosion(x, y):
screen.blit(explosion, (x, y))
# Game Loop
running = True
while running:
# Background Color
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
enemy(enemyX, enemyY)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -3
if event.key == pygame.K_RIGHT:
playerX_change = 3
if event.key == pygame.K_UP:
playerY_change = -3
if event.key == pygame.K_DOWN:
playerY_change = 3
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_sound = mixer.Sound("laser.wav")
bullet_sound.play()
bulletX = playerX
bulletY = playerY
fire(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
# Player Move
playerX += playerX_change
# Make X axis boundary
if playerX <= 0:
playerX = 0
elif playerX >= screenX - 32:
playerX = screenX - 32
playerY += playerY_change
# Make Y axis boundary
if playerY <= 0:
playerY = 0
elif playerY >= screenY - 32:
playerY = screenY - 32
# Enemy Move
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 1.5
elif enemyX >= screenX - 100:
enemyX_change = -1.5
# Bullet Move
if bulletY < 0:
bulletY = playerY
bulletX = playerX
bullet_state = "ready"
if bullet_state == "fire":
fire(bulletX, bulletY)
bulletY -= bulletY_change
# Check Collision
if isCollision(enemyX, enemyY, bulletX, bulletY):
col_sound = mixer.Sound("explosion.wav")
col_sound.play()
show_explosion(bulletX, bulletY)
bulletX = playerX
bulletY = playerY
bullet_state = "ready"
enemy_hp -= 10
if enemy_hp == 0:
show_explosion(enemyX, enemyY)
enemyY = 5000
game_over()
show_hp()
player(playerX, playerY)
pygame.display.update()