0
#importing 
import turtle
import random
import time

def snake_game():
      #making head global so it can be used in other functions
      global head
      #scores
      score = 0
      high_score = 0

      #setting up screen
      wn = turtle.Screen()
      wn.title("Snake Game")
      wn.bgcolor('yellow')
      wn.setup(width=600, height=600)
      wn.tracer(0)

      # making snake head
      head = turtle.Turtle()
      head.speed(0)
      head.shape("square")
      head.color("black")
      head.penup()
      head.goto(0,0)
      head.direction = "stop"

      #snake food
      food= turtle.Turtle()
      food.speed(0)
      food.shape("square")
      food.color("red")
      food.penup()
      food.goto(0,100)

      segments = []

      #scoreboards
      sc = turtle.Turtle()
      sc.speed(0)
      sc.shape("square")
      sc.color("black")
      sc.penup()
      sc.hideturtle()
      sc.goto(0,260)
      sc.write("score: 0  High score: 0", align = "center", font=("ds-digital", 24, "normal"))
        
      #keyboard bindings
      wn.listen()
      wn.onkeypress(go_up, "w")
      wn.onkeypress(go_down, "s")
      wn.onkeypress(go_left, "a")
      wn.onkeypress(go_right, "d")

      #MainLoop
      while True:
            wn.update()
            #check collision with border area
            if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:
                   time.sleep(1)
                   head.goto(0,0)
                   head.direction = "stop"
                   #hide the segments of body
                   for segment in segments:
                           segment.goto(1000,1000) #out of range
                           #clear the segments
                           segments.clear()

                           #reset score
                           score = 0

                           #reset delay
                           delay = 0.1

                           sc.clear()
                           sc.write("score: {}  High score: {}".format(score, high_score), align="center", font=("ds-digital", 24, "normal"))


            #check collision
            if head.distance(food) <20:
                     # move the food to random place
                     x = random.randint(-290,290)
                     y = random.randint(-290,290)
                     food.goto(x,y)

                      #add a new segment to the head
                     new_segment = turtle.Turtle()
                     new_segment.speed(0)
                     new_segment.shape("square")
                     new_segment.color("black")
                     new_segment.penup()
                     segments.append(new_segment)
                     #shorten the delay
                     delay -= 0.001
                     #increase the score
                     score += 10
                     if score > high_score:
                           high_score = score
                      sc.clear()
                      sc.write("score: {}  High score: {}".format(score,high_score), align="center", font=("ds-digital", 24, "normal")) 
             
           #move the segments in reverse order
           for index in range(len(segments)-1,0,-1):
                    x = segments[index-1].xcor()
                    y = segments[index-1].ycor()
                    segments[index].goto(x,y)
            #move segment 0 to head
           if len(segments)>0:
               x = head.xcor()
               y = head.ycor()
               segments[0].goto(x,y)
           
            move()
            for segment in segments:
                  if segment.distance(head)<20:
                        time.sleep(1)
                        head.goto(0,0)
                        head.direction = "stop"

                       #hide segments
                       for segment in segments:
                             segment.goto(1000,1000)
                        segments.clear()
                        score = 0
                        delay = 0.1

        #update the score     
                        sc.clear()
                        sc.write("score: {}  High score: {}".format(score,high_score), align="center", font=("ds-digital", 24, "normal"))
       

           time.sleep(delay)
     wun.mainloop()
def go_up():
     if head.direction != "down":
            head.direction = "up"
def go_down():
     if head.direction != "up":
            head.direction = "down"
def go_left():
     if head.direction != "right":
            head.direction = "left"
def go_right():
     if head.direction != "left":
            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)

Well it was working fine as a normal program as long as the code in the function snake_game was the main program it was moving, but I have to append this game with other games. I have to call this game using a function, so when I converted it to a function it didn't move . The code is running and squares dots are also coming but on pressing keys the snake is not moving. I don't know where it went wrong.

5
  • 1
    I recommend you use Pygame to make games. read pygame.org/docs
    – MHP
    Aug 11, 2021 at 14:24
  • 2
    where in the code snake_game() is being invoked? As far as I see - there is none.
    – balderman
    Aug 11, 2021 at 14:26
  • 1
    I recommend that you use pure tkinter not turtle.
    – TheLizzard
    Aug 11, 2021 at 14:27
  • i have evoked snake_game() in my main program . I forgot to mention it here.
    – tanvi
    Aug 11, 2021 at 14:30
  • 1
    @tanvi you should post a minimal reproducible example, in this case it would be one file that reproduces the problem and is as short as possbile, remove all unrelated code (that way you may yourself find the issue)
    – Matiiss
    Aug 11, 2021 at 19:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.