import pygame
import random
# Инициализация Pygame
pygame.init()
# Константы
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
# Цвета
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
# Создание экрана
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Фэнтези 2D-экшен")
class Player(pygame.sprite.Sprite):
def __init__(self, faction):
super().__init__()
self.faction = faction
self.health = 100
self.image = pygame.Surface((32, 32))
self.image.fill(GREEN if faction == "Эльфы" else BLUE if faction == "Охрана" else RED)
self.rect = self.image.get_rect()
self.rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
def move(self, dx, dy):
self.rect.x += dx
self.rect.y += dy
self.rect.clamp_ip(screen.get_rect())
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((20, 20))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, SCREEN_WIDTH - 20)
self.rect.y = random.randint(0, SCREEN_HEIGHT - 20)
def update(self):
self.rect.x += random.randint(-2, 2)
self.rect.y += random.randint(-2, 2)
self.rect.clamp_ip(screen.get_rect())
class World:
def __init__(self):
self.zones = ["Нейтральная", "Императорская", "Эльфийская", "Злодейская"]
self.current_zone = 0
def change_zone(self):
self.current_zone = (self.current_zone + 1) % len(self.zones)
def game_loop():
clock = pygame.time.Clock()
player = Player("Эльфы")
world = World()
enemies = pygame.sprite.Group()
for _ in range(5):
enemies.add(Enemy())
score = 0
font = pygame.font.Font(None, 36)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
world.change_zone()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.move(-5, 0)
if keys[pygame.K_RIGHT]:
player.move(5, 0)
if keys[pygame.K_UP]:
player.move(0, -5)
if keys[pygame.K_DOWN]:
player.move(0, 5)
enemies.update()
# Проверка столкновений
hits = pygame.sprite.spritecollide(player, enemies, True)
for hit in hits:
score += 1
enemies.add(Enemy())
# Отрисовка
screen.fill(BLACK)
screen.blit(player.image, player.rect)
enemies.draw(screen)
zone_text = font.render(f"Зона: {world.zones[world.current_zone]}", True, WHITE)
screen.blit(zone_text, (10, 10))
score_text = font.render(f"Счет: {score}", True, WHITE)
screen.blit(score_text, (10, 50))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
if __name__ == "__main__":
game_loop()