猫史档案馆


【Python作品分享】简单的3d射击游戏

用户:无敌战神中的王者无敌战神中的王者查看:0 回复:1 评论:0 创建时间:2023-07-16T17:32:09


【作品展示】

center_image

 

【作品介绍】

地图大更新,地形森林,增加石头,

j瞬移,k往上移动,tab切换第三人称/第一人称,敌人设置为透明,满意点点赞,支持一下,谢谢

 

【作品源代码】

from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import lit_with_shadows_shader
import time

x = []
z = []
y = []
x_1 = []
z_1 = []
y_1 = []
x_2 = []
z_2 = []
y_2 = []

app = Ursina()
Entity.default_shader = lit_with_shadows_shader
DirectionalLight(y=2,rotation=(45,45,0))
Sky()
ground = Entity(model="plane",collider='box',scale=300,texture='grass',origin_z=-0.5,scale_y=3)

for i in range(300):
    cube = Entity(model='cube',scale=2,color=color.brown,
    texture_scale=(1,2),
    x=random.uniform(-150,150),
    z=random.uniform(-150,150)+150,
    collider='box',
    scale_y=20,origin_y=-0.5)

    x.append(cube.x)
    z.append(cube.z)
    y.append(cube.y)

for i in range(len(x)):
    Entity(model='cube',color=color.green,
    x=x[i],z=z[i],y=y[i]+20,scale=3,scale_x=12,scale_z=12,scale_y=3,collider='box')
    Entity(model='cube', color=color.green,
    x=x[i], z=z[i], y=y[i]+22, scale=3, scale_x=8, scale_z=8,scale_y=3,collider='box')

for i in range(100):
    Entity(model='cube',color=color.gray,scale=random.randint(1,2),x=random.uniform(-150,150),
    z=random.uniform(-150,150)+150,origin_y=-0.5,collider='box')

for i in range(random.randint(50,len(x))):
    cube_1 = Entity(model='cube',color=color.brown,z=z[i],x=x[i],scale_x=4.5,collider='box',origin_x=0.5,y=random.randint(11,16))
    z_1.append(cube_1.z)
    x_1.append(cube_1.x)
    y_1.append(cube_1.y)
for i in range(len(z_1)):
    cube_2 = Entity(model='cube',color=color.brown,z=z_1[i],x=x_1[i],y=y_1[i]+2,scale_y=3,origin_x=4,collider='box')
    z_2.append(cube_2.z)
    x_2.append(cube_2.x)
    y_2.append(cube_2.y)
for i in range(len(z_1)):
    Entity(model='cube', color=color.green,
    z=z_2[i], x=x_2[i], y=y_2[i], scale=2,origin_x=2,origin_y=-0.35, collider='box')

editor_camera = EditorCamera(enabled=False)

def input_pause(key):
    if key == 'tab':
        editor_camera.enabled = not editor_camera.enabled
        mouse.locked = not editor_camera.enabled
        player.cursor.enabled = not editor_camera.enabled
        player.visible_self = editor_camera.enabled
        editor_camera.position = player.position
    if key == 'j':
        player.speed = 300
        time.sleep(2)
        player.speed = 8
    if key == 'q':
        quit()
    if key == 'k':
        player.y += 25

pause_handler = Entity(input=input_pause)

player = FirstPersonController(model='cube', color=color.orange, z=-10, origin_y=-0.5,speed=8,collider='box',scale=1
)
player.position = (50,-1,50)

喵 = Entity(model='cube',parent=camera,scale=(0.3,0.2,0.5),position=(0.5,-0.25,0.5),color=color.red,on_cooldown=False)
喵.flash = Entity(parent=喵,model='quad',z=1,color=color.yellow,enabled=False)
def shoot():
    if not 喵.on_cooldown:
        喵.on_cooldown = True
        喵.flash.enabled = True
        from ursina.prefabs.ursfx import ursfx
        ursfx([(0.0, 0.0), (0.1, 0.9), (0.15, 0.75), (0.3, 0.14), (0.6, 0.0)], volume=0.5, wave='noise',
             pitch=random.uniform(-13,-12),pitch_change=-12,speed=100)
        invoke(喵.flash.disable, delay=0.15)
        invoke(setattr, 喵, 'on_cooldown', False, delay=0.15)
        if mouse.hovered_entity and hasattr(mouse.hovered_entity,'hp'):
            mouse.hovered_entity.blink(color.red)
            mouse.hovered_entity.hp -= 10
            if mouse.hovered_entity.hp <=0:
                destroy(mouse.hovered_entity)
            mouse.hovered_entity.health_bar.scale_x = mouse.hovered_entity.hp/mouse.hovered_entity.max_hp*1.5

def update():
    if held_keys['left mouse'] or held_keys['right mouse']:
        shoot()

class Enemy(Entity):
    def __init__(self,add_to_scene_entities=True,**kwargs):
        super().__init__(add_to_scene_entities=True,model='cube',collider='box' ,
                         scale_y=2, origin_y=-0.5,
                         color=color.white10,**kwargs)
        self.health_bar = Entity(parent=self,model='cube',color=color.red,y=1.2,
                                scale=(1.5,0.1,0.1))
        self.max_hp = random.randint(60,100)
        self.hp = self.max_hp

    def update(self):
        self.look_at_2d(player.position,'y')
        hit_info = raycast(self.position+Vec3(0,1,0),self.forward,30,ignore=(self,),debug=False)
        if hit_info.entity == player:
            if distance_xz(self.position,player.position)>2:
                self.position = self.position + self.forward*time.dt*5

for i in range(random.randint(15,20)):
    Enemy(x=i*4)
app.run()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
勤奋的程序喵勤奋的程序喵

厉害呀

点赞0


评论