如何在 pygame 返回主页?
How to go back to main page in pygame?
我有一个 Pygame 程序,它有一个 'Home' 屏幕,该屏幕有 5 个按钮,按下这些按钮会调用一个新功能,为每个不同的按钮打开一个不同的屏幕。对于这些屏幕中的每一个,我都在右上角添加了一个小 'Home' 按钮,以便用户可以从任何调用的屏幕返回到 'Home' 屏幕。
import pygame
window = pygame.display.set_mode((1500, 800), pygame.RESIZABLE)
def common_screen():
button("Home", 1400, 0, 100, 50, (0,200,0), (255,255,210), home_intro) **#Problem at this point**
*#do something else too*
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()
def button(msg, x, y, w, h, ic, ac, action):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(window, ac, (x, y, w, h))
if click[0] == 1:
action()
else:
pygame.draw.rect(window, ic, (x, y, w, h))
smallText = pygame.font.SysFont(None, 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)), (y+(h/2)))
window.blit(textSurf, textRect)
def home_intro():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(image, (0,0))
pygame.display.set_caption("PROGRAM")
button("Start", 150, 450, 100, 50, (0,200,0), (255,255,210), start)
button("Screen 1", 150, 450, 100, 50, (0,200,0), (255,255,210), screen1)
button("Screen 2", 150, 450, 100, 50, (0,200,0), (255,255,210), screen2)
button("Stop", 550, 450, 100, 50, (0,200,0), (255,255,210), stop)
if game_state == 'screen1':
common_screen()
pygame.display.flip()
home_intro()
pygame.quit()
quit()
common_screen() 函数在右上角创建了一个 'Home' 按钮,但是如果我使用 home_intro() 作为其中 button() 函数的最后一个参数,它会导致递归错误。每次回到主页面都要调用main函数,好像不对。创建新函数没有意义,因为它与 home_intro() 函数相同。
不要递归调用home_intro
,而是添加并调用一个改变game_state
变量的函数:
def home():
global game_state
game_state = "home"
def common_screen():
button("Home", 1400, 0, 100, 50, (0,200,0), (255,255,210), home)
def home_intro():
global game_state
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(image, (0,0))
pygame.display.set_caption("PROGRAM")
button("Start", 150, 450, 100, 50, (0,200,0), (255,255,210), start)
button("Screen 1", 150, 450, 100, 50, (0,200,0), (255,255,210), screen1)
button("Screen 2", 150, 450, 100, 50, (0,200,0), (255,255,210), screen2)
button("Stop", 550, 450, 100, 50, (0,200,0), (255,255,210), stop)
if game_state == 'screen1':
common_screen()
pygame.display.flip()
我有一个 Pygame 程序,它有一个 'Home' 屏幕,该屏幕有 5 个按钮,按下这些按钮会调用一个新功能,为每个不同的按钮打开一个不同的屏幕。对于这些屏幕中的每一个,我都在右上角添加了一个小 'Home' 按钮,以便用户可以从任何调用的屏幕返回到 'Home' 屏幕。
import pygame
window = pygame.display.set_mode((1500, 800), pygame.RESIZABLE)
def common_screen():
button("Home", 1400, 0, 100, 50, (0,200,0), (255,255,210), home_intro) **#Problem at this point**
*#do something else too*
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()
def button(msg, x, y, w, h, ic, ac, action):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(window, ac, (x, y, w, h))
if click[0] == 1:
action()
else:
pygame.draw.rect(window, ic, (x, y, w, h))
smallText = pygame.font.SysFont(None, 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)), (y+(h/2)))
window.blit(textSurf, textRect)
def home_intro():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(image, (0,0))
pygame.display.set_caption("PROGRAM")
button("Start", 150, 450, 100, 50, (0,200,0), (255,255,210), start)
button("Screen 1", 150, 450, 100, 50, (0,200,0), (255,255,210), screen1)
button("Screen 2", 150, 450, 100, 50, (0,200,0), (255,255,210), screen2)
button("Stop", 550, 450, 100, 50, (0,200,0), (255,255,210), stop)
if game_state == 'screen1':
common_screen()
pygame.display.flip()
home_intro()
pygame.quit()
quit()
common_screen() 函数在右上角创建了一个 'Home' 按钮,但是如果我使用 home_intro() 作为其中 button() 函数的最后一个参数,它会导致递归错误。每次回到主页面都要调用main函数,好像不对。创建新函数没有意义,因为它与 home_intro() 函数相同。
不要递归调用home_intro
,而是添加并调用一个改变game_state
变量的函数:
def home():
global game_state
game_state = "home"
def common_screen():
button("Home", 1400, 0, 100, 50, (0,200,0), (255,255,210), home)
def home_intro():
global game_state
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.blit(image, (0,0))
pygame.display.set_caption("PROGRAM")
button("Start", 150, 450, 100, 50, (0,200,0), (255,255,210), start)
button("Screen 1", 150, 450, 100, 50, (0,200,0), (255,255,210), screen1)
button("Screen 2", 150, 450, 100, 50, (0,200,0), (255,255,210), screen2)
button("Stop", 550, 450, 100, 50, (0,200,0), (255,255,210), stop)
if game_state == 'screen1':
common_screen()
pygame.display.flip()