Python利用3D引擎制作一个3D迷宫游戏
Ursina是一个3D引擎,初步使用方法,见文章:详解Python 3D引擎Ursina如何绘制立体图形
了解完Ursina的初步用法,接下来,我们就开始设计这个3D迷宫游戏啦!
效果:
墙面、地板、起始块、终点块所需要的图像资源我放在下面供大家下载
↓↓
brick.png
redstoneblock.jpg
greendiamondblock.jpg
plank.jpg
代码详解:
首先,要用Prim最小生成树的方法生成迷宫,原理就是不断遍历还未遍历过的墙,并不断地删除不需要的墙块,代码见文章:Python Prim算法通过遍历墙实现迷宫的生成
这里,还有用遍历网格生成迷宫的方法,不过在编辑这个3D迷宫时,我们最好选用上方遍历墙的方式,顺便送上遍历网格的文章:Python利用Prim算法生成迷宫
把生成迷宫的代码命名为create.py
在主程序main.py中导入相关模块,其中ursina是整个3D引擎,ursina中有一个prefabs模块,prefabs中有一些可以直接拿出来用的东西,比如first_person_controller中的FirstPersonController,是用于设置第一人称的,sky中的Sky可以直接生成天空
然后,create模块中createMaze模块就是我们的Prim生成迷宫的算法
from ursina import * from ursina.prefabs.first_person_controller import FirstPersonController from ursina.prefabs.sky import Sky from create import createMaze
接下来,创建Ursina主程序
app=Ursina()
把刚刚提供给大家的图片存到同目录下的texture文件夹,作为材质包
在程序中引入这些材质
wall_texture=load_texture("texture/brick.png") start_texture=load_texture("texture/redstoneblock.jpg") end_texture=load_texture("texture/greendiamondblock.jpg") ground_texture=load_texture("texture/plank.jpg")
创建Wall类,墙壁方块,继承于Button类,注意,这里不要用Entity,Entity是没有碰撞体积的,所以要判断玩家是否落地,需要写更多代码,这里用Button类,后面使用第一人称时可以更方便,这里parent是场景,也就是Ursina里默认的scene,系统已经自动定义好了,model是cube,也就是正方体,texture即材质,position是坐标,坐标现在还未确定,所以在__init__中添加为参数,color设置为白色,这样贴材质的时候才不会让材质变色,设置为白色,可以让材质显示自己本身的颜色,Ursina中有color变量,一些常用的颜色可以这样使用:color.颜色名,或者用rgb的形式
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 )
再创建一个类Start,用作起始方块,这里参数都差不多,只改了材质
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 )
然后Ground(地板)和End(结束点)的类也差不多,都只改了个材质
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 )
接下来是Player,我们这里不直接使用Ursina的FirstPersonController,因为它系统已经帮你设置好了行走速度、重力加速度、跳跃等等,这里在Pycharm编辑器里,ctrl+鼠标点FirstPersonController即可看到对应的FirstPersonController的系统代码,系统代码如下:
class FirstPersonController(Entity): def __init__(self, **kwargs): self.cursor = Entity(parent=camera.ui, model='quad', color=color.pink, scale=.008, rotation_z=45) super().__init__() self.speed = 5 self.height = 2 self.camera_pivot = Entity(parent=self, y=self.height) camera.parent = self.camera_pivot camera.position = (0,0,0) camera.rotation = (0,0,0) camera.fov = 90 mouse.locked = True self.mouse_sensitivity = Vec2(40, 40) self.gravity = 1 self.grounded = False self.jump_height = 2 self.jump_up_duration = .5 self.fall_after = .35 # will interrupt jump up self.jumping = False self.air_time = 0 for key, value in kwargs.items(): setattr(self, key ,value) # make sure we don't fall through the ground if we start inside it if self.gravity: ray = raycast(self.world_position+(0,self.height,0), self.down, ignore=(self,)) if ray.hit: self.y = ray.world_point.y def update(self): self.rotation_y += mouse.velocity[0] * self.mouse_sensitivity[1] self.camera_pivot.rotation_x -= mouse.velocity[1] * self.mouse_sensitivity[0] self.camera_pivot.rotation_x= clamp(self.camera_pivot.rotation_x, -90, 90) self.direction = Vec3( self.forward * (held_keys['w'] - held_keys['s']) + self.right * (held_keys['d'] - held_keys['a']) ).normalized() feet_ray = raycast(self.position+Vec3(0,0.5,0), self.direction, ignore=(self,), distance=.5, debug=False) head_ray = raycast(self.position+Vec3(0,self.height-.1,0), self.direction, ignore=(self,), distance=.5, debug=False) if not feet_ray.hit and not head_ray.hit: self.position += self.direction * self.speed * time.dt if self.gravity: # gravity ray = raycast(self.world_position+(0,self.height,0), self.down, ignore=(self,)) # ray = boxcast(self.world_position+(0,2,0), self.down, ignore=(self,)) if ray.distance <= self.height+.1: if not self.grounded: self.land() self.grounded = True # make sure it's not a wall and that the point is not too far up if ray.world_normal.y > .7 and ray.world_point.y - self.world_y < .5: # walk up slope self.y = ray.world_point[1] return else: self.grounded = False # if not on ground and not on way up in jump, fall self.y -= min(self.air_time, ray.distance-.05) * time.dt * 100 self.air_time += time.dt * .25 * self.gravity def input(self, key): if key == 'space': self.jump() def jump(self): if not self.grounded: return self.grounded = False self.animate_y(self.y+self.jump_height, self.jump_up_duration, resolution=int(1//time.dt), curve=curve.out_expo) invoke(self.start_fall, delay=self.fall_after) def start_fall(self): self.y_animator.pause() self.jumping = False def land(self): # print('land') self.air_time = 0 self.grounded = True def on_enable(self): mouse.locked = True self.cursor.enabled = True def on_disable(self): mouse.locked = False self.cursor.enabled = False
我们只需要修改里面一些参数,__init__是一定要被修改的,跳跃的时候有时候会卡墙,然后跳到迷宫顶端,所以咱们这里禁用跳跃,重写jump函数,用pass代替即可。
gravity是重力,我们准备这样设计:一开始,我们是在半空中,然后缓缓下降,进入迷宫初始点,所以要缓缓下降就需要改到重力,将self.gravity改为0.01,也就是默认的1%,然后将玩家移动速度设置为6,也就是将self.speed设置为6,self.position是坐标,暂时未知,用全局变量代替,camera像scene和color一样,也是Ursina系统代码中已经帮我们定义好的了,我们无需重新定义,直接使用即可,camera中的fov是视角,咱们设置大一点,改为140,也差不多算广角了,然后设置self.mouse_sensitivity(鼠标敏感度),这个要像系统代码一样用Vec2,而且要传两个参数,我也不知道为啥这样,反正系统怎么写,咱们格式就尽量跟系统一样。其实这个灵敏度,就是鼠标移动视角的时候的速度,原来是40,40,这里我们设置为原来的4倍,160,160。
别忘了要执行父类的初始化函数哦!(super().__init__())
class Player(FirstPersonController): def __init__(self): global startPosition super().__init__() camera.fov=140 self.position=startPosition self.gravity=0.01 self.speed=6 self.mouse_sensitivity=Vec2(160,160) def jump(self): pass
接下来,生成迷宫,然后定义参数,banPositions存储起始块和结束块的坐标,生成地板的时候要跳过,因为在这两个块的地板是与其他不同的
maze=createMaze(15,15) startPosition=None endPosition=None banPostions=[]
这里,因为一个格宽度的路很窄,显得不宽敞,而且有时候行走也有些不方便,这里,我们把每个块放大为2x2,然后再创建场景
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,3,z*2) banPostions.append([x*2,z*2]) banPostions.append([x*2,z*2+1]) banPostions.append([x*2+1,z*2]) banPostions.append([x*2+1,z*2+1]) 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,3,z*2) banPostions.append([x*2,z*2]) banPostions.append([x*2,z*2+1]) banPostions.append([x*2+1,z*2]) banPostions.append([x*2+1,z*2+1]) 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))
然后实例化player和sky,最后运行程序
player=Player() sky=Sky() app.run()
这样就可以实现文章一开始图片中的效果啦!
不过我们走到终点也没有啥效果,这个就留给大家自己尝试和拓展啦!在这里就不再讲解~
最后,附上main.py的参考代码(迷宫生成的代码到我文章开头给出的链接中查看复制):
from ursina import * from ursina.prefabs.first_person_controller import FirstPersonController from ursina.prefabs.sky import Sky from create import createMaze app=Ursina() wall_texture=load_texture("texture/brick.png") start_texture=load_texture("texture/redstoneblock.jpg") end_texture=load_texture("texture/greendiamondblock.jpg") ground_texture=load_texture("texture/plank.jpg") 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 super().__init__() camera.fov=140 self.position=startPosition self.gravity=0.01 self.speed=6 self.mouse_sensitivity=Vec2(160,160) 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,3,z*2) banPostions.append([x*2,z*2]) banPostions.append([x*2,z*2+1]) banPostions.append([x*2+1,z*2]) banPostions.append([x*2+1,z*2+1]) 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,3,z*2) banPostions.append([x*2,z*2]) banPostions.append([x*2,z*2+1]) banPostions.append([x*2+1,z*2]) banPostions.append([x*2+1,z*2+1]) 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)) player=Player() sky=Sky() app.run()
以上就是Python利用3D引擎制作一个3D迷宫游戏的详细内容,更多关于Python 3D迷宫游戏的资料请关注我们其它相关文章!