-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
316 lines (215 loc) · 6.98 KB
/
game.py
File metadata and controls
316 lines (215 loc) · 6.98 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
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
307
308
309
310
311
312
313
314
315
316
import pygame
import math
import random
import sys
# =====================================================
# CONFIG
# =====================================================
WIDTH, HEIGHT = 900, 600
FOV = math.pi / 3
HALF_FOV = FOV / 2
NUM_RAYS = 120
TILE = 50
# =====================================================
# WORLD STATE
# =====================================================
world = {
"room_id": 0,
"difficulty": 0.5,
}
# =====================================================
# PLAYER
# =====================================================
player = {
"x": 150,
"y": 150,
"angle": 0,
"hp": 100
}
# =====================================================
# MATERIAL DEFINITIONS
# =====================================================
BLOCKS = {
"wall_thick": True,
"wall_thin": True,
"tree_thick": True,
"tree_thin": True,
"arch_thick": False,
"arch_thin": False,
"empty": False
}
COLORS = {
"wall_thick": (60, 60, 60),
"wall_thin": (90, 90, 90),
"tree_thick": (20, 90, 20),
"tree_thin": (40, 140, 40),
"arch_thick": (160, 120, 80),
"arch_thin": (190, 150, 110),
"empty": (0, 0, 0)
}
# =====================================================
# ROOM GENERATION (RADIAL + STRUCTURE GRAMMAR)
# =====================================================
def generate_room(room_id, difficulty):
random.seed(room_id)
size = 20
grid = []
center = size // 2
radius = 6 + int(difficulty * 4)
for y in range(size):
row = []
for x in range(size):
dx = x - center
dy = y - center
dist = math.sqrt(dx*dx + dy*dy)
noise = random.random()
cell = "empty"
# -----------------------------
# OUTER WALL RING
# -----------------------------
if dist > radius + 1.5:
cell = "wall_thick"
elif dist > radius:
cell = "wall_thin" if noise < 0.6 else "wall_thick"
# -----------------------------
# INNER AREA STRUCTURE
# -----------------------------
else:
openness = 0.35 + (1.0 - difficulty) * 0.3
if noise < openness:
cell = "empty"
else:
r = random.random()
if r < 0.25:
cell = "arch_thin" if noise < 0.5 else "arch_thick"
elif r < 0.6:
cell = "tree_thin" if noise < 0.5 else "tree_thick"
else:
cell = "wall_thin"
row.append(cell)
grid.append(row)
return grid
def get_room():
return generate_room(world["room_id"], world["difficulty"])
# =====================================================
# COLLISION
# =====================================================
def is_blocking(cell):
return BLOCKS.get(cell, False)
# =====================================================
# MOVEMENT
# =====================================================
def move():
keys = pygame.key.get_pressed()
speed = 2.5
if keys[pygame.K_a]:
player["angle"] -= 0.04
if keys[pygame.K_d]:
player["angle"] += 0.04
dx = math.cos(player["angle"]) * speed * (keys[pygame.K_w] - keys[pygame.K_s])
dy = math.sin(player["angle"]) * speed * (keys[pygame.K_w] - keys[pygame.K_s])
grid = get_room()
def can_move(nx, ny):
mx = int(nx / TILE)
my = int(ny / TILE)
if my < 0 or mx < 0 or my >= len(grid) or mx >= len(grid[0]):
return False
return not is_blocking(grid[my][mx])
if can_move(player["x"] + dx, player["y"]):
player["x"] += dx
if can_move(player["x"], player["y"] + dy):
player["y"] += dy
# =====================================================
# RAYCAST (WITH MATERIAL SUPPORT)
# =====================================================
def render(screen):
grid = get_room()
ray_angle = player["angle"] - HALF_FOV
step = FOV / NUM_RAYS
for ray in range(NUM_RAYS):
sin_a = math.sin(ray_angle)
cos_a = math.cos(ray_angle)
map_x = int(player["x"] / TILE)
map_y = int(player["y"] / TILE)
delta_x = abs(1 / (cos_a + 1e-6))
delta_y = abs(1 / (sin_a + 1e-6))
step_x = -1 if cos_a < 0 else 1
step_y = -1 if sin_a < 0 else 1
side_x = delta_x
side_y = delta_y
hit_cell = "empty"
dist = 9999
for _ in range(25):
if side_x < side_y:
side_x += delta_x
map_x += step_x
side = 0
else:
side_y += delta_y
map_y += step_y
side = 1
try:
cell = grid[map_y][map_x]
# -----------------------------
# TREE = partial occlusion
# -----------------------------
if cell.startswith("tree"):
continue
# -----------------------------
# ARCH = pass-through + visual frame
# -----------------------------
if cell.startswith("arch"):
hit_cell = cell
continue
# -----------------------------
# WALL = solid stop
# -----------------------------
if is_blocking(cell):
hit_cell = cell
break
except:
break
dist = min(side_x, side_y) * TILE
dist *= math.cos(player["angle"] - ray_angle)
if dist < 1:
dist = 1
h = 5000 / dist
base = 255 / (1 + dist * 0.02)
# MATERIAL LIGHTING
color = COLORS.get(hit_cell, (100, 100, 100))
if hit_cell.startswith("tree"):
base *= 0.6
elif hit_cell.startswith("arch"):
base *= 1.2
r = min(255, color[0] * base / 255)
g = min(255, color[1] * base / 255)
b = min(255, color[2] * base / 255)
pygame.draw.rect(
screen,
(r, g, b),
(ray * (WIDTH // NUM_RAYS), HEIGHT//2 - h//2, WIDTH//NUM_RAYS, h)
)
ray_angle += step
# =====================================================
# MAIN LOOP
# =====================================================
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
running = True
while running:
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (30, 30, 50), (0, 0, WIDTH, HEIGHT//2))
pygame.draw.rect(screen, (20, 20, 20), (0, HEIGHT//2, WIDTH, HEIGHT))
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
move()
render(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()