我该怎么做才能在按键时进入新场景? (Pygame)

How do I make it so it goes to a new scene on keypress? (Pygame)

我是新手,我似乎找不到任何关于如何执行此操作的教程。我想这会很简单。我正在创建一个 Oregon Trail 类型的游戏,我需要它在你按 E 时转到下一张图片,或者在你按 Q 时退出程序。

这是我的代码:

# 1 - Import library
import pygame
from pygame.locals import *

# 2 - Initialize the game
pygame.init()
width, height = 1000, 800
screen=pygame.display.set_mode((width, height))

# 3 - Load images
background = pygame.image.load("start.png")

# 4 - keep looping through
while 1:
    # 5 - clear the screen before drawing it again
    screen.fill(0)
    # 6 - draw the screen elements
    screen.blit(background, (0,0))
    # 7 - update the screen
    pygame.display.flip()
    # 8 - loop through the events
    for event in pygame.event.get():
        # check if the event is the X button 
        if event.type==pygame.QUIT:
            # if it is quit the game
            pygame.quit() 
            exit(0)

我想你正在寻找这样的东西:

# 1 - Import library
import pygame
from pygame.locals import *

# 2 - Initialize the game
pygame.init()
width, height = 1000, 800
screen=pygame.display.set_mode((width, height))

# 3 - Load images
background = pygame.image.load("start.png")

# 4 - keep looping through
while 1:
    # 5 - clear the screen before drawing it again
    screen.fill(0)
    # 6 - draw the screen elements
    screen.blit(background, (0,0))
    # 7 - update the screen
    pygame.display.flip()
    # 8 - loop through the events
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_e:
                background = pygame.image.load("stage2.png")
        # check if the event is the X button 
        if event.type==pygame.QUIT:
            # if it is quit the game
            pygame.quit() 
            exit(0)

当然这只适用于 2 张背景图片 "start.png" 和 stage2.png”。但是你可以通过它创建一个列表和圆圈。