Randomize och skärmklickbar
vad behöver jag göra för att programmmet ska slumpa frågor och att skärmen ska vara klickbar.
Hur hade ni löst uppgiften. Skriv i detalj hur och vad jag ska mata in programmet.
# import modules
import time
import turtle
import random
from random import shuffle
def main():
global wn
global quest
global questions
global alternatives
global answers
global currentQuestionIndex
global maxQuestions
global select
global a
global b
global c
global d
wn = turtle.Screen()
quest = turtle.Turtle()
a=turtle.Turtle()
b=turtle.Turtle()
c=turtle.Turtle()
d=turtle.Turtle()
questions = ['vad är 3+3?','vad är 4+4?','vad är 12*12?','vad är 10+10?','vad är 20+20?','vad är 30+30?','vad är 50+50?','vad är 60+60?','vad är 70+70?','vad är 90+90?']
alternatives = [['6','8','9','7'],['12','9','7', '8'],['12','9','144', '8'],['12','20','7', '8'],['40','9','7', '8'],['12','9','7', '60'],['100','9','7', '8'],['120','9','7', '8'],['140','9','7', '8'],['12','9','7', '180']]
answers = ['6','8','144','20','40','60','100','120','140','180']
currentQuestionIndex = 0
CreateUI()
SetupKeybindings()
printQuestion()
def CreateUI():
#set up the screen
wn.setup(1000.600)
wn.colormode(255)
wn.bgcolor(0,0,70)
wn.title('Kahoot')
#box c
turtle.speed(0)
turtle.hideturtle()
turtle.penup()
turtle.goto(-450,-250)
turtle.pendown()
turtle.begin_fill()
turtle.setheading(0)
turtle.color('red')
for sides in range(2):
turtle.fd(425)
turtle.left(90)
turtle.fd(125)
turtle.left(90)
turtle.end_fill()
#Box d
turtle.penup()
turtle.goto(25,-250)
turtle.pendown()
turtle.begin_fill()
turtle.setheading(0)
for sides in range(2):
turtle.fd(425)
turtle.left(90)
turtle.fd(125)
turtle.left(90)
turtle.end_fill()
#box a
turtle.penup()
turtle.goto(-450,-75)
turtle.pendown()
turtle.begin_fill
turtle.setheading(0)
for sides in range (2):
turtle.fd(425)
turtle.left(90)
turtle.fd(125)
turtle.left(90)
turtle.end_fill()
#box b
turtle.penup()
turtle.goto(25,-75)
turtle.pendown()
turtle.begin_fill()
turtle.setheading(0)
for sides in range(2):
turtle.fd(500)
turtle.left(90)
turtle.fd(125)
turtle.left(90)
turtle.end_fill()
#frågor box
turtle.penup()
turtle.goto(-450,100)
turtle.pendown()
turtle.begin_fill()
turtle.setheading(0)
for sides in range (2):
turtle.fd(625)
turtle.left(90)
turtle.fd(150)
turtle.left(90)
turtle.end_fill()
#score box
turtle.penup()
turtle.goto(225,100)
turtle.pendown()
turtle.begin_fill()
turtle.setheading(0)
for sides in range(2):
turtle.fd(225)
turtle.left(90)
turtle.fd(150)
turtle.left(90)
turtle.end_fill()
#frågor tuurtle
quest.speed(0)
quest.hideturtle()
quest.penup()
quest.goto(-425,150)
# score turtle
score1 = turtle.Turtle()
score1.speed(0)
score1.hideturtle()
score1.penup()
score1.goto(250,125)
#svar turtle
#a
a.speed(0)
a.hideturtle()
a.penup()
a.goto(-425,-50)
#b
b.speed(0)
b.hideturtle()
b.penup()
b.goto(50,-50)
#c
c.speed(0)
c.hideturtle()
c.penup()
c.goto(-425, -225)
#d
d.speed(0)
d.hideturtle()
d.penup()
d.goto(50,-225)
#öpnings poäng
quest.write("välkommen till kahoot!",font= ("Verdana",23, "bold"))
time.sleep(2)
quest.clear()
quest.write("Press A,B,C, eller, D för att svara!", font=("verdana",23, "bold"))
time.sleep(2)
quest.clear()
quest.write("lycka till",font=("verdana",23,"bold"))
time.sleep(2)
quest.clear()
#Antal rätt svar
#variabel
CurrentQ = 1
def SetupKeybindings():
wn.listen()
wn.onkeypress(chooseAnswerA, 'a')
wn.onkeypress(chooseAnswerb, 'b')
wn.onkeypress(chooseAnswerc, 'c')
wn.onkeypress(chooseAnswerd, 'd')
#Nyckel funktion
def chooseAnswerA():
print('test', currentQuestionIndex)
evaluate(alternatives[currentQuestionIndex][0])
def chooseAnswerb():
evaluate(alternatives[currentQuestionIndex][1])
def chooseAnswerc():
evaluate(alternatives[currentQuestionIndex][2])
def chooseAnswerd():
evaluate(alternatives[currentQuestionIndex][3])
def evaluate(selectedAnswer):
currentQuestionIndex
print('in evaluate')
print(currentQuestionIndex)
quest.clear()
correctAnswer = answers[currentQuestionIndex]
print('Rätt svar är', correctAnswer)
print('Du valde',selectedAnswer)
if correctAnswer == selectedAnswer:
quest.write("Correct!!",font=("Verdana",62, "bold"))
else:
stringToWrite = "Fel! Svaret var ("
stringToWrite+= correctAnswer
stringToWrite+= ')'
quest.write(stringToWrite, font=("Verdana",23,"bold"))
time.sleep(5)
clearBoard()
NextQuestion()
def getQuestion(qIndex):
return q[qIndex]
def NextQuestion():
global currentQuestionIndex
print('Current question is', currentQuestionIndex)
currentQuestionIndex += 1
print('Moving on to next question:', currentQuestionIndex)
if(currentQuestionIndex < 10):
printQuestion()
def printQuestion():
quest.write(questions[currentQuestionIndex], font=("Verdana",23, "bold"))
a.write("a. " + alternatives[currentQuestionIndex][0],font=("Verdana",23, "bold"))
b.write("b. " + alternatives[currentQuestionIndex][1], font=("Verdana",23, "bold"))
c.write("c. " + alternatives[currentQuestionIndex][2], font=("Verdana",23, "bold"))
d.write("d. " + alternatives[currentQuestionIndex][3], font=("Verdana",23, "bold"))
def clearBoard():
quest.clear()
a.clear()
b.clear()
c.clear()
d.clear()
if __name__ == "__main__":
main()
Börja med att läsa i dokumentationen för turtle
. Det finns metoder som reagerar på händelser. Musklick hanteras av onclick()
.