Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame简单的程序来测试显示是否冻结_Python_Pygame - Fatal编程技术网

Python Pygame简单的程序来测试显示是否冻结

Python Pygame简单的程序来测试显示是否冻结,python,pygame,Python,Pygame,有没有比我更有经验的人能告诉我为什么下面的代码会冻结(挂起)窗口?它应该做的就是显示一个红色屏幕,并在窗口名中更新其FPS。谢谢!:) 在游戏类中,您将runGame声明在\uuuu init\uuuu函数之外,并且不将self.runGame用于减速。您还仅在首次创建游戏对象时更新FPS,因此它将保持相同的值 通过以下方式解决此问题: 在初始化代码内移动runGame减速,并使其self.runGame 添加一个更新self.fps的调用。我在draw函数下添加了它 因此,您的结束代码应该如下

有没有比我更有经验的人能告诉我为什么下面的代码会冻结(挂起)窗口?它应该做的就是显示一个红色屏幕,并在窗口名中更新其FPS。谢谢!:)


在游戏类中,您将
runGame
声明在
\uuuu init\uuuu
函数之外,并且不将
self.runGame
用于减速。您还仅在首次创建游戏对象时更新FPS,因此它将保持相同的值

通过以下方式解决此问题:

  • 在初始化代码内移动
    runGame
    减速,并使其
    self.runGame
  • 添加一个更新
    self.fps
    的调用。我在
    draw
    函数下添加了它
  • 因此,您的结束代码应该如下所示(注释包含在我更改内容的地方):


    我做了你的编辑,谢谢你,这种方式更加一致,但窗口对我来说仍然冻结。因为我必须强制退出,它根本不允许我移动窗口。这可能是因为你没有任何事件处理,但我不是100%同意,嗯,可能就是这样。我要拍一些进去看看!。。。就这样。谢谢Deachex!让我知道它是如何进行的,我在这里帮助更新了一些事件代码的OP,现在可以工作了!谢谢你的帮助
    import pygame
    from pygame.locals import *
    from pygame import Color
    
    
    class Game():
        """ Lets try to get this going by simple steps
        One by one. First step, lets figure how to make a class
        that can do the display stuff. Lord have mercy on my soul"""
    
        runGame = True
    
        def __init__(self, wi=256, hi=224, multii=3):
            """Initialization"""
            pygame.init()
            self.width      = wi*multii
            self.height     = hi*multii
            self.spritesize = 16*multii
            self.clock      = pygame.time.Clock()
            self.fps        = self.clock.get_fps()
            self.screen     = pygame.display.set_mode((self.width, self.height))
    
        def mainLoop(self):
            """Loop through the main game routines
            1. Drawing  2. Input handling  3. Updating
            Then loop through it until user quits"""
            while self.runGame:
                self.clock.tick(12)
                self.draw()
                self.events()
    
        def events(self):
            """Time to handle some events"""
            events = pygame.event.get()
            for e in events:
                print e
                if (e.type == pygame.QUIT) or 
                (e.type == KEYDOWN and e.key == K_ESCAPE):
                    self.runGame = False
    
        def draw(self):
            """Draw and update the main screen"""
            self.screen.fill(Color('red'))
            pygame.display.set_caption('Grid2. FPS: '+str(self.fps))
            pygame.display.update()
    
    
    game = Game()
    game.mainLoop()
    
    import pygame
    from pygame.locals import *
    from pygame import Color
    
    
    class Game():
        """ Lets try to get this going by simple steps
        One by one. First step, lets figure how to make a class
        that can do the display stuff. Lord have mercy on my soul"""
    
    
    
    def __init__(self, wi=256, hi=224, multii=3):
        """Initialization"""
        pygame.init()
        self.width      = wi*multii
        self.height     = hi*multii
        self.spritesize = 16*multii
        self.clock      = pygame.time.Clock()
        self.fps        = self.clock.get_fps()
        self.screen     = pygame.display.set_mode((self.width, self.height))
        self.runGame = True # I've moved the runGame decleration
    
    def mainLoop(self):
        """Loop through the main game routines
        1. Drawing  2. Input handling  3. Updating
        Then loop through it until user quits"""
        while self.runGame:
            self.clock.tick(12)
            self.draw()
            self.event()
    
    def events(self):
        """Time to handle some events"""
        events = pygame.event.get()
        for e in events:
            print e
            if (e.type == pygame.QUIT) or 
            (e.type == KEYDOWN and e.key == K_ESCAPE):
                self.runGame = False
    
    
    def draw(self):
        """Draw and update the main screen"""
        self.screen.fill(Color('red'))
        self.fps = self.clock.get_fps() # I reupdate the FPS counter
        pygame.display.set_caption('Grid2. FPS: '+str(self.fps))
        pygame.display.update()
    
    
    game = Game()
    game.mainLoop()