-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKill Corona
158 lines (126 loc) · 3.69 KB
/
Kill Corona
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
import pygame
import random
import math
from pygame import mixer
# initialize the pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((800, 600))
# background
mixer.music.load('background.wav')
mixer.music.play(-1)
# Title and Icon
pygame.display.set_caption("KILL CORONA")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
# tap
tapImg = pygame.image.load('tap.png')
tapX = 370
tapY = 50
tapX_change = 0
# corona
coronaImg = []
coronaX = []
coronaY = []
coronaX_change = []
coronaY_change = []
num_of_corona = 3
for i in range(num_of_corona):
coronaImg.append(pygame.image.load('corona.png'))
coronaX.append(random.randint(20, 780))
coronaY.append(random.randint(200, 220))
coronaX_change.append(4)
coronaY_change.append(4)
# drop
dropImg = pygame.image.load('drop.png')
dropX = 0
dropY = 150
dropX_change = 0
dropY_change = 1
drop_state = "ready" # Ready - No drop on screen
# Score
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
textX = 10
textY = 10
# Game over
over_text = pygame.font.Font('freesansbold.ttf', 72)
def show_score(x, y):
score = font.render("Score : " + str(score_value), True, (0, 0, 0))
screen.blit(score, (x, y))
def game_over_text():
over_text = font.render("Game over ", True, (0, 0, 0))
screen.blit(over_text, (300, 250))
def tap(x, y):
screen.blit(tapImg, (x, y))
def corona(x, y, i):
screen.blit(coronaImg[i], (x, y))
def fire_drop(x, y):
global drop_state
drop_state = "fire"
screen.blit(dropImg, (x + 16, y + 10))
def isCollision(coronaX, coronaY, dropX, dropY):
distance = math.sqrt((math.pow(coronaX - dropX, 2)) + (math.pow(coronaY - dropY, 2)))
if distance < 27:
return True
# Game Loop
running = True
while running:
# Adding colour to screen
screen.fill((255, 228, 181))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
tapX_change = -5
if event.key == pygame.K_RIGHT:
tapX_change = 5
if event.key == pygame.K_UP or event.key == pygame.K_SPACE:
if drop_state is "ready":
dropX = tapX
fire_drop(dropX, dropY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
tapX_change = 0
# tap movement
tapX += tapX_change
if tapX <= 0:
tapX = 0
elif tapX >= 690:
tapX = 690
# corona movement
for i in range(num_of_corona):
# Game Over
if coronaY[i] > 400:
for j in range(num_of_corona):
coronaY[j] = 2000
game_over_text()
break
coronaX[i] += coronaX_change[i]
if coronaX[i] <= 0:
coronaX_change[i] = 1
coronaY[i] += coronaY_change[i]
elif coronaX[i] >= 736:
coronaX_change[i] = -1
coronaY[i] += coronaY_change[i]
# Collision
collision = isCollision(coronaX[i], coronaY[i], dropX, dropY)
if collision:
dropY = 20
drop_state = "ready"
score_value += 1
coronaX[i] = random.randint(0, 736)
coronaY[i] = random.randint(200, 220)
corona(coronaX[i], coronaY[i], i)
# drop movement
if dropY >= 600:
dropY = 150
drop_state = "ready"
if drop_state is "fire":
fire_drop(dropX, dropY)
dropY += dropY_change
tap(tapX, tapY)
show_score(textX, textY)
pygame.display.update()