【问题标题】:Pygame simple program to test displaying is freezing用于测试显示的 Pygame 简单程序冻结
【发布时间】:2014-01-02 12:31:25
【问题描述】:

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

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

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    在 Game 类中,您在 __init__ 函数之外声明 runGame,并且您没有将 self.runGame 用于减速。您也只在第一次创建 Game 对象时更新 FPS,因此它将保持相同的值。

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

    1. 在初始化代码中移动runGame减速并使其成为self.runGame
    2. 添加调用以更新self.fps。我已经在draw 函数下添加了它

    所以你的结束代码应该是这样的(我改变的地方包括注释):

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

    【讨论】:

    • 我做了你的编辑,谢谢你,这样更一致,但窗口对我来说仍然冻结。在那我必须强制退出它,它根本不会让我移动窗口。它只是保持冻结状态。
    • 这可能是因为你没有任何事件处理,但我不是 100% 的
    • 嗯,可能就是这样。我会拍一些进去看看!......就是这样。感谢 DeadChex!
    • 告诉我进展如何,我是来帮忙的
    • 用一些事件代码更新了 OP,现在可以使用了!感谢您的帮助
    猜你喜欢
    • 2015-10-20
    • 2023-04-06
    • 2016-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多