单击其中一个答案后,如何更改测验中的图片和答案

时间:2014-03-08 10:41:58

标签: python python-2.7 python-3.x pygame

如何在点击其中一个后更改图片和3个回答选项。我的代码已经检查答案是真还是假,但我似乎无法找到如何在点击其中一个之后添加新图片和新答案选项。

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()

1 个答案:

答案 0 :(得分:2)

我会尝试使用问题类。所以这基本上就是我要做的。

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

另一件事是你的代码非常草率,因为你在主游戏循环中有一个while循环,我不建议最多,特别是考虑到你只有一次休息,那就是你的x大于500。而且我不理解你的任何变量,因为我说英语,但告诉我你是否需要更多关于访问课程或使用它的说明。只是为了您的信息类,在任何编程语言中都非常有用,例如此类。