开发者

Debugging sample Pygame code?

A simple question: Why does this the first code work but the seemingly identical se开发者_如何学Ccond 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:

  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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜