python - pygame - 如何完成游戏?

标签 python python-3.x pygame pgzero

我正在创建一个小游戏。当用户达到 100 分或更多分数时,游戏应停止并出现新屏幕。我尝试过,但它不起作用。游戏不会停止,屏幕会“闪烁”。我怎样才能完成游戏并“制作”一个新屏幕?我认为需要一个 while 循环,但我不知道如何进行。

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

score = 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()
    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
    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 endoflevel1():
    screen.clear()
    global score
    screen.fill("green")
    screen.draw.text("Game Over: Du hast das 1. Level erfolgreich abgeschlossen!", topleft=(100,350), fontsize=30)
    pygame.display.flip()

最佳答案

不要在 update() 中进行任何绘图。在 draw() 中完成所有绘图,取决于分数:

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

def drawgame():
    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 endoflevel1():
    global score
    screen.fill("green")
    screen.draw.text("Game Over: Du hast das 1. Level erfolgreich abgeschlossen!",
                     topleft=(100,350), fontsize=30)

请注意,您还必须在 update 中评估分数是否大于或等于 100。当游戏结束时,分数不应改变:

def update():
    global score

    # do not change the score if the game has end
    if score >= 100:
        pygame.mixer.music.stop() # optionally stop music
        return

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

    # [...]

关于python - pygame - 如何完成游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59107919/

相关文章:

python - 多迹线图表上不必要的额外边距

python - 删除与模型关联的文件 - django

python - 在 Python 中创建带零的 UUID

Python 访问变量

python - Python 中的 yield 问题 |使用辅助函数

python-3.x - Python Pandas 将 Na 或 Null 值移动到新的数据帧

Python 函数 - 循环字典并更新值

Python 3.2 与 Python 2.7 代码问题

python - 我的暂停显示如何在 Pygame 中正常工作?

Python 覆盖 : A case for monkey Patching