sep-pm-platformer/project/main.py

182 lines
6.2 KiB
Python

import random
import pygame
from pygame.font import Font
from level.LevelManager import LevelManager
from physics.SpriteManager import SpriteManager, DrawLayers
from sprite.DynamicSprite import DynamicSprite
from sprite.PositionScale import PositionScale
from sprite.SpritesheetManager import SpritesheetManager
from physics.PhysicsElementsHandler import PhysicsElementsHandler
from sprite.Sprite import Sprite
from sprite.StaticSprite import StaticSprite
from ui_elements.ClickEvent import ClickEvent
from ui_elements.TextLabel import TextLabel
from ui_elements.UiElement import UiElement
what_to_run = 'physics'
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)
if what_to_run == 'level':
csv_parse_test = LevelManager('data/levels')
csv_parse_test.load_from_config('data/levels/levels.json')
print(csv_parse_test.levels[0])
for row in csv_parse_test.levels[0].tiles:
print(row)
elif what_to_run == 'physics':
screen_transform = PositionScale((0, 0), (4, 4))
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("PM GAME")
clock = pygame.time.Clock()
frame_rate = 59.52
spritesheet_manager = SpritesheetManager("data/sprites", "data/sprites/sprites.json")
sprite_manager = SpriteManager()
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)
text_1 = TextLabel("Frame: 0", 10, 110, 50, alignment="left")
text_1.position_scale.scale = (0.1, 0.1)
sprite_manager.add_ui_element(DrawLayers.UI, text_1)
counter = 0
while True:
clock.tick(frame_rate)
skip = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
skip = False
elif event.type == pygame.MOUSEBUTTONUP:
screen_pos = pygame.mouse.get_pos()
world_pos = UiElement.transform_screen_to_world(screen_pos, screen_transform)
click_event = ClickEvent(world_pos, screen_pos, event)
if event.button == 1:
test_dyn_sprite = DynamicSprite(spritesheet_manager.get_sheet("test_1"))
test_dyn_sprite.position_scale = PositionScale(world_pos, (1, 1))
sprite_manager.add_ui_element(DrawLayers.OBJECTS, test_dyn_sprite)
elif event.button == 3:
test_dyn_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1"))
test_dyn_sprite.position_scale = PositionScale(world_pos, (1, 1))
sprite_manager.add_ui_element(DrawLayers.OBJECTS, test_dyn_sprite)
elif event.button == 2:
for element in sprite_manager.find_elements_at_position(world_pos):
sprite_manager.remove_ui_element(element)
element.click(click_event)
if skip:
continue
counter += 1
text_1.set_text(f"Frame: {counter}")
screen.fill((0, 0, 0))
sprite_manager.tick(apply_frame_rate(1))
sprite_manager.draw(screen, screen_transform)
pygame.display.update()
elif what_to_run == 'textlabel':
screen_transform = PositionScale((0, 0), (4, 4))
pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("PM GAME")
clock = pygame.time.Clock()
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
while True:
clock.tick(5)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill((0, 0, 110))
test1.draw(screen, screen_transform)
test2.draw(screen, screen_transform)
test3.draw(screen, screen_transform)
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)