我在这个游戏中有这个脚本。这将用于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循环中,我将变量从其原始值更改为其旧值的倒数。但是因为我在脚本的开头声明了变量的值,所以速度变量不会保留在我将其更改为的位置。
我需要一种方法永久地更改变量的值或仅声明一次。
谢谢!
答案 0 :(得分:2)
在循环之前放置velocity_x
和velocity_y
声明。如果您正在使用类,请将它们作为对象的属性,并在其__init__()
内进行一次初始化。
答案 1 :(得分:1)
然后在脚本中写道:
如果oneTime == False: 做事。 一次性=真
这是我能找到的唯一方法。
答案 2 :(得分:1)
如果每次运行脚本时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()
是的,它可以调整为函数或花哨的类,但如果这是你正在做的程度,你不需要更多。
答案 3 :(得分:0)
使用全球的一个例子。
#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
依此类推,引用实例?
答案 4 :(得分:0)
要处理脚本代码的连续循环,您需要一个在代码之外编写的值。否则它无法工作。您的脚本应该如何知道它之前已经运行过?以下代码使用Blender 2.6和2.7系列运行:
可能性1:Blenders Global Dictionary。 添加一个Subdictionary(也可以是空的):
bge.logic.globalDict['mysubdictionaryName'] = { 'namestring' : False}
你可以保存这样的值:
bge.globalDict['mysubdictionaryName'] = myValue
。
可能性2:Objectproperty a)使用python:
myObject = bge.logic.getCurrentController().owner
myObject['myproperty_named_has_run_before'] = True
b)在Logic-editor
中使用Logicbricks和Add Properties在您的情况下,您应该使用objectproperties,因为当多个对象进行通信或者您需要将数据传递到另一个游戏时,使用globalDict。