Community
    • Login

    The snake game in python

    Scheduled Pinned Locked Moved Blogs
    2 Posts 2 Posters 1.5k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Tulio PiolaT Offline
      Tulio Piola
      last edited by

      Hi: D

      My native language is not Spanish and I am using the translator, sorry if you found spelling problems

      Well, in this publication I will share the code of the snake game, first of all, credits to the channel “Python World”, or, in English “Python World”

      here is the code:

      import turtle
      import time
      import random

      posponer = 0.1

      score = 0
      high_score = 0

      wn = turtle.Screen()
      wn.title(“Snake”)
      wn.bgcolor(“light green”)
      wn.setup(width = 600, height = 600)
      wn.tracer(0)

      cabeza = turtle.Turtle()
      cabeza.speed(0)
      cabeza.shape(“square”)
      cabeza.color(“green”)
      cabeza.penup()
      cabeza.goto(0,0)
      cabeza.direction = “stop”

      comida = turtle.Turtle()
      comida.speed(0)
      comida.shape(“circle”)
      comida.color(“red”)
      comida.penup()
      comida.goto(0,100)

      segmentos = []

      texto = turtle.Turtle()
      texto.speed(0)
      texto.color(“white”)
      texto.penup()
      texto.hideturtle()
      texto.goto(0,220)
      texto.write(“Score: 0 High Score: 0”, align = “center”, font =(“Arial Black”, 24, “normal”))

      def arriba():
      cabeza.direction = “up”
      def abajo():
      cabeza.direction = “down”
      def izquierda():
      cabeza.direction = “left”
      def derecha():
      cabeza.direction = “right”

      def mov():
      if cabeza.direction == “up”:
      y = cabeza.ycor()
      cabeza.sety(y + 20)

      if cabeza.direction == "down":
          y = cabeza.ycor()
          cabeza.sety(y - 20)
          
      if cabeza.direction == "left":
          x = cabeza.xcor()
          cabeza.setx(x - 20)
          
      if cabeza.direction == "right":
          x = cabeza.xcor()
          cabeza.setx(x + 20)
      

      wn.listen()
      wn.onkeypress(arriba, “Up”)
      wn.onkeypress(abajo, “Down”)
      wn.onkeypress(izquierda, “Left”)
      wn.onkeypress(derecha, “Right”)

      while True:
      wn.update()

      if cabeza.xcor() > 280 or cabeza.xcor() < -290 or cabeza.ycor() > 280 or cabeza.ycor() < -280:
          time.sleep(1)
          cabeza.goto(0,0)
          cabeza.direction = "stop"
          
          for segmento in segmentos:
              segmento.goto(1000,1000)
              
          segmentos.clear()
          
          score = 0
          texto.clear()
          texto.write("Score: {}      High Score: {}".format(score, high_score), 
                  align = "center", font =("Arial Black", 24, "normal"))
          
      
      if cabeza.distance(comida) < 20:
          x = random.randint(-280,280)
          y = random.randint(-280,280)
          comida.goto(x,y)
          
          cuerpo = turtle.Turtle()
          cuerpo.speed(0)
          cuerpo.shape("square")
          cuerpo.color("green")
          cuerpo.penup()
          segmentos.append(cuerpo)
          
          score += 10
          
          if score > high_score:
              high_score = score
              
          texto.clear()
          texto.write("Score: {}     High Score: {}".format(score, high_score), 
                  align = "center", font =("Arial Black", 24, "normal"))
          
      totalSeg = len(segmentos)
      for index in range(totalSeg -1, 0, -1):
          x = segmentos[index - 1].xcor()
          y = segmentos[index - 1].ycor()
          segmentos[index].goto(x,y)
          
      if totalSeg > 0:
          x = cabeza.xcor()
          y = cabeza.ycor()
          segmentos[0].goto(x,y)
          
      mov()
      
      for segmento in segmentos:
          if segmento.distance(cabeza) < 20:
              time.sleep(1)
              cabeza.goto(0,0)
              cabeza.direction = "stop"
              
              for segmento in segmentos:
                  segmento.goto(1000,1000)
                  
              segmentos.clear
      
      time.sleep(posponer)
      
      1 Reply Last reply Reply Quote 0
      • Alan KilbornA Offline
        Alan Kilborn
        last edited by

        One question: WHY?

        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors