From db3ea32c75fa1bc7637188973ed574a511122f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Sun, 7 Apr 2024 16:50:37 +0500 Subject: [PATCH] Change how games are initialized. --- games/squareroom/__init__.py | 10 ++- games/triangleroom/__init__.py | 10 ++- main.py | 135 ++++++++++++++------------------- 3 files changed, 72 insertions(+), 83 deletions(-) diff --git a/games/squareroom/__init__.py b/games/squareroom/__init__.py index 79485b1..20b1da4 100644 --- a/games/squareroom/__init__.py +++ b/games/squareroom/__init__.py @@ -2,10 +2,16 @@ import importlib.resources as impr import sys import json -def init(serialize): +def init(modules, aoEngineEvents): + serialize = modules['serialize'] + EngineEvent = modules['engineevents'].EngineEvent curm = sys.modules[__name__] files = impr.files(curm) level = None with files.joinpath("squareroom.json").open('r') as leveldir: level = json.loads(leveldir.read(), object_hook=serialize.object_hook) - return level + aoEngineEvents.append(EngineEvent( + 'level', + lambda _: level, + tsArguments=() + )) diff --git a/games/triangleroom/__init__.py b/games/triangleroom/__init__.py index a2f1d56..8d7f4c9 100644 --- a/games/triangleroom/__init__.py +++ b/games/triangleroom/__init__.py @@ -2,10 +2,16 @@ import importlib.resources as impr import sys import json -def init(serialize): +def init(modules, aoEngineEvents): + serialize = modules['serialize'] + EngineEvent = modules['engineevents'].EngineEvent curm = sys.modules[__name__] files = impr.files(curm) level = None with files.joinpath("triangle.json").open('r') as leveldir: level = json.loads(leveldir.read(), object_hook=serialize.object_hook) - return level + aoEngineEvents.append(EngineEvent( + 'level', + lambda _: level, + tsArguments=() + )) diff --git a/main.py b/main.py index f631cda..ff46273 100755 --- a/main.py +++ b/main.py @@ -2,21 +2,20 @@ from math import copysign, pi, acos import sys +from importlib import import_module import pygame import pygame.freetype from gyro import GyroVector, Poincare2Klein -from levels import open_level, save_level, make_wall from constants import I, WHITE, BLACK, IROT, ROT, OFFSET from draw import draw from alert import Alert -from importlib import import_module +from engineevents import EngineEvent, EngineEventProcessingError +from defaultcontrols import defaultcontrols gOrigin = GyroVector(0,1) -SKY = (127,127,255) -GROUND = (102, 51, 0) def renderDebugInfo(gPlayer, clock, fontSize = 18): font = pygame.freetype.Font(None, fontSize) @@ -33,67 +32,11 @@ def nonzerocap(n): if n <= 0: return 1 return n -class EngineEvent: - def __init__(self, sVariableToModify, fLambda, tsArguments=None): - self.sVariableToModify = sVariableToModify - self.fLambda = fLambda - if tsArguments is not None: - self.tsArguments = tsArguments - else: - self.tsArguments = (sVariableToModify,) -def defaultcontrols(): - aoEngineEvents = list() - for event in pygame.event.get(): - if event.type == pygame.QUIT: - aoEngineEvents.append(EngineEvent("bCont", lambda _: False)) - if event.type == pygame.KEYDOWN: - if event.key == pygame.K_r: - aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(-1), tsArguments=("gPlayer",))) - if event.key == pygame.K_F3: - aoEngineEvents.append(EngineEvent("debugInfo", lambda args: not args[0])) - if event.key == pygame.K_F1: - aoEngineEvents.append(EngineEvent("bCap", lambda args: not args[0])) - if event.key == pygame.K_F2: - def fX(args): - #0: wall_buffer - #1: gPlayer - #2: level - if (len(args[0]) == 1): - args[0].append(args[1].cPos) - make_wall(args[0], args[2]) - args[0].clear() - else: - args[0].append(args[1].cPos) - aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("wall_buffer", "gPlayer", "level") )) - if event.key == pygame.K_F5: - #0: level - #1: alert_append - #2: display - def fX(args): - filename = save_level(args[0]) - alert = Alert(f"File saved as {filename}", args[2]) - - args[1](alert, 5) - aoEngineEvents.append(EngineEvent(None, fX, tsArguments=("level", "alert_append", "display") )) - if event.key == pygame.K_F4: - aoEngineEvents.append(EngineEvent(None, lambda args: print(args[0].cPos), tsArguments=("gPlayer",))) - keys = pygame.key.get_pressed() - if keys[pygame.K_d]: - aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate( ROT), tsArguments=("gPlayer",))) - if keys[pygame.K_a]: - aoEngineEvents.append(EngineEvent(None, lambda args: args[0].rotate(IROT), tsArguments=("gPlayer",))) - if keys[pygame.K_q]: - aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(OFFSET * args[0].cRot*I, 1)), tsArguments=("gPlayer",))) - if keys[pygame.K_e]: - aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(OFFSET * args[0].cRot*I, 1)), tsArguments=("gPlayer",))) - if keys[pygame.K_w]: - aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__isub__(GyroVector(OFFSET * args[0].cRot , 1)), tsArguments=("gPlayer",))) - if keys[pygame.K_s]: - aoEngineEvents.append(EngineEvent(None, lambda args: args[0].__iadd__(GyroVector(OFFSET * args[0].cRot , 1)), tsArguments=("gPlayer",))) - return aoEngineEvents def mainLoop(): + sky = (127,127,255) + ground = (102, 51, 0) bCap = True bCont = True gPlayer = GyroVector(0,1) @@ -105,7 +48,8 @@ def mainLoop(): alerts = [] aoEngineEvents = [] fvControl_ao = defaultcontrols - + level = [] + def alert_append(alert, delay): if len(alerts) >= 1: alerts.pop() @@ -113,15 +57,20 @@ def mainLoop(): alerts.append(alert) alert.start_hide_thread(alerts, delay) - try: - game = import_module('games.' + sys.argv[1]) - except IndexError: - game = import_module("games.squareroom") - - level = game.init(import_module('serialize')) - - while bCont: - aoEngineEvents = fvControl_ao() + def processevents(): + nonlocal bCap + nonlocal bCont + nonlocal gPlayer + nonlocal display + nonlocal fontSize + nonlocal debugInfo + nonlocal wall_buffer + nonlocal alerts + nonlocal alert_append + nonlocal aoEngineEvents + nonlocal level + nonlocal sky + nonlocal ground state = { 'bCap': bCap, 'bCont': bCont, @@ -133,24 +82,52 @@ def mainLoop(): 'alerts': alerts, 'alert_append': alert_append, 'aoEngineEvents': aoEngineEvents, - 'level': level + 'level': level, + 'sky': sky, + 'ground': ground } for i in aoEngineEvents: if i.sVariableToModify == "bCont": bCont = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) - if i.sVariableToModify == "debugInfo": + elif i.sVariableToModify == "debugInfo": debugInfo = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) - if i.sVariableToModify == "gPlayer": + elif i.sVariableToModify == "gPlayer": gPlayer = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) - if i.sVariableToModify == "bCap": + elif i.sVariableToModify == "bCap": bCap = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) - if i.sVariableToModify is None: + elif i.sVariableToModify == "level": + level = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) + elif i.sVariableToModify is None: i.fLambda(list(map(lambda oX: state[oX], i.tsArguments))) + else: + raise EngineEventProcessingError('Unknown variable: ' + i.sVariableToModify) + aoEngineEvents.clear() + try: + game = import_module('games.' + sys.argv[1]) + except IndexError: + game = import_module("games.squareroom") + + game.init( + { + 'serialize': import_module('serialize'), + 'alert': import_module('alert'), + 'gyro': import_module('gyro'), + 'engineevents': import_module('engineevents'), + 'constants': import_module('constants'), + 'lines': import_module('lines'), + }, + aoEngineEvents + ) + processevents() + + while bCont: + aoEngineEvents = fvControl_ao() + processevents() display.fill(WHITE) drawn = draw(level,gPlayer,pi/4,320) - pygame.draw.rect(display,SKY, (0,0,1280,360)) - pygame.draw.rect(display,GROUND, (0,360,1280,360)) + pygame.draw.rect(display,sky, (0,0,1280,360)) + pygame.draw.rect(display,ground, (0,360,1280,360)) n = 0 for i in drawn: pygame.draw.rect(display,i.color, (n,int((nonzerocap(i.dist)) * 360),8,int((1 -nonzerocap(i.dist)) * 1280)))