Загрузка данных
import pygame
import sys
# Инициализация Pygame
pygame.init()
SCREEN_WIDTH = 960
SCREEN_HEIGHT = 640
FPS = 60
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("The Witcher 2D - Prototype")
clock = pygame.time.Clock()
# Цвета
COLOR_BG = (34, 40, 49)
COLOR_WITCHER = (238, 238, 238)
COLOR_SWORD = (180, 200, 220)
COLOR_HP = (219, 68, 85)
COLOR_STAMINA = (244, 180, 26)
COLOR_BAR_BG = (50, 50, 50)
class Witcher(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.rect = pygame.Rect(x, y, 36, 48)
self.base_speed = 4
self.speed = self.base_speed
# Характеристики
self.max_hp = 100
self.hp = 100
self.max_stamina = 100
self.stamina = 100
self.stamina_regen = 0.4
# Состояние боя
self.is_attacking = False
self.attack_timer = 0
self.facing = pygame.Vector2(0, 1) # Смотрит вниз по умолчанию
self.attack_rect = pygame.Rect(0, 0, 0, 0)
# Состояние уклонения (рывок)
self.is_dodging = False
self.dodge_timer = 0
def handle_input(self):
keys = pygame.key.get_pressed()
move_vec = pygame.Vector2(0, 0)
if keys[pygame.K_w] or keys[pygame.K_UP]:
move_vec.y -= 1
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
move_vec.y += 1
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
move_vec.x -= 1
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
move_vec.x += 1
if move_vec.length() > 0:
move_vec = move_vec.normalize()
self.facing = move_vec
# Рывок (Кувырок) на Shift
if (keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) and not self.is_dodging and self.stamina >= 25:
if move_vec.length() > 0:
self.is_dodging = True
self.dodge_timer = 12
self.stamina -= 25
# Атака мечом на Пробел
if keys[pygame.K_SPACE] and not self.is_attacking and self.stamina >= 10:
self.is_attacking = True
self.attack_timer = 15
self.stamina -= 10
# Перемещение с учетом рывка
current_speed = self.base_speed * 2.2 if self.is_dodging else self.base_speed
self.rect.x += int(move_vec.x * current_speed)
self.rect.y += int(move_vec.y * current_speed)
# Ограничение экраном
self.rect.clamp_ip(screen.get_rect())
def update(self):
# Регенерация выносливости
if self.stamina < self.max_stamina:
self.stamina = min(self.max_stamina, self.stamina + self.stamina_regen)
# Таймер кувырка
if self.is_dodging:
self.dodge_timer -= 1
if self.dodge_timer <= 0:
self.is_dodging = False
# Расчет зоны удара мечом
if self.is_attacking:
self.attack_timer -= 1
reach = 32
sword_x = self.rect.centerx + int(self.facing.x * reach) - 12
sword_y = self.rect.centery + int(self.facing.y * reach) - 12
self.attack_rect = pygame.Rect(sword_x, sword_y, 24, 24)
if self.attack_timer <= 0:
self.is_attacking = False
self.attack_rect = pygame.Rect(0, 0, 0, 0)
def draw(self, surface):
# Отрисовка персонажа
color = (150, 150, 250) if self.is_dodging else COLOR_WITCHER
pygame.draw.rect(surface, color, self.rect, border_radius=4)
# Отрисовка взмаха меча
if self.is_attacking:
pygame.draw.rect(surface, COLOR_SWORD, self.attack_rect)
def draw_hud(surface, player):
# Полоска здоровья
pygame.draw.rect(surface, COLOR_BAR_BG, (20, 20, 200, 16), border_radius=3)
hp_width = int(200 * (player.hp / player.max_hp))
pygame.draw.rect(surface, COLOR_HP, (20, 20, hp_width, 16), border_radius=3)
# Полоска энергии (выносливости)
pygame.draw.rect(surface, COLOR_BAR_BG, (20, 42, 200, 12), border_radius=3)
stamina_width = int(200 * (player.stamina / player.max_stamina))
pygame.draw.rect(surface, COLOR_STAMINA, (20, 42, stamina_width, 12), border_radius=3)
# Главный цикл
witcher = Witcher(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
witcher.handle_input()
witcher.update()
screen.fill(COLOR_BG)
witcher.draw(screen)
draw_hud(screen, witcher)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()