Pygame 零:创建多个级别

Pygame Zero: Creating multiple level

我用 Python 和 Pygame 零创建了一个游戏。当用户达到 200 点时,会出现一个带有按钮的屏幕。当用户单击按钮(下一级)时,应出现一个新级别。我尝试使用 gamemode = 3 和 gamestart = 1 来管理它(参见代码)。但是当用户点击 "NEXT LEVEL" 时什么也没有发生。那么,我怎样才能添加多个级别?

from random import randint
import pygame

WIDTH   = 800
HEIGHT  = 800

apple = Actor("apple")
apple.pos = randint(0, 800), randint(-800, 0)

pear = Actor("pear")
pear.pos = randint(0, 800), randint(-800, 0)

plum = Actor("plum")
plum.pos = randint(0, 800), randint(-800, 0)

donut = Actor("donut")
donut.pos = randint(0, 800), randint(-800, 0)

ice = Actor("ice")
ice.pos = randint(0, 800), randint(-800, 0)

chips = Actor("chips")
chips.pos = randint(0, 800), randint(-800, 0)

happysmiley = Actor("happysmiley")
happysmiley.pos = 300, 750

start = Actor("start")
start.pos = 700,750

quitgame = Actor("quit")
quitgame.pos = 600, 750

creditsinfo = Actor("credits")
creditsinfo.pos = 485, 750

back = Actor("back")
back.pos = 600, 100

nextlevel = Actor("nextlevel")
nextlevel.pos = 700, 750

gameover = False
score = 0
gamemode = 1
gamestart = 0

background = pygame.image.load("images\background.png")

pygame.mixer.init()
pygame.mixer.music.load("music\funmusic.mp3")
pygame.mixer.music.play(-1)

def draw():
    screen.clear()
    if score >= 200:
        endoflevel1()
    else:
        drawgame()

def drawgame():
    global gamemode

    if gamemode == 1:
        screen.clear()
        screen.blit("background",(0,0))
        apple.draw()
        pear.draw()
        plum.draw()
        donut.draw()
        ice.draw()
        chips.draw()
        quitgame.draw()
        happysmiley.draw()
        start.draw()
        creditsinfo.draw()
        screen.draw.text("Punkte: " + str(score), (700, 5), color = "black")

    elif gamemode == 0:
        screen.clear()
        screen.blit("background",(0,0))     
        apple.draw()
        pear.draw()
        plum.draw()
        donut.draw()
        ice.draw()
        chips.draw()
        happysmiley.draw()
        screen.draw.text("Punkte: " + str(score), (700, 5), color = "black")

    elif gamemode == 2:
        screen.clear()
        screen.fill("orange")
        back.draw()
        screen.draw.text("Credits", topleft=(350,10), fontsize=40)
        pygame.display.flip()

    elif gamemode == 3:
        screen.clear()
        screen.blit("background",(0,0))     
        apple.draw()
        pear.draw()
        plum.draw()
        donut.draw()
        ice.draw()
        chips.draw()
        happysmiley.draw()
        screen.draw.text("Punkte: " + str(score), (700, 5), color = "black")

def update():
    global score, gamestart

    if gamestart == 1:

        if score >= 200:
            pygame.mixer.music.stop()
            return

        if apple.y < 800:
            apple.y = apple.y + 4
        else:
            apple.x = randint(0, 800)
            apple.y = randint(-800, 0)

        if pear.y < 800:
            pear.y = pear.y + 4
        else:
            pear.x = randint(0, 800)
            pear.y = randint(-800, 0)

        if plum.y < 800:
            plum.y = plum.y + 4
        else:
            plum.x = randint(0, 800)
            plum.y = randint(-800, 0)

        if donut.y < 800:
            donut.y = donut.y + 4
        else:
            donut.x = randint(0, 800)
            donut.y = randint(-800, 0)

        if ice.y < 800:
            ice.y = ice.y + 4
        else:
            ice.x = randint(0, 800)
            ice.y = randint(-800, 0)

        if chips.y < 800:
            chips.y = chips.y + 4
        else:
            chips.x = randint(0, 800)
            chips.y = randint(-800, 0)

        if keyboard.left:
            happysmiley.x = happysmiley.x - 5
        elif keyboard.right:
            happysmiley.x = happysmiley.x + 5

        if happysmiley.collidepoint (apple.x, apple.y):
            score = score + 2
            effect = pygame.mixer.Sound("sounds\bonus.wav")
            effect.play()
        if happysmiley.collidepoint (pear.x, pear.y):
            score = score + 1
            effect = pygame.mixer.Sound("sounds\bonus.wav")
            effect.play()
        if happysmiley.collidepoint (plum.x, plum.y):
            score = score + 1
            effect = pygame.mixer.Sound("sounds\bonus.wav")
            effect.play()
        if happysmiley.collidepoint (donut.x, donut.y):
            score = score - 1
            effect = pygame.mixer.Sound("sounds\no.wav")
            effect.play()
        if happysmiley.collidepoint (ice.x, ice.y):
            score = score - 1
            effect = pygame.mixer.Sound("sounds\no.wav")
            effect.play()
        if happysmiley.collidepoint (chips.x, chips.y):
            score = score - 1
            effect = pygame.mixer.Sound("sounds\no.wav")
            effect.play()


def on_mouse_down(pos): # wurde Maustaste gedrückt?
    global score, gamestart, gamemode

    if start.collidepoint(pos):
        gamestart = 1
        gamemode = 0
    if quitgame.collidepoint(pos):
        exit()
    if creditsinfo.collidepoint(pos):
        gamemode = 2
    if  back.collidepoint(pos):
        gamemode = 1


def endoflevel1():
    global score, gamemode, gamestart
    screen.clear()    
    screen.fill("green")
    screen.draw.text("Congratulations! You have successfully completed the 1st level!", topleft=(100,350), fontsize=30, color = "black")
    nextlevel.draw()
    if  nextlevel.collidepoint:
        gamemode = 3
        gamestart = 1
    pygame.display.flip()

我做到了。在 endoflevel1 函数中,需要使用 pygame 事件处理程序来检查鼠标按钮是否被单击以及是否与 "NEXT LEVEL" 图形的位置匹配。这是现在正在运行的 endoflevel1 函数的代码。在 endoflevel1 函数中,不可能像在 on_mouse_down(pos) 函数中那样使用 "easy way" 来检查鼠标按钮是否被单击并与指定的 graphic/actor 匹配:

def endoflevel1():
global score, gamemode, gamestart, level
screen.clear()
screen.fill("green")
screen.draw.text("Congratulations! You have successfully completed the 1st level!", topleft=(100,350), fontsize=30, color = "black")
nextlevel.draw()  
gamestart = 0
pygame.display.flip()
running = True
while (running):
    for event in pygame.event.get():  
        if event.type == pygame.MOUSEBUTTONDOWN:
            if  nextlevel.collidepoint(event.pos):
                score = 0
                gamemode = 3
                gamestart = 1
                level = 2
                running = False