[英]How to add delay in Ursina Python Game Engine
I've recently started using Ursina Game Engine (runs in python), and have run into a problem throughout many of my projects.我最近开始使用 Ursina 游戏引擎(在 python 中运行),并且在我的许多项目中都遇到了问题。 I don't know how to implement delay, or sleeping between functions.我不知道如何实现延迟,或者在函数之间休眠。 There is a Wait function that I haven't been able to get to work.有一个等待功能,我无法开始工作。 I've tried alternatives like time.sleep() and having a function that using delay, but none of them seemed to work.我尝试过像 time.sleep() 这样的替代方法,并且有一个使用延迟的函数,但它们似乎都不起作用。 Since this isn't a very popular game engine, there aren't many guides or helpful information online.由于这不是一个非常流行的游戏引擎,因此在线指南或有用信息并不多。 If you can think of or know of a way to implement time delay, please let me know.如果您能想到或知道实现时间延迟的方法,请告诉我。
Thanks谢谢
Calling a function with a delay is done by using the invoke()
function, like that :调用具有延迟的函数是通过使用invoke()
函数完成的,如下所示:
def foo():
print('bar')
invoke(foo, delay=5) # Calls myFunc after 5 seconds
and for functions that require arguments :对于需要参数的函数:
def foobar(foo, bar):
print(foo + bar)
invoke(Func(foobar, 'foo', 'this is foo\'s value', 'bar', 'and this is bar\'s'), delay=5)
Thats actually easy to use delay, here's an example:这实际上很容易使用延迟,这是一个例子:
from ursina import *
app = Ursina()
player = Entity(model='cube', color=color.gray, scale_y=2)
def input(key):
if(key == 'space'):
player.y +=1
invoke(setattr,player,'y',player.y-1,delay=.25)
app.run()
In this example, basically, when the player press space
, the entity's y increases by 1 and after the delay the entity's y decreases by 1, in other words we created a jump with the delay.在这个例子中,基本上,当玩家按下space
键时,实体的 y 增加 1,延迟后实体的 y 减少 1,换句话说,我们创建了一个带有延迟的跳跃。
Note: The setattr()
function sets the value of the attribute of an object.注意: setattr()
函数设置对象的属性值。 You can see more about this function here: https://www.programiz.com/python-programming/methods/built-in/setattr您可以在此处查看有关此功能的更多信息: https : //www.programiz.com/python-programming/methods/built-in/setattr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.