目前我正在尝试用一匹跳过障碍的马来制作游戏,而我正在制作马动画。我可以让马跳起来,但我仍然未能实施" gravity"并使马在跳跃后重新回到地面。
这是我目前的代码:
import pyglet
import time
# background_sound = pyglet.media.load(
# 'Documents/Leave The Night On.mp3',
# streaming=False)
class horse(pyglet.sprite.Sprite):
def __init__(self, batch):
self._img_main = pyglet.image.load("sihorseav.png")
self.img_right1 = pyglet.image.load("sihorseav.png")
self.img_right2 = pyglet.image.load("sihorseav.png")
self.anim_right = pyglet.image.Animation.from_image_sequence([self.img_right1, self.img_right2], 0.5, True)
pyglet.sprite.Sprite.__init__(self, self._img_main)
self.time = 0
def forward_movement(self, flag=True):
if keymap[pyglet.window.key.UP]:
self.horse.time = time.time()
self.horse.jump()
def jump(self):
print time.time()-self.time
self.y -= -200 +20**(time.time()-self.time+.2)
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
self.batch = pyglet.graphics.Batch()
self.label = pyglet.text.Label(text="Score: 0", x=850, y=650, batch=self.batch)
image = pyglet.resource.image('sibackground.jpg')
self.background = pyglet.sprite.Sprite(image, batch=self.batch)
self.player = horse(batch = self.batch)
self.keys_held = []
def on_key_press(self, symbol, modifiers):
self.keys_held.append(symbol)
if symbol == pyglet.window.key.UP:
self.player.time=time.time()
self.player.jump()
def on_draw(self):
self.clear()
self.batch.draw()
self.player.draw()
self.label.draw()
def main():
window = Window(width=960, height=700, caption='Pyglet')
pyglet.app.run()
if __name__ == '__main__':
main()