python - Making a sprite character jump naturally with pyglet and pymunk -
this code displays image assassin1.png
on black screen. image has pymunk body , shape associated it. there invisible static pymunk object called floor
present beneath it. gravity induced on image , resting on invisible floor.
i make image jump naturally when press up
key. how can implement this?
import pyglet import pymunk def assassin_space(space): mass = 91 radius = 14 inertia = pymunk.moment_for_circle(mass, 0, radius) body = pymunk.body(mass, inertia) body.position = 50, 80 shape = pymunk.circle(body, radius) space.add(body, shape) return shape def add_static_line(space): body = pymunk.body() body.position = (0,0) floor = pymunk.segment(body, (0, 20), (300, 20), 0) space.add_static(floor) return floor 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.player1 = assassin(batch = self.batch_draw, img = pyglet.image.load("assassin1.png"), space = assassin_space(space)) pyglet.clock.schedule(self.update) add_static_line(space) def on_draw(self): self.clear() self.batch_draw.draw() self.player1.draw() space.step(1/50.0) def on_key_press(self, symbol, modifiers): if symbol == pyglet.window.key.up: print "the 'up' key pressed" def update(self, dt): self.player1.update() space.step(dt) if __name__ == "__main__": space = pymunk.space() # space.gravity = (0.0, -900.) # window = game() pyglet.app.run()
you need apply impulse body. impulse change in momentum, mass times velocity. assume want asassin jump straight up. if case, have apply impulse center of body.
body.apply_impulse(pymunk.vec2d(0, 60), (0, 0))
Comments
Post a Comment