我还没能在网上找到任何东西,但我需要创建一个程序:
如果按下左键,乌龟应移动到该位置并画一个小方块。
如果按下右键,乌龟应移动该位置并画一个小圆圈。
如果按下中间按钮,乌龟应该更改为不同的随机颜色。
如果使用按空格键,您还应该更改颜色。
有关如何开始的任何建议吗?
以下是我到目前为止尝试过的一些代码:
def k2(x,y): turtle.penup() turtle.setposition(x,y) turtle.pendown() turtle.circle(radius)
这是乌龟的顶级
import * setup(500, 500) Screen() title("Turtle Keys") move = Turtle() showturtle()
这是底部的
onkey(k1, "Up") onkey(k2, "Left") onkey(k3, "Right") onkey(k4, "Down") listen() mainloop()
答案 0 :(得分:0)
以下是我和您和您的代码片段描述的内容。请注意,我将左右键盘按钮的圆形和方形功能更改为鼠标左键和右键,这似乎在“移动到该位置”的上下文中更有意义:
import turtle
import random
colors = ["red", "orange", "yellow", "green", "blue", "violet"]
radius = 10
width = 20
LEFT, MIDDLE, RIGHT = 1, 2, 3
def k1(x=None, y=None): # dummy arguments so can be a click or key
turtle.color(random.choice(colors))
def k2(x, y):
turtle.penup()
turtle.setposition(x, y)
turtle.pendown()
for _ in range(4):
turtle.forward(width)
turtle.right(90)
def k3(x, y):
turtle.penup()
turtle.setposition(x, y)
turtle.pendown()
turtle.circle(radius)
turtle.setup(500, 500)
turtle.Screen().title("Turtle Keys")
turtle.onkey(k1, " ")
turtle.onscreenclick(k2, btn=LEFT)
turtle.onscreenclick(k1, btn=MIDDLE)
turtle.onscreenclick(k3, btn=RIGHT)
turtle.listen()
turtle.mainloop()