elukjanovica 2024-01-29 12:47:42 +02:00
parent bbfd4eaa6c
commit 81654999d7
1 changed files with 35 additions and 21 deletions

56
main.py
View File

@ -1,5 +1,6 @@
import pygame
# Загрузка спрайтов движения
def load_movement_sprites(prefix, num_sprites):
sprite_list = []
for i in range(1, num_sprites + 1):
@ -8,13 +9,14 @@ def load_movement_sprites(prefix, num_sprites):
sprite_list.append(sprite)
return sprite_list
# Спрайты
# Спрайты игрока
player_image = pygame.image.load("player.png")
forward_sprites = load_movement_sprites("forward", 5)
backward_sprites = load_movement_sprites("backward", 5)
left_sprites = load_movement_sprites("left", 5)
right_sprites = load_movement_sprites("right", 5)
# Игрок
class Player:
def __init__(self, x, y):
self.x = x
@ -39,6 +41,7 @@ class Player:
def draw(self):
screen.blit(self.movement_sprites[self.current_movement][self.current_sprite_index], (self.x, self.y))
# Камера
class Camera:
def __init__(self, width, height):
self.width = width
@ -55,6 +58,7 @@ class Camera:
self.x = -target.x + self.width // 2
self.y = -target.y + self.height // 2
# Стена
class Wall(pygame.sprite.Sprite):
def __init__(self, color, size, pos):
super().__init__()
@ -62,6 +66,7 @@ class Wall(pygame.sprite.Sprite):
self.image.fill(color)
self.rect = self.image.get_rect(center=pos)
# Кнопка
class Button:
def __init__(self, up_image_path, over_image_path, position):
self.up_image = pygame.image.load(up_image_path)
@ -79,20 +84,30 @@ class Button:
else:
self.image = self.up_image
# Класс игрового состояния
class GameState:
MENU = 0
GAMEPLAY = 1
# Инициализация Pygame
pygame.init()
# Настройки
FPS = 60
BACKGROUND_IMAGE = "background.png"
FONT, SIZE = 'comicsansms', 14
screen_info = pygame.display.Info() # Пока-что пусть будет
# Информация о экране
screen_info = pygame.display.Info()
screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN)
pygame.display.set_caption('Hell Circus')
programIcon = pygame.image.load('icon.png')
pygame.display.set_icon(programIcon)
# Шрифт
font = pygame.font.SysFont(FONT, SIZE)
# Часы и время
clock = pygame.time.Clock()
deltatime = 0
running = True
@ -102,11 +117,13 @@ play_button = Button("play1.png", "play2.png", (125, 3))
continue_button = Button("continue1.png", "continue2.png", (110, 3))
menu_button = Button("menu1.png", "menu2.png", (95, 3))
# Стены, тут надо всё менять
walls = pygame.sprite.Group(Wall("white", (1280, 20), (640, 0)), Wall("white", (1280, 20), (640, 720)),
Wall("white", (20, 720), (0, 360)), Wall("white", (1280, 20), (1280, 720)),
Wall("white", (200, 20), (640, 360)), Wall("white", (20, 200), (640, 360)),
Wall("white", (1280, 20), (1280, 320)))
# Класс персонажа
class Character(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color, name):
super().__init__()
@ -115,6 +132,7 @@ class Character(pygame.sprite.Sprite):
self.rect = self.image.get_rect(topleft=(x, y))
self.name = name
# Класс игрока
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color):
super().__init__()
@ -122,19 +140,18 @@ class Player(pygame.sprite.Sprite):
self.image.fill(color)
self.rect = self.image.get_rect(topleft=(x, y))
# Камера
camera = Camera(1920, 1080)
player = Player(200, 200, 50, 50, (255, 0, 0))
character1 = Character(200, 200, 50, 50, "red", "Character 1")
character2 = Character(400, 300, 50, 50, "blue", "Character 2")
# Группы спрайтов
character_sprites = pygame.sprite.Group(character1, character2)
player_sprite = pygame.sprite.Group(player)
# Менюм / Игровой режим
class GameState:
MENU = 0
GAMEPLAY = 1
# Игровое состояние
current_state = GameState.MENU
while running:
@ -145,17 +162,14 @@ while running:
if event.key == pygame.K_ESCAPE:
running = False
if current_state == GameState.MENU:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouse_x, mouse_y = pygame.mouse.get_pos()
if play_button.is_hovered((mouse_x, mouse_y)):
# Переход в игровой режим
current_state = GameState.GAMEPLAY
if current_state == GameState.MENU:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
mouse_x, mouse_y = pygame.mouse.get_pos()
if play_button.is_hovered((mouse_x, mouse_y)):
# Переход в игровой режим
current_state = GameState.GAMEPLAY
elif current_state == GameState.GAMEPLAY:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
nearest_character = min(character_sprites, key=lambda c: pygame.math.Vector2(c.rect.center).distance_to(player.rect.center))
@ -170,7 +184,7 @@ while running:
if camera.follow_player:
camera.update(player.rect)
background_img_image = pygame.image.load(BACKGROUND_IMAGE).convert_alpha()
screen.blit(background_img_image, (0, 0))
@ -197,9 +211,9 @@ while running:
for sprite in player_sprite:
screen.blit(sprite.image, camera.apply(sprite.rect))
pygame.display.flip()
deltatime = clock.tick(FPS) / 1000
pygame.display.flip()
deltatime = clock.tick(FPS) / 1000
screen.fill("black")
screen.fill("black")
pygame.quit()
pygame.quit()