Flying High with Python Pygame: Building a Flappy Bird Game
In this section, let's dive into some of the code snippets to give you a taste of what's in store:
import pygame
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 800
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Create the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Bird Game")
# Load assets - background, bird, and pipes
background = pygame.image.load("background.png")
bird = pygame.image.load("bird.png")
pipe = pygame.image.load("pipe.png")
# Bird properties
bird_x = 100
bird_y = 300
bird_velocity = 0
# Gravity
gravity = 0.5
# Pipe properties
pipe_x = 700
pipe_height = [200, 300, 400]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Handle bird's movement
bird_velocity += gravity
bird_y += bird_velocity
# Draw the background
screen.blit(background, (0, 0))
# Draw pipes
for height in pipe_height:
screen.blit(pipe, (pipe_x, height))
pipe_x -= 3 # Move pipes towards the left
# Draw the bird
screen.blit(bird, (bird_x, bird_y))
pygame.display.update()
Comments
Post a Comment