Вот пример простой игры-погони, реализованной на Python с использованием библиотеки Pygame. В этой игре игрок управляет персонажем, который пытается поймать движущуюся цель.
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the game window
width, height = 800, 600
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Simple Chase Game")
# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Player
player_radius = 20
player_x = width // 2
player_y = height // 2
player_speed = 5
# Target
target_radius = 10
target_x = random.randint(0, width)
target_y = random.randint(0, height)
target_speed = 3
# Game loop
running = True
while running:
window.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < width:
player_x += player_speed
if keys[pygame.K_UP] and player_y > 0:
player_y -= player_speed
if keys[pygame.K_DOWN] and player_y < height:
player_y += player_speed
# Update target position
if target_x < player_x:
target_x += target_speed
elif target_x > player_x:
target_x -= target_speed
if target_y < player_y:
target_y += target_speed
elif target_y > player_y:
target_y -= target_speed
# Check collision
distance = ((player_x - target_x) 2 + (player_y - target_y) 2) 0.5
if distance < player_radius + target_radius:
target_x = random.randint(0, width)
target_y = random.randint(0, height)
# Draw player
pygame.draw.circle(window, RED, (player_x, player_y), player_radius)
# Draw target
pygame.draw.circle(window, BLUE, (target_x, target_y), target_radius)
pygame.display.update()
# Quit the game
pygame.quit()
Этот код предоставляет базовую реализацию игры-погони на Python с использованием библиотеки Pygame. Игрок управляет персонажем и пытается поймать движущуюся цель.