如果答案错误,为什么我的程序不会减少生命?

问题描述

对于我的程序,我正在制作一个涉及多项选择题的游戏。除此之外,我还使用了三个独立的心脏图像来向用户展示生活。

代码中,我设置了条件,例如,如果生命值为 1,则屏幕上只有一个心脏图像。只有当用户选择的答案不正确时,生命数量才会减少。

下面显示了我的代码的相关部分:

class GameState:
     def __init__(self,difficulty):
         self.difficulty = difficulty

         self.questions = [
             ("Q1: 4 _ 6 = 10?"),("Q2: ___ = 1"),("Q3: 1 * 3?")
         ]
         self.answers = [4,2,2]
         self.current_question = None
         self.question_index = 0

     def pop_question(self):
         q = self.questions[0]
         self.questions.remove(q)
         self.current_question = q

         self.question_index += 1

         return q

     def answer(self,answer):
         lives = 3
         if self.answers == self.current_question[1]:
            lives = lives - 1
         else:
            lives = lives

         if lives == 1:
            screen.blit(Heart,(500,10))

         if lives == 2:
            screen.blit(Heart,10))
            screen.blit(Heart,(X // 2,10))

         if lives == 3:
            screen.blit(Heart,10))
            screen.blit(Heart1,10))
            screen.blit(Heart2,(775,10))


class GameScene:

def __init__(self):
    if SimpleScene.FONT == None:
       SimpleScene.FONT = pygame.freetype.SysFont(None,32)

    self.rects = []

    for n in range(4):
        rect = pygame.Rect(420,(n * 70) + 300,500,50)
        self.rects.append(rect)

    self.choices = [['x','-','*','+'],["number","fruit","weather","letter"],["4","3","-2","13"]]

     def start(self,gamestate):
         self.background = pygame.Surface((X,Y))
         self.background.fill(pygame.Color("white"))
         self.gamestate = gamestate
         question = gamestate.pop_question()
         SimpleScene.FONT.render_to(self.background,(20,150),question,(blue))

     def draw(self,screen):
         screen.blit(self.background,(0,0))
         n = 0
         for rect in self.rects:
             if rect.collidepoint(pygame.mouse.get_pos()):
                 pygame.draw.rect(screen,pygame.Color('darkgrey'),rect)
             pygame.draw.rect(screen,rect,5)
             screen.blit(Heart,10))
             screen.blit(Heart1,10))
             screen.blit(Heart2,10))

             for i in range(len(self.choices)):
                 if self.gamestate.question_index == i + 1:
                     SimpleScene.FONT.render_to(screen,(rect.x + 30,rect. y + 20),str(self.choices[i][n]),(green))
                     SimpleScene.FONT.render_to(screen,(rect.x + 29,rect.y + 19),str(self.choices [i][n]),(green))
              n += 1

     def update(self,events,dt):
         for event in events:
             if event.type == pygame.MOUSEBUTTONDOWN:
                n = 1
                for rect in self.rects:
                    if rect.collidepoint(event.pos):
                       self.gamestate.answer(n)
                       if self.gamestate.questions:
                           return ('GAME',self.gamestate)
                       else:
                           quit()

                    n += 1

当我运行程序时,没有一颗心消失。如果答案错误,是不是因为我在将生命条件设置为减一时犯了错误

解决方法

lives 需要是 GameState 的一个属性:

class GameState:
    def __init__(self,difficulty):
        # [...]

        self.lives = 3

    # [...]

    def pop_question(self):
        q = self.questions[0]
        self.current_question = q
        return q

    def answer(self,answer):
        if answer != self.answers[self.question_index]:
            self.lives -= 1
        else:
            self.question_index += 1
            self.questions.pop(0)

但是,您必须在 GameScene.draw 中绘制心形,具体取决于 self.gamestate.lives 的值:

class GameScene:
    # [...]

    def draw(self,screen):
        screen.blit(self.background,(0,0))

        if self.gamestate.lives >= 1:
            screen.blit(Heart,(500,10))
        if self.gamestate.lives >= 2:
            screen.blit(Heart1,(X // 2,10))
        if self.gamestate.lives >= 3:
            screen.blit(Heart2,(775,10))

        n = 0
        for rect in self.rects:
            if rect.collidepoint(pygame.mouse.get_pos()):
                pygame.draw.rect(screen,pygame.Color('darkgrey'),rect)
            pygame.draw.rect(screen,rect,5)

            for i in range(len(self.choices)):
                if self.gamestate.question_index == i:
                    SimpleScene.FONT.render_to(screen,(rect.x + 30,rect. y + 20),str(self.choices[i][n]),(green))
                    SimpleScene.FONT.render_to(screen,(rect.x + 29,rect.y + 19),str(self.choices [i][n]),(green))
            n += 1

结束游戏

def main():
    # [...]

    while True:
    # [...]

        game = scene.update(events,dt)
        if game:
            next_scene,state = game
            if next_scene:
               scene = scenes[next_scene]
               scene.start(state)
            if state and state.lives == 0:
                print("GAME OVER")
                break