使用pymunk重力与pyglet

时间:2012-07-06 10:10:31

标签: python game-physics pyglet pymunk

此代码在静止位置(50,80)的黑色屏幕上显示图像assassin1.png

目标是在该图像上诱导花栗鼠重力,使其落下。我跟着使用pygame编写的pymunk tutorial并试图适应它。我不知道为什么我的代码没有将重力应用于我的图像。有人能告诉我我做错了什么以及我应该改变什么以使其正常工作?

import pyglet
import pymunk

class PymunkSpace(object):
    def assassin_space(self, space):
        self.space = space
        self.mass = 91
        self.radius = 14
        self.inertia = pymunk.moment_for_circle(self.mass, 0, self.radius) 
        self.body = pymunk.Body(self.mass, self.inertia) 
        self.body.position = 50, 80 
        self.shape = pymunk.Circle(self.body, self.radius) 
        self.space.add(self.body, self.shape) 
        return self.shape 

class Assassin(pyglet.sprite.Sprite):
    def __init__(self, batch, img, x, y):
        pyglet.sprite.Sprite.__init__(self, img, x , y )

class Game(pyglet.window.Window):
    def __init__(self):
        pyglet.window.Window.__init__(self, width = 315, height = 220)
        self.batch_draw = pyglet.graphics.Batch()
        self.a_space = PymunkSpace().assassin_space(space)
        self.player1 = Assassin(batch = self.batch_draw, img = pyglet.image.load("assassin1.png"), x = self.a_space.body.position.x ,y = self.a_space.body.position.y )

    def on_draw(self):
        self.clear()
        self.batch_draw.draw()
        self.player1.draw()
        space.step(1/50.0) 

if __name__ == "__main__":
    space = pymunk.Space() 
    space.gravity = (0.0, -900.) 
    window = Game()
    pyglet.app.run()

1 个答案:

答案 0 :(得分:5)

问题是您只需设置Sprite的位置:

self.player1 = Assassin(batch = self.batch_draw, img = pyglet.image.load("assassin1.png"), x = self.a_space.body.position.x ,y = self.a_space.body.position.y )

class Assassin(pyglet.sprite.Sprite):
  def __init__(self, batch, img, x, y):
      pyglet.sprite.Sprite.__init__(self, img, x , y )

但此位置永远不会更新。

另一个问题是,在pygame示例中,主循环的每次迭代都会调用space.step(1/50.0),而在绘制窗口时只会调用它。

因此,首先,您应确保每帧都调用space.step。使用clock

这样做
class Game(pyglet.window.Window):
    def __init__(self):
        ...
        pyglet.clock.schedule(self.update)

    def on_draw(self):
        self.clear()
        self.batch_draw.draw()
        self.player1.draw()

    def update(self, dt):
        space.step(dt)

下一步是确保sprite的位置与pyglet对象保持同步。

更改你的刺客课程:

class Assassin(pyglet.sprite.Sprite):
    def __init__(self, batch, img, space):
        self.space = space
        pyglet.sprite.Sprite.__init__(self, img, self.space.body.position.x , self.space.body.position.y)

    def update(self):
        self.x = self.space.body.position.x
        self.y = self.space.body.position.y

像这样创建你的刺客实例:

class Game(pyglet.window.Window):
    def __init__(self):
        pyglet.window.Window.__init__(self, width = 315, height = 220)
        self.batch_draw = pyglet.graphics.Batch()
        self.a_space = PymunkSpace().assassin_space(space)
        self.player1 = Assassin(batch = self.batch_draw, img = pyglet.image.load("assassin1.png"), space = self.a_space)
        pyglet.clock.schedule(self.update)

并在新的self.player1.update()方法中致电update

class Game(pyglet.window.Window):
    def __init__(self):
        ...

    def on_draw(self):
        ...

    def update(self, dt):
        self.player1.update()
        space.step(dt)

(你应该摆脱一些不需要的实例成员,但这是另一个话题)