1

My team and I are trying to create a trivia game in python using pygame and are having trouble with the main part of the game:

  • Displaying the multiple choice questions and answers
  • The buttons for the user to choose the answer
  • How to update the score (troops) once the answer is chosen depending on if it is right or wrong.

So far, we have the question and answer displaying, as well as the buttons, but the buttons don't connect with the right answer from the randomly selected keys in the dictionary. The questions also repeat when we don't want them to. Any help would be appreciated.

#global areas
a_q={'Berlin':'What is east capital city for German?',
       'Bonn':'What is west capital city for German?',
       'Michael Schumacher':'What is a famous formula one driver?',
       'Albert Einstein':'He was one of the smartest one.'}
keys=[x for x in random.sample(a_q,3)]
correctanswer=a_q[random.choice(keys)]
key1, key2, key3= keys[0], keys[1], keys[2]
print '\nA. %s \nB. %s \nC. %s' % (key1, key2, key3)

def troop():
    count=400
    font=pygame.font.SysFont(None, 25)
    text = font.render("Troops: "+str(count), True, black)
    gameDisplay.blit(text, (0, 0))

    pygame.display.update()

def button(msg, x, y, w, h, ic, ac, action=None):
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    print(click)

    if x+w > mouse[0] > x and y+h>mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac, (x, y, w, h))
        if click[0]==1 and action!= None:
            action()
    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))

    smallText=pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect=text_objects(msg, smallText)
    textRect.center=((x+w/2), (y+h/2))
    gameDisplay.blit(textSurf, textRect)

def game_loop():
    gameExit=False
    while not gameExit:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.fill(green)

        largeText = pygame.font.Font("freesansbold.ttf", 20)
        TextSurf, TextRect = text_objects("What is the right answer",largeText)
        TextRect.center=((400), (300))
        gameDisplay.blit(TextSurf, TextRect)

        largeText = pygame.font.Font("freesansbold.ttf", 20)
        TextSurf, TextRect = text_objects("Question: "+correctanswer, largeText)
        TextRect=((0), (80))
        gameDisplay.blit(TextSurf, TextRect)

        print 'Question: ', correctanswer

        largeText = pygame.font.Font("freesansbold.ttf", 20)
        TextSurf, TextRect = text_objects("A. "+key1, largeText)
        TextRect=((0), (120))
        gameDisplay.blit(TextSurf, TextRect)

        largeText = pygame.font.Font("freesansbold.ttf", 20)
        TextSurf, TextRect = text_objects("B. "+key2, largeText)
        TextRect=((0), (160))
        gameDisplay.blit(TextSurf, TextRect)

        largeText = pygame.font.Font("freesansbold.ttf", 20)
        TextSurf, TextRect = text_objects("C. "+key3, largeText)
        TextRect=((0), (200))
        gameDisplay.blit(TextSurf, TextRect)

        for key in a_q:
            correctans=key
            print correctans
            question=a_q[key]
            print question

        button("A", 150, 400, 100, 50, blue, bright_green, troop)
        button("B", 350,400, 100, 50, blue, bright_green, troop)
        button("C", 550, 400, 100, 50, blue, bright_green, troop)

        pygame.time.delay(500)#was used to pause the displaying questions
        pygame.display.update()
OhBeWise
  • 5,350
  • 3
  • 32
  • 60

0 Answers0