Pygame Zero:创建多个

2024-04-28 06:17:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python和Pygame Zero创建了一个游戏。当用户达到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()

Tags: posappleifscreenpygamescorepeardraw
1条回答
网友
1楼 · 发布于 2024-04-28 06:17:37

我成功了。在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

相关问题 更多 >