簡體   English   中英

我的游戲中的“生命”不會出現? pygame的

[英]The “lives” in my game won't appear? pygame

所以,在我的記憶游戲中,當你點擊一個錯誤的按鈕時,我得到了它,你會失去生命。 這有效! 失去3條生命后,你輸了。 但問題在於,一旦啟動,生命的文本就不會出現在我的游戲中。 任何人都可以幫我嗎? (如果你有興趣看到整個代碼,那么你可以在這里查看它http://pastebin.com/rkBGC2rD

def main():
    global FPSCLOCK, DISPLAYSURF, BASICFONT, BEEP1, BEEP2, BEEP3, BEEP4

    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption('Simulate')

    # font 
    BASICFONT = pygame.font.Font(None, 30)

    # load the sound files
    BEEP1 = pygame.mixer.Sound('beep1.wav')
    BEEP2 = pygame.mixer.Sound('beep2.wav')
    BEEP3 = pygame.mixer.Sound('beep3.wav')
    BEEP4 = pygame.mixer.Sound('beep4.wav')
    SOUNDTRACK = pygame.mixer.Sound('soundtrack.wav')
    ERROR = pygame.mixer.Sound('error.wav')

    # initialize some variables for a new game
    pattern = [] # stores the pattern of colors
    currentStep = 0 # the color the player must push next
    lastClickTime = 0 # "time stamp" of the player's last button push
    score = 0

    # plays the soundtrack music 
    SOUNDTRACK.play(-1, 0, 1000)

    # start-up screen text
    text = BASICFONT.render('Press enter to play!', 1, WHITE)
    textRect = text.get_rect()
    textRect.centerx = DISPLAYSURF.get_rect().centerx
    textRect.y = 150
    DISPLAYSURF.blit(text, textRect)

    # update the screen
    pygame.display.update()

    # the start up screen command
    waiting = True
    while waiting:      
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    waiting = False

    # amount of lives
    lives = 3
    img = BASICFONT.render('I'*lives, 1, WHITE)
    livesRect = img.get_rect()
    livesRect.topleft = (10, 10)
    DISPLAYSURF.blit(img, livesRect)

    # when False, the pattern is playing. when True, waiting for the player to click a colored button:
    waitingForInput = False

    while True: # main game loop
        clickedButton = None # button that was clicked (set to YELLOW, RED, GREEN, or BLUE)
        DISPLAYSURF.fill(bgColor)
        drawButtons()

        scoreSurf = BASICFONT.render('Score: ' + str(score), 1, WHITE)
        scoreRect = scoreSurf.get_rect()
        scoreRect.topleft = (WIDTH - 100, 10)
        DISPLAYSURF.blit(scoreSurf, scoreRect)      

        checkForQuit()
        for event in pygame.event.get(): # event handling loop
            if event.type == MOUSEBUTTONUP:
                mousex, mousey = event.pos
                clickedButton = getButtonClicked(mousex, mousey)

        if not waitingForInput:
            # play the pattern
            pygame.display.update()
            pygame.time.wait(1000)
            pattern.append(random.choice((YELLOW, BLUE, RED, GREEN)))
            for button in pattern:
                flashButtonAnimation(button)
                pygame.time.wait(FLASHDELAY)
            waitingForInput = True
        else:
            # wait for the player to enter buttons
            if clickedButton and clickedButton == pattern[currentStep]:
                # pushed the correct button
                flashButtonAnimation(clickedButton)
                currentStep += 1
                lastClickTime = time.time()

                if currentStep == len(pattern):
                    # pushed the last button in the pattern
                    score += 1
                    waitingForInput = False
                    currentStep = 0 # reset back to first step

            elif (clickedButton and clickedButton != pattern[currentStep]) or (currentStep != 0 and time.time() - TIMEOUT > lastClickTime):
                # pushed the incorrect button, or has timed out
                pattern = []
                currentStep = 0
                waitingForInput = False
                lives = lives - 1
                SOUNDTRACK.stop()
                ERROR.play()
                pygame.time.wait(1000)
                SOUNDTRACK.play(-1, 0, 1000)
                pygame.display.update()
                if lives < 1:               
                    gameOverAnimation()
                    # reset the variables for a new game:
                    pattern = []
                    currentStep = 0
                    waitingForInput = False
                    score = 0
                    pygame.time.wait(1000)

        pygame.display.update()
        FPSCLOCK.tick(FPS) 

每當你改變lives你必須再次渲染

img = BASICFONT.render('I'*lives, 1, WHITE)

並在每個循環中顯示它

DISPLAYSURF.blit(img, livesRect)

在主循環中,使用DISPLAYSURF.fill(bgColor)填充整個背景,但不要再次繪制livesRect

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM