2024-04-03 11:20:42 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2024-04-04 19:05:23 +00:00
|
|
|
from math import copysign, pi, acos
|
2024-04-04 19:40:37 +00:00
|
|
|
import sys
|
2024-04-07 11:50:37 +00:00
|
|
|
from importlib import import_module
|
2024-04-04 19:03:41 +00:00
|
|
|
|
2024-04-03 11:20:42 +00:00
|
|
|
import pygame
|
|
|
|
import pygame.freetype
|
2024-04-04 19:03:41 +00:00
|
|
|
|
2024-04-05 12:40:54 +00:00
|
|
|
from gyro import GyroVector, Poincare2Klein
|
|
|
|
from constants import I, WHITE, BLACK, IROT, ROT, OFFSET
|
2024-04-07 16:11:31 +00:00
|
|
|
from draw import draw, DrawnSegment
|
2024-04-04 20:55:54 +00:00
|
|
|
from alert import Alert
|
2024-04-07 11:50:37 +00:00
|
|
|
from engineevents import EngineEvent, EngineEventProcessingError
|
|
|
|
from defaultcontrols import defaultcontrols
|
2024-04-03 11:20:42 +00:00
|
|
|
|
2024-04-04 18:03:47 +00:00
|
|
|
gOrigin = GyroVector(0,1)
|
2024-04-04 16:05:20 +00:00
|
|
|
|
2024-04-03 11:20:42 +00:00
|
|
|
|
2024-04-03 14:07:30 +00:00
|
|
|
def renderDebugInfo(gPlayer, clock, fontSize = 18):
|
|
|
|
font = pygame.freetype.Font(None, fontSize)
|
|
|
|
font_fg = (255, 255, 255)
|
|
|
|
font_bg = (255, 255, 255, 80)
|
|
|
|
|
|
|
|
fps_surf = font.render("FPS: " + str(int(clock.get_fps())), font_fg, font_bg)[0]
|
|
|
|
cPos_surf = font.render("cPos: " + str(gPlayer.cPos), font_fg, font_bg)[0]
|
2024-04-04 18:31:45 +00:00
|
|
|
cRot_surf = font.render("cRot: " + str(copysign(acos(gPlayer.cRot.real),gPlayer.cRot.imag) / pi * 180), font_fg, font_bg)[0]
|
2024-04-03 14:07:30 +00:00
|
|
|
|
|
|
|
return fps_surf, cPos_surf, cRot_surf
|
|
|
|
|
2024-04-04 20:51:26 +00:00
|
|
|
def nonzerocap(n):
|
|
|
|
if n <= 0: return 1
|
|
|
|
return n
|
2024-04-04 18:41:10 +00:00
|
|
|
|
2024-04-07 11:50:37 +00:00
|
|
|
|
2024-04-06 20:12:28 +00:00
|
|
|
|
2024-04-03 11:20:42 +00:00
|
|
|
def mainLoop():
|
2024-04-07 11:50:37 +00:00
|
|
|
sky = (127,127,255)
|
|
|
|
ground = (102, 51, 0)
|
2024-04-05 12:40:54 +00:00
|
|
|
bCap = True
|
|
|
|
bCont = True
|
2024-04-03 11:20:42 +00:00
|
|
|
gPlayer = GyroVector(0,1)
|
|
|
|
display = pygame.display.set_mode((1280,720))
|
2024-04-03 14:07:30 +00:00
|
|
|
clock = pygame.time.Clock()
|
|
|
|
fontSize = 18
|
|
|
|
debugInfo = True
|
2024-04-04 17:01:33 +00:00
|
|
|
wall_buffer = []
|
2024-04-04 20:55:54 +00:00
|
|
|
alerts = []
|
2024-04-06 20:12:28 +00:00
|
|
|
aoEngineEvents = []
|
|
|
|
fvControl_ao = defaultcontrols
|
2024-04-07 11:50:37 +00:00
|
|
|
level = []
|
2024-04-07 12:48:14 +00:00
|
|
|
iDistScale = 1.1
|
2024-04-07 11:50:37 +00:00
|
|
|
|
2024-04-04 20:55:54 +00:00
|
|
|
def alert_append(alert, delay):
|
|
|
|
if len(alerts) >= 1:
|
|
|
|
alerts.pop()
|
|
|
|
|
|
|
|
alerts.append(alert)
|
|
|
|
alert.start_hide_thread(alerts, delay)
|
2024-04-03 14:07:30 +00:00
|
|
|
|
2024-04-07 11:50:37 +00:00
|
|
|
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
|
2024-04-07 12:48:14 +00:00
|
|
|
nonlocal iDistScale
|
2024-04-06 20:12:28 +00:00
|
|
|
state = {
|
|
|
|
'bCap': bCap,
|
|
|
|
'bCont': bCont,
|
|
|
|
'gPlayer': gPlayer,
|
|
|
|
'display': display,
|
|
|
|
'fontSize': fontSize,
|
|
|
|
'debugInfo': debugInfo,
|
|
|
|
'wall_buffer': wall_buffer,
|
|
|
|
'alerts': alerts,
|
|
|
|
'alert_append': alert_append,
|
|
|
|
'aoEngineEvents': aoEngineEvents,
|
2024-04-07 11:50:37 +00:00
|
|
|
'level': level,
|
|
|
|
'sky': sky,
|
2024-04-07 12:48:14 +00:00
|
|
|
'ground': ground,
|
|
|
|
'iDistScale': iDistScale
|
2024-04-06 20:12:28 +00:00
|
|
|
}
|
|
|
|
for i in aoEngineEvents:
|
2024-04-07 12:22:41 +00:00
|
|
|
if i.sVariableToModify == "bCap":
|
2024-04-07 16:03:00 +00:00
|
|
|
bCap = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 12:22:41 +00:00
|
|
|
elif i.sVariableToModify == "bCont":
|
2024-04-07 16:03:00 +00:00
|
|
|
bCont = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 11:50:37 +00:00
|
|
|
elif i.sVariableToModify == "gPlayer":
|
2024-04-07 16:03:00 +00:00
|
|
|
gPlayer = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 12:22:41 +00:00
|
|
|
elif i.sVariableToModify == "fontSize":
|
2024-04-07 16:03:00 +00:00
|
|
|
fontSize = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 12:22:41 +00:00
|
|
|
elif i.sVariableToModify == "wall_buffer":
|
|
|
|
wall_buffer = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
|
|
|
elif i.sVariableToModify == "alerts":
|
2024-04-07 16:03:00 +00:00
|
|
|
alerts = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 12:22:41 +00:00
|
|
|
elif i.sVariableToModify == "sky":
|
2024-04-07 16:03:00 +00:00
|
|
|
sky = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 12:22:41 +00:00
|
|
|
elif i.sVariableToModify == "ground":
|
2024-04-07 16:03:00 +00:00
|
|
|
ground = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 12:22:41 +00:00
|
|
|
elif i.sVariableToModify == "debugInfo":
|
2024-04-07 16:03:00 +00:00
|
|
|
debugInfo = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 11:50:37 +00:00
|
|
|
elif i.sVariableToModify == "level":
|
2024-04-07 16:03:00 +00:00
|
|
|
level = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 12:48:14 +00:00
|
|
|
elif i.sVariableToModify == "iDistScale":
|
2024-04-07 16:03:00 +00:00
|
|
|
iDistScale = i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 11:50:37 +00:00
|
|
|
elif i.sVariableToModify is None:
|
2024-04-07 16:03:00 +00:00
|
|
|
i.fLambda(list(map(lambda oX: state[oX], i.tsArguments)))
|
2024-04-07 11:50:37 +00:00
|
|
|
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()
|
2024-04-03 14:07:30 +00:00
|
|
|
|
2024-04-07 11:50:37 +00:00
|
|
|
while bCont:
|
|
|
|
aoEngineEvents = fvControl_ao()
|
|
|
|
processevents()
|
2024-04-03 11:20:42 +00:00
|
|
|
display.fill(WHITE)
|
2024-04-07 16:11:31 +00:00
|
|
|
if len(level) != 0:
|
|
|
|
drawn = draw(level,gPlayer,pi/4,320,iDistScale)
|
|
|
|
else:
|
|
|
|
drawn = [DrawnSegment(0, BLACK)] * 320
|
2024-04-07 11:50:37 +00:00
|
|
|
pygame.draw.rect(display,sky, (0,0,1280,360))
|
|
|
|
pygame.draw.rect(display,ground, (0,360,1280,360))
|
2024-04-03 11:20:42 +00:00
|
|
|
n = 0
|
|
|
|
for i in drawn:
|
2024-04-07 14:14:32 +00:00
|
|
|
pygame.draw.rect(display,i.color, (n,int((nonzerocap(i.dist)) * 360),4,int((1 -nonzerocap(i.dist)) * 1280)))
|
2024-04-04 16:05:20 +00:00
|
|
|
n += 4
|
2024-04-04 19:08:44 +00:00
|
|
|
gPlayer.normalize()
|
2024-04-03 14:07:30 +00:00
|
|
|
if debugInfo:
|
|
|
|
fps_surf, cPos_surf, cRot_surf = renderDebugInfo(gPlayer, clock, fontSize)
|
|
|
|
|
|
|
|
display.blit(fps_surf, (0, 0))
|
|
|
|
display.blit(cPos_surf, (0, fps_surf.get_height()))
|
|
|
|
display.blit(cRot_surf, (0, fps_surf.get_height() + fontSize))
|
2024-04-06 18:43:51 +00:00
|
|
|
pygame.draw.circle(display, BLACK, (100, 200), 100)
|
2024-04-03 14:20:04 +00:00
|
|
|
for i in level:
|
2024-04-06 14:22:34 +00:00
|
|
|
a = Poincare2Klein(i.pointA)
|
|
|
|
b = Poincare2Klein(i.pointB)
|
2024-04-04 17:10:35 +00:00
|
|
|
pygame.draw.line(display, i.color, (a.real * 100 + 100, (a.imag * 100 + 200)), (b.real * 100 + 100, (b.imag * 100 + 200)))
|
2024-04-04 17:02:44 +00:00
|
|
|
c = complex(Poincare2Klein(gPlayer.cPos))
|
|
|
|
pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10))
|
2024-04-03 13:06:58 +00:00
|
|
|
|
2024-04-04 20:55:54 +00:00
|
|
|
for alert in alerts:
|
|
|
|
alert.draw(display)
|
|
|
|
|
2024-04-03 11:20:42 +00:00
|
|
|
pygame.display.update()
|
2024-04-04 18:25:24 +00:00
|
|
|
if bCap:
|
|
|
|
clock.tick(60)
|
|
|
|
else:
|
|
|
|
clock.tick()
|
2024-04-03 13:06:58 +00:00
|
|
|
return True
|
2024-04-03 11:20:42 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
pygame.init()
|
2024-04-03 13:06:58 +00:00
|
|
|
pygame.display.set_caption('P3HE')
|
2024-04-03 11:20:42 +00:00
|
|
|
if not pygame.get_init():
|
|
|
|
return False
|
|
|
|
retstatus = mainLoop()
|
|
|
|
return retstatus
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if not main():
|
|
|
|
print("An error occured")
|