2

I am creating a flashcard game for my kid. Its about Dinos. I am having trouble making "Congrats, You got it right" appear on the screen. I have moved my code all over the place but no luck. Can someone please help me out.

To be clear, What I want to happen is when the user presses the number 1,2,3 on the keypad, and if the key is the correct answer that correlates to the question, the message "Congrats, You got it right!" should appear on the screen.

I know the keydown event right now is the return key, but I did that for testing purposes only. This is also the same for testtext variable. I was using that variable to see if I could print "Hello WOrld" to the screen.

I do have a feeling it has something to do with the loop that is running. My guess would be is that it does show up for a fraction of a second but disappears before anyone can see it.

import pygame, random

pygame.font.init()
pygame.init()

font = pygame.font.Font(None, 48)
#Created the window display
size = width, height = 800,800
screen = pygame.display.set_mode(size)
#Loads the images of the starting game Trex
#t_rex = pygame.image.load('trex1.png')
##Places the image on the screen
#screen.blit(t_rex,(150,50))



count = 0
score = 0
active = False

testtext = font.render("Hello WOrld", True, (250, 250, 250))







#The below code keeps the display window open until user decides to quie app


crashed = False
while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_RETURN:
                screen.blit(testtext, (200,699))

    while count < 2:
        screen.fill(0)

        dinoQuestions = ["Does a t-rex eat meat?\n","Does a trycerotopes have 3 horns?\n"]

        dinoAnswer = ["Yes\n", "No\n","Maybe\n"]

        wordnum = random.randint(0, len(dinoQuestions)-1)

        mainpic = pygame.image.load("trex1.png")

        screen.blit(mainpic, (150, 20))

        options = [random.randint(0, len(dinoAnswer)-1),random.randint(0, len(dinoAnswer)-1)]

        options[random.randint(0,1)] = wordnum

        question_display = font.render(dinoQuestions[wordnum].rstrip('\n'),True, (255, 255, 255))
        text1 = font.render('1 - ' + dinoAnswer[options[0]].rstrip('\n'),True, (255, 255, 255))
        text2 = font.render('2 - ' + dinoAnswer[options[1]].rstrip('\n'),True, (255, 255, 255))

        #the below code is for testing purposes only


        screen.blit(question_display,(200, 590))
        screen.blit(text1, (200, 640))
        screen.blit(text2, (200, 690))


        count = count + 1
        pygame.display.flip()
zootechdrum
  • 111
  • 1
  • 8

2 Answers2

1

The blit to your screen surface you perform when handling a Return key down event is overwritten when you later call screen.fill(0).

I've rearranged your code a little and added displaying a result on appropriate key press.

import pygame
import random

pygame.init()
pygame.font.init()

font = pygame.font.Font(None, 48)
size = width, height = 800,800
screen = pygame.display.set_mode(size) #Created the window display
count = 0
score = 0
active = False

white = pygame.color.Color("white")
black = pygame.color.Color("black")
green = pygame.color.Color("green")

# load/create static resources once
mainpic = pygame.image.load("trex1.png")

testtext = font.render("Hello World", True, (250, 250, 250))
correct_text = font.render("Correct! Well Done!", True, green)

clock = pygame.time.Clock() # for limiting FPS

dinoQuestions = ["Does a t-rex eat meat?","Does a triceratops have 3 horns?"]
dinoAnswer = ["Yes", "No","Maybe"]

# initialise state
show_hello = False
show_correct = False
update_questions = True  # need to update questions on the first iteration
finished = False

while not finished:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            finished = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                show_hello = not show_hello # toggle flag for later display
            elif event.key == pygame.K_SPACE:
                update_questions = True
            elif event.key in [pygame.K_1, pygame.K_2]:
                # an answer has been selected
                # pygame.K_1 is 0, pygame.K_2 is 1
                if dinoAnswer[event.key - pygame.K_1] == "Yes":
                    show_correct = True
                    count += 1
                else:
                    show_correct = False

    screen.fill(black)
    screen.blit(mainpic, (150, 20))
    if show_hello:
        screen.blit(testtext, (200,199))

    if show_correct:
        screen.blit(correct_text, (200, 300))

    if update_questions:
        random.shuffle(dinoQuestions)
        random.shuffle(dinoAnswer)
        question_display = font.render(dinoQuestions[0],True, white)
        text1 = font.render('1 - ' + dinoAnswer[0],True, white)
        text2 = font.render('2 - ' + dinoAnswer[1],True, white)
        update_questions = False
        show_correct = False

    # Display the Question
    screen.blit(question_display,(200, 590))
    screen.blit(text1, (200, 640))
    screen.blit(text2, (200, 690))
    # count = count + 1
    pygame.display.flip()
    clock.tick(60)

Hopefully this is enough of a framework for you to extend.

Let me know if you have any questions about any portions of the code.

import random
  • 3,054
  • 1
  • 17
  • 22
  • 1
    Better use `not` to toggle a boolean variable: `show_hello = not show_hello`. The bitwise NOT `~` will toggle between 0 and -1. It still works since -1 is a "truthy" value but I find it a bit confusing. Also, XOR with 1 is usually used to toggle bits. – skrx Mar 22 '18 at 17:15
0

I am a bit confused about what your exact problem is, so I'm going to try to answer. You say that you want the words "Congrats, , you got it right!", so I can help you with what went wrong. You blit the testtext before you color the screen, so each time the loop loops, it displays the testtext but then almost instantly covers it up with screen.fill(0). To make it better, you should put the blitting of the text after the screen is colored. The best way to do this is to put it right at the start of the loop, or making another event detector after the current position of the screen.fill in the code. Also, I would get rid of the stacked while loop, and instead replace it with if statement because it is already in the while loop. Is this what you were looking for?

Ethanol
  • 370
  • 6
  • 18