From 8591bca030b22a70cd319bdd4ba5271d7d807086 Mon Sep 17 00:00:00 2001 From: harshbam18 <112744382+harshbam18@users.noreply.github.com> Date: Tue, 25 Oct 2022 17:41:54 +0530 Subject: [PATCH] Create Snakegame.py I've created Snake Game using python language --- Python/Snakegame.py | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Python/Snakegame.py diff --git a/Python/Snakegame.py b/Python/Snakegame.py new file mode 100644 index 0000000..f2f6558 --- /dev/null +++ b/Python/Snakegame.py @@ -0,0 +1,48 @@ +from random import randrange +from freegames import square,vector + +food=vector(0,0) +snake=[vector(10,0)] +aim=vector(0,-10) + +def change(x,y): + aim.x=x + aim.y=y + +def inside(head): + return -200 < head.x < 190 and -200 < head.y < 190 + +def move(): + head=snake[-1].copy() + head.move(aim) + if not inside(head) or head in snake: + square(head.x,head.y,9,"red") + update() + return + snake.append() + + if head==food: + print("snake",len(snake)) + food.x=randrange(-15,15)*10 + food.y=randrange(-15,15)*10 + else: + snake.pop(0) + clear() + + for body in snake: + square(body.x,body.y,9,"green") + + square(food.x,food,y,9,"red") + update() + ontimer(move,100) + + hideturtle() + tracer(false) + listen() + onkey(lambda:changes(10,0),"Right") + onkey(lambda:changes(-10,0),"Left") + onkey(lambda:changes(0,10),"Up") + onkey(lambda:changes(0,-10),"Down") + + move() + done()