├── 04B_19.TTF ├── assets ├── background-day.png ├── background-night.png ├── base.png ├── bluebird-downflap.png ├── bluebird-midflap.png ├── bluebird-upflap.png ├── gameover.png ├── message.png ├── pipe-green.png ├── pipe-red.png ├── redbird-downflap.png ├── redbird-midflap.png ├── redbird-upflap.png ├── yellowbird-downflap.png ├── yellowbird-midflap.png └── yellowbird-upflap.png ├── flappy.py ├── flappy_update.py └── sound ├── sfx_die.wav ├── sfx_hit.wav ├── sfx_point.wav ├── sfx_swooshing.wav └── sfx_wing.wav /04B_19.TTF: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/04B_19.TTF -------------------------------------------------------------------------------- /assets/background-day.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/background-day.png -------------------------------------------------------------------------------- /assets/background-night.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/background-night.png -------------------------------------------------------------------------------- /assets/base.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/base.png -------------------------------------------------------------------------------- /assets/bluebird-downflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/bluebird-downflap.png -------------------------------------------------------------------------------- /assets/bluebird-midflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/bluebird-midflap.png -------------------------------------------------------------------------------- /assets/bluebird-upflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/bluebird-upflap.png -------------------------------------------------------------------------------- /assets/gameover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/gameover.png -------------------------------------------------------------------------------- /assets/message.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/message.png -------------------------------------------------------------------------------- /assets/pipe-green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/pipe-green.png -------------------------------------------------------------------------------- /assets/pipe-red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/pipe-red.png -------------------------------------------------------------------------------- /assets/redbird-downflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/redbird-downflap.png -------------------------------------------------------------------------------- /assets/redbird-midflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/redbird-midflap.png -------------------------------------------------------------------------------- /assets/redbird-upflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/redbird-upflap.png -------------------------------------------------------------------------------- /assets/yellowbird-downflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/yellowbird-downflap.png -------------------------------------------------------------------------------- /assets/yellowbird-midflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/yellowbird-midflap.png -------------------------------------------------------------------------------- /assets/yellowbird-upflap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/assets/yellowbird-upflap.png -------------------------------------------------------------------------------- /flappy.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | def draw_floor(): 4 | screen.blit(floor_surface,(floor_x_pos,900)) 5 | screen.blit(floor_surface,(floor_x_pos + 576,900)) 6 | 7 | def create_pipe(): 8 | random_pipe_pos = random.choice(pipe_height) 9 | bottom_pipe = pipe_surface.get_rect(midtop = (700,random_pipe_pos)) 10 | top_pipe = pipe_surface.get_rect(midbottom = (700,random_pipe_pos - 300)) 11 | return bottom_pipe,top_pipe 12 | 13 | def move_pipes(pipes): 14 | for pipe in pipes: 15 | pipe.centerx -= 5 16 | return pipes 17 | 18 | def draw_pipes(pipes): 19 | for pipe in pipes: 20 | if pipe.bottom >= 1024: 21 | screen.blit(pipe_surface,pipe) 22 | else: 23 | flip_pipe = pygame.transform.flip(pipe_surface,False,True) 24 | screen.blit(flip_pipe,pipe) 25 | def remove_pipes(pipes): 26 | for pipe in pipes: 27 | if pipe.centerx == -600: 28 | pipes.remove(pipe) 29 | return pipes 30 | def check_collision(pipes): 31 | for pipe in pipes: 32 | if bird_rect.colliderect(pipe): 33 | death_sound.play() 34 | return False 35 | 36 | if bird_rect.top <= -100 or bird_rect.bottom >= 900: 37 | return False 38 | 39 | return True 40 | 41 | def rotate_bird(bird): 42 | new_bird = pygame.transform.rotozoom(bird,-bird_movement * 3,1) 43 | return new_bird 44 | 45 | def bird_animation(): 46 | new_bird = bird_frames[bird_index] 47 | new_bird_rect = new_bird.get_rect(center = (100,bird_rect.centery)) 48 | return new_bird,new_bird_rect 49 | 50 | def score_display(game_state): 51 | if game_state == 'main_game': 52 | score_surface = game_font.render(str(int(score)),True,(255,255,255)) 53 | score_rect = score_surface.get_rect(center = (288,100)) 54 | screen.blit(score_surface,score_rect) 55 | if game_state == 'game_over': 56 | score_surface = game_font.render(f'Score: {int(score)}' ,True,(255,255,255)) 57 | score_rect = score_surface.get_rect(center = (288,100)) 58 | screen.blit(score_surface,score_rect) 59 | 60 | high_score_surface = game_font.render(f'High score: {int(high_score)}',True,(255,255,255)) 61 | high_score_rect = high_score_surface.get_rect(center = (288,850)) 62 | screen.blit(high_score_surface,high_score_rect) 63 | 64 | def update_score(score, high_score): 65 | if score > high_score: 66 | high_score = score 67 | return high_score 68 | 69 | pygame.mixer.pre_init(frequency = 44100, size = 16, channels = 1, buffer = 512) 70 | pygame.init() 71 | screen = pygame.display.set_mode((576,1024)) 72 | clock = pygame.time.Clock() 73 | game_font = pygame.font.Font('04B_19.ttf',40) 74 | 75 | # Game Variables 76 | gravity = 0.25 77 | bird_movement = 0 78 | game_active = True 79 | score = 0 80 | high_score = 0 81 | 82 | bg_surface = pygame.image.load('assets/background-day.png').convert() 83 | bg_surface = pygame.transform.scale2x(bg_surface) 84 | 85 | floor_surface = pygame.image.load('assets/base.png').convert() 86 | floor_surface = pygame.transform.scale2x(floor_surface) 87 | floor_x_pos = 0 88 | 89 | bird_downflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-downflap.png').convert_alpha()) 90 | bird_midflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-midflap.png').convert_alpha()) 91 | bird_upflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-upflap.png').convert_alpha()) 92 | bird_frames = [bird_downflap,bird_midflap,bird_upflap] 93 | bird_index = 0 94 | bird_surface = bird_frames[bird_index] 95 | bird_rect = bird_surface.get_rect(center = (100,512)) 96 | 97 | BIRDFLAP = pygame.USEREVENT + 1 98 | pygame.time.set_timer(BIRDFLAP,200) 99 | 100 | # bird_surface = pygame.image.load('assets/bluebird-midflap.png').convert_alpha() 101 | # bird_surface = pygame.transform.scale2x(bird_surface) 102 | # bird_rect = bird_surface.get_rect(center = (100,512)) 103 | 104 | pipe_surface = pygame.image.load('assets/pipe-green.png') 105 | pipe_surface = pygame.transform.scale2x(pipe_surface) 106 | pipe_list = [] 107 | SPAWNPIPE = pygame.USEREVENT 108 | pygame.time.set_timer(SPAWNPIPE,1200) 109 | pipe_height = [400,600,800] 110 | 111 | game_over_surface = pygame.transform.scale2x(pygame.image.load('assets/message.png').convert_alpha()) 112 | game_over_rect = game_over_surface.get_rect(center = (288,512)) 113 | 114 | flap_sound = pygame.mixer.Sound('sound/sfx_wing.wav') 115 | death_sound = pygame.mixer.Sound('sound/sfx_hit.wav') 116 | score_sound = pygame.mixer.Sound('sound/sfx_point.wav') 117 | score_sound_countdown = 100 118 | 119 | while True: 120 | for event in pygame.event.get(): 121 | if event.type == pygame.QUIT: 122 | pygame.quit() 123 | sys.exit() 124 | if event.type == pygame.KEYDOWN: 125 | if event.key == pygame.K_SPACE and game_active: 126 | bird_movement = 0 127 | bird_movement -= 12 128 | flap_sound.play() 129 | if event.key == pygame.K_SPACE and game_active == False: 130 | game_active = True 131 | pipe_list.clear() 132 | bird_rect.center = (100,512) 133 | bird_movement = 0 134 | score = 0 135 | 136 | if event.type == SPAWNPIPE: 137 | pipe_list.extend(create_pipe()) 138 | 139 | if event.type == BIRDFLAP: 140 | if bird_index < 2: 141 | bird_index += 1 142 | else: 143 | bird_index = 0 144 | 145 | bird_surface,bird_rect = bird_animation() 146 | 147 | screen.blit(bg_surface,(0,0)) 148 | 149 | if game_active: 150 | # Bird 151 | bird_movement += gravity 152 | rotated_bird = rotate_bird(bird_surface) 153 | bird_rect.centery += bird_movement 154 | screen.blit(rotated_bird,bird_rect) 155 | game_active = check_collision(pipe_list) 156 | 157 | # Pipes 158 | pipe_list = move_pipes(pipe_list) 159 | pipe_list = remove_pipes(pipe_list) 160 | draw_pipes(pipe_list) 161 | 162 | score += 0.01 163 | score_display('main_game') 164 | score_sound_countdown -= 1 165 | if score_sound_countdown <= 0: 166 | score_sound.play() 167 | score_sound_countdown = 100 168 | else: 169 | screen.blit(game_over_surface,game_over_rect) 170 | high_score = update_score(score,high_score) 171 | score_display('game_over') 172 | 173 | 174 | # Floor 175 | floor_x_pos -= 1 176 | draw_floor() 177 | if floor_x_pos <= -576: 178 | floor_x_pos = 0 179 | 180 | 181 | pygame.display.update() 182 | clock.tick(120) 183 | -------------------------------------------------------------------------------- /flappy_update.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random 2 | 3 | def draw_floor(): 4 | screen.blit(floor_surface,(floor_x_pos,900)) 5 | screen.blit(floor_surface,(floor_x_pos + 576,900)) 6 | 7 | def create_pipe(): 8 | random_pipe_pos = random.choice(pipe_height) 9 | bottom_pipe = pipe_surface.get_rect(midtop = (700,random_pipe_pos)) 10 | top_pipe = pipe_surface.get_rect(midbottom = (700,random_pipe_pos - 300)) 11 | return bottom_pipe,top_pipe 12 | 13 | def move_pipes(pipes): 14 | for pipe in pipes: 15 | pipe.centerx -= 5 16 | visible_pipes = [pipe for pipe in pipes if pipe.right > -50] 17 | return visible_pipes 18 | 19 | def draw_pipes(pipes): 20 | for pipe in pipes: 21 | if pipe.bottom >= 1024: 22 | screen.blit(pipe_surface,pipe) 23 | else: 24 | flip_pipe = pygame.transform.flip(pipe_surface,False,True) 25 | screen.blit(flip_pipe,pipe) 26 | 27 | def check_collision(pipes): 28 | global can_score 29 | for pipe in pipes: 30 | if bird_rect.colliderect(pipe): 31 | death_sound.play() 32 | can_score = True 33 | return False 34 | 35 | if bird_rect.top <= -100 or bird_rect.bottom >= 900: 36 | can_score = True 37 | return False 38 | 39 | return True 40 | 41 | def rotate_bird(bird): 42 | new_bird = pygame.transform.rotozoom(bird,-bird_movement * 3,1) 43 | return new_bird 44 | 45 | def bird_animation(): 46 | new_bird = bird_frames[bird_index] 47 | new_bird_rect = new_bird.get_rect(center = (100,bird_rect.centery)) 48 | return new_bird,new_bird_rect 49 | 50 | def score_display(game_state): 51 | if game_state == 'main_game': 52 | score_surface = game_font.render(str(int(score)),True,(255,255,255)) 53 | score_rect = score_surface.get_rect(center = (288,100)) 54 | screen.blit(score_surface,score_rect) 55 | if game_state == 'game_over': 56 | score_surface = game_font.render(f'Score: {int(score)}' ,True,(255,255,255)) 57 | score_rect = score_surface.get_rect(center = (288,100)) 58 | screen.blit(score_surface,score_rect) 59 | 60 | high_score_surface = game_font.render(f'High score: {int(high_score)}',True,(255,255,255)) 61 | high_score_rect = high_score_surface.get_rect(center = (288,850)) 62 | screen.blit(high_score_surface,high_score_rect) 63 | 64 | def update_score(score, high_score): 65 | if score > high_score: 66 | high_score = score 67 | return high_score 68 | 69 | def pipe_score_check(): 70 | global score, can_score 71 | 72 | if pipe_list: 73 | for pipe in pipe_list: 74 | if 95 < pipe.centerx < 105 and can_score: 75 | score += 1 76 | score_sound.play() 77 | can_score = False 78 | if pipe.centerx < 0: 79 | can_score = True 80 | 81 | #pygame.mixer.pre_init(frequency = 44100, size = 16, channels = 2, buffer = 1024) 82 | pygame.init() 83 | screen = pygame.display.set_mode((576,1024)) 84 | clock = pygame.time.Clock() 85 | game_font = pygame.font.Font('04B_19.ttf',40) 86 | 87 | # Game Variables 88 | gravity = 0.25 89 | bird_movement = 0 90 | game_active = True 91 | score = 0 92 | high_score = 0 93 | can_score = True 94 | bg_surface = pygame.image.load('assets/background-day.png').convert() 95 | bg_surface = pygame.transform.scale2x(bg_surface) 96 | 97 | floor_surface = pygame.image.load('assets/base.png').convert() 98 | floor_surface = pygame.transform.scale2x(floor_surface) 99 | floor_x_pos = 0 100 | 101 | bird_downflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-downflap.png').convert_alpha()) 102 | bird_midflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-midflap.png').convert_alpha()) 103 | bird_upflap = pygame.transform.scale2x(pygame.image.load('assets/bluebird-upflap.png').convert_alpha()) 104 | bird_frames = [bird_downflap,bird_midflap,bird_upflap] 105 | bird_index = 0 106 | bird_surface = bird_frames[bird_index] 107 | bird_rect = bird_surface.get_rect(center = (100,512)) 108 | 109 | BIRDFLAP = pygame.USEREVENT + 1 110 | pygame.time.set_timer(BIRDFLAP,200) 111 | 112 | # bird_surface = pygame.image.load('assets/bluebird-midflap.png').convert_alpha() 113 | # bird_surface = pygame.transform.scale2x(bird_surface) 114 | # bird_rect = bird_surface.get_rect(center = (100,512)) 115 | 116 | pipe_surface = pygame.image.load('assets/pipe-green.png') 117 | pipe_surface = pygame.transform.scale2x(pipe_surface) 118 | pipe_list = [] 119 | SPAWNPIPE = pygame.USEREVENT 120 | pygame.time.set_timer(SPAWNPIPE,1200) 121 | pipe_height = [400,600,800] 122 | 123 | game_over_surface = pygame.transform.scale2x(pygame.image.load('assets/message.png').convert_alpha()) 124 | game_over_rect = game_over_surface.get_rect(center = (288,512)) 125 | 126 | flap_sound = pygame.mixer.Sound('sound/sfx_wing.wav') 127 | death_sound = pygame.mixer.Sound('sound/sfx_hit.wav') 128 | score_sound = pygame.mixer.Sound('sound/sfx_point.wav') 129 | score_sound_countdown = 100 130 | SCOREEVENT = pygame.USEREVENT + 2 131 | pygame.time.set_timer(SCOREEVENT,100) 132 | 133 | while True: 134 | for event in pygame.event.get(): 135 | if event.type == pygame.QUIT: 136 | pygame.quit() 137 | sys.exit() 138 | if event.type == pygame.KEYDOWN: 139 | if event.key == pygame.K_SPACE and game_active: 140 | bird_movement = 0 141 | bird_movement -= 12 142 | flap_sound.play() 143 | if event.key == pygame.K_SPACE and game_active == False: 144 | game_active = True 145 | pipe_list.clear() 146 | bird_rect.center = (100,512) 147 | bird_movement = 0 148 | score = 0 149 | 150 | if event.type == SPAWNPIPE: 151 | pipe_list.extend(create_pipe()) 152 | 153 | if event.type == BIRDFLAP: 154 | if bird_index < 2: 155 | bird_index += 1 156 | else: 157 | bird_index = 0 158 | 159 | bird_surface,bird_rect = bird_animation() 160 | 161 | screen.blit(bg_surface,(0,0)) 162 | 163 | if game_active: 164 | # Bird 165 | bird_movement += gravity 166 | rotated_bird = rotate_bird(bird_surface) 167 | bird_rect.centery += bird_movement 168 | screen.blit(rotated_bird,bird_rect) 169 | game_active = check_collision(pipe_list) 170 | 171 | # Pipes 172 | pipe_list = move_pipes(pipe_list) 173 | draw_pipes(pipe_list) 174 | 175 | # Score 176 | pipe_score_check() 177 | score_display('main_game') 178 | else: 179 | screen.blit(game_over_surface,game_over_rect) 180 | high_score = update_score(score,high_score) 181 | score_display('game_over') 182 | 183 | 184 | # Floor 185 | floor_x_pos -= 1 186 | draw_floor() 187 | if floor_x_pos <= -576: 188 | floor_x_pos = 0 189 | 190 | 191 | pygame.display.update() 192 | clock.tick(120) 193 | -------------------------------------------------------------------------------- /sound/sfx_die.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/sound/sfx_die.wav -------------------------------------------------------------------------------- /sound/sfx_hit.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/sound/sfx_hit.wav -------------------------------------------------------------------------------- /sound/sfx_point.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/sound/sfx_point.wav -------------------------------------------------------------------------------- /sound/sfx_swooshing.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/sound/sfx_swooshing.wav -------------------------------------------------------------------------------- /sound/sfx_wing.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/clear-code-projects/FlappyBird_Python/c6f58d9fd5b889aa172c7be7c538b47049b988f8/sound/sfx_wing.wav --------------------------------------------------------------------------------