-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
210 lines (172 loc) · 4.58 KB
/
main.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
204
205
206
207
208
209
210
import pygame
import time
import random
import math
#to initialize the pygame module
pygame.init()
# To start the screen at 600X800 resolution.
screen = pygame.display.set_mode((800, 600))
#background Image
background = pygame.image.load('evening_1.jpg').convert()
#Title and Icon
pygame.display.set_caption("Corona Strike 0.0.1")
icon = pygame.image.load('Code_Maniac.jpg')
pygame.display.set_icon(icon)
running = True
pygame.mixer.music.load('background.wav')
pygame.mixer.music.play(-1)
# Game Over
over_font = pygame.font.Font('freesansbold.ttf', 64)
# score functions
score = 0
font = pygame.font.Font('DoubleFeature20.ttf', 48)
textX = 10
textY = 10
#To make it full screen -
#pygame.display.toggle_fullscreen()
#Player - Protogonist
playerImg = pygame.image.load('spaceship48X48.png')
playerX = 400
playerY = 500
coor_player = (playerX, playerY)
player_movX = 0
player_movY = 0
#Bullet - kill la kill
bulletImg = pygame.image.load('bullet_1.png')
bulletX = 0
bulletY = 0
bullet_movY = 5
bullet_state = 'ready'
#level 1 corona-virus
corona_1_Img = pygame.image.load('corona_1.png')
corona_1X = random.randint(100, 600)
corona_1Y = random.randint(50, 150)
corona_movX = 1
corona_movY = 48
def my_score():
textX = 10
textY = 10
blit_score = font.render("SCORE : " + str(score), True, (255, 255, 255))
screen.blit(blit_score, (textX, textY))
def game_over_text():
over_text = over_font.render("GAME OVER", True, (255, 255, 255))
screen.blit(over_text, (200, 250))
def player(x, y):
screen.blit(playerImg, (x, y))
def fire_bullet(x, y):
global bullet_state # bullet is currently fired. and you can see it
if bullet_state == 'fired':
screen.blit(bulletImg, (x + 10, y - 15))
def corona_1(corona_1X, corona_1Y):
screen.blit(corona_1_Img, (corona_1X, corona_1Y))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.hypot(enemyX - bulletX, enemyY - bulletY)
if distance < 27:
return True
return False
#main game-loop
while running:
# Background color
screen.fill((0,0,0))
# Background Image
screen.blit(background, (0, 0))
player(playerX, playerY)
corona_1(corona_1X, corona_1Y)
#sleep(5)
my_score()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check its left or right
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player_movX = 2
print("right")
elif event.key == pygame.K_LEFT:
player_movX = -2
print("left")
elif event.key == pygame.K_UP:
player_movY = -2
print("Up")
elif event.key == pygame.K_DOWN:
player_movY = 2
print("Down")
elif event.key == pygame.K_SPACE:
print("Shoot.")
if bullet_state == 'ready':
bullet_sound = pygame.mixer.Sound('laser.wav')
bullet_sound.play()
bullet_state = 'fired'
bulletX = playerX
bulletY = playerY
fire_bullet(bulletX, bulletY)
elif event.key == pygame.K_ESCAPE:
print("Escape key.")
else:
pass
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
print("space released")
elif event.key == pygame.K_ESCAPE:
running = False
print("Existing the Game.")
elif event.key == pygame.K_RIGHT:
player_movX = 0
print("right")
elif event.key == pygame.K_LEFT:
player_movX = 0
print("left")
elif event.key == pygame.K_UP:
player_movY = 0
print("Up")
elif event.key == pygame.K_DOWN:
player_movY = 0
print("Down")
# Boundary conditions : don't leave the screen player
#for player
if playerX <= 0:
playerX = 0
elif playerX >= 752:
playerX = 752
if playerY <= 0:
playerY = 0
elif playerY >= 552:
playerY = 552
playerX += player_movX
playerY += player_movY
#for Bullet
if bullet_state == 'fired':
fire_bullet(bulletX, bulletY)
bulletY -= bullet_movY
if bulletY <= 0:
bullet_state = 'ready'
# collision
if isCollision(corona_1X, corona_1Y, bulletX, bulletY):
explosion_sound = pygame.mixer.Sound('explosion.wav')
explosion_sound.play()
bullet_state = 'ready'
bulletY = 800
score += 1
corona_1X = random.randint(100, 600)
corona_1Y = random.randint(50, 150)
print(score)
if isCollision(corona_1X, corona_1Y, playerX, playerY):
running = False
#for corona_1
if corona_1X <= 0:
corona_1X = 0
corona_movX = -corona_movX
corona_1Y += corona_movY
elif corona_1X >= 752:
corona_1X = 752
corona_movX = -corona_movX
corona_1Y += corona_movY
if corona_1Y <= 0:
corona_1Y = 0
elif corona_1Y >= 552:
corona_1Y = 552
corona_1X += corona_movX
if running == False:
game_over_text()
pygame.display.update()
time.sleep(0.5)