【问题标题】:How to make the background reset to blank each time, and how to make it so that if I click a key, the code responds如何让背景每次都重置为空白,以及如何让我点击一个键,代码响应
【发布时间】:2019-08-27 12:00:11
【问题描述】:

我正在一个名为 Trinket.io 的网站上创建 Stroop 测试,我需要知道如何在显示每个单词后使背景变白,以及如何显示“正确”或“不正确” " 单击相应的键后。我不知道如何使代码看起来像代码,对此我深表歉意。

我没有尝试太多,因为我真的不知道从哪里开始,但是我尝试将 bgcolor(white) 放入循环中,以便在每次事件后它都应该变成白色,而这并没有工作。我试图找到一种在每个循环期间清除屏幕的方法,但什么也没找到。

from turtle import Turtle, turtle, Screen
import random
screen = Screen()
screen.setup(500, 500)
tina = turtle.Turtle()
tina.hideturtle()
tina.speed(0)
colors = ['black','blue','yellow','green','red']
texts = ['Black','Blue','Yellow','Green','Red']
NoOfTrials = 0
while NoOfTrials <= 100:
  Start = str(raw_input("Press X to begin the test! "))
  color = random.choice(colors)
  text = random.choice(texts)
  if Start == 'x' or 'X':
    tina.color(color);style = ('Arial', 100, 'bold');tina.write(text, font=style, align='center')
    screen.bgcolor('white')

好的,所以这段代码,当你运行它时,它会要求你写一个 x,然后你点击回车,然后它会给你一个随机颜色的随机单词。理想情况下,您单击键盘上与单词颜色相对应的键(Stroop Test),然后它会显示“正确”或“不正确”,然后清除屏幕并为您提供新的单词和颜色。它最终应该测量你按下按钮所花费的时间,然后给出最后的平均结果,但我不知道如何编码,所以它不存在。

【问题讨论】:

    标签: python python-2.7 turtle-graphics


    【解决方案1】:

    使用turtle.clear() 或turtle.clearscreen() 清除屏幕上的所有内容。

    ...
    if Start == 'x' or 'X':
        tina.clear()
    ...
    

    【讨论】:

      【解决方案2】:

      我的信念是,根据你前进的方向,你不会到达你想要的地方。这不能用一个简单的循环来编程——因为海龟键盘点击是事件,你的代码不能简单地停止等待它们发生。可能对您有用的是一个状态机,它根据接下来应该发生的事情打开和关闭事件,然后让事件系统接管。例如:

      from turtle import Screen, Turtle, mainloop
      from random import choice
      
      NUMBER_OF_TRIALS = 10
      
      COLORS = {'b': 'Blue', 'g': 'Green', 'r': 'Red'}
      
      LARGE_FONT_SIZE = 100
      LARGE_FONT = ('Arial', LARGE_FONT_SIZE, 'bold')
      SMALL_FONT = ('Arial', 24, 'bold')
      
      color = None
      score = 0
      trial = 0
      
      def do_nothing():
          pass
      
      def change_text():
          global color, trial
      
          screen.onkey(do_nothing, 'space')  # disable spacebar
          turtle.clear()  # erase "Press spacebar ..." text
      
          trial += 1
      
          color = choice(list(COLORS.values()))
          turtle.color(color)
      
          text = color
          while text == color:  # make sure color and text don't match
              text = choice(list(COLORS.values()))
      
          turtle.write(text, align='center', font=LARGE_FONT)
      
          for character in COLORS:  # (re)enable keyboard keys that correspond to colors
              screen.onkey(lambda c=character: selected(c), character)
      
          screen.ontimer(get_ready, 3000)  # user has 3 seconds to identify color
      
      def get_ready():
          for character in COLORS:  # disable keyboard keys that correspond to colors
              screen.onkey(do_nothing, character)
      
          turtle.clear()  # erase last word shown
      
          if trial == NUMBER_OF_TRIALS:
              turtle.write("Final score: " + str(score) + " out of " + str(NUMBER_OF_TRIALS), align='center', font=SMALL_FONT)
              return
      
          screen.onkey(change_text, 'space')  # (re)enable spacebar
      
          turtle.color('white')
          turtle.write("Press spacebar to continue.", align='center', font=SMALL_FONT)
      
      def selected(key):
          global score
      
          for character in COLORS:  # disable keyboard keys that correspond to colors
              screen.onkey(do_nothing, character)
      
          if COLORS[key] == color:
              score += 1  # a match!
      
      screen = Screen()
      screen.setup(500, 500)
      screen.bgcolor('black')
      screen.listen()
      
      turtle = Turtle()
      turtle.hideturtle()
      turtle.penup()
      turtle.sety(-LARGE_FONT_SIZE/2)  # adjust center position for font height
      
      get_ready()
      
      mainloop()
      

      如果在标准 Python 乌龟中运行,请确保单击窗口以开始,因为空格键和“r”、“g”和“b”输入是乌龟窗口,而不是控制台。

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 2021-11-27
        • 2011-11-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 2021-05-07
        • 1970-01-01
        • 2017-11-20
        相关资源
        最近更新 更多