-
Notifications
You must be signed in to change notification settings - Fork 0
/
eat-the-bullet.py
212 lines (171 loc) · 5.65 KB
/
eat-the-bullet.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
211
212
import sys, pygame
from random import randint
pygame.init()
#fetch high score
high_score = 0
def getHighScore():
global high_score
high_score_file = open("high_score.txt", "r")
high_score = int(high_score_file.read())
getHighScore()
score = 0
game_over = 0
gameLoop = True
updateContent = True
fps = 60
size = width, height = 500, 700
white = 255, 255, 255
black = 0, 0, 0
movement_on_press = 12
clock = pygame.time.Clock();
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Eat-The-Bullet')
myfont = pygame.font.SysFont('Comic Sans MS', 25)
score_textsurface = myfont.render('Press Up ', False, black)
quit_textsurface = myfont.render('Press \'X\' to quit', False, black)
miss_textsurface = myfont.render('Miss - ' + str(game_over), False, black)
gameOverMyFont = pygame.font.SysFont('Comic Sans MS', 50)
game_over_text_surface = gameOverMyFont.render('GAME OVER', False, black)
retry_text_surface = myfont.render('Press \'R\' to retry', False, black)
high_score_surface = myfont.render('High Score - ' + str(high_score), False, black)
pills = [pygame.image.load("pill.png"), pygame.image.load("pill.png"), pygame.image.load("pill.png"), pygame.image.load("pill.png")]
pills_rects = [pills[0].get_rect(), pills[1].get_rect(), pills[2].get_rect(), pills[3].get_rect()]
flap = pygame.image.load("flappy.png")
flap_width = flap_height = 56
flap_rect = flap.get_rect()
flap_rect.x = 60
flap_rect.y = height - flap_height - 10
y_change = 0
pills_rects[0].x = width
pills_rects[0].y = randint(15, height -flap_height)
pills_rects[1].x = width
pills_rects[1].y = randint(15, height -flap_height)
pills_rects[2].x = width
pills_rects[2].y = randint(15, height -flap_height)
pills_rects[3].x = width
pills_rects[3].y = randint(15, height -flap_height)
def updateHighScore():
high_score_file = open("high_score.txt", "w")
high_score_file.write(str(high_score))
def moveFlappy():
flap_rect.y += y_change
if flap_rect.y > height - flap_height - 10:
flap_rect.y = height - flap_height - 10
elif flap_rect.y < 15:
flap_rect.y = 15
def startPills():
global game_over
pills_rects[0].x -= randint(1,3)
if pills_rects[0].x < 0:
game_over += 1
pills_rects[0].x = width
pills_rects[0].y = randint(15, height-flap_height)
pills_rects[1].x -= randint(1,3)
if pills_rects[1].x < 0:
game_over += 1
pills_rects[1].x = width
pills_rects[1].y = randint(15, height-flap_height)
pills_rects[2].x -= randint(1,3)
if pills_rects[2].x < 0:
game_over += 1
pills_rects[2].x = width
pills_rects[2].y = randint(15, height-flap_height)
pills_rects[3].x -= randint(1,3)
if pills_rects[3].x < 0:
game_over += 1
pills_rects[3].x = width
pills_rects[3].y = randint(15, height-flap_height)
#for i in range(0, 4):
# pills_rects[i].x -= randint(1,3)
# if pills_rects[i].x < 0:
# pills_rects[i].x = width
# pills_rects[i].y = randint(15, height-flap_height)
def collisionCheck():
global score
if flap_rect.colliderect(pills_rects[0]):
score += 1
pills_rects[0].x = width
pills_rects[0].y = randint(15, height-flap_height)
if flap_rect.colliderect(pills_rects[1]):
score += 1
pills_rects[1].x = width
pills_rects[1].y = randint(15, height-flap_height)
if flap_rect.colliderect(pills_rects[2]):
score += 1
pills_rects[2].x = width
pills_rects[2].y = randint(15, height-flap_height)
if flap_rect.colliderect(pills_rects[3]):
score += 1
pills_rects[3].x = width
pills_rects[3].y = randint(15, height-flap_height)
#for i in range(0, 4):
# if flap_rect.colliderect(pills_rects[i]):
# score += 1
# pills_rects[i].x = width
# pills_rects[i].y = randint(15, height-flap_height)
def updateScore():
score_string = 'Score - ' + str(score)
global score_textsurface
score_textsurface = myfont.render(score_string, False, (0, 0, 0))
def updateMisses():
global miss_textsurface
miss_score = 'Miss - ' + str(game_over) + '(Max allowed 5)'
miss_textsurface = myfont.render(miss_score, False, (0, 0, 0))
def gameOver():
global high_score_surface
global updateContent
global high_score
if game_over >= 5:
updateContent = False
if high_score < score:
high_score = score
updateHighScore()
high_score_surface = myfont.render('High Score - ' + str(high_score), False, black)
screen.blit(game_over_text_surface, (width - 300, height - 300))
screen.blit(retry_text_surface, (width - 350, height - 20))
def reset():
global game_over
global score
global updateContent
for i in range(0, 4):
pills_rects[i].x = width
flap_rect.x = 60
game_over = 0
score = 0
updateContent = True
flap_rect.y = height - flap_height - 10
while gameLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
sys.exit()
if event.key == pygame.K_r:
reset()
if event.key == pygame.K_UP:
y_change += -movement_on_press
if event.key == pygame.K_DOWN:
y_change += movement_on_press
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
y_change += movement_on_press
if event.key == pygame.K_DOWN:
y_change += -movement_on_press
if updateContent:
moveFlappy()
startPills()
collisionCheck()
updateScore()
updateMisses()
screen.fill(white)
for i in range(0,4):
screen.blit(pills[i], pills_rects[i])
screen.blit(score_textsurface, (width - 100, 0))
screen.blit(high_score_surface, (0, 0))
screen.blit(quit_textsurface, (width - 150, height - 20))
screen.blit(miss_textsurface, (width - 300, 0))
screen.blit(flap, flap_rect)
gameOver()
pygame.display.update()
clock.tick(fps)