Lv.1
十 一 岁 的 无 名 小 孩
在 【Python作品分享】3D迷宫(分享) 中回复
我也做过3d 迷宫:
from ursina import *
import pygame
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.prefabs.sky import Sky
import sys
app = Ursina()
window.fullscreen = True
pygame.init()
wall_texture = load_texture("brick.png")
start_texture = load_texture("redstoneblock.jpg")
end_texture = load_texture("greendiamondblock.jpg")
ground_texture = load_texture("plank.jpg")
import random as rd
nearmaybe = [
[-2, 0],
[2, 0],
[0, -2],
[0, 2]
]
def createMaze(row, col):
maze = [[0 for i in range(col)] for i in range(row)]
check = []
firstrow = rd.randrange(1, row - 2, 2)
firstcol = rd.randrange(1, col - 2, 2)
maze[firstrow][firstcol] = 1
check.append([firstrow, firstcol])
while len(check):
c = rd.choice(check)
nears = []
conditions = []
for maybe in nearmaybe:
conditions.append([c[0] + maybe[0], c[1] + maybe[1]])
for condition in conditions:
if condition[0] >= 1 and condition[0] <= row - 2 \
and condition[1] >= 1 and condition[1] <= col - 2:
nears.append([condition[0], condition[1]])
for n in nears.copy():
if maze[n[0]][n[1]]:
nears.remove(n)
for block in nears:
if block[0] == c[0]:
if block[1] < c[1]:
maze[block[0]][c[1] - 1] = 1
maze[block[0]][block[1]] = 1
check.append([block[0], block[1]])
else:
maze[block[0]][block[1] - 1] = 1
maze[block[0]][block[1]] = 1
check.append([block[0], block[1]])
else:
if block[0] < c[0]:
maze[c[0] - 1][block[1]] = 1
maze[block[0]][block[1]] = 1
check.append([block[0], block[1]])
else:
maze[block[0] - 1][block[1]] = 1
maze[block[0]][block[1]] = 1
check.append([block[0], block[1]])
if not len(nears):
check.remove(c)
maze[1][1] = "s"
while True:
c = rd.randint(1, col - 2)
if maze[row - 2][c]:
maze[row - 2][c] = "e"
break
return maze
class Wall(Button):
def __init__(self, position):
super().__init__(
parent=scene,
model="cube",
texture=wall_texture,
color=color.white,
position=position,
origin_y=0.5
)
class Start(Button):
def __init__(self, position):
super().__init__(
parent=scene,
model="cube",
texture=start_texture,
color=color.white,
position=position,
origin_y=0.5
)
class End(Button):
def __init__(self, position):
super().__init__(
parent=scene,
model="cube",
texture=end_texture,
color=color.white,
position=position,
origin_y=0.5
)
class Ground(Button):
def __init__(self, position):
super().__init__(
parent=scene,
model="cube",
texture=ground_texture,
color=color.white,
position=position,
origin_y=0.5
)
class Player(FirstPersonController):
def __init__(self):
global startPosition
global endPosition
super().__init__()
camera.fov = 140
self.position = startPosition
self.gravity = 0.01
self.speed = 3
self.mouse_sensitivity = Vec2(90, 90)
def ifend(self):
if self.position == endPosition:
print('123')
sys.exit()
def jump(self):
pass
maze = createMaze(15, 15)
startPosition = None
endPosition = None
banPostions = []
for y in range(1, 4):
for x, row in enumerate(maze):
for z, value in enumerate(row):
if str(value) == "s":
Start((x * 2, 0, z * 2))
Start((x * 2, 0, z * 2 + 1))
Start((x * 2 + 1, 0, z * 2))
Start((x * 2 + 1, 0, z * 2 + 1))
startPosition = (x * 2, 0, z * 2)
elif str(value) == "e":
End((x * 2, 0, z * 2))
End((x * 2, 0, z * 2 + 1))
End((x * 2 + 1, 0, z * 2))
End((x * 2 + 1, 0, z * 2 + 1))
endPosition = (x * 2, 0, z * 2)
elif str(value) == "0":
Wall((x * 2, y, z * 2))
Wall((x * 2, y, z * 2 + 1))
Wall((x * 2 + 1, y, z * 2))
Wall((x * 2 + 1, y, z * 2 + 1))
y2 = 0
for x2 in range(x * 2 + 1):
for z2 in range(z * 2 + 1):
if not ([x2, z2] in banPostions):
Ground((x2, y2, z2))
for x2 in range(x * 2 + 1):
for z2 in range(z * 2 + 1):
if not ([x2, z2] in banPostions):
Ground((x2, y2 + 4, z2))
maze=createMaze(25,25)
for l in maze:
if pygame.mixer.music.get_busy() == False: # 检查是否正在播放音乐
pygame.mixer.music.load('背景音乐.mp3')
pygame.mixer.music.play() # 开始播放音乐流
for s in l:
print(s,end=" ")
print()
player = Player()
sky = Sky()
app.run()
2023-01-13T15:20:27 点赞:0