pythonpython-3.xturtle-graphicspython-turtle

How do I import another turtle in Python?


I want to make a racing game, where two turtles race for the win. I know how to control a turtle.

I'm going to import another turtle, and assign it WASD, and the first turtle is going to have ARROW KEYS assigned. How do I get another turtle, and how do I assign it separate controls?

I would also want to know how I would import a custom image that is a top down racetrack I drew in paint.


Solution

  • Welcome to the team! :) May you have lots of fun! You do not want to import a second turtle, but to instantiate a second turtle.

    Importing refers to importing a library, which runs the library code and allows you to access all the libraries functions. So when you type:

    import turtle
    

    you import / "enable" a set of functions you can refer to. One of them is turtle.Turtle(), the result of which is a new turtle object.

    After this object is created, you need to store it to a variable, so that you can refer to it and call its functions.This is what the command

    t1 = turtle.Turtle()
    

    does. As @Sembei_Norimaki writes, you can instantiate many more turtles e.g. t2 = turtle.Turtle() Each will be a different objects and it is possible e.g. to do

    t1.forward(30)
    t1.penup()
    t2.pendown()
    

    giving each turtle different commands (or in better terms calling the methods of/on each object)

    1 hour ago