pythonturtle-graphics

Snake Game (Python) using Turtle


So I've been working on my project which is to make Snake Game in Python where my snake basically eats the circle shape food and it also can pass through the walls which made it even more easier to play and I think that I might have type something wrong or it might be a version issue. I've also seen many Youtube videos but couldn't get any help. Can anyone tell me the mistake which I've made in the below code?

Snake Game In Python Using Turtle

import random
import time
import turtle

delay=0.1
score=0
highestscore=0
bodies=[]

s=turtle.Screen()
s.bgcolor("black")
s.setup(width=600, height=600)
s.title("Snake Game")
s.tracer(0)

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

food=turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("white")
food.fillcolor("yellow")
food.penup()
food.ht()
food.goto(0,150)
food.st()

sb=turtle.Turtle()
sb.shape("square")
sb.fillcolor("blue")
sb.penup()
sb.ht()

sb.goto(0,280)
sb.write("Score: 0   Highest Score: 0")

def moveup():
    if head.direction!="down":
        head.direction="up"
def movedown():
    if head.direction!="up":
        head.direction="down"
def moveleft():
    if head.direction!="right":
        head.direction="left"
def moveright():
    if head.direction!="left":
        head.direction="right"
def movestop():
    head.direction="stop"
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)

s.listen()
s.onkey(moveup,"Up")
s.onkey(movedown,"Down")
s.onkey(moveleft,"Left")
s.onkey(moveright,"Right")
s.onkey(movestop,"space")

while True:
    s.update()

    if head.xcor()>290:
        head.setx(-290)
    if head.ycor()>290:
        head.sety(-290)
    if head.ycor()<-290:
        head.sety(290)
    if head.xcor()<-290:
        head.setx(290)

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

        body=turtle.Turtle()
        body.speed(0)
        body.shape("square")
        body.color("white")
        body.fillcolor("green")
        body.penup()
        bodies.append(body) 

        score+=10

        delay+=0.004  

        if score>highestscore:
            highestscore=score
        sb.clear()
        sb.write("Score:{}   Highest Score: {}".format(score,highestscore))

    for index in range(len(bodies)-1,0,-1):
        x=bodies[index-1].xcor()
        y=bodies[index-1].ycor()
        bodies[index].goto(x,y)

    if len(bodies)>0:
        x=head.xcor()
        y=head.ycor()
        bodies[0].goto(x,y)
    move()

    for body in bodies:
        if body.distance(head)<20:
            time.sleep(1)
            head.goto(0,0)
            head.direction="stop" 

            for body in bodies:
                body.goto(1000,1000)
        
            bodies.clear()

            score=0
            delay=0.1  

            sb.clear()
            sb.write("Score:{}   Highest Score: {}".format(score,highestscore))
    
    time.sleep(delay)
s.mainloop() 

Solution

  • What keeps your game from working (eating food) are these two lines:

    x=random.randint(290,-290)
    y=random.randint(290,-290)
    

    The arguments need to be the other way around:

    x=random.randint(-290, 290)
    y=random.randint(-290, 290)