为什么我的函数不按照我在ursina python中告诉它们的去做

sycxhyv7  于 2023-11-20  发布在  Python
关注(0)|答案(2)|浏览(201)

函数'aimshot()'在某个位置(瞄准时)播放声音和动画,函数'noaimshot()'也是如此,但在不瞄准时。出于某种原因,当我稍后在'def input(key)'函数中尝试运行这些函数时,它们不起作用。

def aimshot():
        Audio("assets/GunShot.mp3")
        Animation("assets/spark", parent=camera.ui, fps=5, scale=.1, position=(.00, -.726), loop=False)

def noaimshot():
    Audio("assets/GunShot.mp3")
    Animation("assets/spark", parent=camera.ui, fps=5, scale=.1, position=(.34, -.54), loop=False)

def input(key):
    if key == 'escape':
        quit()

    if key == "q":
        aim.enable()
        noaim.disable()
        if key == "left mouse down":
           noaimshot()

    if key == "e":
        aim.disable()
        noaim.enable()
        if key == "left mouse down":
            aimshot()

字符串
完整的代码在这里。注意:你需要pip install ursina来运行它。

from ursina import *
from random import uniform
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()
Sky()
player=FirstPersonController(y=10, origin_y=1)
ground=Entity(model='plane', scale=(20, 1, 20), color=color.lime, texture="white_cube",
              texture_scale=(20, 20), collider='box')

noaim = Entity(model="assets/gun.obj", parent=camera.ui, scale=.03, color=rgb(0, 0, 139), position=(.5, -.7),
            rotation=(-5, 5, 1))

aim = Entity(model="assets/gun.obj", parent=camera.ui, scale=.03, color=rgb(0, 0, 139), position=(.00, -.726),
                     rotation=(-5, 1, -1))
aim.disable()

def aimshot():
        Audio("assets/GunShot.mp3")
        Animation("assets/spark", parent=camera.ui, fps=5, scale=.1, position=(.00, -.726), loop=False)

def noaimshot():
    Audio("assets/GunShot.mp3")
    Animation("assets/spark", parent=camera.ui, fps=5, scale=.1, position=(.34, -.54), loop=False)

def input(key):
    if key == 'escape':
        quit()

    if key == "q":
        aim.enable()
        noaim.disable()
        if key == "left mouse down":
           noaimshot()

    if key == "e":
        aim.disable()
        noaim.enable()
        if key == "left mouse down":
            aimshot()

app.run()

toe95027

toe950271#

因为if语句永远不为True,所以下面的代码永远不会运行。你可以通过打印它来检查变量的状态,例如print(key)
变量key不能同时是“q”和“left mouse down”。如果你想检查“q”是否在你按下“left mouse down”的时候被按住,你可以这样做:

if held_keys["q"] and key == "left mouse down":
    noaimshot()

字符串

ndasle7k

ndasle7k2#


的数据
这就是为什么你的代码不工作的原因,你不能定义一个函数有两个标签,但只有一个

相关问题