如何解决 AttributeError: ‘Snake’ 对象没有 ‘distance’ 属性错误。

huangapple go评论61阅读模式
英文:

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:

  1. 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.

  1. 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(&quot;black&quot;)
screen.title(&quot;My snake game&quot;)
screen.tracer(0)


snake = Snake()
food = Food()

screen.listen()
screen.onkey(snake.up, &quot;Up&quot;)
screen.onkey(snake.down, &quot;Down&quot;)
screen.onkey(snake.right, &quot;Right&quot;)
screen.onkey(snake.left, &quot;Left&quot;)

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) &lt; 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=&quot;square&quot;)
            new_turtle.color(&quot;white&quot;)
            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(&quot;circle&quot;)
        self.shapesize(stretch_len=0.5, stretch_wid=0.5)
        self.penup()
        self.speed(&quot;fastest&quot;)
        self.color(&quot;green&quot;)
        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) &lt; 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) &lt; 15:

     food.refresh()

If the second one works, most likely you have forgotten to define the head in snake.head

huangapple
  • 本文由 发表于 2023年6月26日 01:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76551741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定