【问题标题】:How to make pygame update in real time?如何让pygame实时更新?
【发布时间】:2019-08-01 19:49:20
【问题描述】:

我正在尝试使用 Pygame 1.9.4 在 Python 3.6.5 中制作棒球主题游戏。我可以显示欢迎屏幕,但除非我退出程序,否则我无法使用 runGame() 函数(它使用 while True: 循环)来显示字段和记分牌。游戏远未完成,但我决定在实施游戏机制之前解决这个问题。

我把pygame.display.update() 放在我能想到的任何地方。在我用 Python 2 编写的旧版无限循环游戏中,我已经让 pygame 实时更新。

import pygame, sys
from pygame.locals import *

FPS=15

#Main function
def main():
    global FPSCLOCK,DISPLAYSURF,BASICFONT
    pygame.init()
    FPSCLOCK=pygame.time.Clock()
    DISPLAYSURF=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
    BASICFONT=pygame.font.Font('freesansbold.ttf',18)
    pygame.display.set_caption('Baseball')
    showStartScreen()
    while True:
        runGame()
        showGameOverScreen()

#Shows welcome menu
def showStartScreen():
    titleFont=pygame.font.Font('freesansbold.ttf',100)
    titleSurf=titleFont.render('BASEBALL',True,WHITE,GREEN)
    titleRect=titleSurf.get_rect()
    titleRect.center=(WINDOWWIDTH/2,WINDOWHEIGHT/2)
    DISPLAYSURF.fill(BROWN)
    DISPLAYSURF.blit(titleSurf, titleRect)
    pygame.display.update()
    while True:
        if checkForKeyPress():
            pygame.event.get()
            return

#Main loop for game
def runGame():
    balls=0
    strikes=0
    outs=0
    drawField()
    pygame.display.flip()
    while True:
        drawScoreboard(balls, strikes, outs)
        pygame.display.update()

if __name__=='__main__':
    main()

当我按下一个键开始游戏时,pygame 只显示欢迎屏幕。当我强制退出程序时,pygame 会自动更新以显示字段和记分牌。

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    它已经很接近工作了。

    但是,一些循环条件没有正确调用事物。我不得不发明checkForKeyPress() 和其他功能,因为你没有包括它们——也许这些有问题?当用户想要关闭窗口时,代码需要对每个pygame.QUIT 事件进行特殊处理。用户不想等待关闭程序!

    有时未处理退出,我认为这就是您看到报告的显示更新行为的原因。

    runGame() 也需要处理用户输入,尤其是这个出口。

    import pygame, sys, time
    from pygame.locals import *
    
    WINDOWWIDTH,WINDOWHEIGHT = 800,800
    WHITE=(255,255,255)
    GREEN=(0,200,0)
    BROWN=(164,113,24)
    
    FPS=15
    
    def checkForKeyPress():
        while ( True ):
            for event in pygame.event.get():
                if ( event.type == pygame.QUIT ):
                    pygame.event.post( event ) # re-post the quit event to handle later
                    return False
                # Any keyboard press, or mouse-click
                elif ( event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN ):
                    return True
    
    def drawField():
        global FPSCLOCK,DISPLAYSURF,BASICFONT
        DISPLAYSURF.fill(GREEN)
    
    def drawScoreboard(balls, strikes, outs):
        pass
    
    def showGameOverScreen():
        pass
    
    #Main function
    def main():
        global FPSCLOCK,DISPLAYSURF,BASICFONT
        pygame.init()
        FPSCLOCK=pygame.time.Clock()
        DISPLAYSURF=pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT))
        BASICFONT=pygame.font.Font('freesansbold.ttf',18)
        pygame.display.set_caption('Baseball')
        showStartScreen()
        while True:
            if ( runGame() == False ):
                break
            showGameOverScreen()
        pygame.quit()
    
    #Shows welcome menu
    def showStartScreen():
        titleFont=pygame.font.Font('freesansbold.ttf',100)
        titleSurf=titleFont.render('BASEBALL',True,WHITE,GREEN)
        titleRect=titleSurf.get_rect()
        titleRect.center=(WINDOWWIDTH/2,WINDOWHEIGHT/2)
        DISPLAYSURF.fill(BROWN)
        DISPLAYSURF.blit(titleSurf, titleRect)
        pygame.display.update()
        checkForKeyPress()
        print("showStartScreen() returns")
    
    #Main loop for game
    def runGame():
        global FPSCLOCK,DISPLAYSURF,BASICFONT
        balls=0
        strikes=0
        outs=0
        print("runGame() starts")
        while True:
            drawField()
            drawScoreboard(balls, strikes, outs)
    
            # Handle user-input
            for event in pygame.event.get():
                if ( event.type == pygame.QUIT ):
                    return False # user wants to exit the program
    
            # Movement keys
            keys = pygame.key.get_pressed()
            if ( keys[pygame.K_UP] ):
                print("up")
            elif ( keys[pygame.K_DOWN] ):
                print("down")
            # elif ( ...
    
            pygame.display.flip()
            pygame.display.update()
    
            # Clamp FPS
            FPSCLOCK.tick_busy_loop(60)
    
        return True  # Game Over, but not exiting program
    
    
    
    if __name__=='__main__':
        main()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-04
      • 2023-03-15
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多