【问题标题】:The "lives" in my game won't appear? pygame我的游戏中的“生命”不会出现?游戏
【发布时间】:2013-11-30 06:06:21
【问题描述】:

所以,在我的记忆游戏中,我知道当你点击错误的按钮时,你会失去生命。这行得通!失去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) 

【问题讨论】:

  • 将 mainloop 中的一些代码放入函数中以使其更简洁。创建类。阅读Making Games with Python & Pygame。顺便说一句:您可以使用pygame.time.get_ticks() 代替time.time()

标签: python python-2.7 pygame


【解决方案1】:

每次更改lives 都必须重新渲染一次

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

并在每个循环中显示它

DISPLAYSURF.blit(img, livesRect)

【讨论】:

    【解决方案2】:

    在主循环中,您用DISPLAYSURF.fill(bgColor) 填充整个背景,但不要再次绘制livesRect

    【讨论】:

    • 好的,所以我做到了,它成功了!但是现在生命的数量没有减少吗? @菲利普
    • 您是每次使用更新的生命数重新渲染livesRect,还是只是一遍又一遍地使用相同的生命?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多