Python 我如何使它在按键时进入一个新的场景?(Pygame)

Python 我如何使它在按键时进入一个新的场景?(Pygame),python,python-2.7,pygame,scene,Python,Python 2.7,Pygame,Scene,我是一个新手,我似乎找不到任何关于如何做到这一点的教程。我想这会很简单。我正在创建一个俄勒冈州步道类型的游戏,我需要它转到下一张图片时,你按下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_m

我是一个新手,我似乎找不到任何关于如何做到这一点的教程。我想这会很简单。我正在创建一个俄勒冈州步道类型的游戏,我需要它转到下一张图片时,你按下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)

当然,这只适用于背景“start.png”和stage2.png的两幅图像“。但是你可以创建一个列表并通过它循环。

好吧,这看起来很简单,但我不知道如何让它改变场景。你确定在这方面,纹理UI并不比Pygame更好吗?事实上,我的生成器函数更适合这一点。他可以使用一个函数,每次使用
next()
调用列表时,该函数都会从列表中生成一个项。这个函数可以工作!你知道我怎么做才能让你有一个特定的图像作为按键的背景吗?@Nick在指定背景变量之前添加一个if语句。正如我所说,跟踪当前背景的最简单方法是在列表中设置它,然后使用当前显示背景的索引保留一个变量,因此,if的条件可以基于该索引。这是一个非常简单的方法。祝你好运。@kstenger好的,如果你或任何人能再帮我一件事,那就太好了!我似乎无法获得正确的if语句:if event.key==pygame.K_e:if image==(“start.png”):background=pygame.image.load(“blahblah.png”)@Nick编辑您的原始问题并添加您正在尝试的代码