-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15_pygame_space_war.py
175 lines (140 loc) · 4.38 KB
/
15_pygame_space_war.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
import pygame
import random
import math
from pygame import mixer
pygame.init()
# Main Screen
screen = pygame.display.set_mode((600,957))
# load wallpaper
wallpaper = pygame.image.load('background-wallpaper.jpg')
# background music
mixer.music.load('background.mp3')
mixer.music.play(-1)
# Title and Icon
pygame.display.set_caption(".::SPACE WAR::.")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
# Player
playerImg = pygame.image.load('player.png')
playerX = 268
playerY = 720
playerX_change = 0
# enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 4
for i in range(num_of_enemies):
enemyType = random.randint(1,4)
if enemyType == 1 :
enemyImg.append(pygame.image.load('enemy1.png'))
elif enemyType == 2 :
enemyImg.append(pygame.image.load('enemy2.png'))
elif enemyType == 3 :
enemyImg.append(pygame.image.load('enemy3.png'))
elif enemyType == 4 :
enemyImg.append(pygame.image.load('enemy4.png'))
enemyX.append(random.randint(100,400))
enemyY.append(random.randint(50,150))
enemyX_change.append(0.5)
enemyY_change.append(10)
# bullet
bulletImg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = 720
bulletX_change = 0.1
bulletY_change = 1
bullet_state = "ready"
# display score text
score_value = 0
font = pygame.font.Font('Vazirmatn-Black.ttf',40)
scoreTextX = 10
scoreTextY = 10
def show_score(x,y):
score = font.render("SCORE : " + str(score_value) , True , (255,255,255))
screen.blit(score,(x,y))
# show game over text
def game_over_text():
game_over_font = pygame.font.Font('Vazirmatn-Black.ttf',60)
game_over_txt = game_over_font.render("GAME OVER" , True , (255,255,255))
screen.blit(game_over_txt,(120,400))
def fire_bullet(x,y):
screen.blit(bulletImg,(x+10,y+20))
def player(x,y):
screen.blit(playerImg,(x,y))
def enemy(x,y,i):
screen.blit(enemyImg[i] ,(x,y))
def isCollision(x1,y1,x2,y2):
# calculate distance between (x1,y1) and (x2,y2)
distance = math.sqrt(math.pow((x2-x1),2) + math.pow((y2-y1),2))
if distance < 30 :
return True
else :
return False
# Game Frames
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
playerX_change = 0.2
if event.key == pygame.K_LEFT:
playerX_change = -0.2
if event.key == pygame.K_SPACE:
if bullet_state is 'ready':
bulletX = playerX
bullet_state = "fire"
fire_bullet(bulletX,bulletY)
fire_sound = mixer.Sound('laser.wav')
fire_sound.play()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
screen.fill((0,128,128))
screen.blit(wallpaper,(0,0))
# player movement
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >=536 :
playerX = 536
# enemy movement
for i in range(num_of_enemies):
# GameOver Check
if enemyY[i] > 200:
for j in range(num_of_enemies):
enemyY[j] = 2000
game_over_text()
break
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 0.5
enemyY[i] += enemyY_change[i]
elif enemyX[i] >=536 :
enemyX_change[i] = -0.5
enemyY[i] += enemyY_change[i]
# bullet collision
collision = isCollision(enemyX[i] , enemyY[i] , bulletX , bulletY )
if collision:
bullet_state = 'ready'
bulletY = 720
score_value += 1
enemyX[i] = random.randint(100,400)
enemyY[i] = random.randint(50,150)
collision_sound = mixer.Sound('explosion.wav')
collision_sound.play()
enemy(enemyX[i],enemyY[i],i)
# bullet movement
if bullet_state is 'fire':
if bulletY <= 0 :
bullet_state = 'ready'
bulletY = 720
fire_bullet(bulletX , bulletY)
bulletY -= bulletY_change
player(playerX,playerY)
show_score(scoreTextX,scoreTextY)
pygame.display.update()