sep-pm-platformer/project/main.py

289 lines
11 KiB
Python
Raw Normal View History

2023-03-24 17:41:48 +01:00
import random
import pygame
2023-03-25 15:41:32 +01:00
from level.LevelManager import LevelManager
2023-03-26 15:14:06 +02:00
from level.elements.LoadedLevel import LoadedLevel
2023-03-27 14:38:52 +02:00
from physics import ConstantsParser
2023-03-26 09:51:11 +02:00
from physics.SpriteManager import SpriteManager, DrawLayers
from physics.TickData import TickData
2023-03-26 17:01:28 +02:00
from physics.sprites.PlayerSprite import PlayerSprite
2023-03-25 15:41:32 +01:00
from sprite.PositionScale import PositionScale
from sprite.SpritesheetManager import SpritesheetManager
from sprite.Sprite import Sprite
from sprite.StaticSprite import StaticSprite
from ui_elements import CoordinateTransform
from ui_elements.ClickEvent import ClickEvent
2023-03-26 15:01:58 +02:00
from ui_elements.KeyManager import KeyManager
2023-03-27 15:11:35 +02:00
from ui_elements.LevelMenu import LevelMenu
2023-03-25 15:41:32 +01:00
from ui_elements.TextLabel import TextLabel
2023-03-24 17:41:48 +01:00
2023-03-28 10:13:03 +02:00
what_to_run = 'menu'
2023-03-25 17:18:43 +01:00
def apply_frame_rate(number: float):
"""
this function calculates a factor that will be multiplied with the
physics of the game to provide a constant speed
:param number: The number to scale by the factor
:return: The scaled number
"""
return number / (frame_rate / 30)
2023-03-24 17:41:48 +01:00
2023-03-27 15:11:35 +02:00
if what_to_run == 'menu':
screen_transform = PositionScale((0, 0), (1.5, 1.5))
screen_height = 12 * 40 * 1.5
screen_width = 12 * 71 * 1.5
2023-03-27 15:11:35 +02:00
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
2023-03-27 15:11:35 +02:00
pygame.display.set_caption("PM GAME")
clock = pygame.time.Clock()
frame_rate = 30
spritesheet_manager = SpritesheetManager("data/sprites", "data/sprites/sprites.json")
sprite_manager = SpriteManager()
key_manager = KeyManager()
parsed_levels_manager = LevelManager('data/levels')
parsed_levels_manager.load_from_config('data/levels/levels.json')
generated_level = LoadedLevel(sprite_manager, spritesheet_manager)
level_menu_width = screen_width / 2
level_menu_height = screen_height / 2
level_menu = LevelMenu(parsed_levels_manager.levels, 0, 0, level_menu_width, level_menu_height, 3)
2023-03-27 15:11:35 +02:00
level_menu.level_select_listener = lambda selected_level: print(selected_level.name, "selected")
# generated_level.load_level(selected_level)
for level_text_label in level_menu.level_text_labels:
sprite_manager.add_ui_element(DrawLayers.UI, level_text_label)
while True:
clock.tick(frame_rate)
pygame_events: list[pygame.event.Event] = pygame.event.get()
key_manager.update_key_events(pygame_events)
click_events: list[ClickEvent] = ClickEvent.create_events(pygame_events, screen_transform)
for event in click_events:
for layer in sprite_manager.layers:
for sprite in sprite_manager.layers[layer]:
if sprite.get_bounding_box().contains_point(event.screen_position):
sprite.click(event)
2023-03-27 15:11:35 +02:00
for event in pygame_events:
if event.type == pygame.QUIT:
pygame.quit()
quit()
2023-03-28 10:13:03 +02:00
# elif event.type == pygame.MOUSEBUTTONDOWN:
# generated_level.load_level(parsed_levels_manager.levels[0])
2023-03-27 15:11:35 +02:00
screen.fill((0, 0, 0))
# level_menu.draw(screen, screen_transform)
sprite_manager.tick(TickData(apply_frame_rate(1), pygame_events, key_manager, click_events, screen_transform))
sprite_manager.draw(screen, screen_transform)
pygame.display.update()
elif what_to_run == 'level':
2023-03-26 15:14:06 +02:00
screen_transform = PositionScale((0, 0), (1.5, 1.5))
pygame.init()
screen = pygame.display.set_mode((12 * ConstantsParser.CONFIG.level_size[0] * screen_transform.scale[0],
12 * ConstantsParser.CONFIG.level_size[1] * screen_transform.scale[1]))
2023-03-26 15:14:06 +02:00
pygame.display.set_caption("PM GAME")
clock = pygame.time.Clock()
frame_rate = 30
spritesheet_manager = SpritesheetManager("data/sprites", "data/sprites/sprites.json")
sprite_manager = SpriteManager()
key_manager = KeyManager()
parsed_levels_manager = LevelManager('data/levels')
parsed_levels_manager.load_from_config('data/levels/levels.json')
generated_level = LoadedLevel(sprite_manager, spritesheet_manager)
2023-03-27 18:52:34 +02:00
generated_level.load_level(parsed_levels_manager.levels[2])
2023-03-26 17:01:28 +02:00
ghost_character = PlayerSprite(spritesheet_manager.get_sheet("ghost_character"))
ghost_character.position_scale = PositionScale((90, 50), (1, 1))
sprite_manager.add_ui_element(DrawLayers.OBJECTS, ghost_character)
2023-03-27 14:38:52 +02:00
calculated_frame_rate_text = TextLabel("0 FPS", 2, 2, 70, alignment="left")
2023-03-27 12:54:09 +02:00
calculated_frame_rate_text.position_scale.scale = (0.3, 0.3)
sprite_manager.add_ui_element(DrawLayers.UI, calculated_frame_rate_text)
2023-03-26 17:01:28 +02:00
2023-03-26 15:14:06 +02:00
while True:
clock.tick(frame_rate)
2023-03-27 14:38:52 +02:00
calculated_frame_rate_text.text = f"{round(clock.get_fps())} FPS"
2023-03-26 15:14:06 +02:00
pygame_events: list[pygame.event.Event] = pygame.event.get()
key_manager.update_key_events(pygame_events)
click_events: list[ClickEvent] = ClickEvent.create_events(pygame_events, screen_transform)
for event in pygame_events:
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill((0, 0, 0))
sprite_manager.tick(TickData(apply_frame_rate(1), pygame_events, key_manager, click_events, screen_transform))
sprite_manager.draw(screen, screen_transform)
pygame.display.update()
2023-03-24 17:41:48 +01:00
2023-03-25 15:41:32 +01:00
elif what_to_run == 'physics':
screen_transform = PositionScale((0, 0), (4, 4))
2023-03-24 17:41:48 +01:00
2023-03-25 15:41:32 +01:00
pygame.init()
screen = pygame.display.set_mode((600, 500))
2023-03-25 18:22:37 +01:00
pygame.display.set_caption("PM GAME")
2023-03-25 15:41:32 +01:00
clock = pygame.time.Clock()
2023-03-25 18:27:54 +01:00
frame_rate = 59.52
2023-03-24 17:41:48 +01:00
2023-03-25 15:41:32 +01:00
spritesheet_manager = SpritesheetManager("data/sprites", "data/sprites/sprites.json")
2023-03-26 09:51:11 +02:00
sprite_manager = SpriteManager()
2023-03-26 15:01:58 +02:00
key_manager = KeyManager()
# test_1_sprite = DynamicSprite(spritesheet_manager.get_sheet("test_1"))
# test_1_sprite.position_scale = PositionScale((10, -100), (1, 1))
# sprite_manager.add_ui_element(DrawLayers.OBJECTS, test_1_sprite)
# test_3_sprite = DynamicSprite(spritesheet_manager.get_sheet("test_1"))
# test_3_sprite.position_scale = PositionScale((130, 100), (1, 1))
# test_3_sprite.motion = (-9, -10)
# sprite_manager.add_ui_element(DrawLayers.OBJECTS, test_3_sprite)
# test_2_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1"))
# test_2_sprite.position_scale = PositionScale((10, 80), (1, 1))
# sprite_manager.add_ui_element(DrawLayers.OBJECTS, test_2_sprite)
for x in range(0, 8):
floor_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1"))
floor_sprite.position_scale = PositionScale((x * 16, 100), (1, 1))
sprite_manager.add_ui_element(DrawLayers.OBJECTS, floor_sprite)
for x in range(0, 8):
floor_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1"))
floor_sprite.position_scale = PositionScale((x * 16, 0), (1, 1))
sprite_manager.add_ui_element(DrawLayers.OBJECTS, floor_sprite)
for x in range(0, 6):
floor_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1"))
floor_sprite.position_scale = PositionScale((0, x * 16), (1, 1))
sprite_manager.add_ui_element(DrawLayers.OBJECTS, floor_sprite)
for x in range(0, 6):
floor_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1"))
floor_sprite.position_scale = PositionScale((130, x * 16), (1, 1))
sprite_manager.add_ui_element(DrawLayers.OBJECTS, floor_sprite)
ghost_character = PlayerSprite(spritesheet_manager.get_sheet("ghost_character"))
ghost_character.position_scale = PositionScale((90, 50), (1, 1))
sprite_manager.add_ui_element(DrawLayers.OBJECTS, ghost_character)
text_1 = TextLabel("Frame: 0", 2, 110, 50, alignment="left")
2023-03-26 09:51:11 +02:00
text_1.position_scale.scale = (0.1, 0.1)
sprite_manager.add_ui_element(DrawLayers.UI, text_1)
2023-03-25 17:18:43 +01:00
2023-03-26 15:01:58 +02:00
ghost_character.debug_label = text_1
frame_counter = 0
2023-03-25 15:41:32 +01:00
while True:
2023-03-25 17:18:43 +01:00
clock.tick(frame_rate)
2023-03-25 15:41:32 +01:00
2023-03-26 15:01:58 +02:00
skip = False
pygame_events: list[pygame.event.Event] = pygame.event.get()
2023-03-26 15:01:58 +02:00
key_manager.update_key_events(pygame_events)
click_events: list[ClickEvent] = ClickEvent.create_events(pygame_events, screen_transform)
for event in pygame_events:
2023-03-25 15:41:32 +01:00
if event.type == pygame.QUIT:
pygame.quit()
2023-03-25 18:14:47 +01:00
quit()
2023-03-26 15:01:58 +02:00
if key_manager.is_keymap_down(KeyManager.KEY_RIGHT):
skip = False
2023-03-25 17:18:43 +01:00
if skip:
continue
2023-03-25 15:41:32 +01:00
2023-03-26 15:01:58 +02:00
frame_counter += 1
# text_1.set_text(f"Frame: {frame_counter}")
2023-03-25 15:41:32 +01:00
screen.fill((0, 0, 0))
2023-03-26 15:01:58 +02:00
sprite_manager.tick(TickData(apply_frame_rate(1), pygame_events, key_manager, click_events, screen_transform))
2023-03-26 09:51:11 +02:00
sprite_manager.draw(screen, screen_transform)
2023-03-25 15:41:32 +01:00
pygame.display.update()
elif what_to_run == 'textlabel':
screen_transform = PositionScale((0, 0), (4, 4))
pygame.init()
2023-03-25 18:13:59 +01:00
screen = pygame.display.set_mode((800, 800))
2023-03-25 15:41:32 +01:00
pygame.display.set_caption("PM GAME")
clock = pygame.time.Clock()
2023-03-26 09:51:11 +02:00
test1 = TextLabel("Das ist ein Test", 100, 0, 50, alignment="left")
test2 = TextLabel("Das ist ein Test", 100, 50, 50, alignment="left")
test3 = TextLabel("Das ist ein Test", 100, 20, 50, alignment="left")
test1.position_scale.scale = (0.2, 0.2)
test2.position_scale.scale = (0.4, 0.4)
test3.position_scale.scale = (0.2, 0.2)
test3.visible = False
2023-03-25 15:41:32 +01:00
while True:
clock.tick(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
2023-03-25 18:13:59 +01:00
screen.fill((0, 0, 110))
test1.draw(screen, screen_transform)
test2.draw(screen, screen_transform)
test3.draw(screen, screen_transform)
2023-03-25 15:41:32 +01:00
pygame.display.update()
elif what_to_run == 'sprite':
screen_transform = PositionScale((0, 0), (4, 4))
pygame.init()
screen = pygame.display.set_mode((300, 300))
pygame.display.set_caption("PE GAME")
clock = pygame.time.Clock()
spritesheet_manager = SpritesheetManager("data/sprites", "data/sprites/sprites.json")
test_1_sprite = Sprite(spritesheet_manager.get_sheet("test_1"))
test_2_sprite = Sprite(spritesheet_manager.get_sheet("test_1"))
test_1_sprite.position_scale = PositionScale((10, 10), (1, 1))
test_2_sprite.position_scale = PositionScale((60, 60), (1, 1))
# test_1_sprite.dump("debug.png")
while True:
clock.tick(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill((0, 0, 0))
test_1_sprite.tick(1)
test_1_sprite.draw(screen, screen_transform)
test_2_sprite.tick(1)
test_2_sprite.draw(screen, screen_transform)
pygame.display.update()
if random.randint(1, 10) == 1:
test_1_sprite.set_animation_state(random.choice(["walk_r", "walk_l", "idle", "other_test"]))
print(test_1_sprite.animation_state)