p3he/main.py

176 lines
5.3 KiB
Python
Raw Normal View History

2024-04-03 11:20:42 +00:00
#!/usr/bin/env python3
2024-04-03 14:15:42 +00:00
try:
from numpy import complex256
except ImportError:
from numpy import complex128 as complex256
2024-04-03 14:15:42 +00:00
print("Warning! Cannot use full precision!")
2024-04-03 11:20:42 +00:00
from time import time_ns, sleep
2024-04-03 12:56:02 +00:00
from cmath import exp, pi
2024-04-03 11:20:42 +00:00
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 13:06:28 +00:00
I = complex256(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 12:56:02 +00:00
PI = pi
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
2024-04-04 16:05:20 +00:00
gOrigin = GyroVector(complex256(0),1)
2024-04-03 12:47:27 +00:00
SKY = (127,127,255)
GROUND = (102, 51, 0)
2024-04-03 11:20:42 +00:00
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 13:06:28 +00:00
Segment(True,.6,I*.6,(255,0,0)),
Segment(True,.6,-I*.6,(0,255,0)),
Segment(True,-.6,-I*.6,(0,0,255)),
Segment(True,-.6,I*.6,(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:
2024-04-04 16:05:20 +00:00
#tA = MobiusAdd(j.pointA,-gPlayer.cPos)
#tB = MobiusAdd(j.pointB,-gPlayer.cPos)
try: # Function "LineIntersection" faults from time to time
cInt = MobiusInt(j.pointA,j.pointB,gPlayer.cPos,rot)
2024-04-03 11:46:22 +00:00
except ZeroDivisionError:
continue
2024-04-04 16:05:20 +00:00
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
2024-04-03 13:06:28 +00:00
continue
2024-04-04 16:05:20 +00:00
rDist = MobiusDist(cInt,gPlayer.cPos)
if not (j.isFinite and cBetween(j.pointA,j.pointB,cInt)):
2024-04-03 11:20:42 +00:00
continue
if (1 - m.height) > rDist:
m = DrawnSegment(1-rDist,j.color)
drawn.append(m)
return drawn
2024-04-03 11:20:42 +00:00
def cap(rN):
if rN < 0:
return 0
return rN
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(gPlayer.cRot), font_fg, font_bg)[0]
return fps_surf, cPos_surf, cRot_surf
def make_wall(wall_buffer):
if (len(wall_buffer) != 2):
print('Warning: wall buffer not contains 2 points.')
return
level.append(
Segment(True, wall_buffer[0], wall_buffer[1], (23,74,183))
)
2024-04-03 11:20:42 +00:00
def mainLoop():
gPlayer = GyroVector(0,1)
display = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
fontSize = 18
debugInfo = True
wall_buffer = []
2024-04-03 11:20:42 +00:00
while True:
#framestart = time_ns()
2024-04-03 11:20:42 +00:00
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 14:22:44 +00:00
if event.key == pygame.K_F3:
debugInfo = not debugInfo
if event.key == pygame.K_F2:
if (len(wall_buffer) == 1):
wall_buffer.append(gPlayer.cPos)
make_wall(wall_buffer)
wall_buffer.clear()
else:
wall_buffer.append(gPlayer.cPos)
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)
drawn = draw(level,gPlayer,PI/2,320)
2024-04-03 12:47:27 +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-04 16:05:20 +00:00
pygame.draw.rect(display,i.color, (n,int((1 - cap(i.height)) * 360),8,int(cap(i.height) * 1280)))
n += 4
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-03 14:20:04 +00:00
for i in level:
a = complex(Poincare2Klein(MobiusAdd(i.pointA,-gPlayer.cPos)))
b = complex(Poincare2Klein(MobiusAdd(i.pointB,-gPlayer.cPos)))
2024-04-03 14:22:05 +00:00
pygame.draw.line(display, WHITE, (a.real * 100 + 100, (a.imag * 100 + 200)), (b.real * 100 + 100, (b.imag * 100 + 200)))
2024-04-03 11:20:42 +00:00
pygame.display.update()
#frameend = time_ns()
clock.tick()
#sleep((frameend-framestart) / F_)
return True
2024-04-03 11:20:42 +00:00
def main():
pygame.init()
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")