forked from bedohswe/p3he
Compare commits
14 Commits
799165b225
...
43b2d31980
Author | SHA1 | Date | |
---|---|---|---|
43b2d31980 | |||
fcabed5a19 | |||
9ebde906c2 | |||
d54377e3d6 | |||
53ecde3e0c | |||
04f0f7329c | |||
7f1799acfe | |||
18e4dec821 | |||
bd0aea1728 | |||
9121959b25 | |||
181492cc68 | |||
1845425c16 | |||
3b2522b12b | |||
371491a607 |
40
alert.py
Normal file
40
alert.py
Normal file
@ -0,0 +1,40 @@
|
||||
import pygame
|
||||
import pygame.freetype
|
||||
import threading
|
||||
from time import sleep
|
||||
|
||||
class Alert():
|
||||
def __init__(self, text, display):
|
||||
self.text = text
|
||||
|
||||
font = pygame.freetype.Font(None, 24)
|
||||
font_fg = (255, 255, 255)
|
||||
font_bg = (255, 255, 255, 120)
|
||||
|
||||
text_image = font.render(text, font_fg)[0]
|
||||
|
||||
self.background = pygame.Surface((text_image.get_width() + 12, text_image.get_height() + 12), pygame.SRCALPHA)
|
||||
self.background.fill(font_bg)
|
||||
|
||||
self.rect = self.background.get_rect()
|
||||
text_rect = text_image.get_rect(center = self.rect.center)
|
||||
|
||||
self.background.blit(text_image, text_rect)
|
||||
|
||||
self.rect.topleft = (display.get_width() / 2 - self.background.get_width() / 2, display.get_height() - self.background.get_height() - 8)
|
||||
|
||||
def draw(self, surface):
|
||||
surface.blit(self.background, self.rect)
|
||||
|
||||
def start_hide_thread(self, alerts, delay):
|
||||
def hide_delay(delay):
|
||||
sleep(delay)
|
||||
|
||||
try:
|
||||
alerts.pop(alerts.index(self))
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
p = threading.Thread(target=hide_delay, args=(delay,))
|
||||
p.daemon = True
|
||||
p.start()
|
@ -3,6 +3,6 @@ from cmath import exp
|
||||
I = complex(0,1)
|
||||
WHITE = (255,255,255)
|
||||
BLACK = (0,0,0)
|
||||
OFFSET = 0.015625
|
||||
OFFSET = 0.0078125
|
||||
ROT = cNorm(exp(deg2rad(2)*I))
|
||||
IROT = 1/ROT
|
||||
|
17
draw.py
17
draw.py
@ -4,8 +4,8 @@ from gyro import *
|
||||
from lines import *
|
||||
|
||||
class DrawnSegment:
|
||||
def __init__(self,height,color):
|
||||
self.height = height
|
||||
def __init__(self,dist,color):
|
||||
self.dist = dist
|
||||
self.color = color
|
||||
|
||||
def draw(level,gPlayer,fov,res):
|
||||
@ -13,7 +13,7 @@ def draw(level,gPlayer,fov,res):
|
||||
irot = exp(fov/res*I)
|
||||
iprot = gPlayer.cRot
|
||||
for i in range(res):
|
||||
m = DrawnSegment(0,BLACK)
|
||||
m = None
|
||||
rot = irot**(i-(res/2)) * iprot
|
||||
for j in level:
|
||||
try: # Function "LineIntersection" faults from time to time
|
||||
@ -23,10 +23,13 @@ def draw(level,gPlayer,fov,res):
|
||||
if cDot(rot,MobiusAdd(cInt,-gPlayer.cPos)) > 0:
|
||||
continue
|
||||
rDist = MobiusDist(cInt,gPlayer.cPos)*1.1
|
||||
if not (j.isFinite and cBetween(j.pointA,j.pointB,cInt)):
|
||||
if j.isFinite and not cBetween(j.pointA,j.pointB,cInt):
|
||||
continue
|
||||
if (1 - m.height) > rDist:
|
||||
m = DrawnSegment(1-rDist,j.color)
|
||||
drawn.append(m)
|
||||
if m is None:
|
||||
m = DrawnSegment(rDist,j.color)
|
||||
continue
|
||||
if m.dist > rDist:
|
||||
m = DrawnSegment(rDist,j.color)
|
||||
drawn.append(m if m is not None else DrawnSegment(0, BLACK))
|
||||
return drawn
|
||||
|
||||
|
@ -18,8 +18,8 @@ def parse_level_from_string(string):
|
||||
|
||||
level.append(
|
||||
Segment(
|
||||
bool(raw_segment[0]),
|
||||
float(raw_segment[1]),
|
||||
raw_segment[0] == "True",
|
||||
complex(raw_segment[1]),
|
||||
complex(raw_segment[2]),
|
||||
tuple(map(int, raw_segment[3][1:len(raw_segment[3])-1].split(', ')))
|
||||
)
|
||||
@ -43,9 +43,11 @@ def save_level(level):
|
||||
level_save = save_level_to_string(level)
|
||||
|
||||
unix_timestamp = int(time())
|
||||
save = open(f"./levels/{unix_timestamp}.p3hel", "w")
|
||||
filename = f'{unix_timestamp}.p3hel'
|
||||
save = open(f"./levels/{filename}", "w")
|
||||
save.write(level_save)
|
||||
save.close()
|
||||
return filename
|
||||
|
||||
def open_level(path):
|
||||
save = open(path, "r")
|
||||
|
2
lines.py
2
lines.py
@ -13,7 +13,7 @@ def cLineIntersection(cA0, cA1, cB0, cB1):
|
||||
|
||||
@jit
|
||||
def Between(rA,rB,rN):
|
||||
return (rA < rN and rN < rB) or (rB < rN and rN < rA)
|
||||
return (rA <= rN and rN <= rB) or (rB <= rN and rN <= rA)
|
||||
|
||||
@jit
|
||||
def cBetween(cA,cB,cN):
|
||||
|
44
main.py
44
main.py
@ -3,15 +3,17 @@ 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
|
||||
from levels import Segment, open_level, parse_level_from_string
|
||||
from gyro import *
|
||||
from levels import *
|
||||
from constants import *
|
||||
from draw import draw
|
||||
from alert import Alert
|
||||
|
||||
bCap = True
|
||||
|
||||
@ -20,14 +22,6 @@ gOrigin = GyroVector(0,1)
|
||||
SKY = (127,127,255)
|
||||
GROUND = (102, 51, 0)
|
||||
|
||||
level = [
|
||||
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))
|
||||
]
|
||||
|
||||
|
||||
def renderDebugInfo(gPlayer, clock, fontSize = 18):
|
||||
font = pygame.freetype.Font(None, fontSize)
|
||||
font_fg = (255, 255, 255)
|
||||
@ -39,7 +33,9 @@ def renderDebugInfo(gPlayer, clock, fontSize = 18):
|
||||
|
||||
return fps_surf, cPos_surf, cRot_surf
|
||||
|
||||
|
||||
def nonzerocap(n):
|
||||
if n <= 0: return 1
|
||||
return n
|
||||
|
||||
def mainLoop():
|
||||
gPlayer = GyroVector(0,1)
|
||||
@ -48,6 +44,20 @@ def mainLoop():
|
||||
fontSize = 18
|
||||
debugInfo = True
|
||||
wall_buffer = []
|
||||
alerts = []
|
||||
|
||||
def alert_append(alert, delay):
|
||||
if len(alerts) >= 1:
|
||||
alerts.pop()
|
||||
|
||||
alerts.append(alert)
|
||||
alert.start_hide_thread(alerts, delay)
|
||||
|
||||
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():
|
||||
@ -69,11 +79,10 @@ def mainLoop():
|
||||
else:
|
||||
wall_buffer.append(gPlayer.cPos)
|
||||
if event.key == pygame.K_F5:
|
||||
save_level(level)
|
||||
if event.key == pygame.K_F9:
|
||||
# TODO: @bedohswe make open file dialog
|
||||
# open_level(path)
|
||||
pass
|
||||
filename = save_level(level)
|
||||
alert = Alert(f"File saved as {filename}", display)
|
||||
|
||||
alert_append(alert, 5)
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
if keys[pygame.K_d]:
|
||||
@ -95,7 +104,7 @@ def mainLoop():
|
||||
pygame.draw.rect(display,GROUND, (0,360,1280,360))
|
||||
n = 0
|
||||
for i in drawn:
|
||||
pygame.draw.rect(display,i.color, (n,int((1 - cap(i.height)) * 360),8,int(cap(i.height) * 1280)))
|
||||
pygame.draw.rect(display,i.color, (n,int((nonzerocap(i.dist)) * 360),8,int((1 -nonzerocap(i.dist)) * 1280)))
|
||||
n += 4
|
||||
gPlayer.normalize()
|
||||
if debugInfo:
|
||||
@ -112,6 +121,9 @@ def mainLoop():
|
||||
c = complex(Poincare2Klein(gPlayer.cPos))
|
||||
pygame.draw.rect(display, WHITE, (c.real * 100 + 95, (c.imag * 100 + 195), 10, 10))
|
||||
|
||||
for alert in alerts:
|
||||
alert.draw(display)
|
||||
|
||||
pygame.display.update()
|
||||
if bCap:
|
||||
clock.tick(60)
|
||||
|
1
maps/cross.p3hel
Normal file
1
maps/cross.p3hel
Normal file
@ -0,0 +1 @@
|
||||
False/1/0/(255, 0, 0 |False/1j/0/(0, 255, 0
|
1
maps/empty.p3hel
Normal file
1
maps/empty.p3hel
Normal file
@ -0,0 +1 @@
|
||||
True/0/0/(0, 0, 0
|
1
maps/squareroom.p3hel
Normal file
1
maps/squareroom.p3hel
Normal file
@ -0,0 +1 @@
|
||||
True/0.6/0.6j/(255, 0, 0)|True/0.6/-0.6j/(0, 255, 0)|True/-0.6/-0.6j/(0, 0, 255)|True/-0.6/0.6j/(255, 255, 0)
|
Loading…
Reference in New Issue
Block a user