简体   繁体   中英

How to move a turtle with keys

I want to have a race with these four turtles where each turtle has a different button to move it two pixels forward but i can't get it to work and i was wondering in anyone here could help?

from turtle import *
from random import randint
speed(0)
penup()
goto(-140,140)
for step in range(16):
  write(step, align='center')
  right(90)
  forward(10)
  pendown()
  forward(150)
  penup()
  backward(160)
  left(90)
  forward(20)
ada = Turtle()
ada.color('red')
ada.shape("turtle")
ada.penup()
ada.goto(-160, 100)
ada.right(360)
ada.pendown()
bmb = Turtle()
bmb.color('cyan')
bmb.shape("turtle")
bmb.penup()
bmb.goto(-160, 70)
bmb.right(360)
bmb.pendown()
cbc = Turtle()
cbc.color('lawn green')
cbc.shape("turtle")
cbc.penup()
cbc.goto(-160, 40)
cbc.right(360)
cbc.pendown()
dgd = Turtle()
dgd.color('dark violet')
dgd.shape("turtle")
dgd.penup()
dgd.goto(-160, 10)
dgd.right(360)
dgd.pendown()

You can rely on the turtle library itself for this. For example with the onkey function. Just as an example:

from turtle import *

def up():
    # ...handle...

onkey(up, 'Up')

You can find much more about this here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM