This article aims to explain and build step-by-step flappy birds game with Python.
Install Dependency
pip install pygame
Basics on pygame
import pygame
import sys
pygame.init() # init pygame
size = width, height = 320, 240 # set windows size
screen = pygame.display.set_mode(size) # show the window
while True: # infinite loop to keep the windows on
for event in pygame.event.get(): # loop through all events
if event.type == pygame.QUIT: # if close button clicked, exit
sys.exit()
pygame.quit() # exit pygame
Step 1 Create the game windows
create the game window, width and height 640*480。code below:
import sys
import pygame
pygame.init() # init pygame
size = width, height = 320, 240 # set windows size
screen = pygame.display.set_mode(size) # show the window
Step 2 Keep the windows on
running the code in step one, the window will flash and close immediately because the window is auto close after the script execution. To ensure it sticks we will use a while True
. On top of that, we need a way to exit with clicking the close button