OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

My snake isn't moving left when it does in the tutorial. (Udemy 100 Days of Code Python Bootcamp lesson 189)

  • Thread starter Thread starter William Van Voorst
  • Start date Start date
W

William Van Voorst

Guest
So, I am making the Snake() module in my code, having it move, and my left() function isn't working! It doesn't do anything. I need help.

So, I was setting its heading, and I made it to 180, and it was doing nothing. I do not know what is wrong with it. Please help! Here is my code.

Code:
import turtle as t


STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20


class Snake:
    def __init__(self):

        self.segments = []

        for position in STARTING_POSITIONS:
            new_turtle = t.Turtle(shape='square')
            new_turtle.penup()
            new_turtle.color("white")
            new_turtle.goto(position)
            self.segments.append(new_turtle)

    def move(self):
        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()
            new_coords = (new_x, new_y)
            self.segments[seg_num].goto(new_coords)
        self.segments[0].forward(MOVE_DISTANCE)

    def up(self):
        self.segments[0].setheading(90)

    def left(self):
        self.segments[0].setheading(180)

    def down(self):
        self.segments[0].setheading(270)

    def right(self):
        self.segments[0].setheading(0)

It's in two files. Here is the other:

Code:
import turtle as t
from snake import Snake
import time

screen = t.Screen()
screen.bgcolor('black')
screen.setup(width=600, height=600)
screen.title("Snake(Still in Progress)")
screen.tracer(0)
snake = Snake()
screen.update()

screen.listen()


game_is_on = True

while game_is_on:
    screen.update()
    time.sleep(0.1)
    screen.onkey(snake.up, "Up")
    screen.onkey(snake.down, "Down")
    screen.onkey(snake.up, "Left")
    screen.onkey(snake.right, "Right")
    snake.move()


screen.exitonclick()
<p>So, I am making the Snake() module in my code, having it move, and my left() function isn't working! It doesn't do anything. I need help.</p>
<p>So, I was setting its heading, and I made it to 180, and it was doing nothing. I do not know what is wrong with it. Please help! Here is my code.</p>
<pre><code>import turtle as t


STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20


class Snake:
def __init__(self):

self.segments = []

for position in STARTING_POSITIONS:
new_turtle = t.Turtle(shape='square')
new_turtle.penup()
new_turtle.color("white")
new_turtle.goto(position)
self.segments.append(new_turtle)

def move(self):
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()
new_coords = (new_x, new_y)
self.segments[seg_num].goto(new_coords)
self.segments[0].forward(MOVE_DISTANCE)

def up(self):
self.segments[0].setheading(90)

def left(self):
self.segments[0].setheading(180)

def down(self):
self.segments[0].setheading(270)

def right(self):
self.segments[0].setheading(0)
</code></pre>
<p>It's in two files. Here is the other:</p>
<pre><code>import turtle as t
from snake import Snake
import time

screen = t.Screen()
screen.bgcolor('black')
screen.setup(width=600, height=600)
screen.title("Snake(Still in Progress)")
screen.tracer(0)
snake = Snake()
screen.update()

screen.listen()


game_is_on = True

while game_is_on:
screen.update()
time.sleep(0.1)
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.up, "Left")
screen.onkey(snake.right, "Right")
snake.move()


screen.exitonclick()
</code></pre>
 

Latest posts

ن
Replies
0
Views
1
نعمان منذر محمود الجميلي
ن
Top