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()
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:Import needed libraries
Initialize the game-screen
Declare a sprite which can be displayed on the screen
Declare the main method and setup the game-screen background
Add the above defined sprite to the screen and move it to the position of the mouse-cursor
Make mouse cursor invisible and activate events
Run mainloop. The call of this method says:
Run me( functionmain)over and over!Call main for the first time
In the second example, there is no game-loop. The flow of the application (tighter packed) is like this:
Import libraries, initialize game-screen, declare a sprite
Setup game-screen background and add sprite
Deactivate mouse cursor, enable events
Run mainloop. The call of this method says:
Run me( functionundefined)over and over!And here is the sticking point! You cannot call code that is in the root of a Python-file! The
mainloopfunction 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!