Skip to content

Commit fa4cbf6

Browse files
committed
add flappy bird game tutorial
1 parent 501cd4b commit fa4cbf6

File tree

15 files changed

+271
-0
lines changed

15 files changed

+271
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
299299
- [How to Create a Slide Puzzle Game in Python](https://www.thepythoncode.com/article/slide-puzzle-game-in-python). ([code](gui-programming/slide-puzzle))
300300
- [How to Make a Maze Game in Python](https://www.thepythoncode.com/article/build-a-maze-game-in-python). ([code](gui-programming/maze-game))
301301
- [How to Create a Platformer Game in Python](https://www.thepythoncode.com/article/platformer-game-with-pygame-in-python). ([code](gui-programming/platformer-game))
302+
- [How to Make a Flappy Bird Game in Python](https://thepythoncode.com/article/make-a-flappy-bird-game-python). ([code](gui-programming/flappy-bird-game))
302303

303304

304305
For any feedback, please consider pulling requests.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a Flappy Bird Game in Python](https://thepythoncode.com/article/make-a-flappy-bird-game-python)
Loading
Loading
Loading
Loading
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pygame
2+
from settings import import_sprite
3+
4+
class Bird(pygame.sprite.Sprite):
5+
def __init__(self, pos, size):
6+
super().__init__()
7+
# bird basic info
8+
self.frame_index = 0
9+
self.animation_delay = 3
10+
self.jump_move = -9
11+
12+
# bird animation
13+
self.bird_img = import_sprite("assets/bird")
14+
self.image = self.bird_img[self.frame_index]
15+
self.image = pygame.transform.scale(self.image, (size, size))
16+
self.rect = self.image.get_rect(topleft = pos)
17+
self.mask = pygame.mask.from_surface(self.image)
18+
19+
# bird status
20+
self.direction = pygame.math.Vector2(0, 0)
21+
self.score = 0
22+
23+
# for bird's flying animation
24+
def _animate(self):
25+
sprites = self.bird_img
26+
sprite_index = (self.frame_index // self.animation_delay) % len(sprites)
27+
self.image = sprites[sprite_index]
28+
self.frame_index += 1
29+
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
30+
self.mask = pygame.mask.from_surface(self.image)
31+
if self.frame_index // self.animation_delay > len(sprites):
32+
self.frame_index = 0
33+
34+
# to make the bird fly higher
35+
def _jump(self):
36+
self.direction.y = self.jump_move
37+
38+
# updates the bird's overall state
39+
def update(self, is_jump):
40+
if is_jump:
41+
self._jump()
42+
self._animate()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pygame
2+
from settings import WIDTH, HEIGHT
3+
4+
pygame.font.init()
5+
6+
class GameIndicator:
7+
def __init__(self, screen):
8+
self.screen = screen
9+
self.font = pygame.font.SysFont('Bauhaus 93', 60)
10+
self.inst_font = pygame.font.SysFont('Bauhaus 93', 30)
11+
self.color = pygame.Color("white")
12+
self.inst_color = pygame.Color("black")
13+
14+
def show_score(self, int_score):
15+
bird_score = str(int_score)
16+
score = self.font.render(bird_score, True, self.color)
17+
self.screen.blit(score, (WIDTH // 2, 50))
18+
19+
def instructions(self):
20+
inst_text1 = "Press SPACE button to Jump,"
21+
inst_text2 = "Press \"R\" Button to Restart Game."
22+
ins1 = self.inst_font.render(inst_text1, True, self.inst_color)
23+
ins2 = self.inst_font.render(inst_text2, True, self.inst_color)
24+
self.screen.blit(ins1, (95, 400))
25+
self.screen.blit(ins2, (70, 450))

0 commit comments

Comments
 (0)