-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflappy_bird_clone.py
173 lines (128 loc) · 4.6 KB
/
flappy_bird_clone.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
import pygame
import time
from random import randint, randrange
black = (0, 0, 0)
white = (255, 255, 255)
sunset = (253, 72, 47)
greenyellow = (184,255, 0)
brightblue = (47, 228, 253)
orange = (255, 113, 0)
yellow = (255, 236, 0)
purple = (252, 67, 255)
colorChoices = [greenyellow,brightblue,orange,yellow,purple]
pygame.init()
surfaceWidth = 800
surfaceHeight = 500
imageHeigth = 43
imageWidth = 50
surface = pygame.display.set_mode((surfaceWidth,surfaceHeight))
pygame.display.set_caption('Flappy Birds Look-A-Like')
clock = pygame.time.Clock()
#The line below: If the image is not in the same directory as the script,
#Then put the fill path to the imaing in the ''
img = pygame.image.load('Frame-1.png')
def score(count):
font = pygame.font.Font('freesansbold.ttf', 20)
text = font.render("Score: " +str(count), True, white)
surface.blit(text, [0,0])
def blocks(x_block, y_block, block_width, block_height, gap, colorChoice):
pygame.draw.rect(surface, colorChoice,[x_block,y_block,block_width,block_height])
pygame.draw.rect(surface, colorChoice, [x_block, y_block+block_height+gap, block_width, surfaceHeight])
def replay_or_quit():
for event in pygame.event.get([pygame.KEYDOWN, pygame.KEYUP,pygame.QUIT]):
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
continue
return event.key
return None
def makeTextObjs(text, font):
textSurface = font.render(text, True, sunset)
return textSurface, textSurface.get_rect()
def msgSurface(text):
smallText = pygame.font.Font('freesansbold.ttf', 20)
largeText = pygame.font.Font('freesansbold.ttf', 150)
titleTextSurf, titleTextRect = makeTextObjs(text, largeText)
titleTextRect.center = surfaceWidth / 2, surfaceHeight / 2
surface.blit(titleTextSurf, titleTextRect)
typTextSurf, typTextRect = makeTextObjs('Press any key to continue...', smallText)
typTextRect.center = surfaceWidth / 2, ((surfaceHeight / 2) + 100)
surface.blit(typTextSurf, typTextRect)
pygame.display.update()
time.sleep(1)
while replay_or_quit() == None:
clock.tick()
main()
def gameOver():
msgSurface('Kaboom!')
def bird(x, y, image):
surface.blit(img, (x,y))
def main():
x = 150
y = 200
y_move = 0
x_block = surfaceWidth
y_block = 0
block_width = 75
block_height = randint(0, (surfaceHeight/2))
gap = imageHeigth * 3
block_move = 6
current_score = 0
blockColor = colorChoices[randrange(0, len(colorChoices))]
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_move = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
y_move = 5
y += y_move
surface.fill(black)
bird(x, y, img)
blocks(x_block, y_block, block_width, block_height, gap, blockColor)
score(current_score)
x_block -= block_move
if y > surfaceHeight - 40 or y < 0:
gameOver()
if x_block < (-1*block_width):
x_block = surfaceWidth
block_height = randint(0, surfaceHeight/2)
blockColor = colorChoices[randrange(0, len(colorChoices))]
current_score+=1
if x + imageWidth > x_block:
if x < x_block + block_width:
#print('possibly within the bourndaries of x upper')
if y < block_height:
#print('Y crossover UPPER!')
if x - imageWidth < block_width + x_block:
#print('game over hit upper')
gameOver()
if x + imageWidth > x_block:
#print('x crossover')
if y + imageHeigth > block_height + gap:
#print('Y crossover lower')
if x < block_width + x_block:
#print('game over lower')
gameOver()
#This is a broken score code.
#if x_block < (x - block_width) < x_block + block_move:
#current_score += 1
if 3 <= current_score < 5:
block_move = 5
gap = imageWidth * 2.9
if 5 <= current_score < 8:
block_move = 6
gap = imageHeigth * 2.8
if 8 <= current_score < 14:
block_move = 7
gap = imageHeigth * 2.7
pygame.display.update()
clock.tick(60)
main()
pygame.quit()
quit()