【发布时间】:2017-05-23 16:49:21
【问题描述】:
我正在尝试为我的班级做一个测验,允许用户参加测验,但仅限于海龟图形,因此测验问题将出现在海龟图形屏幕中,他们将通过按选择他们想要的答案屏幕上标有 a、b、c 或 d 的部分。我遇到的问题是,当用户选择他们的选择并且我不知道turtle.listen 是如何工作时,我不知道如何出现新问题。有任何想法吗?
这是我目前所拥有的:
import time
import turtle
x = turtle.xcor()
y = turtle.ycor()
Slytherin = 0
Griffindor = 0
Hufflepuff = 0
Ravenclaw = 0
def introduction():
turtle.penup()
turtle.goto(10,150)
turtle.write("\n This code is a quiz that will determine what Hogwart house you belong in:\n Slytherin, Griffindor, Hufflepuff, or Ravenclaw! \n \n You will be asked quiz questions on this screen, and then \n when you want to answer, you tell the program \n what you chose by pressing the corresponding section. \n", align = "center" , font=("Arial", 20, "normal"))
def user_interface():
turtle.setup(700, 700)
turtle.screensize(700, 700)
turtle.setworldcoordinates(0, 0, 500, 500)
turtle.penup()
turtle.setpos(125, 180)
turtle.pendown()
turtle.setpos(125, 0)
turtle.penup()
turtle.setpos(62.5, 90)
turtle.write("a", font=("Arial", 40, "normal"))
turtle.setpos(250, 0)
turtle.pendown()
turtle.setpos(250, 180)
turtle.penup()
turtle.setpos(187.5, 90)
turtle.write("b", font=("Arial", 40, "normal"))
turtle.setpos(375, 0)
turtle.pendown()
turtle.setpos(375, 180)
turtle.penup()
turtle.setpos(312.5, 90)
turtle.write("c", font=("Arial", 40, "normal"))
turtle.setpos(437.5, 90)
turtle.write("d", font=("Arial", 40, "normal"))
turtle.setpos(0,180)
turtle.right(90)
turtle.pendown()
turtle.setpos(500, 180)
def shapedrawer(x, y):
turtle.penup()
turtle.setpos(x, y)
turtle.pendown()
turtle.begin_fill()
#First Column
if x <= 125 and 0 <= y <= 180:
turtle.color("green")
turtle.circle(10)
Slytherin += 1
#Second Column
elif 125 < x <= 250 and 0 <= y <= 180:
turtle.color("red")
turtle.circle(10)
Griffindor += 1
#Third Column
elif 250 < x <= 375 and 0 <= y <= 180:
turtle.color("purple")
turtle.circle(10)
Hufflepuff += 1
#Fourth Column
elif 375 < x <= 500 and 0 <= y <= 180:
turtle.color("blue")
turtle.circle(10)
Ravenclaw += 1
turtle.end_fill()
def candy_question():
turtle.penup()
turtle.goto(100, 350)
turtle.write("What is your favorite type of candy? \n a. Bertie Bott's Every Flavour Beans \n b. Honeydukes Sherbet Lemons \n c. Chocolate Frogs \n d. Candy Floss", font=("Arial", 20, "normal"))
def pet_question():
turtle.penup()
turtle.goto(170, 250)
turtle.write("Which is animal would you prefer as a pet? \n a. A lizard \n b. A cat \n c. A dog \n d. A ferret", font=("Arial", 20, "normal"))
def class_question():
turtle.penup()
turtle.goto(170, 350)
turtle.write("Which is your favorite class? \n b. Potions \n a. Charms \n c. History of Magic \n d. Transfiguration", font=("Arial", 20, "normal"))
def color_question():
turtle.penup()
turtle.goto(170, 350)
turtle.write("Pick your favorite color \n a. Blue \n b. Purple \n c. White \n d. Orange", font=("Arial", 20, "normal"))
def life_question():
turtle.penup()
turtle.goto(130, 350)
turtle.write("If you could only do one thing for the rest \n of your life what would you do? \n a. Explore the World \n b. Start your own company \n c. Stay in school forever \n d. I don't know", font=("Arial", 20, "normal"))
def vacation_question():
turtle.penup()
turtle.goto(200, 150)
turtle.write("If you were going on a vacation, you would go to... \n a. London \n b. The wilderness \n c. Home, to your family \n d. The beach", font=("Arial", 20, "normal"))
def main_loop(x,y):
introduction()
time.sleep(5)
user_interface()
turtle.onscreenclick(shapedrawer)
pet_question()
turtle.listen()
while turtle.onclick(x) == 0 < x < 125:
turtle.clear()
candy_question()
main_loop(x,y)
turtle.mainloop()
【问题讨论】:
标签: python python-2.7 python-3.x turtle-graphics