【问题标题】:Debugging sample Pygame code?调试示例 Pygame 代码?
【发布时间】:2011-09-22 01:18:13
【问题描述】:

一个简单的问题:为什么第一个代码可以工作,但看似相同的第二个代码在 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()

【问题讨论】:

    标签: python debugging pygame sample livewires


    【解决方案1】:

    我从未使用过 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 函数不知道从哪里返回或从哪里开始。呼叫丢失,您的程序冻结。游戏画面无法更新,因为没有告诉它应该如何更新。

    结论:必须拥有一个用于您的游戏循环的函数!

    【讨论】:

    • 很好的答案,但是为什么第二段代码中的 mainloop() 没有做同样的事情?抱歉,如果这是一个愚蠢的问题,我是新手。
    • 您的问题是完全足够的,应该得到回答,这样您才能获得比您带来的更多的知识。也就是说,这就是为什么mainloop() 在第二个示例中不起作用的原因。 mainloop 所做的,或者我期望它做的更好(我没有找到关于它的任何文档),是再次运行之前调用它的函数,如果没有其他情况阻止它这样做(关闭应用程序等)。如果您现在从任何函数外部调用mainloop,则没有任何mainloop 可以回调。我试图改进我的答案以使其更具解释性!
    • 我把我的答案更新了一篇好文章,希望你能得到线索!
    • 哇,答案很好,内容丰富,写得很好!非常感谢。
    猜你喜欢
    • 2011-09-28
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    相关资源
    最近更新 更多