From 16eaff3a789522cb64159a42ff18f0560d5067be Mon Sep 17 00:00:00 2001 From: hnu202309010118 <1737221798@qq.com> Date: Tue, 28 May 2024 08:06:49 +0800 Subject: [PATCH] ADD file via upload --- eating_snake.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 eating_snake.py diff --git a/eating_snake.py b/eating_snake.py new file mode 100644 index 0000000..b865d58 --- /dev/null +++ b/eating_snake.py @@ -0,0 +1,64 @@ +import turtle +from random import randrange + +snake = [[0, 0]] +aim = [0, 10] +food = [-10, 0] + + +def change_direction(x, y): + aim[0] = x + aim[1] = y + + +def sqaure(x, y, size, color): + turtle.penup() + turtle.goto(x, y) + turtle.pendown() + turtle.begin_fill() + turtle.color(color) + for i in range(4): + turtle.forward(size) + turtle.left(90) + turtle.end_fill() + + +import copy + + +def inside(head): + return -250 < head[0] < 250 and -250 < head[1] < 250 + + +def snake_move(): + head = [snake[-1][0], snake[-1][1]] + head = [head[0] + aim[0], head[1] + aim[1]] + if head in snake or not inside(head): + sqaure(head[0], head[1], 10, 'red') + turtle.update() + return + if head == food: + print("snake", len(snake)) + food[0] = randrange(-15, 15) * 10 + food[1] = randrange(-15, 15) * 10 + else: + snake.pop(0) + snake.append(head) + turtle.clear() + sqaure(food[0], food[1], 10, "green") + for body in snake: + sqaure(body[0], body[1], 10, "black") + turtle.update() + turtle.ontimer(snake_move, 300) + + +turtle.setup(500, 500) +turtle.hideturtle() +turtle.listen() +turtle.onkey(lambda: change_direction(0, 10), "Up") +turtle.onkey(lambda: change_direction(0, -10), "Down") +turtle.onkey(lambda: change_direction(-10, 0), "Left") +turtle.onkey(lambda: change_direction(10, 0), "Right") +turtle.tracer(False) +snake_move() +turtle.done()