I have created a class for drawing images in Turtle that takes a state and a position argument, used for determining what item to draw and where to draw it respectively. Here’s the class with the drawing code taken out for brevity.
class turtle_graphic():
def __init__(self, state, position):
self.state = state
self.position = position
# Function which takes a state ID and a set of coordinates that set the home of the turtle
# to the centre of the current hexagon. Checks the state ID against a series of if statements to
# determine which state to draw.
def draw_state(self):
# Resets turtle to default before drawing
width(2)
color("black")
# Fourth State, or "Level D"
if (self.state == 0):
# Draws Stuff
# Third State, or "Level C"
elif (self.state == 1)
# Draws Stuff
etc, etc
I have also created an empty list:
drawing_history = []
Creating and calling the class seems to work as when I initiate the program it draws the first image perfectly with this call in use:
drawing_history.append(turtle_graphic(state_to_pass, pos()).draw_state())
However, in this program I am drawing images on a grid and the cursor moves around, I am attempting to add these drawings to a list in order to clear them if the cursor moves back towards a grid cell that it has already drawn on. For example:
for i in drawing_history:
if pos() == i.position:
i.clear()
else:
drawing_history.append(turtle_graphic(state_to_pass, pos()).draw_state())
This however returns an error code:
AttributeError: ‘NoneType’ object has no attribute ‘position’
I’m wondering how I can add classes to a list then call variables from classes within that list, if it’s possible. It’s well likely I’m going about this the wrong way entirely.
Thanks for any help you can offer, it’s much appreciated!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This line is creating issue for you.
drawing_history.append(turtle_graphic(state_to_pass, pos()).draw_state())
Because draw_state() method does not return anything so you are creating a list of NoneType and thatswhy you getting this issue.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0