0

I am building a simple python game using turtle graphics that is a car that goes around a track. I have built the track and the car, and I know how to turn the car in different directions, but I don't know how to get the car to move in the specific direction that it is facing. Does anyone have any ideas? This is what I have so far:

    import turtle

    #Screen
    wn = turtle.Screen()
    wn.title('Car')
    wn.bgcolor('black')
    wn.setup(width=1200, height=1200)
    wn.tracer(0)

    #Track
    track = turtle.Turtle()
    track.color('white')
    track.speed(0)
    track.penup()
    track.goto(-550, 0)
    track.pendown()
    track.goto(-550, 300)
    track.goto(-100,370)
    track.goto(100, 210)
    track.goto(300, 380)
    track.goto(580, 100)
    track.goto(570, -300)
    track.goto(300, -370)
    track.goto(0, -250)
    track.goto(-300, -200)
    track.goto(-570, -250)
    track.goto(-550, 0)
    track.hideturtle()

    #Track 2
    track2 = turtle.Turtle()
    track2.color('white')
    track2.speed(0)
    track2.penup()
    track2.goto(-450, 0)
    track2.pendown()
    track2.goto(-450, 230)
    track2.goto(-150, 250)
    track2.goto(100, 100)
    track2.goto(300, 200)
    track2.goto(460, 100)
    track2.goto(450, -220)
    track2.goto(300, -250)
    track2.goto(0, -130)
    track2.goto(-300, -100)
    track2.goto(-450, 0)
    track2.hideturtle()

    #Start line
    line = turtle.Turtle()
    line.color('white')
    line.speed(0)
    line.penup()
    line.goto(-550, 0)
    line.pendown()
    line.goto(-450,0)
    line.hideturtle()

    #Car
    car = turtle.Turtle()
    car.color('red')
    car.speed(0)
    car.penup() 
    car.shape('square')
    car.shapesize(stretch_wid=1, stretch_len=2)
    car.goto(-500, 0)
    car.setheading(90)



    #Move car
    def turn_right():
        car.right(20)
    def turn_left():
        car.left(20)


    #Key Bindings
    wn.listen()
    wn.onkey(turn_right, 'd')
    wn.onkey(turn_left, 'a')


    while True:
        wn.update()

    turtle.mainloop()

1 Answer 1

0

The simple answer is to change this:

while True:
    wn.update()

to instead be:

while True:
    car.forward(1)
    wn.update()

and you'll find your car moves foward and requires you to steer it to stay on the track. The more complicated answer is that while True: has no business in an event-driven environment like turtle and that what you need is a timer event:

from turtle import Screen, Turtle, mainloop

# Move car
def turn_right():
    car.right(20)

def turn_left():
    car.left(20)

def move():
    car.forward(1)
    screen.update()
    screen.ontimer(move, 25)

# Screen
screen = Screen()
screen.title('Car')
screen.bgcolor('black')
screen.setup(width=1200, height=1200)
screen.tracer(0)

# Track
track = Turtle()
track.hideturtle()
track.color('white')

track.penup()
track.goto(-550, 0)
track.pendown()

track.goto(-550, 300)
track.goto(-100, 370)
track.goto(100, 210)
track.goto(300, 380)
track.goto(580, 100)
track.goto(570, -300)
track.goto(300, -370)
track.goto(0, -250)
track.goto(-300, -200)
track.goto(-570, -250)
track.goto(-550, 0)

track.penup()
track.goto(-450, 0)
track.pendown()

track.goto(-450, 230)
track.goto(-150, 250)
track.goto(100, 100)
track.goto(300, 200)
track.goto(460, 100)
track.goto(450, -220)
track.goto(300, -250)
track.goto(0, -130)
track.goto(-300, -100)
track.goto(-450, 0)

# Start line
line = Turtle()
line.hideturtle()
line.color('white')

line.penup()
line.setx(-550)
line.pendown()
line.setx(-450)

# Car
car = Turtle()
car.shape('square')
car.shapesize(stretch_wid=1, stretch_len=2)
car.color('red')
car.setheading(90)
car.penup()
car.setx(-500)

# Key Bindings
screen.onkey(turn_right, 'd')
screen.onkey(turn_left, 'a')
screen.listen()

move()

mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.