Search code examples
pythonattributesturtle-graphics

How can I solve the AttributeError: 'NoneType' object has no attribute 'backward'


I am trying to write a Turtle crossing game using Turtle graphics but getting the attribute error. How can I fix this?

Can someone please help me fix it? Below is my code.

`

car.backward(STARTING_MOVE_DISTANCE)    
    ^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'backward'

This is my main.py

from turtle import Turtle, Screen
from player import Player
from car_manager import CarManager
import time

screen = Screen()
player = Player()
cars = CarManager()

screen.bgcolor = "white"
screen.setup(width=600, height=600)
screen.tracer(0)

screen.listen()
screen.onkey(player.up, "Up")

is_game_on = True

while is_game_on:
    time.sleep = 0.1
    screen.update()

    cars.create_car()
    cars.move()



screen.exitonclick()

This is my player.py

STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280
from turtle import Turtle

class Player(Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.setheading(90)
        self.shape("turtle")
        self.color("black")
        self.goto((STARTING_POSITION))
    
    def up(self):
        if self.ycor() < 280:
            self.forward(MOVE_DISTANCE)

This is car_manager.py

from turtle import Turtle, penup
import turtle
import random

COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 10


class CarManager(Turtle):
    def __init__(self):
        super().__init__()
        self.all_cars = []
        
    def create_car(self):
        new_car = Turtle("square")
        new_car = self.color(random.choice(COLORS))
        new_car = penup()
        new_car = self.shapesize(stretch_wid=1,stretch_len=2)
        random_y = random.randint(-250, 250)
        new_car = self.goto(290, random_y)
        self.all_cars.append(new_car)

    def move(self):
        for car in self.all_cars:
            car.backward(STARTING_MOVE_DISTANCE)

How can I check the attribute error stated above? Can someone please help


Solution

  • The error in the code is due to the assignment statements in the create_car method of the CarManager class. Instead of setting the attributes of the new car, the statements are overwriting the new_car variable with different values, and eventually, new_car becomes None, leading to the AttributeError.

    code of CarManager class

    class CarManager(Turtle):
        def __init__(self):
            super().__init__()
            self.all_cars = []
    
        def create_car(self):
            new_car = Turtle("square")
            new_car.color(random.choice(COLORS))
            new_car.penup()
            new_car.shapesize(stretch_wid=1, stretch_len=2)
            random_y = random.randint(-250, 250)
            new_car.goto(290, random_y) 
            self.all_cars.append(new_car)
    
        def move(self):
            for car in self.all_cars:
                car.backward(STARTING_MOVE_DISTANCE)