如何解决 AttributeError: 'NoneType' 对象没有属性 'backward'

问题描述 投票:0回答:0

我正在尝试使用海龟图形编写海龟穿越游戏,但出现属性错误。我该如何解决这个问题?

有人可以帮我解决这个问题吗?下面是我的代码。

`

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

这是我的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()

这是我的播放器.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)

这是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)

如何查看上述属性错误?有人可以帮忙吗

python attributes turtle-graphics
© www.soinside.com 2019 - 2024. All rights reserved.