updated hud, updated images
parent
54e2b8dcc2
commit
ae308f5946
61
main.py
61
main.py
|
|
@ -4,10 +4,6 @@ import sys
|
|||
pygame.init()
|
||||
pygame.mixer.init()
|
||||
|
||||
# =====================================================
|
||||
# НАСТРОЙКИ
|
||||
# =====================================================
|
||||
|
||||
WIDTH, HEIGHT = 1000, 800
|
||||
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||
pygame.display.set_caption("Goblin & Knight")
|
||||
|
|
@ -19,15 +15,10 @@ GROUND_Y = HEIGHT - 60
|
|||
|
||||
FPS = 60
|
||||
|
||||
# =====================================================
|
||||
# ЗАГРУЗКА ФОНОВ
|
||||
# =====================================================
|
||||
|
||||
BACKGROUND = pygame.image.load("images/back.png")
|
||||
BACK_MAIN = pygame.image.load("images/back_main.jpg")
|
||||
BACK_MAIN = pygame.transform.scale(BACK_MAIN, (WIDTH, HEIGHT))
|
||||
|
||||
# Попытаться загрузить музыку/звуки, если есть
|
||||
ь
|
||||
BG_MUSIC = None
|
||||
try:
|
||||
BG_MUSIC = pygame.mixer.music
|
||||
|
|
@ -36,20 +27,14 @@ try:
|
|||
except Exception:
|
||||
BG_MUSIC = None
|
||||
|
||||
# Шрифты и HUD
|
||||
FONT = pygame.font.SysFont(None, 32)
|
||||
|
||||
# Игровые счётчики
|
||||
score_goblin = 0
|
||||
score_knight = 0
|
||||
|
||||
# Пауза
|
||||
paused = False
|
||||
|
||||
|
||||
# =====================================================
|
||||
# КЛАСС ИГРОКА
|
||||
# =====================================================
|
||||
|
||||
class Player:
|
||||
def __init__(self, x, y, sprites, scale, controls):
|
||||
|
|
@ -71,13 +56,13 @@ class Player:
|
|||
self.anim_counter = 0
|
||||
self.ANIM_SPEED = 8
|
||||
|
||||
# Загрузка спрайтов из файлов
|
||||
|
||||
self.stand = pygame.image.load(sprites["stand"]).convert_alpha()
|
||||
self.walk_left = pygame.image.load(sprites["walk_left"]).convert_alpha()
|
||||
self.walk_right = pygame.image.load(sprites["walk_right"]).convert_alpha()
|
||||
self.jump_img = pygame.image.load(sprites["jump"]).convert_alpha()
|
||||
|
||||
# Масштабирование
|
||||
|
||||
self.stand = pygame.transform.scale(self.stand, (int(self.stand.get_width() * scale), int(self.stand.get_height() * scale)))
|
||||
self.walk_left = pygame.transform.scale(self.walk_left, (int(self.walk_left.get_width() * scale), int(self.walk_left.get_height() * scale)))
|
||||
self.walk_right = pygame.transform.scale(self.walk_right, (int(self.walk_right.get_width() * scale), int(self.walk_right.get_height() * scale)))
|
||||
|
|
@ -86,9 +71,7 @@ class Player:
|
|||
self.surface = self.stand
|
||||
self.rect = self.surface.get_rect(midbottom=(self.x, self.y))
|
||||
|
||||
# -------------------------
|
||||
# СБРОС ПЕРСОНАЖА
|
||||
# -------------------------
|
||||
|
||||
def reset(self):
|
||||
self.x = self.start_x
|
||||
self.y = self.start_y
|
||||
|
|
@ -99,9 +82,7 @@ class Player:
|
|||
self.facing_right = True
|
||||
self.rect = self.surface.get_rect(midbottom=(self.x, self.y))
|
||||
|
||||
# -------------------------
|
||||
# УПРАВЛЕНИЕ
|
||||
# -------------------------
|
||||
|
||||
def handle_input(self, keys):
|
||||
self.moving = False
|
||||
|
||||
|
|
@ -119,15 +100,13 @@ class Player:
|
|||
self.jumping = True
|
||||
self.y_velocity = -self.jump_power
|
||||
|
||||
# Ограничение по экрану
|
||||
|
||||
if self.x < 0:
|
||||
self.x = 0
|
||||
if self.x > WIDTH:
|
||||
self.x = WIDTH
|
||||
|
||||
# -------------------------
|
||||
# ГРАВИТАЦИЯ
|
||||
# -------------------------
|
||||
|
||||
def apply_gravity(self):
|
||||
self.y_velocity += GRAVITY
|
||||
self.y += self.y_velocity
|
||||
|
|
@ -137,9 +116,7 @@ class Player:
|
|||
self.y_velocity = 0
|
||||
self.jumping = False
|
||||
|
||||
# -------------------------
|
||||
# АНИМАЦИЯ
|
||||
# -------------------------
|
||||
|
||||
def update_animation(self):
|
||||
if self.jumping:
|
||||
self.surface = self.jump_img
|
||||
|
|
@ -155,9 +132,7 @@ class Player:
|
|||
|
||||
self.rect = self.surface.get_rect(midbottom=(int(self.x), int(self.y)))
|
||||
|
||||
# -------------------------
|
||||
# ОТРИСОВКА
|
||||
# -------------------------
|
||||
|
||||
def draw(self, screen):
|
||||
screen.blit(self.surface, self.rect)
|
||||
|
||||
|
|
@ -196,9 +171,7 @@ knight = Player(
|
|||
}
|
||||
)
|
||||
|
||||
# =====================================================
|
||||
# ОБЪЕКТЫ УРОВНЯ
|
||||
# =====================================================
|
||||
|
||||
|
||||
door_image = pygame.image.load("images/door.png")
|
||||
door_rect = door_image.get_rect(center=(800, 700))
|
||||
|
|
@ -206,17 +179,11 @@ door_rect = door_image.get_rect(center=(800, 700))
|
|||
button_play = pygame.image.load("images/play_button.png")
|
||||
button_rect = button_play.get_rect(center=(500, 400))
|
||||
|
||||
# =====================================================
|
||||
# ФУНКЦИЯ РЕСТАРТА
|
||||
# =====================================================
|
||||
|
||||
def restart_level():
|
||||
goblin.reset()
|
||||
knight.reset()
|
||||
|
||||
# =====================================================
|
||||
# GAME LOOP
|
||||
# =====================================================
|
||||
|
||||
level = 0
|
||||
level_start_time = None
|
||||
|
|
@ -239,7 +206,6 @@ while True:
|
|||
|
||||
SCREEN.fill((0, 0, 0))
|
||||
|
||||
# ================= MENU =================
|
||||
if level == 0:
|
||||
SCREEN.blit(BACK_MAIN, (0, 0))
|
||||
SCREEN.blit(button_play, button_rect)
|
||||
|
|
@ -257,23 +223,21 @@ while True:
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
# ================= GAME =================
|
||||
elif level == 1:
|
||||
SCREEN.blit(BACKGROUND, (0, 0))
|
||||
SCREEN.blit(door_image, door_rect)
|
||||
|
||||
if not paused:
|
||||
# --- Goblin ---
|
||||
|
||||
goblin.handle_input(keys)
|
||||
goblin.apply_gravity()
|
||||
goblin.update_animation()
|
||||
|
||||
# --- Knight ---
|
||||
|
||||
knight.handle_input(keys)
|
||||
knight.apply_gravity()
|
||||
knight.update_animation()
|
||||
|
||||
# Draw players (even when paused)
|
||||
goblin.draw(SCREEN)
|
||||
knight.draw(SCREEN)
|
||||
elapsed = 0
|
||||
|
|
@ -290,7 +254,6 @@ while True:
|
|||
p = FONT.render('PAUSED - press ESC to resume', True, (255,255,0))
|
||||
SCREEN.blit(p, (WIDTH//2 - p.get_width()//2, HEIGHT//2 - 20))
|
||||
|
||||
# Победа
|
||||
if goblin.rect.colliderect(door_rect) and keys[pygame.K_e]:
|
||||
score_goblin += 1
|
||||
restart_level()
|
||||
|
|
|
|||
Loading…
Reference in New Issue