0% found this document useful (0 votes)
21 views2 pages

Python Snake Game Code Example

Uploaded by

Ka Sun Liew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Python Snake Game Code Example

Uploaded by

Ka Sun Liew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 -200 < head.x < 190 and -200 < head.y < 190

def move():

head=snake[-1].copy()

[Link](aim)

if not inside(head) or head in snake:

square(head.x,head.y,9'red')

update()

return

[Link]()

if head == food:

print('snake',len(snake))

food.x=randrange(-15,15)*10
food.y=randrange(-15,15)*10

else:

[Link](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()

trancer(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()

You might also like