Advertisement
AlfonsoPEREZ

Python basic snake ai

Apr 27th, 2020 (edited)
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. import turtle, time, random
  2.  
  3. face = "stop"
  4. body = []
  5. gameover = False
  6.  
  7. wn = turtle.Screen()
  8. wn.title("basic snake AI")
  9. wn.setup(600, 600)
  10. wn.tracer(0)
  11.  
  12. head = turtle.Turtle()
  13. head.shape("square")
  14. head.color("green")
  15. head.goto(0,0)
  16.  
  17. apple = turtle.Turtle()
  18. apple.shape("circle")
  19. apple.color("red")
  20. apple.penup()
  21. apple.goto(0, 100)
  22.  
  23. def up():
  24.     global face
  25.     if face != "down":
  26.         face = "up"
  27.  
  28. def down():
  29.     global face
  30.     if face != "up":
  31.         face = "down"
  32.  
  33. def right():
  34.     global face
  35.     if face != "left":
  36.         face = "right"
  37.  
  38. def left():
  39.     global face
  40.     if face != "right":
  41.         face = "left"
  42.  
  43. wn.listen()
  44. wn.onkeypress(up, "Up")
  45. wn.onkeypress(down, "Down")
  46. wn.onkeypress(left, "Left")
  47. wn.onkeypress(right, "Right")
  48.  
  49. while True:
  50.     wn.update()
  51.     time.sleep(0)
  52.  
  53.     if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
  54.         face = "stop"
  55.         for i in body:
  56.             i.goto(10000, 10000)
  57.         body.clear()
  58.         head.goto(0, 0)
  59.         gameover = True
  60.  
  61.     if head.xcor() == apple.xcor() and head.ycor() == apple.ycor():
  62.         x = random.randint(-14, 14)
  63.         y = random.randint(-14, 14)
  64.         apple.goto(x * 20, y * 20)
  65.  
  66.         new_segment = turtle.Turtle()
  67.         new_segment.shape("square")
  68.         new_segment.color("limegreen")
  69.         new_segment.up()
  70.         body.append(new_segment)
  71.  
  72.     ################ basic ai ################
  73.  
  74.     if head.ycor() == -280 and head.xcor() != -280: face = "left"
  75.     if head.xcor() == -280 and head.ycor() != 280: face = "up"
  76.     if head.ycor() == 280 and head.xcor() != 280: face = "right"
  77.     if head.xcor() == 280 and head.ycor() != -280: face = "down"
  78.  
  79.     if head.xcor() ==  apple.xcor() and head.ycor() > apple.ycor(): face = "down"
  80.  
  81.     for segment in body:
  82.         if face == "down" and head.ycor() - 20 == segment.ycor() and head.xcor() == segment.xcor(): face = "right"
  83.         if face == "right" and head.xcor() + 20 == segment.xcor() and head.ycor() == segment.ycor(): face = "up"
  84.         if face == "up" and head.ycor() + 20 == segment.ycor() and head.xcor() == segment.xcor(): face = "left"
  85.         if face == "left" and head.xcor() - 20 == segment.xcor() and head.ycor() == segment.ycor(): face = "down"
  86.  
  87.     ############### end of ai ################
  88.  
  89.     if face == "up": head.sety(head.ycor() + 20)
  90.     if face == "down": head.sety(head.ycor() - 20)
  91.     if face == "left": head.setx(head.xcor() - 20)
  92.     if face == "right": head.setx(head.xcor() + 20)
  93.  
  94.     for i in range(len(body)-1, 0, -1):
  95.         body[i].goto(body[i-1].xcor(), body[i-1].ycor())
  96.  
  97.     if len(body) > 0:
  98.         if face == "up":
  99.             body[0].goto(head.xcor(), head.ycor() - 20)
  100.  
  101.         if face == "down":
  102.             body[0].goto(head.xcor(), head.ycor() + 20)
  103.  
  104.         if face == "right":
  105.             body[0].goto(head.xcor() - 20, head.ycor())
  106.  
  107.         if face == "left":
  108.             body[0].goto(head.xcor() + 20, head.ycor())
  109.  
  110.     for segment in body:
  111.         if segment.distance(head) == 0:
  112.             face = "stop"
  113.             for i in body:
  114.                 i.goto(10000, 10000)
  115.             body.clear()
  116.             gameover = True
  117.  
  118.     print(face)
  119.  
  120.     if gameover == True: break
  121.  
  122. wn.exitonclick()
  123.  
  124.    
  125.    
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement