SHARE
TWEET

Untitled

a guest Jun 27th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import turtle
  2. import time
  3. import random
  4.  
  5. delay = 0.1
  6.  
  7. score = 0
  8. f = open(r'C:\Users\Dani2000\Desktop\SnakeGame', 'rb')
  9. high_score = f
  10.  
  11. #set up the screen
  12. ge = turtle.Screen()
  13. ge.title("Snake game")
  14. ge.bgcolor("black")
  15. ge.setup(width = 600, height = 600)
  16. ge.tracer(0)
  17.  
  18. #Snake Head
  19. head = turtle.Turtle()
  20. head.speed(0)
  21. head.shape("square")
  22. head.color("yellow")
  23. head.penup()
  24. head.goto(0,0)
  25. head.direction = "stop"
  26.  
  27. #snake's food
  28. food = turtle.Turtle()
  29. food.speed(0)
  30. food.shape("square")
  31. food.color("red")
  32. food.penup()
  33. food.goto(0,150)
  34.  
  35. body = []
  36.  
  37. pen = turtle.Turtle()
  38. pen.speed(0)
  39. pen.shape("square")
  40. pen.color("white")
  41. pen.penup()
  42. pen.hideturtle()
  43. pen.goto(0, 260)
  44. pen.write("Score: 0     High Score: ", high_score, align="center", font=("Light", 20, "bold italic"))
  45.  
  46. #Functions
  47. def go_up():
  48.     if head.direction != "down":
  49.         head.direction = "up"
  50.  
  51. def go_right():
  52.     if head.direction != "left":
  53.         head.direction = "right"
  54.  
  55. def go_left():
  56.     if head.direction != "right":
  57.         head.direction = "left"
  58.  
  59. def go_down():
  60.     if head.direction != "up":
  61.         head.direction = "down"
  62.  
  63.  
  64. def move():
  65.     if head.direction == "up":
  66.         y = head.ycor()
  67.         head.sety(y + 20)
  68.  
  69.     if head.direction == "down":
  70.         y = head.ycor()
  71.         head.sety(y - 20)
  72.  
  73.     if head.direction == "left":
  74.         x = head.xcor()
  75.         head.setx(x - 20)
  76.  
  77.     if head.direction == "right":
  78.         x = head.xcor()
  79.         head.setx(x + 20)
  80.  
  81. #keyboard
  82. ge.listen()
  83. ge.onkeypress(go_up, "w")
  84. ge.onkeypress(go_right, "d")
  85. ge.onkeypress(go_down, "s")
  86. ge.onkeypress(go_left, "a")
  87.  
  88.  
  89. #main game loop
  90. while True:
  91.     ge.update()
  92.  
  93.     #f = open('High_Score.txt', 'r')
  94.  
  95.     if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
  96.         #time.sleep(1)
  97.         #head.goto(0,0)
  98.         #head.direction = "stop"
  99.         pen.write("Game Over", align="center", font=("Light", 25, "bold italic"))
  100.         f = open(r'C:\Users\Dani2000\Desktop\SnakeGame', 'w')
  101.         f.close()
  102.         #ge._root.mainloop()
  103.         ge.exit()
  104.  
  105.         for i in body:
  106.             i.goto(1000,1000)
  107.         body.clear()
  108.  
  109.         score = 0
  110.         pen.clear()
  111.         pen.write("Score: {}    High Score: {}".format(score, high_score),
  112.             align="center", font=("Light", 20, "bold italic"))
  113.  
  114.  
  115.     if head.distance(food) < 20:
  116.         x = random.randint(-285, 285)
  117.         y = random.randint(-285, 285)
  118.         food.goto(x, y)
  119.         newBody = turtle.Turtle()
  120.         newBody.speed(0)
  121.         newBody.shape("square")
  122.         newBody.color("yellow", "green")
  123.         newBody.penup()
  124.         body.append(newBody)
  125.  
  126.         score += 5
  127.         if score > high_score:
  128.             high_score = score
  129.  
  130.         pen.clear()
  131.         pen.write("Score: {}    High Score: {}".format(score, high_score),
  132.             align="center", font=("Light", 20, "bold italic"))
  133.  
  134.  
  135.     for i in range(len(body)-1, 0, -1):
  136.         x = body[i-1].xcor()
  137.         y = body[i-1].ycor()
  138.         body[i].goto(x, y)
  139.  
  140.     if len(body) > 0:
  141.         x = head.xcor()
  142.         y = head.ycor()
  143.         body[0].goto(x,y)
  144.  
  145.  
  146.  
  147.     move()
  148.  
  149.     for i in body:
  150.         if i.distance(head) < 20:
  151.             #time.sleep(1)d
  152.             #head.goto(0, 0)
  153.             #head.direction = "stop"
  154.        
  155.             #for i in body:
  156.             #    i.goto(1000,1000)
  157.             #body.clear()
  158.             f = open(r'C:\Users\Dani2000\Desktop\SnakeGame', 'w')
  159.             f.close()
  160.             ge.exit()
  161.  
  162.  
  163.  
  164.     time.sleep(delay)
  165.  
  166.  
  167. ge.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top