-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflappy_bird.py
More file actions
71 lines (52 loc) · 1.69 KB
/
flappy_bird.py
File metadata and controls
71 lines (52 loc) · 1.69 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
import pygame
import random
pygame.init()
width, height = 288, 512
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Flappy Bird")
bird_image = pygame.image.load("bird.png").convert_alpha()
# background_image = pygame.image.load("/background.png").convert()
pipe_image = pygame.image.load("pipe.png").convert_alpha()
class Bird(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = bird_image
self.rect = self.image.get_rect(center=(50, 256))
self.velocity = 0
def update(self):
self.velocity += 0.5
self.rect.y += self.velocity
class Pipe(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pipe_image
self.rect = self.image.get_rect(midtop=(width + 50, random.randint(-400, -200)))
self.velocity = -4
def update(self):
self.rect.x += self.velocity
clock = pygame.time.Clock()
bird = Bird()
pipes = pygame.sprite.Group()
score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.velocity = -10
# screen.blit(background_image, (0, 0))
bird.update()
pipes.update()
screen.fill((255, 255, 255))
if len(pipes) < 1:
pipes.add(Pipe())
for pipe in pipes:
if pipe.rect.right < 0:
pipes.remove(pipe)
score += 1
pipes.draw(screen)
screen.blit(bird.image, bird.rect)
pygame.display.update()
clock.tick(60)