Spatialworld's picture
Add files using upload-large-folder tool
8999c1d verified
raw
history blame
3.8 kB
import random
STATIC_LEVELS = [{'name': '入门 - 一字型 (2块)', 'blocks': [(2, 2, 0), (3, 2, 0)]}, {'name': '简单 - 椅子 (4块)', 'blocks': [(2, 2, 0), (2, 3, 0), (2, 2, 1), (2, 2, 2)]}, {'name': '中等 - 拱门 (9块)', 'blocks': [(1, 2, 0), (1, 2, 1), (1, 2, 2), (3, 2, 0), (3, 2, 1), (3, 2, 2), (1, 2, 3), (2, 2, 3), (3, 2, 3)]}, {'name': '困难 - 螺旋梯 (10块)', 'blocks': [(2, 2, 0), (2, 3, 0), (3, 3, 0), (3, 2, 0), (3, 1, 1), (2, 1, 1), (1, 1, 1), (1, 2, 1), (1, 3, 2), (2, 3, 2)]}, {'name': '进阶 - 实心立方体 (8块)', 'blocks': [(2, 2, 0), (3, 2, 0), (2, 3, 0), (3, 3, 0), (2, 2, 1), (3, 2, 1), (2, 3, 1), (3, 3, 1)]}, {'name': '进阶 - 金字塔 (15块)', 'blocks': [(1, 1, 0), (2, 1, 0), (3, 1, 0), (1, 2, 0), (2, 2, 0), (3, 2, 0), (1, 3, 0), (2, 3, 0), (3, 3, 0), (2, 1, 1), (1, 2, 1), (2, 2, 1), (3, 2, 1), (2, 3, 1), (2, 2, 2)]}, {'name': '挑战 - 字母 H (10块)', 'blocks': [(1, 2, 0), (1, 2, 1), (1, 2, 2), (1, 2, 3), (3, 2, 0), (3, 2, 1), (3, 2, 2), (3, 2, 3), (2, 2, 1), (2, 2, 2)]}, {'name': '挑战 - 枯井 (16块)', 'blocks': [(1, 1, 0), (2, 1, 0), (3, 1, 0), (1, 3, 0), (2, 3, 0), (3, 3, 0), (1, 2, 0), (3, 2, 0), (1, 1, 1), (2, 1, 1), (3, 1, 1), (1, 3, 1), (2, 3, 1), (3, 3, 1), (1, 2, 1), (3, 2, 1)]}, {'name': '专家 - 浮空十字 (8块)', 'blocks': [(2, 2, 0), (2, 2, 1), (2, 2, 2), (2, 2, 3), (1, 2, 2), (3, 2, 2), (2, 1, 2), (2, 3, 2)]}]
def generate_random_level_data(num_blocks, grid_size=6):
"""Helpers."""
blocks = set()
start_pos = (random.randint(1, grid_size - 2), random.randint(1, grid_size - 2), 0)
blocks.add(start_pos)
attempts = 0
while len(blocks) < num_blocks and attempts < 1000:
attempts += 1
base = random.choice(list(blocks))
bx, by, bz = base
directions = [(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)]
dx, dy, dz = random.choice(directions)
nx, ny, nz = (bx + dx, by + dy, bz + dz)
if 0 <= nx < grid_size and 0 <= ny < grid_size and (0 <= nz < grid_size):
blocks.add((nx, ny, nz))
return sorted(list(blocks))
def export_to_file(filename='level_data.py'):
all_levels = []
all_levels.extend(STATIC_LEVELS)
random_counts = [1, 1, 1, 3, 3, 3, 4, 4, 4, 5, 5]
for count in random_counts:
blocks = generate_random_level_data(count)
all_levels.append({'name': f'随机生成 - 未知结构 ({count}块)', 'blocks': blocks})
all_levels.sort(key=lambda x: len(x['blocks']))
with open(filename, 'w', encoding='utf-8') as f:
f.write('# Auto-generated Level Data\n')
f.write('# Sorted by block count (Easy -> Hard)\n\n')
f.write('LEVEL_DESIGNS = {\n')
for index, data in enumerate(all_levels):
new_id = str(index + 1)
block_count = len(data['blocks'])
f.write(f' "{new_id}": {{\n')
f.write(f""" "name": "[Lv.{new_id}] {data['name']} (共{block_count}块)",\n""")
f.write(f' "blocks": [\n')
blocks = data['blocks']
line_buffer = ''
for b in blocks:
s = str(b) + ', '
if len(line_buffer) + len(s) > 80:
f.write(f' {line_buffer}\n')
line_buffer = s
else:
line_buffer += s
if line_buffer:
f.write(f' {line_buffer}\n')
f.write(f' ]\n')
f.write(f' }},\n')
f.write('}\n')
print(f'成功生成 {filename}!')
print(f'共导出 {len(all_levels)} 个关卡,已按物块数量从小到大排序。')
print(f'现在你可以按顺序游玩:python game.py 1, python game.py 2 ...')
if __name__ == '__main__':
export_to_file()