Pycharm bug when linking turtle module with separate files - python

The issue begins when I try to link my modules/files, the turtle class does not instantiate objects to their proper values. I tried changing paths for my python interpreter, but that didn't work. The funny thing is, though- the turtle class works fine with only one module, meaning the object from the class Turtle receives the correct data I give to instantiate it: shape, size, color, etc.
# player.py
from turtle import Turtle
class Player(Turtle):
def __int__(self):
super().__init__()
self.shape('turtle')
screen.exitonclick()
#***********************
# main.py
from player import Player
from turtle import Screen
screen = Screen()
player = Player()
screen.exitonclick()

Related

Question about turtle module and Turtle() class [duplicate]

How are turtle and Turtle different from each other in python version 2.7?
import turtle
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
turtle.done()
The turtle module is unusual. To make it easier for beginning programmers, all methods of the Turtle class are also available as top level functions that operate on the default (unnamed) turtle instance. All methods of the Screen class are also available as top level functions that operate on the default (sole) screen instance. So both this:
import turtle
star = turtle.Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = turtle.Screen() # access sole screen instance
screen.mainloop() # screen instance method
and this:
import turtle
for i in range(5):
turtle.forward(50) # function, default turtle
turtle.right(144)
turtle.done() # function, mainloop() synonym, acts on singular screen instance
are both valid implementations. Many turtle programs end up mixing the functional interface with the object interface. To avoid this, I strongly recommend the following import syntax:
from turtle import Turtle, Screen
This forces the object approach to using turtle, making the functional approach unavailable:
from turtle import Turtle, Screen
star = Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = Screen() # access sole screen instance
screen.mainloop() # screen instance method
Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.
hope you have a good time!
"turtle" is actually the module that we use .
Turtle or we can say as turtle.Turtle is actually a object that you can move it change speed, color, shape and ....
here you can see an example:
enemy = turtle.Turtle(shape="rabbit.gif")
enemy.penup()
enemy.speed(0)
enemy.setposition(400, -180)
or another one:
player = turtle.Turtle(shape="turtle.gif")
player.penup()
player.speed(0)
player.left(90)
player.setposition(-400, -200)
or:
border = turtle.Turtle()
border.speed(0)
border.color("white")
border.penup()
border.setposition(-300, -300)
border.pendown()
border.pensize(3)
border.hideturtle()
hope it helps!
turtle is the module that you import while Turtle is that name of the class. Using from turtle import * removes the need for turtle.Turtle.
turtle is the name of the package while Turtle is the name of the class.
An alternate way of importing the module would be:
import turtle.Turtle
Also, are you sure the last line is turtle.done() and not star.done()?
The first turtle is called turtle and is referenced by it's name or it in a variable, the turtle.Turtle method creates a new turtle and (most of the time), you set it to a variable.
turtle is a method which contains a class which is noble Turtle

Why is turtle.done() saying it has no attribute 'done'?

I'm using the turtle module. My goal is to keep the window open, and I have heard to use turtle.done() to do so. So far, I've always got this error message:
AttributeError: type object 'Turtle' has no attribute 'done'. Did you mean: 'clone'?
Here is my code.
import turtle
from turtle import Turtle
turtle = Turtle()
turtle.forward(100)
turtle.done()
You shouldn't have a variable name the same as a module. Change the object name from turtle to t, for example, and it'll work:
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()
done is a function in the turtle module.
By writing turtle = Turtle() you overwrote the name turtle so that it no longer refers to the module, but to an instance of the class Turtle which does in fact not have a method called done.
The simplest solution would be to choose a different variable name for the Turtle instance.
done is a module-level function, not a method on the Turtle object. Import it from the turtle module on the same line where you import the Turtle class:
from turtle import Turtle, done
turtle = Turtle()
turtle.forward(100)
done()
or import the entire module as turtle (in which case you should not reuse the name turtle for one of your own variables):
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()

Is turtle.Screen a subclass of turtle.TurtleScreen in python?

from turtle import Turtle, Screen, TurtleScreen
turtle = Turtle()
screen = Screen()
print(isinstance(turtle, Turtle))
print(isinstance(screen, TurtleScreen))
print(issubclass(Screen,TurtleScreen))
print(isinstance(screen, Screen))
The last 2 prints got some error, debugging says "Screen" is not a class. But from documentation it says "Screen is a subclass of TurtleScreen". So what's wrong here? Is it related to something called Singolton object?
There is no class called Screen. There is a (standalone turtle) function called Screen that returns the singleton TurtleScreen instance. So this is correct:
screen = Screen()
print(isinstance(screen, TurtleScreen))
(The hidden reality is that it is an instance of class _Screen which is a subclass of class TurtleScreen which is a subclass of TurtleScreenBase)
For simplicity, we pretend that we are getting an instance of class Screen that we invoke methods on:
from turtle import Screen
screen = Screen()
screen.setup(800, 600)
But that's a convenient fiction.

What is the difference between turtle and Turtle?

How are turtle and Turtle different from each other in python version 2.7?
import turtle
star = turtle.Turtle()
for i in range(50):
star.forward(50)
star.right(144)
turtle.done()
The turtle module is unusual. To make it easier for beginning programmers, all methods of the Turtle class are also available as top level functions that operate on the default (unnamed) turtle instance. All methods of the Screen class are also available as top level functions that operate on the default (sole) screen instance. So both this:
import turtle
star = turtle.Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = turtle.Screen() # access sole screen instance
screen.mainloop() # screen instance method
and this:
import turtle
for i in range(5):
turtle.forward(50) # function, default turtle
turtle.right(144)
turtle.done() # function, mainloop() synonym, acts on singular screen instance
are both valid implementations. Many turtle programs end up mixing the functional interface with the object interface. To avoid this, I strongly recommend the following import syntax:
from turtle import Turtle, Screen
This forces the object approach to using turtle, making the functional approach unavailable:
from turtle import Turtle, Screen
star = Turtle() # turtle instance creation
for i in range(5):
star.forward(50) # turtle instance method
star.right(144) # turtle instance method
screen = Screen() # access sole screen instance
screen.mainloop() # screen instance method
Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.
hope you have a good time!
"turtle" is actually the module that we use .
Turtle or we can say as turtle.Turtle is actually a object that you can move it change speed, color, shape and ....
here you can see an example:
enemy = turtle.Turtle(shape="rabbit.gif")
enemy.penup()
enemy.speed(0)
enemy.setposition(400, -180)
or another one:
player = turtle.Turtle(shape="turtle.gif")
player.penup()
player.speed(0)
player.left(90)
player.setposition(-400, -200)
or:
border = turtle.Turtle()
border.speed(0)
border.color("white")
border.penup()
border.setposition(-300, -300)
border.pendown()
border.pensize(3)
border.hideturtle()
hope it helps!
turtle is the module that you import while Turtle is that name of the class. Using from turtle import * removes the need for turtle.Turtle.
turtle is the name of the package while Turtle is the name of the class.
An alternate way of importing the module would be:
import turtle.Turtle
Also, are you sure the last line is turtle.done() and not star.done()?
The first turtle is called turtle and is referenced by it's name or it in a variable, the turtle.Turtle method creates a new turtle and (most of the time), you set it to a variable.
turtle is a method which contains a class which is noble Turtle

How to make a turtle object look at where the mouse has clicked

I want to write a turtle program where the turtle goes to wherever you click. So far I have this:
from turtle import *
screen = Screen()
turtle = Turtle()
screen.onscreenclick(turtle.goto)
But the problem is the turtle object just stays facing in the same direction. I want to somehow make it look towards where it's going. How can I achieve this?
This will do what you describe:
import turtle
screen = turtle.Screen()
turtle = turtle.Turtle()
def turtle_headto(x, y):
turtle.left(turtle.towards(x, y) - turtle.heading())
turtle.goto(x, y)
screen.onscreenclick(turtle_headto)
screen.mainloop()
But the motion of the arrow/turtle isn't always optimal, i.e. sometimes it spins the long way 'round, but that's something for you to optimize (e.g. when to call left() and when to call right())

Categories

Resources