128 lines
4.1 KiB
Python
Executable File
128 lines
4.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from numba import jit
|
|
|
|
from math import copysign, pi, acos
|
|
from time import time
|
|
import sys
|
|
|
|
import pygame
|
|
import pygame.freetype
|
|
|
|
from levels import Segment, open_level, parse_level_from_string
|
|
from gyro import *
|
|
from levels import *
|
|
from constants import *
|
|
from draw import draw
|
|
|
|
bCap = True
|
|
|
|
gOrigin = GyroVector(0,1)
|
|
|
|
SKY = (127,127,255)
|
|
GROUND = (102, 51, 0)
|
|
|
|
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]
|
|
cRot_surf = font.render("cRot: " + str(copysign(acos(gPlayer.cRot.real),gPlayer.cRot.imag) / pi * 180), font_fg, font_bg)[0]
|
|
|
|
return fps_surf, cPos_surf, cRot_surf
|
|
|
|
|
|
|
|
def mainLoop():
|
|
gPlayer = GyroVector(0,1)
|
|
display = pygame.display.set_mode((1280,720))
|
|
clock = pygame.time.Clock()
|
|
fontSize = 18
|
|
debugInfo = True
|
|
wall_buffer = []
|
|
|
|
try:
|
|
leveltoparse = open_level(sys.argv[1])
|
|
except IndexError:
|
|
leveltoparse = open_level("maps/squareroom.p3hel")
|
|
level = parse_level_from_string(leveltoparse)
|
|
|
|
while True:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
return True
|
|
if event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_r:
|
|
gPlayer.cRot *= -1
|
|
if event.key == pygame.K_F3:
|
|
debugInfo = not debugInfo
|
|
if event.key == pygame.K_F1:
|
|
global bCap
|
|
bCap = not bCap
|
|
if event.key == pygame.K_F2:
|
|
if (len(wall_buffer) == 1):
|
|
wall_buffer.append(gPlayer.cPos)
|
|
make_wall(wall_buffer, level)
|
|
wall_buffer.clear()
|
|
else:
|
|
wall_buffer.append(gPlayer.cPos)
|
|
if event.key == pygame.K_F5:
|
|
save_level(level)
|
|
|
|
keys = pygame.key.get_pressed()
|
|
if keys[pygame.K_d]:
|
|
gPlayer.rotate(ROT)
|
|
if keys[pygame.K_a]:
|
|
gPlayer.rotate(IROT)
|
|
if keys[pygame.K_q]:
|
|
gPlayer += GyroVector(OFFSET * gPlayer.cRot*I, 1)
|
|
if keys[pygame.K_e]:
|
|
gPlayer -= GyroVector(OFFSET * gPlayer.cRot*I, 1)
|
|
if keys[pygame.K_w]:
|
|
gPlayer -= GyroVector(OFFSET * gPlayer.cRot, 1)
|
|
if keys[pygame.K_s]:
|
|
gPlayer += GyroVector(OFFSET * gPlayer.cRot, 1)
|
|
|
|
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))
|
|
n = 0
|
|
for i in drawn:
|
|
pygame.draw.rect(display,i.color, (n,int((cap(i.dist)) * 360),8,int(cap(1 - i.dist) * 1280)))
|
|
n += 4
|
|
gPlayer.normalize()
|
|
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))
|
|
pygame.draw.rect(display, BLACK, (0, 100, 200, 200))
|
|
for i in level:
|
|
a = complex(Poincare2Klein(i.pointA))
|
|
b = complex(Poincare2Klein(i.pointB))
|
|
pygame.draw.line(display, i.color, (a.real * 100 + 100, (a.imag * 100 + 200)), (b.real * 100 + 100, (b.imag * 100 + 200)))
|
|
c = complex(Poincare2Klein(gPlayer.cPos))
|
|
pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10))
|
|
|
|
pygame.display.update()
|
|
if bCap:
|
|
clock.tick(60)
|
|
else:
|
|
clock.tick()
|
|
return True
|
|
|
|
def main():
|
|
pygame.init()
|
|
pygame.display.set_caption('P3HE')
|
|
if not pygame.get_init():
|
|
return False
|
|
retstatus = mainLoop()
|
|
return retstatus
|
|
|
|
if __name__ == "__main__":
|
|
if not main():
|
|
print("An error occured")
|