Найти в Дзене
Создание игр

Angry birds game (Python code)

import pygame
import random

# Initialize the game
pygame.init()

# Set up the game window
screen_width = 288
screen_height = 512
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Flappy Bird")

# Load the background image
background = pygame.image.load("background.png")

# Load the bird image
bird = pygame.image.load("bird.png")
bird_rect = bird.get_rect(center=(50, screen_height // 2))

# Set up the gravity and bird movement variables
gravity = 0.25
bird_movement = 0

# Set up the pipe variables
pipe_width = 52
pipe_height = random.randint(150, 350)
pipe_gap = 100
pipe_x = screen_width
pipe_y = random.randint(-200, 0)

# Set up the game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Update the bird's movement
bird_movement += gravity
bird_rect.centery += bird_movement

# Draw the background
screen.blit(background, (0, 0))

# Draw the bird
screen.blit(bird, bird_rect)

# Draw the pipes
pygame.draw.rect(screen, (0, 128, 0), (pipe_x, pipe_y, pipe_width, pipe_height))
pygame.draw.rect(screen, (0, 128, 0), (pipe_x, pipe_y + pipe_height + pipe_gap, pipe_width, screen_height))

# Update the pipe position
pipe_x -= 2

# Check for collision with the bird
if bird_rect.colliderect(pygame.Rect(pipe_x, pipe_y, pipe_width, pipe_height)) or bird_rect.colliderect(pygame.Rect(pipe_x, pipe_y + pipe_height + pipe_gap, pipe_width, screen_height)):
running = False

# Check if the pipe has gone off the screen
if pipe_x + pipe_width < 0:
pipe_x = screen_width
pipe_height = random.randint(150, 350)
pipe_y = random.randint(-200, 0)

# Update the display
pygame.display.update()

# Quit the game
pygame.quit()

Окну игры присваивается заданная ширина и высота, загружаются фон и изображения птиц. Игровой цикл выполняется до тех пор, пока игрок не выйдет из игры. Движением птицы управляет гравитация, а трубы генерируются случайным образом и перемещаются по экрану. Обнаружение столкновений реализовано для проверки того, сталкивается ли птица с трубами. Когда происходит столкновение, игра заканчивается. Дисплей обновляется, чтобы отображать изменения в игре. Наконец, игра завершается с помощью pygame.quit().