是否创建与随机选择相关联的单击?Turtle+Trinket.io



所以我正在尝试创建一个显示20种颜色的游戏。程序使用random.choice选择其中一种颜色,然后给用户一个提示。我希望程序能够记住随机选择,然后能够在用户点击色块时告诉他们是/否。这是当前的代码:

这一部分使它绘制了颜色框&制作乌龟按钮:

draw_square(pinky,"#F699CD",30,-170,80)
barbiet = turtle.Turtle()
barbiet.penup()
barbiet.hideturtle()
barbiet.color("white")
barbiet.goto(-135,110)
barbiet.showturtle()
barbiet.onclick(barbiec)

此部分生成提示:

def barbie():
print("your hint is 'Barbie'.")
list1 = [barbie,rose,fuscia,punch,blush,watermelon,flamingo,rouge,salmon,coral,peach,strawberry,rosewood,lemonade,taffy,bubblegum,balletslipper,crepe,magenta,hotpink]
random.choice(list1)()

以下是我试图用来获得与随机选择相对应的点击,但我无法使其工作:

def barbiec(print):
if random.choice(list1)() == barbie():
print("That's Right!")
else:
print("Try Again.")

这是一个初级编码课程的课程项目!

您的一半代码行似乎没有朝着您既定的目标工作:

draw_square(pinky,"#F699CD",30,-170,80)
barbiet.color("white")
barbiet.goto(-135,110)
[barbie,rose,fuscia,punch,blush,watermelon,flamingo,rouge,salmon,coral,peach,strawberry,rosewood,lemonade,taffy,bubblegum,balletslipper,crepe,magenta,hotpink]
random.choice(list1)()
def barbiec(print):
if random.choice(list1)() == barbie():

最突出的问题是你没有引用你的颜色名称。尽我所能挽救你的逻辑,我相信以下大致实现了你所描述的程序的功能:

from turtle import Screen, Turtle
from random import choice, randint
from functools import partial
COLORS = [
'gold', 'pink', 'purple', 'cyan', 'yellow',
'aqua', 'black', 'tan', 'salmon', 'coral',
'orange', 'red', 'brown', 'light yellow', 'beige',
'gray', 'silver', 'light gray', 'magenta', 'hotpink',
]
DIAMETER = 35
CURSOR_SIZE = 20
def create_circle(color):
circle = Turtle()
circle.hideturtle()
circle.shape('circle')
circle.shapesize(DIAMETER / CURSOR_SIZE)
circle.color(color)
circle.onclick(partial(evaluate, circle))
circle.penup()
circle.goto(randint(DIAMETER - width//2, width//2 - DIAMETER), randint(DIAMETER - height//2, height//2 - DIAMETER))
circle.showturtle()
return circle
chosen = None
def choose():
global chosen
chosen = choice(COLORS)
print("Your hint is", chosen)
def evaluate(circle, x, y):
if circle.fillcolor() == chosen:
print("That's Right!")
choose()
else:
print("Try Again.")
screen = Screen()
width, height = screen.window_width(), screen.window_height()
circles = [create_circle(color) for color in COLORS]
choose()
screen.mainloop()

最新更新