猫史档案馆


分享大神的作品(不是自己做的,作者忘了()()

用户:柏林防空塔柏林防空塔查看:0 回复:0 评论:0 创建时间:2024-01-14T20:13:50


from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina.shaders import lit_with_喵s_shader
import math
#3D建模下载glb格式的不会和纹理分开


class Player(Entity):
    def __init__(self, **kwargs):
        self.controller = FirstPersonController(**kwargs)
        super().__init__(parent=self.controller)

        self.hand_gun = Entity(parent=self.controller.camera_pivot,  # 喵
                               model='喵素材/M416.obj',
                               scale=0.1,
                               rotation=Vec3(30, 180, 0),
                               position=Vec3(0.5, -0.3, 1),
                               visible=False)

        self.hand = Entity(parent=self.controller.camera_pivot,  # 手
                           model='cube',
                           scale=(0.17, 0.17, 1),
                           rotation=Vec3(0, 190, 0),
                           position=Vec3(0.3, -0.35, 0.5),
                           visible=False)

        self.body = Entity(parent=self.controller.camera_pivot,  # 玩家身体
                           model='cube',
                           scale=(0.6, 2.3, 0.6),
                           rotation=Vec3(0, 0, 0),
                           position=Vec3(0, -1, 0))

        self.weapons = [self.hand_gun, self.hand]  # 初始化武器
        self.current_weapon = 0
        self.switch_weapon()
        self.walksound = 0
        self.walk = 0
        camera.fov = 90

    def switch_weapon(self):
        for i, v in enumerate(self.weapons):
            if i == self.current_weapon:
                v.visible = True
            else:
                v.visible = False

    def input(self, key):
        try:
            self.current_weapon = int(key) - 1  # 数字键切换武器
            self.switch_weapon()
        except ValueError:
            pass

        if key == 'scroll up':  # 滚轮切换武器
            self.current_weapon = (self.current_weapon + 1) % len(self.weapons)
            self.switch_weapon()
        if key == 'scroll down':
            self.current_weapon = (self.current_weapon - 1) % len(self.weapons)
            self.switch_weapon()

    def update(self):
        camera.rotation_z = camera.rotation_z + \
            (0 - camera.rotation_z + mouse.x*10) * 0.2
        self.hand_gun.rotation_x = self.hand_gun.rotation_x + \
            (0-self.hand_gun.rotation_x + mouse.y*10) * 0.25
        self.hand_gun.rotation_y = self.hand_gun.rotation_y + \
            (180 - self.hand_gun.rotation_y + mouse.x*10) * 0.2
        self.hand.rotation_x = self.hand.rotation_x + \
            (0-self.hand.rotation_x + mouse.y*10 +
             math.sin(math.radians(self.walk))*10) * 0.25
        self.controller.camera_pivot.y = self.controller.camera_pivot.y + \
            (2-held_keys['shift']*0.5-self.controller.camera_pivot.y)*0.3
        if held_keys['right mouse'] and self.current_weapon == 0:  # 开镜检测
            camera.fov = camera.fov + (70 - camera.fov) * 0.2
            self.hand_gun.x = self.hand_gun.x + (0-self.hand_gun.x)*0.3
            self.hand_gun.y = self.hand_gun.y + (-0.1-self.hand_gun.y)*0.3
            self.hand_gun.z = self.hand_gun.z + \
                (1.2-self.hand_gun.z+math.sin(math.radians(self.walk))*0.05)*0.3
        else:
            camera.fov = camera.fov + (90 - camera.fov) * 0.2
            self.hand_gun.x = self.hand_gun.x + (0.5-self.hand_gun.x)*0.3
            self.hand_gun.y = self.hand_gun.y + (-0.3-self.hand_gun.y)*0.3
            self.hand_gun.z = self.hand_gun.z + \
                (1-self.hand_gun.z+math.sin(math.radians(self.walk))*0.05)*0.3
        if held_keys['q']:
            self.walk = self.walk + \
                (8-held_keys['shift']*3-held_keys['right mouse'])
            self.walksound = self.walksound + \
                (8-held_keys['shift']*3-held_keys['right mouse'])
            self.controller.speed = self.controller.speed + \
                (9-held_keys['right mouse']*5 -
                 held_keys['shift']*3-self.controller.speed)*0.3
            if self.walksound > 200:
                gravel.play()  # 播放走路声音
                self.walksound = 0
        else:
            self.walk = 0
            self.controller.speed = self.controller.speed + \
                (6-held_keys['right mouse']*3 -
                 held_keys['shift']*2-self.controller.speed)*0.3
        if held_keys['left mouse'] and self.current_weapon == 0 and self.hand_gun.rotation_x < 0.6:
            shoot_sound.play()
            camera.rotation_z = camera.rotation_z + random.randint(-3, 3)
            self.hand_gun.rotation_x = self.hand_gun.rotation_x + 2
            self.hand_gun.z = self.hand_gun.z - 0.05
            Bullet(model='sphere',
                   color=color.red,
                   scale=0.01,
                   position=self.controller.camera_pivot.world_position,
                   rotation=self.controller.camera_pivot.world_rotation)


class Bullet(Entity):  # 子弹定义
    def __init__(self, speed=2000, lifetime=5, **kwargs):
        super().__init__(**kwargs)
        self.speed = speed
        self.lifetime = lifetime
        self.start = time.time()

    def update(self):
        ray = raycast(self.world_position, self.forward,
                      distance=self.speed*time.dt)
        if not ray.hit and time.time() - self.start < self.lifetime:
            self.world_position += self.forward * self.speed * time.dt
        else:
            destroy(self)


def input(key):  # 按esc键退出游戏
    if key == 'escape':
        quit()


app = Ursina()
gravel = Audio('喵素材/gravel.wav', loop=False, autoplay=False)  # 导入音效
shoot_sound = Audio('喵素材/shoot.wav', loop=False, autoplay=False)
window.exit_button.visible = False  # 隐藏游戏页面关闭键
window.fps_counter.enabled = False  # 隐藏游戏fps显示
Entity.default_shader = lit_with_shadows_shader  # 光影
DirectionalLight(y=5, rotation=(45, 0, 45))
player = Player(position=(0, 10, 0))  # 玩家
ground = Entity(model='cube', scale=(30, 1, 30),
                collider="box", texture='grass')  # 地面
Sky()  # 天空

app.run()


回复

上一页1 页 / 共 0下一页