-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlapy Bird.py
137 lines (106 loc) · 3.83 KB
/
Flapy Bird.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
import pygame
import os, sys, random
logo = pygame.image.load("assets/sprites/yellowbird-midflap.png")
back = pygame.image.load("assets/sprites/background-day.png")
pipe = pygame.image.load("assets/sprites/pipe-green.png")
game_over = pygame.image.load("assets/sprites/gameover.png")
score = 0
class Bird(pygame.sprite.Sprite) :
def __init__(self, speed = 1):
pygame.sprite.Sprite.__init__(self)
screen = pygame.display.get_surface()
flapyBird = pygame.image.load("assets/sprites/yellowbird-midflap.png")
self.image = flapyBird
self.rect = flapyBird.get_rect()
self.rect.center = 50,256
self.speed = speed
self.state = []
def update(self):
self.rect.move_ip(0,self.speed)
if self.state == "flying":
self.speed = 1
self.state = []
def going_up(self):
if self.state != "flying" :
self.state = "flying"
self.speed = -25
class Pipes(pygame.sprite.Sprite) :
def __init__(self, x_pos=250, speed = 4):
self.speed = speed
height = random.randrange(192, 400)
pygame.sprite.Sprite.__init__(self)
screen = pygame.display.get_surface()
self.image = pipe
self.rect = pipe.get_rect()
self.rect.midtop = x_pos, height
self.passed = False
def update(self):
self.rect.move_ip(-self.speed, 0)
def main():
#Initialize pygame
pygame.init()
font = pygame.font.Font(None, 36)
global score
#load logo and name
pygame.display.set_icon(logo)
pygame.display.set_caption("Flapy Bird")
#Define and load game screen
screen = pygame.display.set_mode((288,512))
screen.blit(back, (0,0))
pipe = Pipes()
bird = Bird()
pipes = pygame.sprite.Group((pipe))
bird_group = pygame.sprite.Group((bird))
text_score = font.render(str(score), 1 ,(255,255,255))
#True if playing
playing = True
#Main loop
while playing:
pygame.time.Clock().tick(60)
screen.blit(back, (0,0))
screen.blit(text_score, (0,0))
pipes.update()
bird_group.update()
pipes.draw(screen)
bird_group.draw(screen)
pygame.display.flip()
#Creation of new pipes
if pipe.rect.topright[0] < -50 :
pipe = Pipes()
pipes = pygame.sprite.Group((pipe))
if (pipe.passed == False and bird.rect.centerx > pipe.rect.centerx):
pipe.passed = True
score +=1
text_score = font.render(str(score), 1 ,(255,255,255))
#Check collisions
if pygame.sprite.spritecollide(bird, pipes, False):
playing = False
print(score)
gameOver()
for event in pygame.event.get():
if event.type == pygame.QUIT:
playing = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
playing = False
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
bird.going_up()
def gameOver():
global score
score = 0
font = pygame.font.Font(None, 36)
screen = pygame.display.set_mode((288,512))
background_black = pygame.Surface(screen.get_size())
background = background_black.convert()
background_black.fill((0,0,0))
text = font.render("Game Over", 1 ,(255,255,255))
screen.blit(background_black, (0,0))
screen.blit(text, (144,256))
pygame.display.flip()
g_over = True
while g_over:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
g_over = False
main()
if __name__ =="__main__":
main()