2

一个简单的问题:为什么第一个代码可以工作,但看似相同的第二个代码在 pygame 窗口出现时冻结了?

# 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()

故障二:

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()
4

1 回答 1

2

我从未使用过 livewires,但在游戏中,您通常需要一个或多或少无休止的游戏循环

游戏循环背后的意义是,您总是想知道鼠标在哪里或按下了哪些键,而不仅仅是一次!所以你必须Where is the mouse?一遍又一遍地问。为了实现这一点,您使用一个循环来检查您每次执行时想要的所有内容。

在第一个示例中,游戏循环是函数main。申请流程是这样的:

  1. 导入需要的库

    from livewires import games
    
  2. 初始化游戏画面

    games.init(screen_width = 640, screen_height = 480, fps = 50)
    
  3. 声明一个可以在屏幕上显示的精灵

    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. 声明 main 方法并设置游戏画面背景

    def main():
        wall_image = games.load_image("wall.jpg", transparent = False)
        games.screen.background = wall_image
    
  5. 将上面定义的精灵添加到屏幕并将其移动到鼠标光标的位置

        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. 使鼠标光标不可见并激活事件

        games.mouse.is_visible = False
        games.screen.event_grab = True
    
  7. 运行主循环。这个方法的调用说:Run me( functionmain)over and over!

        games.screen.mainloop()
    
  8. 第一次调用main

    main()
    

在第二个示例中,没有游戏循环。应用程序的流程(更紧凑)是这样的:

  1. 导入库,初始化游戏画面,声明精灵

    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. 设置游戏屏幕背景并添加精灵

    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. 停用鼠标光标,启用事件

    games.mouse.is_visible = False
    myscr.event_grab = True
    
  4. 运行主循环。这个方法的调用说:Run me( functionundefined)over and over!

    myscr.mainloop()
    

这是症结所在!您不能调用 Python 文件根目录中的代码!mainloop函数不知道从哪里返回或从哪里开始。呼叫丢失,您的程序冻结。游戏画面无法更新,因为没有告诉它应该如何更新。

结论:你的游戏循环必须有一个函数!

于 2011-06-24T14:38:57.467 回答