【发布时间】: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 会自动更新以显示字段和记分牌。
【问题讨论】: