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 6073837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:18:55+00:00 2026-05-23T10:18:55+00:00

A simple question: Why does this the first code work but the seemingly identical

  • 0

A simple question: Why does this the first code work but the seemingly identical second code freezes up when the pygame window comes?

# Moving Pan
# Demonstrates mouse input

from livewires import games

games.init(screen_width = 640, screen_height = 480, fps = 50)

class Pan(games.Sprite):
    """ A pan controlled by the mouse. """
    def update(self):
        """ Move to mouse coordinates. """
        self.x = games.mouse.x
        self.y = games.mouse.y

def main():
    wall_image = games.load_image("wall.jpg", transparent = False)
    games.screen.background = wall_image

    pan_image = games.load_image("pan.bmp")
    the_pan = Pan(image = pan_image,
                  x = games.mouse.x,
                  y = games.mouse.y)
    games.screen.add(the_pan)

    games.mouse.is_visible = False

    games.screen.event_grab = True

    games.screen.mainloop()

# kick it off!
main()

The malfunctioning second:

from livewires import games,color
games.init (screen_width = 640, screen_height = 480, fps = 50)

#Creating a moving object tied to the cursor. This includes one method with two
#lines of code.
class Pan (games.Sprite):
    def moved (self):
    #Receives mouse position
        self.x = games.mouse.x
    #Changes mouse position to new x,y values.
        self.y = games.mouse.y

#The Main
myscr = games.screen
myscr.set_background (games.load_image ("wall.jpg", transparent = False))
pan_image = games.load_image ("pan.bmp")
le_pan = Pan (image = pan_image,
              x = games.mouse.x,
              y = games.mouse.y)

games.mouse.is_visible = False
myscr.add (le_pan)
myscr.event_grab = True
myscr.mainloop()
  • 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-05-23T10:18:56+00:00Added an answer on May 23, 2026 at 10:18 am

    I have never worked with livewires, but in games you usually need a – more or less – endless game-loop.

    The sense behind a game-loop is, that you always want to know where the mouse is or what keys are pressed, not only once! So you have to ask Where is the mouse? over and over. And to achieve that, you use a loop that checks everything you want everytime it gets executed.

    In the first example the game-loop is the function main. The flow of the application is like this:

    1. Import needed libraries

      from livewires import games
      
    2. Initialize the game-screen

      games.init(screen_width = 640, screen_height = 480, fps = 50)
      
    3. Declare a sprite which can be displayed on the screen

      class Pan(games.Sprite):
          """ A pan controlled by the mouse. """
          def update(self):
              """ Move to mouse coordinates. """
              self.x = games.mouse.x
              self.y = games.mouse.y
      
    4. Declare the main method and setup the game-screen background

      def main():
          wall_image = games.load_image("wall.jpg", transparent = False)
          games.screen.background = wall_image
      
    5. Add the above defined sprite to the screen and move it to the position of the mouse-cursor

          pan_image = games.load_image("pan.bmp")
          the_pan = Pan(image = pan_image,
                        x = games.mouse.x,
                        y = games.mouse.y)
          games.screen.add(the_pan)
      
    6. Make mouse cursor invisible and activate events

          games.mouse.is_visible = False
          games.screen.event_grab = True
      
    7. Run mainloop. The call of this method says: Run me( functionmain)over and over!

          games.screen.mainloop()
      
    8. Call main for the first time

      main()
      

    In the second example, there is no game-loop. The flow of the application (tighter packed) is like this:

    1. Import libraries, initialize game-screen, declare a sprite

      from livewires import games,color
      games.init (screen_width = 640, screen_height = 480, fps = 50)
      
      class Pan (games.Sprite):
          def moved (self):
              self.x = games.mouse.x
              self.y = games.mouse.y
      
    2. Setup game-screen background and add sprite

      myscr = games.screen
      myscr.set_background (games.load_image ("wall.jpg", transparent = False))
      pan_image = games.load_image ("pan.bmp")
      le_pan = Pan (image = pan_image,
                    x = games.mouse.x,
                    y = games.mouse.y)
      myscr.add(le_pan)
      
    3. Deactivate mouse cursor, enable events

      games.mouse.is_visible = False
      myscr.event_grab = True
      
    4. Run mainloop. The call of this method says: Run me( functionundefined)over and over!

      myscr.mainloop()
      

    And here is the sticking point! You cannot call code that is in the root of a Python-file! The mainloop function doesn’t know where to return or where to start off. The call gets lost, your program freezes. The game-screen can’t get updated, because nothing is telling it how it should update.

    Conclusion: You have to have a function for your game-loop!

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

Sidebar

Related Questions

This should be a simple question, but I haven't been able to find a
All, This question probably has a very simple answer - something I'm overlooking. But
Simple question: do I have to delete or delete [] c ? Does the
Simple question, but one that I've been curious about...is there a functional difference between
Simple question: How do I do this on one line: my $foo = $bar->{baz};
Simple question: Can I mix in my desktop application Java and JavaFX Script code?
It's a simple question, but no matter where I look, I can't seem to
Simple question, how do you list the primary key of a table with T-SQL?
Simple question - I've got a bucketload of cruddy html pages to clean up
Simple question that keeps bugging me. Should I HTML encode user input right away

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.