├── Flappy_bird ├── background.jpg ├── bird1.png └── main.py └── README.md /Flappy_bird/background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anish-Malla/Flappy-birds-game-using-pygame/59cbb30c4399c2632bdc7be1162fde651cc54421/Flappy_bird/background.jpg -------------------------------------------------------------------------------- /Flappy_bird/bird1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anish-Malla/Flappy-birds-game-using-pygame/59cbb30c4399c2632bdc7be1162fde651cc54421/Flappy_bird/bird1.png -------------------------------------------------------------------------------- /Flappy_bird/main.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | import random 3 | 4 | # Initialising the modules in pygame 5 | pygame.init() 6 | 7 | SCREEN = pygame.display.set_mode((500, 750)) # Setting the display 8 | 9 | # background 10 | BACKGROUND_IMAGE = pygame.image.load('background.jpg') 11 | 12 | # BIRD 13 | BIRD_IMAGE = pygame.image.load('bird1.png') 14 | bird_x = 50 15 | bird_y = 300 16 | bird_y_change = 0 17 | 18 | def display_bird(x, y): 19 | SCREEN.blit(BIRD_IMAGE, (x, y)) 20 | 21 | # OBSTACLES 22 | OBSTACLE_WIDTH = 70 23 | OBSTACLE_HEIGHT = random.randint(150,450) 24 | OBSTACLE_COLOR = (211, 253, 117) 25 | OBSTACE_X_CHANGE = -4 26 | obstacle_x = 500 27 | 28 | def display_obstacle(height): 29 | pygame.draw.rect(SCREEN, OBSTACLE_COLOR, (obstacle_x, 0, OBSTACLE_WIDTH, height)) 30 | bottom_obstacle_height = 635 - height - 150 31 | pygame.draw.rect(SCREEN, OBSTACLE_COLOR, (obstacle_x, 635, OBSTACLE_WIDTH, -bottom_obstacle_height)) 32 | 33 | # COLLISION DETECTION 34 | def collision_detection (obstacle_x, obstacle_height, bird_y, bottom_obstacle_height): 35 | if obstacle_x >= 50 and obstacle_x <= (50 + 64): 36 | if bird_y <= obstacle_height or bird_y >= (bottom_obstacle_height - 64): 37 | return True 38 | return False 39 | 40 | # SCORE 41 | score = 0 42 | SCORE_FONT = pygame.font.Font('freesansbold.ttf', 32) 43 | 44 | def score_display(score): 45 | display = SCORE_FONT.render(f"Score: {score}", True, (255,255,255)) 46 | SCREEN.blit(display, (10, 10)) 47 | 48 | # START SCREEN 49 | startFont = pygame.font.Font('freesansbold.ttf', 32) 50 | def start(): 51 | # displays: "press space bar to start) 52 | display = startFont.render(f"PRESS SPACE BAR TO START", True, (255, 255, 255)) 53 | SCREEN.blit(display, (20, 200)) 54 | pygame.display.update() 55 | 56 | # GAME OVER SCREEN 57 | # This list will hold all of the scores 58 | score_list = [0] 59 | 60 | game_over_font1 = pygame.font.Font('freesansbold.ttf', 64) 61 | game_over_font2 = pygame.font.Font('freesansbold.ttf', 32) 62 | 63 | def game_over(): 64 | # check for the maximum score 65 | maximum = max(score_list) 66 | # "game over" 67 | display1 = game_over_font1.render(f"GAME OVER", True, (200,35,35)) 68 | SCREEN.blit(display1, (50, 300)) 69 | # shows your current score and your max score 70 | display2 = game_over_font2.render(f"SCORE: {score} MAX SCORE: {maximum}", True, (255, 255, 255)) 71 | SCREEN.blit(display2, (50, 400)) 72 | # If your new score is the same as the maximum then u reached a new high score 73 | if score == maximum: 74 | display3 = game_over_font2.render(f"NEW HIGH SCORE!!", True, (200,35,35)) 75 | SCREEN.blit(display3, (80, 100)) 76 | 77 | running = True 78 | # waiting is going to refer to our end or start screen 79 | waiting = True 80 | # set collision to false in the beginning so that we only see the start screen in the beginning 81 | collision = False 82 | 83 | while running: 84 | 85 | SCREEN.fill((0, 0, 0)) 86 | 87 | # display the background image 88 | SCREEN.blit(BACKGROUND_IMAGE, (0, 0)) 89 | 90 | # we will be sent into this while loop at the beginning and ending of each game 91 | while waiting: 92 | if collision: 93 | # If collision is True (from the second time onwards) we will see both the end screen and the start screen 94 | game_over() 95 | start() 96 | else: 97 | # This refers to the first time the player is starting the game 98 | start() 99 | 100 | for event in pygame.event.get(): 101 | if event.type == pygame.KEYDOWN: 102 | if event.key == pygame.K_SPACE: 103 | # If we press the space bar we will exit out of the waiting while loop and start to play the game 104 | # we will also reset some of the variables such as the score and the bird's Y position and the obstacle's starting position 105 | score = 0 106 | bird_y = 300 107 | obstacle_x = 500 108 | # to exit out of the while loop 109 | waiting = False 110 | 111 | if event.type == pygame.QUIT: 112 | # in case we exit out make both running and waiting false 113 | waiting = False 114 | running = False 115 | 116 | for event in pygame.event.get(): 117 | if event.type == pygame.QUIT: 118 | # If you press exit you exit out of the while loop and pygame quits 119 | running = False 120 | 121 | if event.type == pygame.KEYDOWN: 122 | if event.key == pygame.K_SPACE: 123 | # if you press spacebar you will move up 124 | bird_y_change = -6 125 | 126 | if event.type == pygame.KEYUP: 127 | if event.key == pygame.K_SPACE: 128 | # when u release space bar you will move down automatically 129 | bird_y_change = 3 130 | 131 | # moving the bird vertically 132 | bird_y += bird_y_change 133 | # setting boundaries for the birds movement 134 | if bird_y <= 0: 135 | bird_y = 0 136 | if bird_y >= 571: 137 | bird_y = 571 138 | 139 | # Moving the obstacle 140 | obstacle_x += OBSTACE_X_CHANGE 141 | 142 | # COLLISION 143 | collision = collision_detection(obstacle_x, OBSTACLE_HEIGHT, bird_y, OBSTACLE_HEIGHT + 150) 144 | 145 | if collision: 146 | # if a collision does occur we are gonna add that score to our list of scores and make waiting True 147 | score_list.append(score) 148 | waiting = True 149 | 150 | # generating new obstacles 151 | if obstacle_x <= -10: 152 | obstacle_x = 500 153 | OBSTACLE_HEIGHT = random.randint(200, 400) 154 | score += 1 155 | # displaying the obstacle 156 | display_obstacle(OBSTACLE_HEIGHT) 157 | 158 | # displaying the bird 159 | display_bird(bird_x, bird_y) 160 | 161 | # display the score 162 | score_display(score) 163 | 164 | # Update the display after each iteration of the while loop 165 | pygame.display.update() 166 | 167 | # Quit the program 168 | pygame.quit() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flappy-birds-using-pygame 2 | 3 | I used Python and the Pygame package to re create the game Flappy bird. 4 | I added comments throughout which hopefully makes it easier to understand for a beginner and I also refrained from using object oriented programming for the same reason. 5 | 6 | # Video Tutorial 7 | I made a beginner friendly tutorial on my YouTube channel: https://www.youtube.com/watch?v=rO_UU_Uu8EQ 8 | --------------------------------------------------------------------------------