【问题标题】:On Click Command点击命令
【发布时间】: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


    【解决方案1】:

    在使用事件时,您将控制权交给海龟的事件处理程序,您无法再提前确定何时提出问题。

    解决方案是创建一个问题列表(在本例中为问题函数列表),并在前一个问题得到回答后让shapedrawer() 触发下一个问题:

    import time
    import turtle
    
    turtle.setup(700, 700)
    turtle.screensize(700, 700)
    turtle.setworldcoordinates(0, 0, 500, 500)
    
    TEXTFONT = ("Arial", 20, "normal")
    QUIZFONT = ("Arial", 40, "normal")
    
    Slytherin = 0
    Griffindor = 0
    Hufflepuff = 0
    Ravenclaw = 0
    
    def introduction():
        marker_turtle.penup()
        marker_turtle.goto(250, 200)
        marker_turtle.write("""
    This code is a quiz that will determine what Hogwart house you
    belong in: Slytherin, Griffindor, Hufflepuff, or Ravenclaw!
    
    You will be asked quiz questions on this screen, and
    then when you want to answer, you tell the program
    what you chose by pressing the corresponding section
    """, align="center", font=TEXTFONT)
    
    def user_interface():
        turtle.penup()
    
        turtle.setpos(62.5, 90)
        turtle.write("a", font=QUIZFONT)
        turtle.setpos(125, 180)
        turtle.pendown()
        turtle.setpos(125, 0)
        turtle.penup()
    
        turtle.setpos(187.5, 90)
        turtle.write("b", font=QUIZFONT)
        turtle.setpos(250, 0)
        turtle.pendown()
        turtle.setpos(250, 180)
        turtle.penup()
    
        turtle.setpos(312.5, 90)
        turtle.write("c", font=QUIZFONT)
        turtle.setpos(375, 0)
        turtle.pendown()
        turtle.setpos(375, 180)
        turtle.penup()
    
        turtle.setpos(437.5, 90)
        turtle.write("d", font=QUIZFONT)
        turtle.setpos(500, 180)
        turtle.right(90)
        turtle.pendown()
        turtle.setpos(0, 180)
    
        turtle.hideturtle()
    
    def shapedrawer(x, y):
        global Slytherin, Griffindor, Hufflepuff, Ravenclaw
    
        turtle.onscreenclick(None)
    
        marker_turtle.penup()
        marker_turtle.setpos(x, y)
        marker_turtle.pendown()
    
        # First Column
        if 0 <= x <= 125 and 0 <= y <= 180:
            marker_turtle.dot(20, "green")
            Slytherin += 1
        # Second Column
        elif 125 < x <= 250 and 0 <= y <= 180:
            marker_turtle.dot(20, "red")
            Griffindor += 1
        # Third Column
        elif 250 < x <= 375 and 0 <= y <= 180:
            marker_turtle.dot(20, "purple")
            Hufflepuff += 1
        # Fourth Column
        elif 375 < x <= 500 and 0 <= y <= 180:
            marker_turtle.dot(20, "blue")
            Ravenclaw += 1
    
        time.sleep(5)
        marker_turtle.clear()
        marker_turtle.color("black")
    
        if questions:
            (questions.pop(0))()
            turtle.onscreenclick(shapedrawer)
        else:
            # here's where you display the results!
            # first erase the default turtle too.
            pass
    
    def candy_question():
        marker_turtle.penup()
        marker_turtle.goto(100, 350)
        marker_turtle.write("""
    What is your favorite type of candy?
     a. Bertie Bott's Every Flavour Beans
     b. Honeydukes Sherbet Lemons
     c. Chocolate Frogs
     d. Candy Floss""", font=TEXTFONT)
    
    def pet_question():
        marker_turtle.penup()
        marker_turtle.goto(170, 250)
        marker_turtle.write("""
    Which is animal would you prefer as a pet?
     a. A lizard
     b. A cat
     c. A dog
     d. A ferret""", font=TEXTFONT)
    
    def class_question():
        marker_turtle.penup()
        marker_turtle.goto(170, 350)
        marker_turtle.write("""
    Which is your favorite class?
     b. Potions
     a. Charms
     c. History of Magic
     d. Transfiguration""", font=TEXTFONT)
    
    def color_question():
        marker_turtle.penup()
        marker_turtle.goto(170, 350)
        marker_turtle.write("""
    Pick your favorite color:
     a. Blue
     b. Purple
     c. White
     d. Orange""", font=TEXTFONT)
    
    def life_question():
        marker_turtle.penup()
        marker_turtle.goto(130, 350)
        marker_turtle.write("""
    If you could only do one thing for the
    rest of your life what would you do?
     a. Explore the World
     b. Start your own company
     c. Stay in school forever
     d. I don't know""", font=TEXTFONT)
    
    def vacation_question():
        marker_turtle.penup()
        marker_turtle.goto(150, 250)
        marker_turtle.write("""
    If you were going on a vacation,
    you would go to:
     a. London
     b. The wilderness
     c. Home, to your family
     d. The beach""", font=TEXTFONT)
    
    questions = [pet_question, class_question, color_question, life_question, vacation_question]
    
    marker_turtle = turtle.Turtle(visible=False)
    
    introduction()
    time.sleep(5)
    marker_turtle.clear()
    
    user_interface()
    (questions.pop(0))()
    turtle.onscreenclick(shapedrawer)
    turtle.listen()
    
    turtle.mainloop()
    

    另一个关键变化是屏幕的永久部分和您要擦除的部分的分离海龟。我也稍微修饰了一下风格。现在您需要添加最终屏幕来显示结果——我留下了一个存根来指示可能在哪里完成。

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      相关资源
      最近更新 更多