Search code examples
pythonpygamepgzero

Pygame Zero: Creating multiple level


I created a game with Python and Pygame Zero. When the user reaches 200 points a screen appear with a button appear. When the user clicks the button (next level) a new level should appear. I try to managed this with gamemode = 3 and gamestart = 1 (see code). But when the user clicks "NEXT LEVEL" nothing happens. So, how can i add multiple 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()

Solution

  • I made it. In the endoflevel1 function it was neccessary to use the pygame event handlers to check if the mouse button is clicked and matches the position with the "NEXT LEVEL" graphic. This is the code of endoflevel1 function that is working now. In the endoflevel1 function it is not possible to go the "easy way" like in the on_mouse_down(pos) function to check if the mousebutton is clicked and matches with a designated 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