Advertisement
AlfonsoPEREZ

snake python turtle by alfonsoperez

Apr 15th, 2020
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. from turtle import Turtle, Screen
  2. import time
  3. import random
  4.  
  5. delay = 0.1
  6.  
  7. screen = Screen()
  8. screen.title("Snake Game")
  9. screen.bgcolor("white")
  10. screen.setup(width=650, height=750)
  11. screen.tracer(0)
  12.  
  13. t = Turtle()
  14. t.width(15)
  15. t.hideturtle()
  16. t.left(180)
  17. t.up()
  18. t.forward(310)
  19. t.right(90)
  20. t.forward(310)
  21. t.right(90)
  22. t.down()
  23. t.forward(615)
  24. t.right(90)
  25. t.forward(615)
  26. t.right(90)
  27. t.forward(615)
  28. t.right(90)
  29. t.forward(615)
  30.  
  31. food = Turtle()
  32. food.speed(0)
  33. food.shape("circle")
  34. food.color("red")
  35. food.penup()
  36. food.goto(0,100)
  37.  
  38. hd = Turtle()
  39. hd.shape("square")
  40. hd.penup()
  41. hd.color("green")
  42. hd.speed(-1)
  43. hd.direction = "stop"
  44.  
  45. pen = Turtle()
  46. pen.speed(-1)
  47. pen.color("Black")
  48. pen.up()
  49. pen.hideturtle()
  50. pen.goto(0, 330)
  51. pen.write("SNAKE LENGTH: 1",False,"center",font=("Arial Narrow",20,"bold"))
  52.  
  53. length = 1
  54.  
  55. segments = []
  56.  
  57. def go_up():
  58.     if hd.direction != "down":
  59.         hd.direction = "up"
  60.  
  61. def go_down():
  62.     if hd.direction != "up":
  63.         hd.direction = "down"
  64.  
  65. def go_left():
  66.     if hd.direction != "right":
  67.         hd.direction = "left"
  68.  
  69. def go_right():
  70.     if hd.direction != "left":
  71.         hd.direction = "right"
  72.  
  73. def movement():
  74.     if hd.direction == "up":
  75.         y = hd.ycor()
  76.         hd.sety(y + 20)
  77.  
  78.     if hd.direction == "down":
  79.         y = hd.ycor()
  80.         hd.sety(y - 20)
  81.  
  82.     if hd.direction == "left":
  83.         x = hd.xcor()
  84.         hd.setx(x - 20)
  85.  
  86.     if hd.direction == "right":
  87.         x = hd.xcor()
  88.         hd.setx(x + 20)
  89.        
  90.  
  91. screen.listen()
  92. screen.onkeypress(go_up, "Up")
  93. screen.onkeypress(go_up, "w")
  94.  
  95. screen.onkeypress(go_down, "Down")
  96. screen.onkeypress(go_down, "s")
  97.  
  98. screen.onkeypress(go_left, "Left")
  99. screen.onkeypress(go_left, "a")
  100.  
  101. screen.onkeypress(go_right, "Right")
  102. screen.onkeypress(go_right, "d")
  103.  
  104. gameover = False
  105.  
  106. while True:
  107.  
  108.     screen.update()
  109.     time.sleep(delay)
  110.  
  111.     if hd.distance(food) == 0:
  112.         x = random.randint(-14, 14)
  113.         y = random.randint(-14, 14)
  114.         food.goto(x * 20, y * 20)
  115.  
  116.         length += 1
  117.         pen.clear()
  118.         pen.write("SNAKE LENGTH: {}".format(length),False,"center",font=("Arial Narrow",20,"bold"))
  119.  
  120.         new_segment = Turtle()
  121.         new_segment.speed(-1)
  122.         new_segment.shape("square")
  123.         new_segment.color("limegreen")
  124.         new_segment.penup()
  125.         segments.append(new_segment)
  126.  
  127.         if delay > 0.001:
  128.             delay -= 0.001
  129.  
  130.     if hd.xcor()>290 or hd.xcor()<-290 or hd.ycor()>290 or hd.ycor()<-290:
  131.         pen.clear()
  132.         pen.write("GAME OVER, YOUR FINAL LENGTH IS: {}".format(length),False,"center",font=("Arial Narrow",20,"bold"))
  133.         gameover = True
  134.  
  135.     for i in range(len(segments)-1, 0, -1):
  136.         x = segments[i-1].xcor()
  137.         y = segments[i-1].ycor()
  138.         segments[i].goto(x, y)
  139.  
  140.  
  141.     if len(segments) > 0:
  142.         x = hd.xcor()
  143.         y = hd.ycor()
  144.         segments[0].goto(x,y)
  145.  
  146.     movement()
  147.  
  148.     for segment in segments:
  149.         if segment.distance(hd) == 0:
  150.             pen.clear()
  151.             pen.goto(0, 0)
  152.             pen.write("GAME OVER, YOUR FINAL LENGTH IS: {}".format(length),False,"center",font=("Arial Narrow",20,"bold"))
  153.             gameover = True
  154.             break
  155.  
  156.     if gameover == True:
  157.         break
  158. #https://www.youtube.com/channel/UCQor7IURWM-lGT-tmFbFSCw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement