1
此代碼在黑色背景上顯示圖像assassin1.png
。這個想法是,只要我按下<right-direction>
鍵,角色就會移動並使用assassin2.png
和assassin3.png
顯示動畫。我已經能夠移動角色併成功顯示動畫,而不是同時顯示動畫。Pyglet步行動畫。錯誤:'無類型'對象沒有'繪製'屬性
下面的代碼,只要我不按<right direction>
鍵運行:一旦
import pyglet
def sprite_type(type_ = "standing"):
if type_ == "moving-forward":
moving_forward_image_list = [pyglet.image.load('assassin2.png'), pyglet.image.load('assassin3.png')]
moving_forward_animation = pyglet.image.Animation.from_image_sequence(moving_forward_image_list, 0.3)
return moving_forward_animation
if type_ == "standing":
standing_animation = pyglet.image.load("assassin1.png")
return standing_animation
class Assassin(pyglet.sprite.Sprite):
def __init__(self, batch, img):
pyglet.sprite.Sprite.__init__(self, img, x = 50, y = 30)
def stand(self, batch, img):
self.batch = batch
self.img = img
def move(self, batch, img):
self.batch = batch
self.img = img
class Game(pyglet.window.Window):
def __init__(self):
pyglet.window.Window.__init__(self, width = 315, height = 220)
self.batch_draw = pyglet.graphics.Batch()
self.player = Assassin(batch = self.batch_draw, img = sprite_type())
self.fps_display = pyglet.clock.ClockDisplay()
self.keys_held = []
self.schedule = pyglet.clock.schedule_interval(func = self.update, interval = 1/60.)
def on_draw(self):
self.clear()
self.fps_display.draw()
self.batch_draw.draw()
self.player.draw()
def on_key_press(self, symbol, modifiers):
self.keys_held.append(symbol)
if symbol == pyglet.window.key.RIGHT:
self.player = self.player.move(batch = self.batch_draw, img = sprite_type("moving-forward"))
print "The 'RIGHT' key was pressed"
def on_key_release(self, symbol, modifiers):
self.keys_held.pop(self.keys_held.index(symbol))
self.player = self.player.stand(batch = self.batch_draw, img = sprite_type("standing"))
def update(self, interval):
if pyglet.window.key.RIGHT in self.keys_held:
self.player.x += 50 * interval
if __name__ == "__main__":
window = Game()
pyglet.app.run()
我按<right-direction>
鍵會出現以下錯誤:
Traceback (most recent call last):
File "ircex.py", line 55, in <module>
pyglet.app.run()
File "/usr/local/lib/python2.7/dist-packages/pyglet/app/__init__.py", line 264, in run
EventLoop().run()
File "/usr/local/lib/python2.7/dist-packages/pyglet/app/xlib.py", line 93, in run
sleep_time = self.idle()
File "/usr/local/lib/python2.7/dist-packages/pyglet/app/__init__.py", line 193, in idle
window.dispatch_event('on_draw')
File "/usr/local/lib/python2.7/dist-packages/pyglet/window/__init__.py", line 1219, in dispatch_event
EventDispatcher.dispatch_event(self, *args)
File "/usr/local/lib/python2.7/dist-packages/pyglet/event.py", line 349, in dispatch_event
return getattr(self, event_type)(*args)
File "pyglet-walk.py", line 37, in on_draw
self.player.draw()
AttributeError: 'NoneType' object has no attribute 'draw'
我在'move'和'stand'方法的末尾放了'return self'。現在按下''鍵時不會產生運行時錯誤。但是,釋放密鑰時會產生相同的錯誤,並調用「stand」方法。而且,當角色向右移動時,它只會再次顯示站立動畫。有任何想法嗎? –
Bentley4
'stand()'方法的相同之處 - 它返回'None'! –
還有另一個編輯器在後臺運行相同的文件,我只是繼續運行它。對不起!現在錯誤消失了。但是當我按下<<方向按鈕>時,行走動畫仍然不顯示。只有站立的動畫顯示。你知道爲什麼嗎? – Bentley4