我試圖在我的游戲 SpaceInvaders 中添加一個暫停選單,并在后臺顯示游戲的最后一幀,但是當我加載暫停場景時,我的游戲沒有在后臺顯示游戲
背景游戲看起來像這樣
我第一次加載暫停它顯示這個
然后每當我加載暫停它的行為通常是這樣的
我的游戲有 3 個檔案:
1)游戲檔案
2)暫停檔案
3)init檔案(連接以上兩個)
游戲檔案
import pygame
from __init__ import __INIT__
pygame.display.set_caption("Endless")
pygame.init()
screen=pygame.display.set_mode((1920/2,1080/2),pygame.RESIZABLE)
def menu():
running=True
while running:
screen.blit(pygame.image.load("docs/bg.png"),(0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
__INIT__("pause")
pygame.display.update()
menu()
暫停檔案
import pygame
from __init__ import __INIT__
pygame.display.set_caption("Pause")
pygame.init()
screen=pygame.display.set_mode((1920/2,1080/2),pygame.RESIZABLE)
def PAUSE():
running=True
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
__INIT__("menu")
if event.key == pygame.K_h:
from __init__ import hello
hello()
screen.blit(pygame.image.load("docs/D_effect.png"),(0,0))
pygame.display.update()
PAUSE()
初始化檔案
def __INIT__(a):
if a=="pause":
from pause import PAUSE
PAUSE()
if a=="menu":
from endless import menu
menu()
我不知道是什么導致了這個問題,因為它只在我是初學者的時候出現過這可能是一個愚蠢的錯誤如果發生這種情況我很抱歉
無論如何,如果您發現我的問題難以理解,請運行此處提供的 Space_invaders/Space_invaders/endless.py 中的無盡檔案 https://drive.google.com/file/d/1xkSKcptJpuY9mZhQtl6mwU1Hh3p2L5j4/view?usp=sharing
uj5u.com熱心網友回復:
問題是遞回。menu電話PAUSE,PAUSE電話menu:
menu
|
-> PAUSE
|
-> menu
|
-> PAUSE
|
...
您根本不需要 PAUSE 的額外應用程式回圈。只需添加使用game_state變數。例如:
import pygame
pygame.display.set_caption("Endless")
pygame.init()
screen=pygame.display.set_mode((1920/2,1080/2),pygame.RESIZABLE)
def menu():
bg = pygame.image.load("docs/bg.png")
bg_pause = pygame.image.load("docs/D_effect.png")
game_state = 'running'
while game_state != 'quit':
if game_state == 'pause':
screen.blit(bg_pause, (0,0))
else:
screen.blit(bg, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_state = 'quit'
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
if game_state == 'running':
game_state = 'pause'
else:
game_state = 'running'
if game_state == 'running':
# draw game scene
# [...]
pass
pygame.display.update()
pygame.quit()
quit()
menu()
不要在每一幀中加載背景影像。這導致滯后。pygame.image.load這是一個非常耗時的程序,必須從卷中加載影像檔案并進行解碼。在應用程式回圈之前加載影像并在回圈中使用它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/396535.html
下一篇:在熊貓資料框中選擇具有最小值的列
