2

私が作っているゲームにこのスクリプトがあります。これは、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 ループで、変数を元の値から古い値の逆数に変更することです。しかし、スクリプトの先頭で変数の値を宣言しているため、速度変数は変更後の値にとどまりません。

変数の値を永続的に変更するか、一度だけ宣言する方法が必要です。

ありがとうございました!

4

5 に答える 5

2

velocity_xandvelocity_y宣言をループの前に置きます。クラスを使用している場合は、それらをオブジェクトの属性にし、その .xml 内で一度だけ初期化します__init__()

編集: Blender ゲーム エンジンがどのように機能するかはわかりませんが、スクリプトを大きなループに入れることに加えて、ループが始まる前に何かを初期化する方法があるはずです。本当に、あなたの特定の状況に関する私の限られた知識を考えると、私が言えることはそれだけです.

于 2010-04-18T20:09:28.587 に答える
1

同じ質問の答えを探しています。私が見つけることができる1つの方法があります。uは「プロパティの追加」ボタンをクリックして、blender UIにプロパティを追加する必要があります。たとえば、oneTime=Falseです。

次に、スクリプトに次のように記述します。

oneTime == Falseの場合:イベントを実行します。oneTime = True

これが私が見つけることができる唯一の方法です。

于 2010-06-19T10:08:17.677 に答える
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()

はい、関数または派手なクラスに調整できますが、それがあなたがしていることの範囲であれば、それ以上は必要ありません。

于 2010-06-30T15:41:09.010 に答える
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

など、インスタンスを参照するには?

于 2010-04-18T20:18:05.757 に答える