如何实时更新pygame?

时间:2019-08-01 19:49:20

标签: python pygame

我正在尝试使用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自动更新以显示该字段和记分板。

1 个答案:

答案 0 :(得分: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()