使用 Python 创建游戏:如何添加游戏菜单?

Creating Game with Python: How to add game menu?

我制作了一个小游戏。我想添加一个小菜单(START、QUIT、CREDITS)。当用户点击开始时,菜单应该消失,游戏应该开始。当用户点击 CREDITS 时,应该会出现一个带有学分的新屏幕(见屏幕截图)。我试过了,但我真的不知道如何添加这个。现在只有 "QUIT" 按钮可以使用了。这是我的代码:

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

gameover = False
score = 0
gamemode = 1

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 >= 100:
        endoflevel1()
    else:
        drawgame()

def drawgame():
    global game_mode
    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 game_mode == 2: 
        screen.clear()
        screen.fill("orange")       
        screen.draw.text("Credits", topleft=(10,10), fontsize=40)     
        pygame.display.flip()    


def update():
    global score

    if score >= 100:
        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.y, chips.y):
        score = score - 1  
        effect = pygame.mixer.Sound("sounds\no.wav")
        effect.play()

    if score >= 100:
        endoflevel1()

def on_mouse_down(pos): # wurde Maustaste gedrückt?
    global score       
    global game_mode   
    if start.collidepoint(pos):
        game_mode = 0   
    if quitgame.collidepoint(pos):
        exit()
    if creditsinfo.collidepoint(pos):
        game_mode = 2

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

我建议您添加一种将所有内容(背景、精灵等)切换到级别 1 的方法,然后当程序 运行 您将背景初始化为菜单和按钮。当你移动到第一级时,你可以摆脱按钮并切换背景。

希望对您有所帮助!

phylo

是的,我做到了! :) 我包含了一个名为 "gamestart" 的变量,并在 on_mouse_down(pos) 函数中添加了一个查询以检查是否单击了 "START" 按钮。如果是这样,我在更新函数中包含一个查询,仅当 gamestart == 1 时才启动 "falling" 糖果和水果。所以我没有添加其他游戏模式。解释起来比较复杂,这里是代码:

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
    global gamestart
    global 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