Flappy Bird In Python Pygame with source code

Flappy Bird In Python Pygame

CREATING A WELCOME SCREEN FOR FLAPPY BIRD IN PYTHON PYGAME

Now, we are going to create the welcome screen function that I said to you earlier. Now, we will blit the images. But before that let me tell you how images are displayed on our game window. Hope you are familiar with it already but if not let’s see.

Every image we blit on our screen is with the help of their coordinates. Let’s see the image below of the pygame window that we have created with the dimensions of (289 X 511).

COORDINATES EXPLANATION

Every top left corner of the image or screen is the origin like you are seeing in the image below. X-axis and Y-axis can also be negative but negative values will be displayed outside our pygame Window.

While Y-axis increases when we go downside and decreases when we go upside.

So, whenever we operate with any images just keep it in mind that its top-left corner will be (0,0) means will be the origin and by the hit and trial method you can get your desired coordinates.

The values which we are going to use in our game has been came through several attempts. So, don’t bother from where I got these values. There is nothing in it. You have to check your own which values satisfy you.

axes of the game window in Flappy Bird game
axes of the game window
how do axes changes
HOW DO AXES CHANGES

So now we know the Science behind blitting of images on the screen. So let’s start blitting means coding to blit our images on the screen.
But first, have a look at our welcome screen image which we named as a message.

welcome screen of Flappy Bird In Pygame Python
Welcome Screen image
import random #for generating random numbers
import sys #To Exit the game
import pygame
from pygame.locals import * #Basic Pygame Imports

FPS = 32
SCREENWIDTH = 289
SCREENHEIGHT = 511
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
GROUNDY = SCREENHEIGHT*0.8
GAME_SPRITES = {}
GAME_SOUNDS = {}
PLAYER = 'resources\SPRITES\\bird.png'
BACKGROUND = 'resources\SPRITES\\bg.jpeg'
PIPE = 'resources\SPRITES\pipe.png '

def welcomeScreen():

    #IT WIll Show Welcome Screen TO user to make the game more interactive and interesting
    playerx = int(SCREENWIDTH/5)
    playery = int(SCREENHEIGHT - GAME_SPRITES['player'].get_height())/2
    messagex = int(SCREENWIDTH - GAME_SPRITES['message'].get_width())/2
    messagey = int(SCREENHEIGHT * 0.13)
    basex = 0

 ### This is the point from Where Our Game is going to be Started ###
if __name__ == "__main__":

    pygame.init() #Initializing the Modules of Pygame 
    FPSCLOCK = pygame.time.Clock() #for controlling the FPS
    pygame.display.set_caption('Flappy Bird With Sameer') #Setting the Caption of The Game

    #### LOADING THE SPRITES ####

    GAME_SPRITES['numbers'] = (
        pygame.image.load('resources\SPRITES\\0.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\1.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\2.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\3.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\4.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\5.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\6.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\7.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\8.png').convert_alpha(),
        pygame.image.load('resources\SPRITES\\9.png').convert_alpha(),
    
    ) 
    S
    
    GAME_SPRITES['background'] = pygame.image.load(BACKGROUND).convert_alpha()
    GAME_SPRITES['player'] = pygame.image.load(PLAYER).convert_alpha()
    GAME_SPRITES['message'] = pygame.image.load('resources\SPRITES\message.png').convert_alpha()
    GAME_SPRITES['base'] = pygame.image.load('resources\SPRITES\\base.png').convert_alpha()
    GAME_SPRITES['pipe'] = (
        
        
    pygame.transform.rotate(pygame.image.load(PIPE).convert_alpha(), 180), #### UPPER PIPES, WE JUST ROTATED THE PIPE BY 180deg
    pygame.image.load(PIPE).convert_alpha()   #### LOWER PIPES  
    )

    #Game Sounds
    GAME_SOUNDS['die'] = pygame.mixer.Sound('resources\AUDIO\die.wav')
    GAME_SOUNDS['hit'] = pygame.mixer.Sound('resources\AUDIO\hit.wav')
    GAME_SOUNDS['point'] = pygame.mixer.Sound('resources\AUDIO\point.wav')
    GAME_SOUNDS['swoosh'] = pygame.mixer.Sound('resources\AUDIO\swoosh.wav')
    GAME_SOUNDS['wing'] = pygame.mixer.Sound('resources\AUDIO\wing.wav')
  while True:
        welcomeScreen() #Shows a welcomescreen to the user until they starts the game
        mainGame() #This is our main game funtion

CODE EXPLANATION

What we have created is we defined or created a function welcomeScreen() in which we declared a variable ‘playerx’ which will be x coordinate of our player which is bird.

The second is ‘playery’ which is a little bit technical. So, understand it with your full patience.

playery = int(SCREENHEIGHT - GAME_SPRITES['player'].get_height())/2

What we did is, we subtracted the height of the player from the screen height and then divide it by 2.

get_height() is a function used to get the height of the object as the name itself tells. In our case, the object is the player which is nothing but the image of the bird.

We did it because we wanted to place our birds at the centre so it gives the sense of symmetry.

Let’s understand it through example.
Suppose the height of the screen is 30
height of the player is 02
playery = (30-02)/2

The same logic goes with the ‘messagex’ but in this case, it’s not height it’s the width of the image. From the width of the screen, we subtracted the width of the message which is an image.
And then we have ‘messagey’ in which it stores the y-coordinate for the message.

After then we have basex which is the x-coordinate of our base image and its Y we have already declared in Global Variables named as GROUNDY.

base image
base image

All the values we are using came from various attempts. So, it’s not the point to be worried that how these values are coming. It will just come up with you with the experience.

Share:

Author: Sameer Pandey