forked from ToanPhamPhuc/Minecraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminecraft.py
More file actions
199 lines (167 loc) · 6.21 KB
/
minecraft.py
File metadata and controls
199 lines (167 loc) · 6.21 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
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
import sys
import os
import math
import msvcrt # Windows-specific keyboard input module
Y_PIXELS = 180
X_PIXELS = 900
Z_BLOCKS = 10
Y_BLOCKS = 20
X_BLOCKS = 20
EYE_HEIGHT = 1.5
VIEW_HEIGHT = 0.7
VIEW_WIDTH = 1.0
BLOCK_BORDER_SIZE = 0.05
keystate = [0] * 256
def process_input():
global keystate
keystate = [0] * 256
while msvcrt.kbhit():
key = msvcrt.getch()
if len(key) == 1:
keystate[ord(key.decode('utf-8'))] = 1
def is_key_pressed(key):
return keystate[ord(key)]
def init_picture():
return [[[' ' for _ in range(X_BLOCKS)] for _ in range(Y_BLOCKS)] for _ in range(Z_BLOCKS)]
class Vector:
def __init__(self, x=0, y=0, z=0):
self.x, self.y, self.z = x, y, z
class Vector2:
def __init__(self, psi=0, phi=0):
self.psi, self.phi = psi, phi
class Player:
def __init__(self):
self.pos = Vector(5, 5, 4 + EYE_HEIGHT)
self.view = Vector2(0, 0)
def angles_to_vect(angles):
x = math.cos(angles.psi) * math.cos(angles.phi)
y = math.cos(angles.psi) * math.sin(angles.phi)
z = math.sin(angles.psi)
return Vector(x, y, z)
def vect_add(v1, v2):
return Vector(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z)
def vect_sub(v1, v2):
return Vector(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z)
def vect_scale(s, v):
return Vector(s * v.x, s * v.y, s * v.z)
def vect_normalize(v):
length = math.sqrt(v.x ** 2 + v.y ** 2 + v.z ** 2)
return Vector(v.x/length, v.y/length, v.z/length)
def init_directions(view):
view_down = Vector2(view.psi - VIEW_HEIGHT / 2.0, view.phi)
screen_down = angles_to_vect(view_down)
view_up = Vector2(view.psi + VIEW_HEIGHT / 2.0, view.phi)
screen_up = angles_to_vect(view_up)
view_left = Vector2(view.psi, view.phi - VIEW_WIDTH / 2.0)
screen_left = angles_to_vect(view_left)
view_right = Vector2(view.psi, view.phi + VIEW_WIDTH / 2.0)
screen_right = angles_to_vect(view_right)
screen_mid_vert = vect_scale(0.5, vect_add(screen_up, screen_down))
screen_mid_hor = vect_scale(0.5, vect_add(screen_left, screen_right))
mid_to_left = vect_sub(screen_left, screen_mid_hor)
mid_to_up = vect_sub(screen_up, screen_mid_vert)
dir_grid = [[None for _ in range(X_PIXELS)] for _ in range(Y_PIXELS)]
for y_pix in range(Y_PIXELS):
for x_pix in range(X_PIXELS):
tmp = vect_add(screen_mid_hor, mid_to_left)
tmp = vect_add(tmp, mid_to_up)
tmp = vect_sub(tmp, vect_scale((x_pix / (X_PIXELS - 1)) * 2, mid_to_left))
tmp = vect_sub(tmp, vect_scale((y_pix / (Y_PIXELS - 1)) * 2, mid_to_up))
dir_grid[y_pix][x_pix] = vect_normalize(tmp)
return dir_grid
def ray_outside(pos):
return not (0 <= pos.x < X_BLOCKS and 0 <= pos.y < Y_BLOCKS and 0 <= pos.z < Z_BLOCKS)
def on_block_border(pos):
cnt = 0
if abs(pos.x - round(pos.x)) < BLOCK_BORDER_SIZE: cnt += 1
if abs(pos.y - round(pos.y)) < BLOCK_BORDER_SIZE: cnt += 1
if abs(pos.z - round(pos.z)) < BLOCK_BORDER_SIZE: cnt += 1
return cnt >= 2
def raytrace(pos, dir, blocks):
eps = 0.01
while not ray_outside(pos):
block = blocks[int(pos.z)][int(pos.y)][int(pos.x)]
if block != ' ':
return '-' if on_block_border(pos) else block
dist = 2
if dir.x > eps:
dist = min(dist, ((int(pos.x) + 1 - pos.x) / dir.x))
elif dir.x < -eps:
dist = min(dist, ((int(pos.x) - pos.x) / dir.x))
if dir.y > eps:
dist = min(dist, ((int(pos.y) + 1 - pos.y) / dir.y))
elif dir.y < -eps:
dist = min(dist, ((int(pos.y) - pos.y) / dir.y))
if dir.z > eps:
dist = min(dist, ((int(pos.z) + 1 - pos.z) / dir.z))
elif dir.z < -eps:
dist = min(dist, ((int(pos.z) - pos.z) / dir.z))
pos = vect_add(pos, vect_scale(dist + eps, dir))
return ' '
def get_picture(picture, posview, blocks):
directions = init_directions(posview.view)
for y in range(Y_PIXELS):
for x in range(X_PIXELS):
picture[y][x] = raytrace(posview.pos, directions[y][x], blocks)
def draw_ascii(picture):
sys.stdout.flush()
os.system("cls") # Windows clear screen
for row in picture:
current_color = 0
for char in row:
if char == 'o' and current_color != 32:
print("\033[32m", end="")
current_color = 32
elif char != '0' and current_color != 0:
print("\033[0m", end="")
current_color = 0
print(char, end="")
print("\033[0m")
def update_pos_view(posview, blocks):
tilt_eps = 0.1
x, y = int(posview.pos.x), int(posview.pos.y)
z = int(posview.pos.z - EYE_HEIGHT + 0.01)
# if blocks[z][y][x] != ' ':
# posview.pos.z += 1
# z = int(posview.pos.z - EYE_HEIGHT - 0.01)
# if blocks[z][y][x] == ' ':
# posview.pos.z -= 1
# Check bounds before accessing blocks
if 0 <= z < Z_BLOCKS and 0 <= y < Y_BLOCKS and 0 <= x < X_BLOCKS:
if blocks[z][y][x] != ' ':
posview.pos.z += 1
z = int(posview.pos.z - EYE_HEIGHT - 0.01)
if 0 <= z < Z_BLOCKS and 0 <= y < Y_BLOCKS and 0 <= x < X_BLOCKS:
if blocks[z][y][x] == ' ':
posview.pos.z -= 1
# Camera rotation
if is_key_pressed('w'):
posview.view.psi += tilt_eps
if is_key_pressed('s'):
posview.view.psi -= tilt_eps
if is_key_pressed('a'):
posview.view.phi -= tilt_eps
if is_key_pressed('d'):
posview.view.phi += tilt_eps
if __name__ == "__main__":
import time
picture = [[' ' for _ in range(X_PIXELS)] for _ in range(Y_PIXELS)]
blocks = init_picture()
# Add some blocks to the world so something is visible
for z in range(3):
for y in range(8, 12):
for x in range(8, 12):
blocks[z][y][x] = 'o' # green block
for x in range(X_BLOCKS):
for y in range(Y_BLOCKS):
blocks[0][y][x] = '0' # ground layer
player = Player()
try:
while True:
process_input()
update_pos_view(player, blocks)
get_picture(picture, player, blocks)
draw_ascii(picture)
time.sleep(0.05)
except KeyboardInterrupt:
print("Exiting...")