-1

I am trying to move a drawing slowly across the screen as I move another turtle with the arrow keys. The problem is that my code only makes the drawing move across the screen, not allowing me to move the other turtle with the keys. I have tried defining the keys inside of the while true statement and it still came out with the same outcome.

Here is my code:

from turtle import * 
setup(500, 500)
Screen()
screen = Screen()
title("Turtle Keys")
turtle1 = Turtle() 
turtle2 = Turtle()

def moving_square():
    turtle1.fillcolor("Red")
    turtle1.begin_fill()
    for i in range(4):
        turtle1.forward(50)
        turtle1.right(90)
    turtle1.end_fill()
turtle1.goto(-350, 0)
turtle1.pendown()
turtle1.hideturtle()

def k1():
    turtle2.forward(50)

def k2():
    turtle2.left(45)

def k3():
    turtle2.right(45)

def k4():
    turtle2.back(25)

onkey(k1, "Up")
onkey(k2, "Left")
onkey(k3, "Right")
onkey(k4, "Down")

while True:
    moving_square()
    screen.update()         
    turtle1.forward(5)
Matt
  • 134
  • 10
  • You need to learn multiprocessing and multithreading. Stack Overflow is not intended to replace existing documentation and tutorials. – Prune Feb 09 '21 at 22:00

2 Answers2

0

It needs listen() to react on pressed keys.

And you have this information even in documentation for onkey()

from turtle import * 

# --- functions ---

def moving_square():
    turtle1.fillcolor("Red")
    turtle1.begin_fill()
    for i in range(4):
        turtle1.forward(50)
        turtle1.right(90)
    turtle1.end_fill()
    
def k1():
    turtle2.forward(50)

def k2():
    turtle2.left(45)

def k3():
    turtle2.right(45)

def k4():
    turtle2.back(25)

# -- main ---

setup(500, 500)
Screen()
screen = Screen()
title("Turtle Keys")
turtle1 = Turtle() 
turtle2 = Turtle()

turtle1.goto(-350, 0)
turtle1.pendown()
turtle1.hideturtle()

onkey(k1, "Up")
onkey(k2, "Left")
onkey(k3, "Right")
onkey(k4, "Down")

listen()

while True:
    moving_square()
    screen.update()         
    turtle1.forward(5)
    
    
furas
  • 134,197
  • 12
  • 106
  • 148
0

For more game-like action, I would go about this a different way. First, by not drawing a red square but by making the first turtle into a red square. Second, by using a timed event instead of a while True: loop:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 500, 500
BLOCK_SIZE = 50
CURSOR_SIZE = 20

def k1():
    turtle.forward(50)

def k2():
    turtle.left(45)

def k3():
    turtle.right(45)

def k4():
    turtle.back(25)

def move():
    block.forward(1)

    if block.xcor() < WIDTH/2 + BLOCK_SIZE/2:
        screen.ontimer(move, 75)  # milliseconds

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.title("Turtle Keys")

block = Turtle()
block.hideturtle()
block.shape('square')
block.fillcolor('red')
block.shapesize(BLOCK_SIZE / CURSOR_SIZE)
block.penup()
block.setx(-WIDTH/2 - BLOCK_SIZE/2)
block.showturtle()

turtle = Turtle()
turtle.shape('turtle')
turtle.penup()

screen.onkey(k1, 'Up')
screen.onkey(k2, 'Left')
screen.onkey(k3, 'Right')
screen.onkey(k4, 'Down')
screen.listen()

move()

screen.mainloop()

You can increase the speed of the block by adjusting block.forward(1) to higher value. And if you need a trailing line, you can put the pen down.

cdlane
  • 40,441
  • 5
  • 32
  • 81