from turtle import Turtle

#Constants
START_POSITIONS =[(0,0),(-20,0),(-40,0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0

#Class
class Snake:

    def __init__(self):
        self.segments =[]
        self.create_snake()
        # segments[0] is the first square(head) of the snake
        self.head = self.segments[0]

    def create_snake(self):
        # Create snake parts
        for position in START_POSITIONS:
            self.add_segment(position)
    def add_segment(self, position):
        new_snake = Turtle("square")
        new_snake.color("white")
        new_snake.penup()
        new_snake.goto(position)
        self.segments.append(new_snake)
    def extend(self):
        self.add_segment(self.segments[-1].position())

    def move(self):
        #Moving the snake
        # (start,stop,step) sequence
        for seg_num in range(len(self.segments) - 1, 0, -1):
            new_x = self.segments[seg_num - 1].xcor()
            new_y = self.segments[seg_num - 1].ycor()
            self.segments[seg_num].goto(new_x, new_y)
        self.head.forward(MOVE_DISTANCE)

    def up(self):
        if self.head.heading()!=DOWN:
           self.head.setheading(UP)

    def down(self):
        if self.head.heading()!=UP:
           self.head.setheading(DOWN)

    def left(self):
        if self.head.heading()!=RIGHT:
           self.head.setheading(LEFT)

    def right(self):
        if self.head.heading()!=LEFT:
            self.head.setheading(RIGHT)
