-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakegame.py
55 lines (44 loc) · 1.33 KB
/
snakegame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from turtle import*
from random import randrange
from freegames import square,vector
food = vector(0,0)
snake = [vector(10,0)]
aim = vector(0,-10)
turtle.bgcolor('turquoise')
def change(x,y): # x and y axis
"change snake direction"
aim.x = x
aim.y = y
def inside(head): # boundary values
"return true if head inside boundaries"
return -200 < head.x<190 and -200 < head.y < 190
def move(): #for movement of the snake
"Move snake forward one segment"
head = snake[-1].copy() #forward movement value
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: # conditions
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: #snake is combination of squares
square(body.x,body.y,9,'black')
square(food.x,food.y,9,'orange')
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()