Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle, time, random
- face = "stop"
- body = []
- gameover = False
- wn = turtle.Screen()
- wn.title("basic snake AI")
- wn.setup(600, 600)
- wn.tracer(0)
- head = turtle.Turtle()
- head.shape("square")
- head.color("green")
- head.goto(0,0)
- apple = turtle.Turtle()
- apple.shape("circle")
- apple.color("red")
- apple.penup()
- apple.goto(0, 100)
- def up():
- global face
- if face != "down":
- face = "up"
- def down():
- global face
- if face != "up":
- face = "down"
- def right():
- global face
- if face != "left":
- face = "right"
- def left():
- global face
- if face != "right":
- face = "left"
- wn.listen()
- wn.onkeypress(up, "Up")
- wn.onkeypress(down, "Down")
- wn.onkeypress(left, "Left")
- wn.onkeypress(right, "Right")
- while True:
- wn.update()
- time.sleep(0)
- if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
- face = "stop"
- for i in body:
- i.goto(10000, 10000)
- body.clear()
- head.goto(0, 0)
- gameover = True
- if head.xcor() == apple.xcor() and head.ycor() == apple.ycor():
- x = random.randint(-14, 14)
- y = random.randint(-14, 14)
- apple.goto(x * 20, y * 20)
- new_segment = turtle.Turtle()
- new_segment.shape("square")
- new_segment.color("limegreen")
- new_segment.up()
- body.append(new_segment)
- ################ basic ai ################
- if head.ycor() == -280 and head.xcor() != -280: face = "left"
- if head.xcor() == -280 and head.ycor() != 280: face = "up"
- if head.ycor() == 280 and head.xcor() != 280: face = "right"
- if head.xcor() == 280 and head.ycor() != -280: face = "down"
- if head.xcor() == apple.xcor() and head.ycor() > apple.ycor(): face = "down"
- for segment in body:
- if face == "down" and head.ycor() - 20 == segment.ycor() and head.xcor() == segment.xcor(): face = "right"
- if face == "right" and head.xcor() + 20 == segment.xcor() and head.ycor() == segment.ycor(): face = "up"
- if face == "up" and head.ycor() + 20 == segment.ycor() and head.xcor() == segment.xcor(): face = "left"
- if face == "left" and head.xcor() - 20 == segment.xcor() and head.ycor() == segment.ycor(): face = "down"
- ############### end of ai ################
- if face == "up": head.sety(head.ycor() + 20)
- if face == "down": head.sety(head.ycor() - 20)
- if face == "left": head.setx(head.xcor() - 20)
- if face == "right": head.setx(head.xcor() + 20)
- for i in range(len(body)-1, 0, -1):
- body[i].goto(body[i-1].xcor(), body[i-1].ycor())
- if len(body) > 0:
- if face == "up":
- body[0].goto(head.xcor(), head.ycor() - 20)
- if face == "down":
- body[0].goto(head.xcor(), head.ycor() + 20)
- if face == "right":
- body[0].goto(head.xcor() - 20, head.ycor())
- if face == "left":
- body[0].goto(head.xcor() + 20, head.ycor())
- for segment in body:
- if segment.distance(head) == 0:
- face = "stop"
- for i in body:
- i.goto(10000, 10000)
- body.clear()
- gameover = True
- print(face)
- if gameover == True: break
- wn.exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement