-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpycasting2.0.py
More file actions
168 lines (147 loc) · 6.48 KB
/
pycasting2.0.py
File metadata and controls
168 lines (147 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import numpy as np
from matplotlib import pyplot as plt
from pynput import keyboard, mouse
from time import time
def main():
size = 15
global key; key = None # register keypresses
listener = keyboard.Listener(on_press=on_press);listener.start()
last_mouse = [0,0]
posx, posy, rot = (1, np.random.randint(1, size -1), 1) # player pos
bg = np.linspace(0, 1, 150) #background gradient
mapc, maph, mapr, ex, ey = maze_generator(posx, posy, size)# map, exit
plt.figure(num = 'Pycaster 2.0')
while True: #main game loop
start = time()
rot, last_mouse = rotation(rot, last_mouse)
plt.hlines(-0.5, 0, 60, colors='k', lw=165, alpha=np.sin((rot+np.pi/2)/2)**2/2)
plt.hlines(0.5, 0, 60, colors='k', lw=165, alpha=np.sin((rot-np.pi/2)/2)**2/2)
plt.scatter([30]*150, -bg, c=-bg, s=200000, marker='_', cmap='Greys')
plt.scatter([30]*150, bg, c=bg, s=200000, marker='_', cmap='Blues')
tx, ty, tc = ([], [], [])
for i in range(60): #vision loop
rot_i = rot + np.deg2rad(i - 30)
x, y = (posx, posy)
sin, cos = (0.04*np.sin(rot_i), 0.04*np.cos(rot_i))
n, half = 0, None
c, h, x, y, n, half, tx, ty, tc = caster(x, y, i, ex, ey, maph, mapc, sin, cos, n, half, tx, ty, tc)
if mapr[int(x)][int(y)] == 1:
c, h, x, y, n, half2, tx, ty, tc = reflection(x, y, i, ex, ey, maph, mapc, sin, cos, n, c, h, half, tx, ty, tc)
else:
plt.vlines(i, -h, h, lw = 8, colors = c)
if half != None:
plt.vlines(i, -half[0], 0, lw = 8, colors = half[1])
plt.axis('off'); plt.tight_layout(); plt.axis([0, 60, -1, 1])
plt.scatter(tx, ty, c=tc, zorder = 2, alpha=0.5, marker='s') # draw ts on the floor
plt.text(57, 0.9, str(round(1/(time()-start),1)), c='y')
plt.draw();plt.pause(0.1); plt.clf()
# player's movement
posx, posy, rot, keyout = movement(posx, posy, rot, maph)
if (int(posx) == ex and int(posy) == ey) or keyout == 'esc':
break
plt.close()
def maze_generator(x, y, size):
mapc = np.random.uniform(0,1, (size,size,3))
mapr = np.random.choice([0, 0, 0, 0, 1], (size,size))
maph = np.random.choice([0, 0, 0, 0, .5, 1], (size,size))
maph[0,:], maph[size-1,:], maph[:,0], maph[:,size-1] = (1,1,1,1)
mapc[x][y], maph[x][y], mapr[x][y] = (0, 0, 0)
count = 0
while 1:
testx, testy = (x, y)
if np.random.uniform() > 0.5:
testx = testx + np.random.choice([-1, 1])
else:
testy = testy + np.random.choice([-1, 1])
if testx > 0 and testx < size -1 and testy > 0 and testy < size -1:
if maph[testx][testy] == 0 or count > 5:
count = 0
x, y = (testx, testy)
mapc[x][y], maph[x][y], mapr[x][y] = (0, 0, 0)
if x == size-2:
ex, ey = (x, y)
break
else:
count = count+1
return np.asarray(mapc), np.asarray(maph), np.asarray(mapr), ex, ey
def rotation(rot, last_mouse): # for 1080p screen
with mouse.Controller() as check:
position = check.position
if position[0] != last_mouse[0] or position[0]>1860 or position[0] < 60:
delta = last_mouse[0] - position[0]
if position[0]>1860:
delta = 1860 - position[0]
if position[0] < 60:
delta = 60 - position[0]
rot = rot + 4*np.pi*(0.5-delta/1920)
return(rot, position)
def on_press(key_new):
global key
key = key_new
def movement(posx, posy, rot, maph):
global key
x, y = (posx, posy)
keyout = None
if key is not None:
if key == keyboard.Key.up:
x, y = (x + 0.3*np.cos(rot), y + 0.3*np.sin(rot))
elif key == keyboard.Key.down:
x, y = (x - 0.3*np.cos(rot), y - 0.3*np.sin(rot))
elif key == keyboard.Key.left:
rot = rot - np.pi/8
elif key == keyboard.Key.right:
rot = rot + np.pi/8
elif key == keyboard.Key.esc:
keyout = 'esc'
key = None
if maph[int(x)][int(y)] == 0:
posx, posy = (x, y)
return posx, posy, rot, keyout
def caster(x, y, i, ex, ey, maph, mapc, sin, cos, n, half, tx, ty, tc):
while True: # ray loop
xx, yy = (x, y)
x, y = (x + cos, y + sin)
n = n+1
if half == None and int(x*2)%2 == int(y*2)%2:#(abs(int(3*xx)-int(3*x)) > 0 or abs(int(3*yy)-int(3*y))>0):
tx.append(i)
ty.append(-1/(0.04 * n*np.cos(np.deg2rad(i - 30))))
if int(x) == ex and int(y) == ey:
tc.append('b')
else:
tc.append('k')
if maph[int(x)][int(y)] == 1 or (maph[int(x)][int(y)] == 0.5 and half == None):
h , c = shader(n, maph, mapc, sin, cos, x, y, i)
if maph[int(x)][int(y)] == 0.5 and half == None:
half = [h, c, n]
else:
break
return(c, h, x, y, n, half, tx, ty, tc)
def shader(n, maph, mapc, sin, cos, x, y, i):
h = np.clip(1/(0.04 * n*np.cos(np.deg2rad(i-30))), 0, 1)
c = np.asarray(mapc[int(x)][int(y)])*(0.4 + 0.6 * h)
if maph[int(x+cos)][int(y-sin)] != 0:
c = 0.85*c
if maph[int(x-cos)][int(y+sin)] != 0 and sin >0:
c = 0.7*c
return h, c
def reflection(x, y, i, ex, ey, maph, mapc, sin, cos, n, c, h, half, tx, ty, tc):
if half != None:
plt.vlines(i, 0, h, lw = 8, colors = c, alpha=0.5) #top reflected
plt.vlines(i, -half[0], 0, lw = 8, colors = half[1])# bottom regular
else:
plt.vlines(i, -h, h, lw = 8, colors = c, alpha=0.5) # draw vertical lines
if maph[int(x+cos)][int(y-sin)] != 0:
cos = -cos
else:
sin = -sin
c2, h, x, y, n, half2, tx, ty, tc = caster(x, y, i, ex, ey, maph, mapc, sin, cos, n, half, tx, ty, tc)
c = (c + c2)/2
if half != None:
plt.vlines(i, 0, h, lw = 8, colors = c) # draw vertical lines
else:
plt.vlines(i, -h, h, lw = 8, colors = c) # draw vertical lines
if half2 != None:
plt.vlines(i, -half2[0], 0, lw = 8, colors = half2[1])
return c, h, x, y, n, half2, tx, ty, tc
if __name__ == '__main__':
main()