当前位置: 首页 > 知识库问答 >
问题:

Pygame Zero:创建多个关卡

郎吉星
2023-03-14

我用Python和PygameZero创建了一个游戏。当用户达到200点时,屏幕会出现一个按钮。当用户单击按钮(下一个级别)时,应该会出现一个新的级别。我尝试用gamemode=3和gamestart=1来管理它(见代码)。但是当用户单击“下一个级别”时,什么都不会发生。那么,我如何添加多层次?

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()

共有1个答案

路奇
2023-03-14

我成功了。在endoflevel1函数中,有必要使用pygame事件处理程序来检查鼠标按钮是否被单击,以及是否与“下一级”图形的位置相匹配。这是正在运行的endoflevel1函数的代码。在endoflevel1功能中,不可能像在鼠标按下(pos)功能中那样使用“简易方式”来检查鼠标按钮是否被点击并与指定的图形/演员匹配:

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
 类似资料:
  • 如果不检查If语句中的myRange值,它不会给出错误,但不会创建pendingIntent2和PendingIntent3。 我尝试发送不同的请求代码,但它没有工作。

  • 问题内容: 我想用Python(不是数组)中的一个创建10个变量。像这样: 我想创建的变量名,在这种情况下: ,,,…, 我不想一个数组 我有一个坐标图(640 x 480)。我正在确定像素颜色为白色的坐标。地面上有3个球,我想获取每个球的中心坐标,因此,如果要评估的坐标距离最后一个球很近,则x和y坐标将被平均(如果该坐标属于a)新球的坐标将属于新坐标组。 问题答案: 您不应该这样做,但是如果您真

  • 我需要创建一个学生管理系统,它可以帮助多个老师教多个学生,多个学生可以有多个老师。现在我已经在下面创建了一个代码。另外,如果你认为有更好的方法来实现我的目标,请指导,我对多对多的关系是新的,互联网上有这么多方法,这只是让人困惑: 错误是:

  • 问题内容: 我需要一次创建多个表。我很难找出正确的方法来完成此任务。目前,我的脚本如下所示: 显然,这是行不通的,并且不会创建任何表。有一种简单的方法可以一次创建多个表吗? 问题答案: MySQL变得令人困惑,因为您没有划定查询范围。在第一条语句后添加分号: 另外,根据Heredoc文档,请确保位于行的开头, 没有其他字符,除了分号外 。 鉴于上述方法似乎无效,请尝试以下代码: 您 可以 使用(M

  • 我知道在最新版本的Mongoose中,您可以将多个文档传递给create方法,在我的例子中,甚至可以传递一个文档数组。 我的问题是数组的大小是动态的,所以在回调中创建一个对象数组会很有帮助。 文档中没有,但这样做可能吗?

  • 所以我不确定是0还是2。 如果有人知道请告诉我。