demaglio
demaglio

Reputation: 3

Problem moving snake in a snake game using Turtle

I'm trying to create an easier version of the snake game. Everything in the code looks ok for me but I can't make snake move as I want to. Can you help me why my code doesn't work? I don't understand why my code is not okay.

I searched for some similar games codes, but they all used time. and I couldn't understand the need for that.

Here is the code:

import turtle
import random


window = turtle.Screen()
window.screensize(600, 600)
window.title("Snake Eats Tomato Game")
window.bgcolor("skyblue")
window.tracer(0)


snake = turtle.Turtle()
snake.color("dark blue")
snake.shape("square")
snake.shapesize(1)
snake.speed(1)
snake.penup()
snake.goto(0, 100)
snake.direction = "stop"


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

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

    if snake.direction == "left":
        x = snake.xcor()
        snake.setx(x + 20)

    if snake.direction == "right":
        x = snake.xcor()
        snake.setx(x - 20)


point = 0

point_table = turtle.Turtle()
point_table.speed(0)
point_table.shape("square")
point_table.color("green")
point_table.penup()
point_table.hideturtle()
point_table.goto(-200, 200)
point_table.write(
    "POİNT: {}".format(point), align="center", font=("Courier", 25, "normal")
)


def go_left():
    if snake.direction != "right":
        snake.direction = "left"


def go_right():
    if snake.direction != "left":
        snake.direction = "right"


def go_up():
    if snake.direction != "down":
        snake.direction = "up"


def go_down():
    if snake.direction != "up":
        snake.direction = "down"


window.listen()
window.onkey(go_left, "Left")
window.onkey(go_right, "Right")
window.onkey(go_up, "Up")
window.onkey(go_down, "Down")


tomato = turtle.Turtle()
tomato.penup()
tomato.color("tomato")
tomato.shape("circle")
tomato.speed(0)
tomato.setposition(random.randint(-300, 300), random.randint(-300, 300))


while True:
    window.update()
    snake.forward(3)
    move()

    if snake.xcor() < -330 or snake.xcor() > 330:
        snake.right(90)
    if snake.ycor() < -330 or snake.ycor() > 330:
        snake.right(90)

    if snake.distance(tomato) < 20:
        point += 1
        point_table.clear()
        point_table.write(
            "PUAN: {}".format(point), align="center", font=("Courier", 25, "normal")
        )
        tomato.setposition(random.randint(-300, 300), random.randint(-300, 300))

Upvotes: 0

Views: 446

Answers (1)

0stone0
0stone0

Reputation: 44275

There were some 'errors' in your code, I'll try to adres most of them, one by one:


  1. Instead of always calling snake.forward(3) in the while True, change snake.direction = "stop" to snake.direction = "right" as the starting movement. This will keep the while loop clean

  2. The go_left (etc) functions had an if to check the reversed way, I've removed those, just overwrite the direction

  3. snake.xcor() < -330 these won't work, not every screen is 330 pixels. You'll need to use window.window_width() and window.window_height() to get the current window size, and use those values to detect the edges

  4. WHen you detect an edge, don't do snake.right(90), as this will instant move the snake. Just change the direction, to bounce it:

    if (curX < window_max_left):
        snake.direction = 'left'
    if (curX > window_max_right):
        snake.direction = 'right'
    if (curY > window_max_up):
        snake.direction = 'down'
    if (curY < window_max_down):
        snake.direction = 'up'
    

Applying those points, fixes the movement, and let the snake bounce of walls
* Please see the example recording below the code


The final code looks like:

import turtle
import random


window = turtle.Screen()
window.screensize(600, 600)
window.title("Snake Eats Tomato Game")
window.bgcolor("skyblue")
window.tracer(0)


snake = turtle.Turtle()
snake.color("dark blue")
snake.shape("square")
snake.shapesize(1)
snake.speed(1)
snake.penup()
snake.goto(0, 100)
snake.direction = "right"


def move():
    if snake.direction == "up":
        y = snake.ycor()
        snake.sety(y + 10)

    if snake.direction == "down":
        y = snake.ycor()
        snake.sety(y - 10)

    if snake.direction == "left":
        x = snake.xcor()
        snake.setx(x + 10)

    if snake.direction == "right":
        x = snake.xcor()
        snake.setx(x - 10)


point = 0

point_table = turtle.Turtle()
point_table.speed(0)
point_table.shape("square")
point_table.color("green")
point_table.penup()
point_table.hideturtle()
point_table.goto(-200, 200)
point_table.write(
    "POİNT: {}".format(point), align="center", font=("Courier", 25, "normal")
)


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


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


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


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


window.listen()
window.onkey(go_left, "Left")
window.onkey(go_right, "Right")
window.onkey(go_up, "Up")
window.onkey(go_down, "Down")


tomato = turtle.Turtle()
tomato.penup()
tomato.color("tomato")
tomato.shape("circle")
tomato.speed(0)
tomato.setposition(random.randint(-300, 300), random.randint(-300, 300))

window_width = window.window_width()
window_height = window.window_height()

window_max_left = -abs(window_width / 2)
window_max_right = window_width / 2
window_max_down = -abs(window_height / 2)
window_max_up = window_height / 2


while True:

    curX = snake.xcor()
    curY = snake.ycor()

    if (curX < window_max_left):
        snake.direction = 'left'
    if (curX > window_max_right):
        snake.direction = 'right'
    if (curY > window_max_up):
        snake.direction = 'down'
    if (curY < window_max_down):
        snake.direction = 'up'

    if snake.distance(tomato) < 20:
        point += 1
        point_table.clear()
        point_table.write(
            "PUAN: {}".format(point), align="center", font=("Courier", 25, "normal")
        )
        tomato.setposition(random.randint(-300, 300), random.randint(-300, 300))

    move()
    window.update()

How it looked: example gif

Upvotes: 1

Related Questions