如何制作它以便在按键上进入新场景? (pygame的)

时间:2015-04-28 23:45:29

标签: python python-2.7 pygame scene

我很新,我似乎无法找到有关如何执行此操作的任何教程。我想这会很简单。我正在创建一个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 个答案:

答案 0 :(得分: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“。但你可以通过它创建一个列表和cicle。