2024-04-03 11:20:42 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from time import time_ns, sleep
|
|
|
|
from cmath import exp
|
|
|
|
import pygame
|
|
|
|
import pygame.freetype
|
|
|
|
from gyro import *
|
|
|
|
from lines import *
|
|
|
|
|
|
|
|
|
2024-04-03 11:46:22 +00:00
|
|
|
def deg2rad(rA): return rA/180*PI
|
|
|
|
|
2024-04-03 12:30:52 +00:00
|
|
|
I = complex(0,1)
|
2024-04-03 11:20:42 +00:00
|
|
|
WHITE = (255,255,255)
|
|
|
|
BLACK = (0,0,0)
|
2024-04-03 12:20:11 +00:00
|
|
|
OFFSET = 0.015625
|
2024-04-03 11:20:42 +00:00
|
|
|
PI = 3.14159265358979323846264338327
|
2024-04-03 11:46:22 +00:00
|
|
|
ROT = cNorm(exp(deg2rad(2)*I))
|
2024-04-03 11:20:42 +00:00
|
|
|
IROT = 1/ROT
|
|
|
|
F = 5 * 10**7
|
|
|
|
F_ = 10**9
|
|
|
|
|
|
|
|
|
|
|
|
class Segment:
|
|
|
|
def __init__(self,bA,cA,cB,trColor):
|
|
|
|
self.isFinite = bA
|
|
|
|
self.pointA = cA
|
|
|
|
self.pointB = cB
|
|
|
|
self.color = trColor
|
|
|
|
|
|
|
|
class DrawnSegment:
|
|
|
|
def __init__(self,height,color):
|
|
|
|
self.height = height
|
|
|
|
self.color = color
|
|
|
|
|
|
|
|
level = [
|
2024-04-03 12:20:11 +00:00
|
|
|
Segment(False,.5,I*.5,(255,0,0)),
|
|
|
|
Segment(False,.5,-I*.5,(0,255,0)),
|
|
|
|
Segment(False,-.5,-I*.5,(0,0,255)),
|
2024-04-03 12:30:52 +00:00
|
|
|
Segment(False,-.5,I*.5,(255,255,0))
|
2024-04-03 11:20:42 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def draw(level,gPlayer,fov,res):
|
|
|
|
drawn = list()
|
|
|
|
irot = exp(fov/res*I)
|
2024-04-03 11:46:22 +00:00
|
|
|
iprot = gPlayer.cRot
|
2024-04-03 11:20:42 +00:00
|
|
|
for i in range(res):
|
|
|
|
m = DrawnSegment(0,BLACK)
|
2024-04-03 11:46:22 +00:00
|
|
|
rot = irot**(i-(res/2)) * iprot
|
2024-04-03 11:20:42 +00:00
|
|
|
for j in level:
|
|
|
|
tA = Poincare2Klein(MobiusAdd(j.pointA,-gPlayer.cPos))
|
|
|
|
tB = Poincare2Klein(MobiusAdd(j.pointB,-gPlayer.cPos))
|
2024-04-03 11:46:22 +00:00
|
|
|
try: # This function faults from time to time
|
|
|
|
cInt = cLineIntersection(tA,tB,complex(0),rot)
|
|
|
|
except ZeroDivisionError:
|
|
|
|
continue
|
|
|
|
rDist = cDist(0,cInt)
|
2024-04-03 11:20:42 +00:00
|
|
|
if cDot(cInt,rot) > 0:
|
|
|
|
continue
|
|
|
|
if (1 - m.height) > rDist:
|
|
|
|
m = DrawnSegment(1-rDist,j.color)
|
|
|
|
drawn.append(m)
|
|
|
|
return drawn
|
|
|
|
|
|
|
|
def cap(rN):
|
|
|
|
if rN < 0:
|
|
|
|
return 0
|
|
|
|
return rN
|
|
|
|
|
|
|
|
def mainLoop():
|
|
|
|
gPlayer = GyroVector(0,1)
|
|
|
|
display = pygame.display.set_mode((1280,720))
|
|
|
|
font = pygame.freetype.Font(None,24)
|
|
|
|
while True:
|
|
|
|
framestart = time_ns()
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
return True
|
2024-04-03 12:20:11 +00:00
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == pygame.K_q:
|
|
|
|
gPlayer.cRot *= -1
|
2024-04-03 11:20:42 +00:00
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_d]:
|
|
|
|
gPlayer.rotate(ROT)
|
2024-04-03 12:20:11 +00:00
|
|
|
if keys[pygame.K_a]:
|
|
|
|
gPlayer.rotate(IROT)
|
2024-04-03 11:20:42 +00:00
|
|
|
if keys[pygame.K_w]:
|
2024-04-03 12:35:29 +00:00
|
|
|
gPlayer -= GyroVector(OFFSET * gPlayer.cRot, 1)
|
2024-04-03 12:20:11 +00:00
|
|
|
if keys[pygame.K_s]:
|
2024-04-03 12:35:29 +00:00
|
|
|
gPlayer += GyroVector(OFFSET * gPlayer.cRot, 1)
|
2024-04-03 11:20:42 +00:00
|
|
|
display.fill(WHITE)
|
|
|
|
#pygame.draw.rect(display,BLACK, c_tr(Poincare2Klein(gPlayer.cPos) * -100) + (100,100),0)
|
2024-04-03 11:46:22 +00:00
|
|
|
drawn = draw(level,gPlayer,PI/2,640)
|
2024-04-03 11:20:42 +00:00
|
|
|
n = 0
|
|
|
|
for i in drawn:
|
2024-04-03 11:46:22 +00:00
|
|
|
pygame.draw.rect(display,i.color, (n,360 - (cap(i.height) * 500),10,cap(i.height) * 1000))
|
|
|
|
n += 2
|
2024-04-03 11:20:42 +00:00
|
|
|
font.render_to(display, (20, 150), str(gPlayer.cPos), (0, 0, 0))
|
|
|
|
font.render_to(display, (20, 250), str(gPlayer.cRot), (0, 0, 0))
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
frameend = time_ns()
|
|
|
|
sleep((frameend-framestart) / F_)
|
|
|
|
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")
|