[英]How to make pygame update in real time?
我正在嘗試使用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會自動更新以顯示字段和記分板。
它非常接近工作。
然而,一些循環條件並未正確調用。 我不得不發明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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.