我正在一个名为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,然后单击Enter,然后会给您一个随机颜色的随机单词。理想情况下,单击键盘上与单词颜色相对应的键(Stroop测试),然后说“正确”或“不正确”,然后清除屏幕,并为您提供新的单词和颜色。最终应该测量一下按下按钮并最终给出平均结果所花费的时间,但是我不知道如何编写代码,所以它不存在。
答案 0 :(得分:0)
使用turtle.clear()或turtle.clearscreen()清除屏幕上的所有内容。
...
if Start == 'x' or 'X':
tina.clear()
...
答案 1 :(得分:0)
我的信念是,按照前进的方向,您不会到达想要的地方。这不能通过简单的循环进行编程-由于点击龟键盘是事件,因此您的代码无法简单地停止等待它们的发生。 状态机可能对您有用,该状态机根据接下来应该发生的事件打开和关闭事件,然后让事件系统接管。例如:
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”输入输入到乌龟窗口,而不是控制台。