-
Notifications
You must be signed in to change notification settings - Fork 0
/
fox3d.py
306 lines (270 loc) · 8.53 KB
/
fox3d.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
######################
# Foxenstein Raycaster
# Spiralwise - 2016
#
# This raycaster is inspired by http://lodev.org/cgtutor/raycasting.html
#
# TODO : Find a vector library for a more efficient vector manipulation
######################
import sys
import pygame
from pygame.locals import *
from math import *
from time import perf_counter
DUMMY_MAP = [
[1, 1, 2, 1, 1, 1],
[1, 3, 0, 0, 5, 1],
[1, 3, 0, 0, 5, 1],
[1, 0, 0, 0, 0, 1],
[1, 0, 2, 0, 2, 1],
[1, 1, 1, 6, 1, 1]
]
BLOCK_SIZE = 2
SCREEN_DISTANCE = 2
SCREEN_SIZE = (640, 480)
RESOLUTION = 480
FOCAL_LENGTH = 0.3
MAP_POS = [5, 5]
MAP_SIZE = 10
COLOR = {
'BLACK': (0, 0, 0),
'WHITE': (255, 255, 255),
'YELLOW': (196, 196, 0),
'RED': (196, 32, 32),
'BLUE': (32, 32, 196),
'CEIL': (0, 127, 127),
'FLOOR': (96, 96, 127),
}
WALL_HEIGHT = SCREEN_SIZE[1]/2
WALL_HEIGHT_DEMI = WALL_HEIGHT/2
HORIZONT_LINE = SCREEN_SIZE[1]/2
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("Foxenstein 3D")
print("==== Foxcaster Demo alpha ====")
Player = {
'pos': [BLOCK_SIZE*3, BLOCK_SIZE*3],
'dir': [0.0, -1.0],
'speed': 1.4,
'rot_speed': 1.2,
'move_state': 0,
'turn_state': 0
}
Camera = {
'dir': [1.0, 0.0],
'fisheye': False
}
Font = pygame.font.SysFont("monospace", 10)
print("Loading textures...")
TEXTURES = pygame.image.load("wolftextures.png")
TEXTURE_SIZE = 64
if TEXTURES.get_width() % TEXTURE_SIZE or TEXTURES.get_height() % TEXTURE_SIZE:
print("Textures loading error.")
sys.exit(-1)
Textures = [[], []]
num_textures = int(TEXTURES.get_width() / TEXTURE_SIZE)
for t in range(num_textures):
texture_buffer = pygame.Surface((64, 64), pygame.HWSURFACE)
texture_buffer.blit(TEXTURES,
(0, 0),
(t*TEXTURE_SIZE, 0, 64, 64))
Textures[0].append(texture_buffer)
texture_buffer = pygame.Surface((64, 64), pygame.HWSURFACE)
texture_buffer.blit(TEXTURES,
(0, 0),
(t*TEXTURE_SIZE, 0, 64, 64))
for y in range(texture_buffer.get_height()):
for x in range(texture_buffer.get_width()):
texture_buffer.set_at((x, y),
texture_buffer.get_at((x, y)) \
// pygame.Color(2, 2, 2))
Textures[1].append(texture_buffer)
print(len(Textures[0]), "textures loaded.")
def draw_minimap(distance):
for y, row in enumerate(DUMMY_MAP):
for x, square in enumerate(row):
if square:
pygame.draw.rect(screen
, (200, 0, 0)
, (MAP_POS[0]+MAP_SIZE*x
, MAP_POS[1]+MAP_SIZE*y
, MAP_SIZE, MAP_SIZE))
pos_on_map_x = int(MAP_POS[0]+MAP_SIZE*(Player['pos'][0]/BLOCK_SIZE))
pos_on_map_y = int(MAP_POS[1]+MAP_SIZE*(Player['pos'][1]/BLOCK_SIZE))
pygame.draw.circle(screen
, (0, 0, 200)
, (pos_on_map_x, pos_on_map_y)
, 3)
pygame.draw.line(screen
, (0, 0, 200)
, (pos_on_map_x, pos_on_map_y)
, (pos_on_map_x+Player['dir'][0]*6, pos_on_map_y+Player['dir'][1]*6))
text = Font.render(str(Player["pos"])
+ ", "
+ str(Player["dir"])
+ ", "
+ str(distance), 1, (25, 25, 50))
screen.blit(text, (10, SCREEN_SIZE[0]-200))
def display_framerate(framerate):
text = Font.render(str(framerate), 1, (28, 28, 50))
screen.blit(text, (10, 10))
def rotate_vector(vec, angle):
rot_mtx = [
[cos(angle), -sin(angle)],
[sin(angle), cos(angle)]
]
vx = rot_mtx[0][0] * vec[0] + rot_mtx[0][1] * vec[1]
vy = rot_mtx[1][0] * vec[0] + rot_mtx[1][1] * vec[1]
return [vx, vy]
def hit_wall(x):
'''Cast a ray to the nearest wall and returns the length
input x: x-screen pixel
output:
- length of the ray in units
- which side has been hit
- wall index
- wall impact position in ratio
'''
hit = False
blockHit = [int(Player['pos'][0]/BLOCK_SIZE),
int(Player['pos'][1]/BLOCK_SIZE)]
rayDir = [0.0, 0.0]
cameraX = 2 * x / (SCREEN_SIZE[0]-1) -1
rayDir[0] = Player['dir'][0] + Camera['dir'][0] * cameraX
rayDir[1] = Player['dir'][1] + Camera['dir'][1] * cameraX
delta = [0.0, 0.0]
if rayDir[0] == 0.0:
delta[0] = inf
else:
rayRatio = (rayDir[1] * rayDir[1]) / (rayDir[0] * rayDir[0])
delta[0] = sqrt(1 + rayRatio) * BLOCK_SIZE
if rayDir[1] == 0.0:
delta[1] = inf
else:
rayRatio = (rayDir[0] * rayDir[0]) / (rayDir[1] * rayDir[1])
delta[1] = sqrt(1 + rayRatio) * BLOCK_SIZE
step = [0, 0]
sideHitDist = [0.0, 0.0]
if rayDir[0] > 0:
sideHitDist[0] = blockHit[0]+1 - Player['pos'][0]/BLOCK_SIZE
sideHitDist[0] *= delta[0]
step[0] = 1
else:
sideHitDist[0] = Player['pos'][0]/BLOCK_SIZE - blockHit[0]
sideHitDist[0] *= delta[0]
step[0] = -1
if rayDir[1] > 0:
sideHitDist[1] = blockHit[1]+1 - Player['pos'][1]/BLOCK_SIZE
sideHitDist[1] *= delta[1]
step[1] = 1
else:
sideHitDist[1] = Player['pos'][1]/BLOCK_SIZE - blockHit[1]
sideHitDist[1] *= delta[1]
step[1] = -1
while not hit:
if sideHitDist[0] < sideHitDist[1]:
distance = sideHitDist[0]
sideHitDist[0] += delta[0]
blockHit[0] += step[0]
side = 0
else:
distance = sideHitDist[1]
sideHitDist[1] += delta[1]
blockHit[1] += step[1]
side = 1
if (DUMMY_MAP[blockHit[1]][blockHit[0]] > 0):
hit = True
distanceXY = (blockHit[side]-((step[side]-1)/2))*BLOCK_SIZE \
- Player['pos'][side]
otherSide = 0 if side else 1
wallHit = sqrt(distance*distance - distanceXY*distanceXY) * step[otherSide]
wallHit += Player['pos'][otherSide] - blockHit[otherSide]*BLOCK_SIZE
wallHit /= BLOCK_SIZE
if rayDir[0] < 0 and side == 0 or rayDir[1] > 0 and side == 1:
wallHit = 1 - wallHit
if not Camera['fisheye']:
rayDist = sqrt(rayDir[0]*rayDir[0] + rayDir[1]*rayDir[1])
distPlaneToHit = distance
playerDist = sqrt(Player['dir'][0]*Player['dir'][0] \
+ Player['dir'][1]*Player['dir'][1])
distance = playerDist * distPlaneToHit / rayDist
return distance, side, DUMMY_MAP[blockHit[1]][blockHit[0]], wallHit
#FIXME Make it RayHit object
def draw_wall(x, rayhit):
'''Draw a wall according to the distance to the player.
If the wall has a texture, it will be textured.
input x: vertical strip on screen
input rayhit
'''
perceivedHeight = int(WALL_HEIGHT/rayhit[0])
if rayhit[2] > 1:
txt_index = rayhit[2] - 2
texX = int(rayhit[3] * TEXTURE_SIZE)
screen.blit(
pygame.transform.scale(Textures[rayhit[1]][txt_index],
(TEXTURE_SIZE, perceivedHeight*2)),
(x, -perceivedHeight+SCREEN_SIZE[1]/2),
(texX, 0, 1, perceivedHeight*2))
else:
color = COLOR['YELLOW']
if rayhit[1]:
color = list(map(lambda x: x/2, color)) #NOTE Maybe use bitshifting?
pygame.draw.line(screen,
color,
(x, (perceivedHeight + SCREEN_SIZE[1]/2)),
(x, (-perceivedHeight + SCREEN_SIZE[1]/2)))
def process_player(delta_time):
if Player['move_state'] == 1:
Player['pos'][0] += Player['dir'][0] * Player['speed']*delta_time
Player['pos'][1] += Player['dir'][1] * Player['speed']*delta_time
elif Player['move_state'] == -1:
Player['pos'][0] -= Player['dir'][0] * Player['speed']*delta_time
Player['pos'][1] -= Player['dir'][1] * Player['speed']*delta_time
if Player['turn_state'] == 1:
Player['dir'] = rotate_vector(Player['dir'], -Player['rot_speed']*delta_time)
Camera['dir'] = rotate_vector(Camera['dir'], -Player['rot_speed']*delta_time)
elif Player['turn_state'] == -1:
Player['dir'] = rotate_vector(Player['dir'], Player['rot_speed']*delta_time)
Camera['dir'] = rotate_vector(Camera['dir'], Player['rot_speed']*delta_time)
# ---- MAIN
cur_time = perf_counter()
running = True
while running:
# ---- Draw
screen.fill(COLOR['BLACK'])
pygame.draw.rect(screen, COLOR['CEIL'], (1, 0, SCREEN_SIZE[0], SCREEN_SIZE[1]/2))
pygame.draw.rect(screen, COLOR['FLOOR'], (0, SCREEN_SIZE[1]/2, SCREEN_SIZE[0], SCREEN_SIZE[1]/2))
for col in range(SCREEN_SIZE[0]):
draw_wall(col, hit_wall(col))
last_time = cur_time
cur_time = perf_counter()
frame_duration = cur_time - last_time
framerate = int(1.0/frame_duration)
display_framerate(framerate)
#draw_minimap(hit_wall(SCREEN_SIZE[0]/2))
pygame.display.flip()
# ---- Input
for event in pygame.event.get():
if event.type == QUIT:
break
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.key == K_w:
Player['move_state'] = 1
elif event.key == K_s:
Player['move_state'] = -1
elif event.key == K_a:
Player['turn_state'] = 1
elif event.key == K_d:
Player['turn_state'] = -1
elif event.key == K_f:
Camera['fisheye'] = not Camera['fisheye']
elif event.type == KEYUP:
if event.key == K_w or event.key == K_s:
Player['move_state'] = 0
elif event.key == K_a or event.key == K_d:
Player['turn_state'] = 0
process_player(frame_duration)