Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please accept my PR #338

Merged
merged 2 commits into from
Oct 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#snake game
from turtle import *
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 True if head inside boundaries"
return -200<head.x<190 and -200<head.y<190

def move():
"Move snake forward in one segment"
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(head)

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 :change(10,0),'Right')
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')

move()
done()