Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8196551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:31:02+00:00 2026-06-07T05:31:02+00:00

This code displays the image assassin1.png on a black screen standing still at position

  • 0

This code displays the image assassin1.png on a black screen standing still at position (50, 80).

The goal is to induce the pymunk gravity on that image so that it falls down. I followed the pymunk tutorial which is written using pygame and tried to adapt that. I don’t know why my code isn’t applying gravity to the my image. Could someone tell me what I am doing wrong and what I should change to make it work properly?

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:31:03+00:00Added an answer on June 7, 2026 at 5:31 am

    The problem is that you set the location of your Sprite once:

    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 )
    

    but this location never gets updated.

    Another problem is that in the pygame example, space.step(1/50.0) is called every iteration of the main loop, while your’s only gets called when the window is drawn.

    So, first, you should ensure space.step gets called every frame. Do so by using a 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)
    

    The next step is ensuring the location of the sprite is keeped in sync with the pyglet object.

    Change your Assassin class:

    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
    

    Create your Assassin instance like this:

    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)
    

    and call self.player1.update() in your new update method:

    class Game(pyglet.window.Window):
        def __init__(self):
            ...
    
        def on_draw(self):
            ...
    
        def update(self, dt):
            self.player1.update()
            space.step(dt)
    

    (And you should probably get rid of some unneeded instance members, but that’s another topic)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code displays the image assassin1.png on a black background. The idea is that
I have this code that pulls an image from an url and displays it
Currently my code displays a single image from a file path (in SDCard). This
How can I alter this code so that It only displays one image at
This code when executed displays the expected output but prints segmentation fault (core dumped)
This code correctly fetches data, and displays it; however, the sort is completely ignroed.
I have found this line of MatLab code on the internet that displays a
I have following code that displays an Image with letters, public class MainActivity extends
The following code displays correctly in Chrome or IE (the image is 200px wide).
the image that is being displayed in this code is leaking but I cant

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.