-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
194 lines (158 loc) · 4.93 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import pygame
import random
import math
# initilize the pygame and create window
pygame.init()
window = pygame.display.set_mode((800, 800))
# logo and title
pygame.display.set_caption(" Space Invaders")
pygame.display.set_icon(pygame.image.load("images/rocket-ship.png"))
# importing images
spaceImg = pygame.image.load("images/space.png")
spaceshipImg = pygame.image.load("images/rocket.png")
enemy1Img = pygame.image.load("images/ufo1.png")
enemy2Img = pygame.image.load("images/ufo2.png")
enemy3Img = pygame.image.load("images/ufo3.png")
bulletImg = pygame.image.load("images/bullet.png")
# player
playerX = 350
playerY = 700
playerXChange = 0
playerYChange = 0
# enemy1
enemyX1 = random.randint(0, 736)
enemyY1 = random.randint(0, 100)
enemyX1Change = 0.5
enemyY1Change = 50
# enemy2
enemyX2 = random.randint(0, 736)
enemyY2 = random.randint(0, 100)
enemyX2Change = 0.8
enemyY2Change = 20
# enemy3
enemyX3 = random.randint(0, 736)
enemyY3 = random.randint(0, 100)
enemyX3Change = 1
enemyY3Change = 20
# bullet
showBullet = False
bulletX = playerX + 20
bulletY = playerY
bulletXChange = 0
bulletYChange = -1.5
# score
score = 0
font = pygame.font.Font("freesansbold.ttf", 32)
scoreX = 10
scoreY = 10
def showScore(x, y):
scoreToShow = font.render("Score: " + str(score), True, (255, 255, 255))
window.blit(scoreToShow, (x, y))
def player(x, y):
# top screen drawing
window.blit(spaceshipImg, (x, y))
def enemy(img, x, y):
# top screen drawing
window.blit(img, (x, y))
def bullet(x, y):
# top screen drawing
window.blit(bulletImg, (x, y))
# find distance between bullet and enemy
def distance(x1, x2, y1, y2):
return int(math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))))
# running the game
running = True
while running:
# background of game
window.fill((0, 0, 0))
window.blit(spaceImg, (0, 0))
# checks if the program has exited
for events in pygame.event.get():
if events.type == pygame.QUIT:
running = False
# check if keyboard is pressed
if events.type == pygame.KEYDOWN:
if events.key == pygame.K_LEFT:
playerXChange -= 1.2
if events.key == pygame.K_RIGHT:
playerXChange += 1.2
# checks if bullet is fired
if events.key == pygame.K_SPACE and showBullet == False:
showBullet = True
bulletX = playerX + 20
bulletY = playerY
if events.type == pygame.KEYUP:
if events.key == pygame.K_RIGHT or events.key == pygame.K_LEFT:
playerXChange = 0
# checks if bullet hit enemy
if distance(int(bulletX) + 16, int(enemyX1) + 32, int(bulletY) + 16, int(enemyY1) + 32) <= 32:
score += 1
enemyX1 = random.randint(0, 736)
enemyY1 = random.randint(0, 100)
bulletY = 1000
showBullet = False
if distance(int(bulletX) + 16, int(enemyX2) + 32, int(bulletY) + 16, int(enemyY2) + 32) <= 32:
score += 1
enemyX2 = random.randint(0, 736)
enemyY2 = random.randint(0, 100)
bulletY = 1000
showBullet = False
if distance(int(bulletX) + 16, int(enemyX3) + 32, int(bulletY) + 16, int(enemyY3) + 32) <= 40:
score += 1
enemyX3 = random.randint(0, 736)
enemyY3 = random.randint(0, 100)
bulletY = 1000
showBullet = False
# change the value of x
playerX += playerXChange
enemyX1 += enemyX1Change
enemyX2 += enemyX2Change
enemyX3 += enemyX3Change
# checks if player is in window
if playerX >= 736:
playerX = 736
if playerX <= 0:
playerX = 0
# enemy1 movement
if enemyX1 >= 736:
enemyX1 = 736
enemyX1Change = -0.5
enemyY1 += enemyY1Change
if enemyX1 <= 0:
enemyX1 = 0
enemyX1Change = 0.5
enemyY1 += enemyY1Change
# enemy2 movement
if enemyX2 >= 736:
enemyX2 = 736
enemyX2Change = -0.8
enemyY2 += enemyY1Change
if enemyX2 <= 0:
enemyX2 = 0
enemyX2Change = 0.8
enemyY2 += enemyY1Change
# enemy2 movement
if enemyX3 >= 736:
enemyX3 = 736
enemyX3Change = -1
enemyY3 += enemyY1Change
if enemyX3 <= 0:
enemyX3 = 0
enemyX3Change = 1
enemyY3 += enemyY1Change
# bullet movement
if bulletY <= 0:
bulletY = 1000
showBullet = False
# calling player function
player(playerX, playerY)
# calling enemy functions
enemy(enemy1Img, enemyX1, enemyY1)
enemy(enemy2Img, enemyX2, enemyY2)
enemy(enemy3Img, enemyX3, enemyY3)
# calling bullet function
if showBullet:
bullet(bulletX, bulletY)
bulletY += bulletYChange
showScore(scoreX, scoreY)
pygame.display.update()