2

比我更有经验的人能给我一个线索,说明为什么下面的代码会冻结(挂起)窗口吗?它应该做的就是显示一个红色屏幕并在窗口名称中更新其 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()
4

1 回答 1

0

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

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

  1. 在初始化代码中移动runGame减速并使其self.runGame
  2. 添加调用 update 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()
于 2012-12-18T02:47:00.727 回答