помощь по созданию игры Snake

AlexSendler

Новичок
Пользователь
Июл 21, 2020
10
1
3
Операционная система Windows 10
Python version 3.8.5
среда разработки Visual studio 2019
Pip List
{
urlib3(1.25.10)
requests(2.24.0)
pyRect(0.1.4)
pip(20.2.3)
pillow(7.2.0)
chardet(3.0.4)
certifi(2020.6.20)
idna(2.20)
turtle
time
}

ошибка при запуске:

Traceback (most recent call last):
File "C:\Users\rafes\source\repos\Snake\Snake\Snake.py", line 63, in <module>
move()
File "C:\Users\rafes\source\repos\Snake\Snake\Snake.py", line 35, in move
if head.direction == "up":
AttributeError: 'Turtle' object has no attribute 'direction'
Для продолжения нажмите любую клавишу . . .


Код:
import turtle
import time

delay = 0.1

Window = turtle.Screen()
Window.title("Snake")
Window.bgcolor("green")
Window.setup(width=800,height=600)
Window.cv._rootwindow.resizable(False, False)
Window.tracer(0)

head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.penup()
head.goto(0,0)


def go_up():
    head.direction = "up"

def go_down():
    head.direction = "down"

def go_left():
    head.direction = "left"

def go_right():
    head.direction = "right"


def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

        if head.direction == "down":
            y = head.ycor()
        head.sety(y - 20)

        if head.direction == "left":
            x = head.xcor()
        head.setx(x - 20)

        if head.direction == "right":
            x = head.xcor()
        head.setx(x + 20)


Window.listen()
Window.onkeypress(go_up , "w")
Window.onkeypress(go_down , "s")
Window.onkeypress(go_left , "a")
Window.onkeypress(go_right , "d")


while True:
    Window.update()

    move()

    time.sleep(delay)


Window.mainloop()
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
После строки
Python:
head.goto(0,0)
добавите строку
Python:
head.direction = "stop"
и в функции move правильно нужно правильно расставить отступы.
 

AlexSendler

Новичок
Пользователь
Июл 21, 2020
10
1
3
Операционная система Windows 10
Python version 3.8.5
среда разработки Visual studio 2019
Pip List
{
urlib3(1.25.10)
requests(2.24.0)
pyRect(0.1.4)
pip(20.2.3)
pillow(7.2.0)
chardet(3.0.4)
certifi(2020.6.20)
idna(2.20)
turtle
time
}

сам код запускается однако работает только кнопка W нажимая на остальные кнопки объект останавливается но не поворачивается в другие стороны не могу понять в чем проблема помогите пожалуйста

Код:
import turtle
import time
import random


delay = 0.1

Window = turtle.Screen()
Window.title("Snake")
Window.bgcolor("green")
Window.setup(width=800,height=600)
Window.cv._rootwindow.resizable(False, False)
Window.tracer(0)

head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.penup()
head.goto(0,0)
head.direction = "stop"

FOOD = turtle.Turtle()
FOOD.speed(0)
FOOD.shape("circle")
FOOD.color("red")
FOOD.penup()
FOOD.goto(0,150)


def go_up():
    head.direction = "up"

def go_down():
    head.direction = "down"

def go_left():
    head.direction = "left"
        

def go_right():
     head.direction = "right"
          
      

def move():
        if head.direction == "up":
            y = head.ycor()
            head.sety(y +20)

            if head.direction == "down":
                y = head.ycor()
                head.sety(y - 20)

                if head.direction == "left":
                    x = head.xcor()
                    head.setx(x - 20)

                    if head.direction == "right":
                        x = head.xcor()
                        head.setx(x + 20)



Window.listen()
Window.onkeypress(go_up, "w")
Window.onkeypress(go_down, "s")
Window.onkeypress(go_left, "a")
Window.onkeypress(go_right, "d")


while True:
    Window.update()

    if head.distance(FOOD) < 20:
        x = random.randint(-290, 290)
        y = random.randint(-290, 290)
        FOOD.goto(x,y)


    move()

    time.sleep(delay)


Window.mainloop()
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
Python:
def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y +20)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 20)
 

Форум IT Специалистов