'How do i import another turtle in python
im studying coding in bugaria in highschool. Im in 8th grade, and were studying python and processing. Since the end of this schoolyear is near, our python teacher gave us a project, he said that the project could be whatever we want. I told him, i would make a racing game, where two turtles race for the win, he just introduced us to how to control your turtle. Now, im going to import another turtle, and assign it WASD, and the first turtle is going to have ARROW KEYS assigned. now. 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, any help is highly appreciated. Also, its my first time here.Sorry for bad english, im not american.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ntg |