如何永久更改 Python 游戏循环中的变量
题
我正在制作的游戏中有这个脚本。这将在 Blender 游戏引擎中使用。Blender 从上到下不断地一遍又一遍地运行脚本,所以如果我在脚本的开头声明一个变量,它就会不断地被初始化。
#The current location of the object
loc_x = obj.getPosition()[0]
loc_y = obj.getPosition()[1]
#The velocity of the object
velocity_x = 0.09
velocity_y = 0.03
#If the location of the object is over 5, bounce off.
if loc_x > 5:
velocity_x = (velocity_x * -1)
if loc_y > 5:
velocity_y = (velocity_y * -1)
#Every frame set the object's position to the old position plus the velocity
obj.setPosition([(loc_x + velocity_x),(loc_y + velocity_y),0])
基本上,我的问题是在 if 循环中,我将变量从其原始值更改为其旧值的倒数。但是因为我在脚本开头声明了变量的值,所以速度变量不会保留在我将其更改为的值上。
我需要一种方法来永久更改变量的值或仅声明一次。
谢谢你!
解决方案
要通过你的脚本代码处理连续循环播放您需要的IST编写的代码之外的价值。否则它不能正常工作。您应该怎样脚本知道它之前已经运行?下面的代码与搅拌机2.6道和2.7系列:
<强>可能性1:搅拌器全局词典强> 添加一个子词典(可得为空):
bge.logic.globalDict['mysubdictionaryName'] = { 'namestring' : False}
您可以保存这样的价值观:
bge.globalDict['mysubdictionaryName'] = myValue
。
<强>可能性2:OBJECTPROPERTY 强> a)用蟒:
myObject = bge.logic.getCurrentController().owner
myObject['myproperty_named_has_run_before'] = True
b)该逻辑编辑器的内部使用Logicbricks和添加属性
在你的情况,你应该使用objectproperties,因为globalDict使用,当多个对象进行通信,或者如果你需要数据到另一个gamescene。
其他提示
循环之前把velocity_x
和velocity_y
声明。如果您使用的类,使它们对象的属性和intialize他们只有一次,它的__init__()
内。
编辑:我不知道该怎么Blender的游戏引擎的工作原理,但除了具有一个大循环的脚本,应该在循环开始前并初始化的东西的一种方式。真的,这就是我能说给我的有限的特定情况的知识。
即时寻找相同的问题的答案。有一种方法我coould find.u必须点击“添加属性”按钮,并在混合器添加属性UI.for例如,一次性=假。
然后在脚本写:
如果一次性==题: 办赛事。 一次性=真
这是我可以找到的唯一途径。
如果每次运行脚本时您的 python 运行时环境都相同,请尝试将初始化移至异常处理程序。就像这样:
try:
velocity_x = (velocity_x * -1)
except:
velocity_x = 0.09
您还可以尝试将变量填充到 __main__
模块,如果这不起作用。就像这样:
try:
__main__.velocity_x = (velocity_x * -1)
except:
__main__.velocity_x = 0.09
如果这不起作用,您将需要一些轻量级的内置组件,例如 sqlite3 模块。我重写了你的整个代码片段:
import sqlite3
#The current location of the object
loc_x = obj.getPosition()[0]
loc_y = obj.getPosition()[1]
c = sqlite3.connect('/tmp/globals.db')
#c = sqlite3.connect('/dev/shm/globals.db')
# Using the commented connection line above instead will be
# faster on Linux. But it will not persist beyond a reboot.
# Both statements create the database if it doesn't exist.
# This will auto commit on exiting this context
with c:
# Creates table if it doesn't exist
c.execute('''create table if not exist vectors
(vector_name text primary key not null,
vector_value float not null,
unique (vector_name))''')
# Try to retrieve the value from the vectors table.
c.execute('''select * from vectors''')
vector_count = 0
for vector in c:
vector_count = vector_count + 1
# sqlite3 always returns unicode strings
if vector['vector_name'] == u'x':
vector_x = vector['vector_value']
elif vector['vector_name'] == u'y':
vector_y = vector['vector_value']
# This is a shortcut to avoid exception logic
# Change the count to match the number of vectors
if vector_count != 2:
vector_x = 0.09
vector_y = 0.03
# Insert default x vector. Should only have to do this once
with c:
c.executemany("""replace into stocks values
(?, ?)""", [('x', vector_x), ('y', vector_y)])
#If the location of the object is over 5, bounce off.
if loc_x > 5:
velocity_x = (velocity_x * -1)
if loc_y > 5:
velocity_y = (velocity_y * -1)
# Update stored vectors every time through the loop
with c:
c.executemany("""update or replace stocks set vector_name = ?,
vector_value = ?)""", [('x', vector_x), ('y', vector_y)])
#Every frame set the object's position to the old position plus the velocity
obj.setPosition([(loc_x + velocity_x),(loc_y + velocity_y),0])
# We can also close the connection if we are done with it
c.close()
是的,它可以调整为函数或奇特的类,但如果这就是您正在做的事情的范围,那么您不需要更多。
使用全局的一个例子。
#The velocity of the object
velocity_x = 0.09
velocity_y = 0.03
loc_x = 0
loc_y = 0
def update_velocity():
#If the location of the object is over 5, bounce off.
global velocity_x, velocity_y
if loc_x > 5:
velocity_x = (velocity_x * -1)
if loc_y > 5:
velocity_y = (velocity_y * -1)
def update_position():
global loc_x, loc_y # global allows you to write to global vars
# otherwise you're creating locals :)
loc_x += velocity_x
loc_y += velocity_y
#Every frame set the object's position to the old position plus the velocity
while True:
update_velocity()
update_position()
# undoubtedly you do more than this...
obj.setPosition([loc_x,loc_y,0])
修改强>
我看到一些评论的__init__
。如果你在一个类是你不应该写的是这样的:
self.loc_x += self.velocity_x
等,以引用的实例?