Найти тему
Broud

Flappy Bird на python

На python можно сделать многое, но я хочу показать крутую и не сложную игру для новичков. Flappy Bird

Код от игры Flappy Bird:

import pygame

import random

#Инициализация Pygame

pygame.init()

# Определяем размеры экрана

SCREEN_WIDTH = 576

SCREEN_HEIGHT = 1024

SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Определяем цвета

BLACK = (0, 0, 0)

WHITE = (255, 255, 255)

# Определяем шрифты

FONT = pygame.font.SysFont('Calibri', 45)

# Определяем переменные

GRAVITY = 0.25

BIRD_SIZE = 40

PIPE_WIDTH = 80

PIPE_HEIGHT = 500

PIPE_GAP = 200

PIPE_VELOCITY = 5

GAME_SPEED = 3

bird_x = int(SCREEN_WIDTH / 2 - BIRD_SIZE / 2)

bird_y = int(SCREEN_HEIGHT / 2 - BIRD_SIZE / 2)

bird_velocity = 0

score = 0

pipes = []

# Функция для создания новой трубы

def create_pipe():

pipe_x = SCREEN_WIDTH

pipe_y = random.randint(-PIPE_HEIGHT // 2, PIPE_HEIGHT // 2)

top_pipe_rect = pygame.Rect(pipe_x, pipe_y - PIPE_HEIGHT // 2, PIPE_WIDTH, PIPE_HEIGHT)

bottom_pipe_rect = pygame.Rect(pipe_x, pipe_y + PIPE_GAP - PIPE_HEIGHT // 2, PIPE_WIDTH, PIPE_HEIGHT)

pipes.append((top_pipe_rect, bottom_pipe_rect))

# Функция для отрисовки птицы

def draw_bird():

pygame.draw.rect(SCREEN, WHITE, (bird_x, bird_y, BIRD_SIZE, BIRD_SIZE))

# Функция для отрисовки труб

def draw_pipes():

for top_pipe_rect, bottom_pipe_rect in pipes:

pygame.draw.rect(SCREEN, WHITE, top_pipe_rect)

pygame.draw.rect(SCREEN, WHITE, bottom_pipe_rect)

# Функция для обновления труб

def update_pipes():

for i, (top_pipe_rect, bottom_pipe_rect) in enumerate(pipes):

top_pipe_rect.move_ip(-PIPE_VELOCITY, 0)

bottom_pipe_rect.move_ip(-PIPE_VELOCITY, 0)

if top_pipe_rect.right < 0 and bottom_pipe_rect.right < 0:

global score

score += 1

create_pipe()

pipes.pop(i)

# Функция для обновления птицы

def update_bird():

global bird_y, bird_velocity

bird_y += bird_velocity

bird_velocity += GRAVITY

# Функция для отрисовки текста счета

def draw_score():

score_text = FONT.render(str(score), True, WHITE)

score_rect = score_text.get_rect(center=(SCREEN_WIDTH // 2, 50))

SCREEN.blit(score_text, score_rect)

# Функция для проверки столкновений

def check_collisions():

bird_rect = pygame.Rect(bird_x, bird_y, BIRD_SIZE, BIRD_SIZE)

for top_pipe_rect, bottom_pipe_rect in pipes:

if bird_rect.colliderect(top_pipe_rect) or bird_rect.colliderect(bottom_pipe_rect):

return True

if bird_y < 0 or bird_y + BIRD_SIZE > SCREEN_HEIGHT:

return True

return False

# Основной игровой цикл

clock = pygame.time.Clock()

running = True

create_pipe()

while running:

# Обрабатываем события

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:

bird_velocity = -8

# Обновляем игровые объекты

update_bird()

update_pipes()

# Проверяем столкновения

if check_collisions():

running = False

# Отрисовываем игровые объекты

SCREEN.fill(BLACK)

draw_pipes()

draw_bird()

draw_score()

pygame.display.flip()

# Устанавливаем скорость экрана

clock.tick(60)

# Завершаем Pygame

pygame.quit()

Вот в этой небольшой статье я показал как сделать игру flappy Bird.

python kuds

Наука
7 млн интересуются