-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflappy_bird.py
More file actions
185 lines (135 loc) · 3.95 KB
/
flappy_bird.py
File metadata and controls
185 lines (135 loc) · 3.95 KB
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
import pygame
import random
import math
import time
pygame.init()
clock=pygame.time.Clock()
width=600
height=600
jump_speed=0.1
jump=False
start=False
score=0
display=pygame.display.set_mode((width,height))
bg_image=pygame.image.load('background.png').convert_alpha()
bird_image=pygame.image.load('bird.png').convert_alpha()
upper_pipe=pygame.image.load('upper_pipe.png').convert_alpha()
lower_pipe=pygame.image.load('lower_pipe.png').convert_alpha()
font=pygame.font.Font('freesansbold.ttf',32)
text=font.render('GAME OVER',True,(0,0,0),(255,255,255))
textRect=text.get_rect()
textRect.center=(width//2,height//2)
pathway=175
bg_color=(0,0,255)
gravity_angle=math.pi
gravity_magnitude=0
rectangle_speed=0.3
temp=350
collision_detection=False
class Bird:
def __init__(self,x,y):
self.x=x
self.y=y
self.size=18
self.color=(255,255,0)
self.speed=0
self.angle=0
self.mass=1
def move(self):
(self.angle,self.speed)=addvectors(self.angle,self.speed,gravity_angle,gravity_magnitude)
self.y-=math.cos(self.angle)*self.speed
if self.y<20:
self.y=20
if self.y>580:
self.y=580
self.speed=0
self.angle=0
def display(self):
if self.angle<math.pi/4:
a=pygame.transform.rotate(bird_image,40)
elif self.angle==1.5707963267948966:
a=pygame.transform.rotate(bird_image,0)
else:
a=pygame.transform.rotate(bird_image,-40)
display.blit(a,(self.x-20,self.y-20))
def jump():
new_bird.angle=0
new_bird.speed=0.5
jumping=False
def addvectors(angle1,vector1,angle2,vector2):
x_coordinate=math.sin(angle1)*vector1+math.sin(angle2)*vector2
y_coordinate=math.cos(angle1)*vector1+math.cos(angle2)*vector2
length=math.hypot(x_coordinate,y_coordinate)
angle=0.5*math.pi-math.atan2(y_coordinate,x_coordinate)
return(angle,length)
class Rectangle:
def __init__(self,rx,rh):
self.x=rx
self.y=0
self.width=50
self.height=rh
self.color=(0,255,0)
self.x_change=rectangle_speed
def display(self):
display.blit(upper_pipe,(self.x,self.height-width))
display.blit(lower_pipe,(self.x,self.height+pathway))
def move(self):
self.x-=self.x_change
if self.x<-50:
self.x=600
self.height=random.randint(100,400)
def collision(tempr,tempb):
if tempr.x<=tempb.x+tempb.size and tempr.x>tempb.x-tempb.size:
if tempb.y<tempr.height or tempb.y>tempr.height+pathway:
return True
if tempr.x+tempr.width<=tempb.x+tempb.size and tempr.x+tempr.width>tempb.x-tempb.size:
if tempb.y<tempr.height or tempb.y>tempr.height+pathway:
return True
if tempr.x<=tempb.x and tempr.x>tempb.x-tempb.size:
if tempb.y+tempb.size>tempr.height+pathway or tempb.y-tempb.size<tempr.height:
return True
if tempr.x+50<=tempb.x + tempb.size and tempr.x + 50 > tempb.x:
if tempb.y+tempb.size>tempr.height+pathway or tempb.y-tempb.size<tempr.height:
return True
my_rectangles=[]
for i in range(2):
x=600+i*temp
h=random.randint(100,400)
my_rectangles.append(Rectangle(x,h))
new_bird=Bird(40,300)
jumping=False
loop=True
while loop:
display.blit(bg_image,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop= False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
jumping=True
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
jumping=False
if jumping:
if collision_detection==False:
jump()
new_bird.move()
keys=pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
start=True
gravity_magnitude=0.003
if start:
for rect in my_rectangles:
if collision(rect,new_bird):
collision_detection=True
for rect in my_rectangles:
rect.move()
rect.display()
if collision_detection:
new_bird.speed=0
gravity_magnitude=0
my_rectangles[0].x_change=0
my_rectangles[1].x_change=0
display.blit(text,textRect)
new_bird.display()
pygame.display.update()