sep-pm-platformer/project/main.py

142 lines
4.2 KiB
Python
Raw Normal View History

2023-03-24 17:41:48 +01:00
import random
import pygame
2023-03-25 16:18:44 +01:00
from pygame.font import Font
2023-03-24 17:41:48 +01:00
2023-03-25 15:41:32 +01:00
from level.LevelManager import LevelManager
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.TextLabel import TextLabel
2023-03-24 17:41:48 +01:00
2023-03-25 17:18:43 +01:00
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)
2023-03-24 17:41:48 +01:00
2023-03-25 15:41:32 +01:00
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])
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))
pygame.display.set_caption("PE GAME")
clock = pygame.time.Clock()
2023-03-25 18:14:47 +01:00
frame_rate = 80
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-24 17:41:48 +01:00
2023-03-25 17:18:43 +01:00
physics_handler = PhysicsElementsHandler()
2023-03-25 15:41:32 +01:00
test_1_sprite = DynamicSprite(spritesheet_manager.get_sheet("test_1"))
2023-03-25 17:18:43 +01:00
test_1_sprite.position_scale = PositionScale((10, -100), (1, 1))
physics_handler.add_sprite(test_1_sprite)
2023-03-24 17:41:48 +01:00
2023-03-25 17:18:43 +01:00
test_3_sprite = DynamicSprite(spritesheet_manager.get_sheet("test_1"))
test_3_sprite.position_scale = PositionScale((130, 100), (1, 1))
test_3_sprite.motion = (-4, -11)
physics_handler.add_sprite(test_3_sprite)
2023-03-25 15:41:32 +01:00
2023-03-25 17:18:43 +01:00
test_2_sprite = StaticSprite(spritesheet_manager.get_sheet("test_1"))
test_2_sprite.position_scale = PositionScale((10, 80), (1, 1))
2023-03-25 15:41:32 +01:00
physics_handler.add_sprite(test_2_sprite)
2023-03-25 17:18:43 +01:00
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-25 18:14:47 +01:00
skip = True
2023-03-25 15:41:32 +01:00
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
2023-03-25 18:14:47 +01:00
quit()
2023-03-25 17:18:43 +01:00
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
skip = False
if skip:
continue
2023-03-25 15:41:32 +01:00
screen.fill((0, 0, 0))
2023-03-25 17:18:43 +01:00
physics_handler.tick(apply_frame_rate(1))
2023-03-25 15:41:32 +01:00
physics_handler.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((300, 300))
pygame.display.set_caption("PM GAME")
clock = pygame.time.Clock()
2023-03-25 15:59:59 +01:00
test = TextLabel("Das ist ein Text", 100, 100, 50, Font('data/font/MilkyNice.otf', 50))
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()
screen.fill((0, 0, 0))
test.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)