-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathraycaster.py
163 lines (140 loc) · 5.09 KB
/
raycaster.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# raycaster-tk
# https://github.com/vesche/raycaster-tk
#
import math
from Tkinter import Tk, Canvas
m = [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1],
[1,2,1,2,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,2,0,0,0,3,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,1,0,2,0,3,0,2,0,1,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] ]
# constants
WIDTH = 1024
HEIGHT = 768
MOVSPEED = 0.2
ROTSPEED = 0.05
# trig constants
TGM = (math.cos(ROTSPEED), math.sin(ROTSPEED))
ITGM = (math.cos(-ROTSPEED), math.sin(-ROTSPEED))
COS, SIN = 0, -1
# init vars
positionX = 3.0
positionY = 7.0
directionX = 1.0
directionY = 0.0
planeX = 0.0
planeY = 0.66
# init tkinter
root = Tk()
root.title("raycaster-tk")
canvas = Canvas(root, width=WIDTH, height=HEIGHT)
canvas.pack()
def leftKey(event):
global directionX
global directionY
global planeX
global planeY
directionY = directionX * ITGM[SIN] + directionY * ITGM[COS]
directionX = directionX * ITGM[COS] - directionY * ITGM[SIN]
planeY = planeX * ITGM[SIN] + planeY * ITGM[COS]
planeX = planeX * ITGM[COS] - planeY * ITGM[SIN]
def rightKey(event):
global directionX
global directionY
global planeX
global planeY
directionY = directionX * TGM[SIN] + directionY * TGM[COS]
directionX = directionX * TGM[COS] - directionY * TGM[SIN]
planeY = planeX * TGM[SIN] + planeY * TGM[COS]
planeX = planeX * TGM[COS] - planeY * TGM[SIN]
def upKey(event):
global positionX
global positionY
if not m[int(positionX + directionX * MOVSPEED)][int(positionY)]:
positionX += directionX * MOVSPEED
if not m[int(positionX)][int(positionY + directionY * MOVSPEED)]:
positionY += directionY * MOVSPEED
def downKey(event):
global positionX
global positionY
if not m[int(positionX - directionX * MOVSPEED)][int(positionY)]:
positionX -= directionX * MOVSPEED
if not m[int(positionX)][int(positionY - directionY * MOVSPEED)]:
positionY -= directionY * MOVSPEED
root.bind('<Left>', leftKey)
root.bind('<Right>', rightKey)
root.bind('<Up>', upKey)
root.bind('<Down>', downKey)
while True:
canvas.delete('all')
for x in range(WIDTH):
mapX = int(positionX)
mapY = int(positionY)
cameraX = 2.0 * x / WIDTH - 1.0
rayDirX = directionX + planeX * cameraX
rayDirY = directionY + planeY * cameraX + .0000001 # avoid death by ZDE
deltaDistX = math.sqrt(1.0 + (rayDirY * rayDirY) / ((rayDirX * rayDirX)))
deltaDistY = math.sqrt(1.0 + (rayDirX * rayDirX) / ((rayDirY * rayDirY)))
if rayDirX < 0:
stepX = -1
sideDistX = (positionX - mapX) * deltaDistX
else:
stepX = 1
sideDistX = (mapX + 1.0 - positionX) * deltaDistX
if rayDirY < 0:
stepY = -1
sideDistY = (positionY - mapY) * deltaDistY
else:
stepY = 1
sideDistY = (mapY + 1.0 - positionY) * deltaDistY
hit = False
while not hit:
if sideDistX < sideDistY:
sideDistX += deltaDistX
mapX += stepX
side = False
else:
sideDistY += deltaDistY
mapY += stepY
side = True
if m[mapX][mapY] > 0:
hit = True
if not side:
perpWallDist = abs((mapX - positionX + (1.0 - stepX) / 2.0) / rayDirX)
else:
perpWallDist = abs((mapY - positionY + (1.0 - stepY) / 2.0) / rayDirY)
lineHeight = abs(int(HEIGHT / (perpWallDist + .0000001))) # avoid death by ZDE again
drawStart = -lineHeight / 2.0 + HEIGHT / 2.0
drawEnd = lineHeight / 2.0 + HEIGHT / 2.0
if drawStart < 0:
drawStart = 0
if drawEnd >= HEIGHT:
drawEnd = HEIGHT - 1
# colors
wallcolors = [[], [150,0,0], [0,150,0], [0,0,150]]
color = wallcolors[m[mapX][mapY]]
if side:
for k, v in enumerate(color):
color[k] = int(v / 1.2)
hex_color = '#%02x%02x%02x' % (color[0], color[1], color[2])
canvas.create_line(x, drawStart, x, drawEnd, fill=hex_color)
# update screen
root.update()