-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_mac.py
271 lines (232 loc) · 8.46 KB
/
main_mac.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import pygame
import time
import random
from pygame import mixer
speed_background = -3
pygame.init()
screen_dimensions = (1280, 720)
screen = pygame.display.set_mode(screen_dimensions)
# CAPTION AND ICON
pygame.display.set_caption('Flappy Bird')
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
# BACKGROUND
background = pygame.image.load('background.png')
background = pygame.transform.scale(background, screen_dimensions)
# BACKGROUND SOUND
mixer.music.load("Background.mpeg")
mixer.music.play(-1)
# Lower Part
lower_part = pygame.image.load('lower_part.png')
lower_part = pygame.transform.scale(lower_part, (1280, 70))
lower_part_X = 0
lower_part_2 = pygame.image.load('lower_part.png')
lower_part_2 = pygame.transform.scale(lower_part, (1280, 70))
lower_part_2_X = 1280
# BIRD
player_size = (70, 70)
player = pygame.image.load('player.png')
player = pygame.transform.scale(player, player_size)
player_up = pygame.image.load('player_up.png')
player_up = pygame.transform.scale(player_up, player_size)
player_down = pygame.image.load('player_down.png')
player_down = pygame.transform.scale(player_down, player_size)
player_down_killed = pygame.transform.rotate(player_down, -90)
player_up_killed = pygame.transform.rotate(player_up, -90)
player_X = 200
player_Y = 360
player_Y_change = 3
# OBSTACLES
obstacle_size = (480, 900)
obstacle_upper = pygame.image.load('obstacle.png')
obstacle_upper = pygame.transform.scale(obstacle_upper, obstacle_size)
obstacle_lower = pygame.transform.rotate(obstacle_upper, 180)
obstacle_X = [300, 650, 1000, 1350]
obstacle_upper_Y = [random.randint(-820, -520) for i in range(4)]
obstacle_lower_Y = [1100-abs(i) for i in obstacle_upper_Y]
# FONT
intro_font = pygame.font.Font("intro_font.ttf", 15)
font_start = pygame.font.Font('Minecraft.ttf', 25)
font1 = pygame.font.Font('Minecraft.ttf', 50)
font2 = pygame.font.Font('Minecraft.ttf', 150)
font3 = pygame.font.Font('Minecraft.ttf', 100)
# SCORE
score = 0
highest_score = 0
score_Img = pygame.image.load("Score.png")
# End Game
end_game = pygame.image.load('end_game.png')
end_game = pygame.transform.scale(end_game, (400, 100))
end_game_X = 430
end_game_Y = 200
end_game_change = -3
#
credit = intro_font.render("©AkiDudeja125", True, (0, 0, 0))
insta = intro_font.render("@akshitdudeja125", True, (0, 0, 0))
insta_logo = pygame.image.load("insta_logo.png")
insta_logo = pygame.transform.scale(insta_logo, (15, 15))
# Assigning various variables
attempt_no = 1
start_time = time.time()
flap = 0
restarter = 0
game_started = False
sound_played = 0
score_added = False
killed = False
restart_status = False
run_state = True
quit_game = False
# Game Loop
while run_state:
# Filling Screen
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
# Score Comparision
if score > highest_score:
highest_score = score
if game_started:
# Player
if killed == False:
if flap in range(0, 6):
player = player_up
elif flap in range(6, 11):
player = player_down
else:
flap = -1
else:
if flap in range(0, 6):
player = player_up_killed
elif flap in range(6, 11):
player = player_down_killed
else:
flap = -1
flap += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_state = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and not killed:
mixer.Sound("jump.mpeg").play()
player_Y_change = -12
if event.type == pygame.KEYUP and not killed:
player_Y_change = 7
if player_Y <= -10:
player_Y = -10
player_Y_change = 3
if player_Y > 600:
killed = True
player = player_down
player_Y = 600
if end_game_X <= -30 or end_game_X > 900:
end_game_change = -end_game_change
player_Y += player_Y_change
screen.blit(player, (player_X, player_Y))
# Score
if killed:
if flap in range(0, 6):
screen.blit(end_game, (end_game_X, end_game_Y))
if flap > 10:
flap = 0
player_Y_change = 7
speed_background = 0
for i in range(len(obstacle_X)):
obstacle_X[i] = 2000
play_again = font1.render(
'Auto Replay in Progress!', True, (0, 0, 0))
screen.blit(play_again, (330, 480))
screen.blit(score_Img, (500, 330))
sc = font1.render(str(score), True, (0, 0, 0))
bsc = font1.render(str(highest_score), True, (0, 0, 0))
if score<10:
screen.blit(sc, (555, 365))
else:
screen.blit(sc, (542, 365))
if highest_score<10:
screen.blit(bsc, (675, 365))
else:
screen.blit(bsc, (663, 365))
end_game_X += end_game_change
else:
if score<10:
sc = font2.render(str(score), True, (0,0,0))
screen.blit(sc, (605, 15))
sc = font2.render(str(score), True, (255,255,255))
screen.blit(sc, (600, 10))
else:
sc = font2.render(str(score), True, (0,0,0))
screen.blit(sc, (555, 15))
sc = font2.render(str(score), True, (255,255,255))
screen.blit(sc, (550, 10))
# Obstacle Movement
for i in range(len(obstacle_X)):
if obstacle_X[i] <= -300:
obstacle_X[i] = 1080
score_added = False
obstacle_upper_Y[i] = random.randint(-820, -520)
obstacle_lower_Y[i] = 1100-abs(obstacle_upper_Y[i])
obstacle_X[i] += speed_background
screen.blit(obstacle_upper, (obstacle_X[i], obstacle_upper_Y[i]))
screen.blit(obstacle_lower, (obstacle_X[i], obstacle_lower_Y[i]))
# Collision
if killed == False:
if obstacle_X[i] in range(-67, 53):
if player_Y not in range(obstacle_upper_Y[i]+885, obstacle_lower_Y[i]-60):
killed = True
elif obstacle_X[i] < -67 and score_added == False:
score += 1
if score % 5 == 0:
speed_background -= 1
score_added = True
else:
if sound_played == 1:
mixer.Sound("coll2.mpeg").play()
elif sound_played == 50:
mixer.Sound("coll.mpeg").play()
sound_played += 1
# Lower Part Movement
screen.blit(lower_part, (lower_part_X, 660))
lower_part_X += speed_background
screen.blit(lower_part_2, (lower_part_2_X, 660))
lower_part_2_X += speed_background
if lower_part_X <= -1280:
lower_part_X = 0
lower_part_2_X = 1280
# GAME RESTART
if killed and not restart_status:
restarter += 1
loading = font1.render(
str("..Loading..")[0:int(restarter/20)], True, (0, 0, 0))
screen.blit(loading, (520, 550))
if restarter == 250:
restart_status = True
if restart_status:
killed = False
restarter = 0
restart_status = False
player_Y = 360
obstacle_X = [300, 650, 1000, 1350]
obstacle_upper_Y = [random.randint(-820, -520) for i in range(4)]
obstacle_lower_Y = [1100-abs(i) for i in obstacle_upper_Y]
score = 0
player_Y_change = 3
sound_played = 0
speed_background = -3
end_game_change = -3
attempt_no += 1
if attempt_no > 10:
break
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_state = False
if time.time()-start_time > 4:
game_started = True
intro_screen = pygame.image.load('intro_screen.png')
screen.blit(intro_screen, (320, 120))
start_screen_text_2 = font_start.render(
'Game starting in ' + str(4-int(time.time()-start_time)), True, (0, 0, 0))
screen.blit(start_screen_text_2, (510, 400))
screen.blit(credit, (5, 695))
screen.blit(insta, (1150, 695))
screen.blit(insta_logo, (1130, 698))
pygame.display.update()