英文:
How can I solve the AttributeError: 'Snake' object has no attribute 'distance'
问题
You can fix the attribute error in your Snake game code by ensuring that the distance
method is called correctly on the objects you intend to use it with. In your case, the error message indicates that you're trying to use distance
on a "Snake" object, which does not have that attribute. It's likely that you want to calculate the distance between the snake's head and the food.
To fix this issue, you can make the following modifications:
- In your
main.py
file, change this line:
if snake.head.distance(food) < 15:
to:
if snake.turtle_list[0].distance(food) < 15:
This change accesses the head of the snake by using turtle_list[0]
, and then calculates the distance between the snake's head and the food.
- Additionally, in your
food.py
file, modify this line:
self.refresh
to:
self.refresh()
You need to call the refresh
method with parentheses to make it work as intended.
These modifications should help you resolve the attribute error you mentioned in your Snake game code.
英文:
I am trying to write a Snake game using Turtle graphics but getting the attribute error. How can I fix this?
Error:
> AttributeError: 'Snake' object has no attribute 'distance.'
This is my main.py
from turtle import Screen, Turtle
import turtle
import time
from snake import Snake
from food import Food
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("My snake game")
screen.tracer(0)
snake = Snake()
food = Food()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.right, "Right")
screen.onkey(snake.left, "Left")
snake.create_turtle()
is_game_on = True
while is_game_on:
screen.update()
time.sleep(0.1)
snake.move()
#detect collision with food
if snake.head.distance(food) < 15:
food.refresh()
screen.exitonclick()
This is my snake.py
from turtle import Turtle
X_POSITION = [0, -20, -40]
MOVE_POSITION = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.turtle_list = []
self.create_turtle()
self.move()
def create_turtle(self):
for turtle_index in range(0, 3):
new_turtle = Turtle(shape="square")
new_turtle.color("white")
new_turtle.penup()
new_turtle.goto(x=X_POSITION[turtle_index],y=0)
self.turtle_list.append(new_turtle)
def move(self):
for seg in range(len(self.turtle_list) -1, 0, -1): #(same as start= 2, stop= 0, step= 1):
new_x = self.turtle_list[seg -1].xcor()
new_y = self.turtle_list[seg -1].ycor()
self.turtle_list[seg].goto(x=new_x, y=new_y)
self.turtle_list[0].forward(MOVE_POSITION)
def up(self):
if self.turtle_list[0].heading() != DOWN:
self.turtle_list[0].setheading(UP)
def down(self):
if self.turtle_list[0].heading() != UP:
self.turtle_list[0].setheading(DOWN)
def left(self):
if self.turtle_list[0].heading() != RIGHT:
self.turtle_list[0].setheading(LEFT)
def right(self):
if self.turtle_list[0].heading() != LEFT:
self.turtle_list[0].setheading(RIGHT)
This is my food.py
from turtle import Turtle, circle
import random
class Food(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.shapesize(stretch_len=0.5, stretch_wid=0.5)
self.penup()
self.speed("fastest")
self.color("green")
self.refresh
def refresh(self):
random_x = random.randint(-280, 280)
random_y = random.randint(-280, 280)
self.goto(random_x,random_y)
How can I check the attribute error stated above?
答案1
得分: 0
snake.head在snake.py中没有提到snake.turtle_list[0]。
以下代码有效。
如果snake.turtle_list[0]与食物的距离小于15:
食物刷新()
英文:
snake.head was not mentioned in the snake.py for snake.turtle_list[0].
The below code worked.
if snake.turtle_list[0].distance(food) < 15:
food.refresh()
答案2
得分: 0
我认为你应该将蛇类的定义更改为这样:
class Snake(Turtle):
def __init__(self):
super().__init__()
'(Turtle)' 表示它将继承自海龟类。这意味着你忘记了从海龟类继承。
如果这不能解决问题,请尝试这样做:
if snake.turtle_list[0].distance(food) < 15:
food.refresh()
如果第二种方法有效,很可能你忘记了在 snake.head
中定义头部。
英文:
I think you should change your definition of the snake class to this:
class Snake(Turtle):
def __init__(self):
super().__init__()
The '(Turtle)' means it will inherit from the turtle class. This means you have forgotten to inherit from the turtle class.
If that doesn't fix the problem, try this:
if snake.turtle_list[0].distance(food) < 15:
food.refresh()
If the second one works, most likely you have forgotten to define the head in snake.head
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论