user3395816
user3395816

Reputation: 1

How do I change the picture and the answers on the quiz after clicking one of the answers

How do I change the picture and 3 answering options after clicking on one of them. My code already checks if the answer is true or false but I can't seem to find how to put on a new picture and new answer options after I have clicked one of them.

import os, sys

import math

from pygame import *
from random import *


init()

must = [0,0,0]
suurus = [1000,600]
screen = display.set_mode(suurus)
screen.fill(must)
punktid = 1

samblikud=[]
vastused=[]
#pildid = {"samblik1.jpg":"vaguapiksamblik","samblik2.jpg":"alpipõdrasamblik"}
for i in range(7):
    pilt = image.load("samblik"+str(i+1)+".jpg")
    samblikud.append(pilt)

for i in range(7):
    pilt = image.load("vastus"+str(i+1)+".jpg")
    vastused.append(pilt)

jargmine = image.load("jargmine.jpg")
s = randint(0,6) #pildi number
nr = {} #sõnastik, kuhu valime kolm vastusevarianti
nr[s]=1 #samblik

vo = True
to = True
while (vo == True) or (to == True):
    v = randint(0,6)#vastusevariandi nr
    t = randint(0,6)
    if v in nr:
        v = randint(0,6) #kui olemas genereerib uue
    else:
        nr[v]=0
        vo = False
    if t in nr: #kui t on olemas
        t = randint(0,6)
    else:
        nr[t]=0
        to = False

var=[]  
for key in nr:
    var.append(key)

o = [] #milline õige, kui väärtus on 1
for key in nr:
    if nr[key] == 1:
        o.append(key)

print(var)
print(nr)


def joonista():

    screen.fill(must)
    screen.blit(samblikud[o[0]],[0,0]) #samblik
    screen.blit(vastused[var[0]], [500,100])
    screen.blit(vastused[var[1]], [500,200])
    screen.blit(vastused[var[2]], [500,300])
    screen.blit(jargmine, [500, 500])
    display.flip()



def kysimus():
    teksti_font = font.Font(None,25)
    tekst1_pildina = teksti_font.render('Milline samblik on pildil?',1,[0,255,0])
    tekst1_raam = tekst1_pildina.get_rect()
    screen.blit(tekst1_pildina,(500,50))

    tekst2_pildina = teksti_font.render('Vastamiseks vajuta õigel vastusel.',1,[0,255,0])
    tekst2_raam = tekst2_pildina.get_rect()
    screen.blit(tekst2_pildina,(500,75))
    display.flip()

x_v=347#vastuse kasti x suurus
y_v=81#vastuse kasti y suurus
def kontrolli():
        while True:
            if var[0]==o[0]:
                y_o = 100 #õige vastuse koordi
                y_w1 = 200
                y_w2 = 300
            elif var[1]==o[0]:
                y_o = 200
                y_w1 = 100
                y_w2 = 300
            elif var[2]==o[0]:
                y_o = 300
                y_w1 = 200
                y_w2 = 100
            for i in event.get():
                if i.type == QUIT:
                    sys.exit()
                if i.type == MOUSEBUTTONDOWN:
                    x, y = i.pos
                    # Check to see if the click is in the range we want

                    if ( x in range(500,500+x_v)) and (y in range(y_o,y_o+y_v)):
                        # Grab the next image,
                        next_image = image.load('oige_v.png') #õige vastuse kuvamine
                        screen.blit(next_image, (500,y_o)) #uus pilt
                        punktid = punktid + 1
                        display.flip()
                    elif (x in range(500,500+x_v)) and (y in range(y_w1,y_w1+y_v)):
                        next_image = image.load('vale_v.png')
                        screen.blit(next_image, (500,y_w1)) # vale vastus
                        display.flip()
                    elif (x in range(500,500+x_v)) and (y in range(y_w2,y_w2+y_v)):
                        next_image = image.load('vale_v.png')
                        screen.blit(next_image, (500,y_w2)) #vale vastus
                        display.flip()
                    elif (x in range(500,500+x_v)) and (y in range(500,500+y_v)):
                        break


while True:
    for i in range(punktid):
        joonista()                     
        kysimus()

    #joonista()                     
    #kysimus()
    while True:


            if var[0]==o[0]:
                y_o = 100
                y_w1 = 200
                y_w2 = 300
            elif var[1]==o[0]:
                y_o = 200
                y_w1 = 100
                y_w2 = 300
            elif var[2]==o[0]:
                y_o = 300
                y_w1 = 200
                y_w2 = 100
            for i in event.get():
                if i.type == QUIT:
                    sys.exit()
                if i.type == MOUSEBUTTONDOWN:
                    x, y = i.pos
                    # Check to see if the click is in the range we want

                    if ( x in range(500,500+x_v)) and (y in range(y_o,y_o+y_v)):
                        # Grab the next image,
                        next_image = image.load('oige_v.png') #õige vastuse kuvamine
                        screen.blit(next_image, (500,y_o)) #uus pilt
                        punktid = punktid + 1
                        display.flip()
                    elif (x in range(500,500+x_v)) and (y in range(y_w1,y_w1+y_v)):
                        next_image = image.load('vale_v.png')
                        screen.blit(next_image, (500,y_w1)) # vale vastus
                        display.flip()
                    elif (x in range(500,500+x_v)) and (y in range(y_w2,y_w2+y_v)):
                        next_image = image.load('vale_v.png')
                        screen.blit(next_image, (500,y_w2)) #vale vastus
                        display.flip()
                    elif (x in range(500,500+x_v)) and (y in range(500,500+y_v)):
                        next_image = image.load('vale_v.png')
                        screen.blit(next_image, (500,500)) #vale vastus
                        display.flip()




    #kontrolli()
    display.flip()
    done()

Upvotes: 0

Views: 133

Answers (1)

KodyVanRy
KodyVanRy

Reputation: 1108

I would try and use a question class. So here is basically what I would do.

class Question:
    def __init__(self, image, question, answer, numOfAnswers):
        self.image = image
        self.question = question
        self.answer = {}
        self.answer[1] = answer
        self.totalAnswers = len(answer)
    def newImage(image):
        self.image = image
    def newQuestion(question, answer):
        self.question = question
        self.answer.clear()
        self.answer[1] = answer
    def addAnswer(answer):
        self.answer[len(answer)+1] = answer

Another thing is that your code is really sloppy because you have a while loop inside of your main game loop which i wouldn't recommend most especially considering that you only have one break and that is if your x is greater than 500. And I dont understand any of your variables, because I speak English, but tell me if you need any more clarification regarding accessing the class or using it. Just for your information classes are very useful in any programming language for uses such as this.

Upvotes: 2

Related Questions