-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake_Game.py
More file actions
95 lines (76 loc) · 1.99 KB
/
Copy pathSnake_Game.py
File metadata and controls
95 lines (76 loc) · 1.99 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import turtle
import random
import time
s = turtle.Screen()
s.bgcolor("black")
snake = turtle.Turtle()
snake.color("green")
snake.shape("square")
snake.penup()
snake.direction = "stop"
segment = []
score = 0
def movesnake():
if snake.direction == "up":
y = snake.ycor()
snake.sety(y+5)
if snake.direction == "down":
y = snake.ycor()
snake.sety(y-5)
if snake.direction == "left":
x = snake.xcor()
snake.setx(x-5)
if snake.direction == "right":
x = snake.xcor()
snake.setx(x+5)
def go_up():
snake.direction = "up"
def go_down():
snake.direction ="down"
def go_left():
snake.direction ="left"
def go_right():
snake.direction ="right"
s.listen()
s.onkey(go_up, "Up")
s.onkey(go_down, "Down")
s.onkey(go_right, "Right")
s.onkey(go_left, "Left")
food = turtle.Turtle()
food.shape("circle")
food.penup()
food.color("red")
food.goto(100, 100)
pen = turtle.Turtle()
pen.hideturtle()
pen2 = turtle.Turtle()
pen2.hideturtle()
while True:
s.update()
movesnake()
time.sleep(0.1)
if snake.distance(food) < 20:
x = random.randint(-100, 100)
y = random.randint(-100, 100)
food.goto(x, y)
new_segment = turtle.Turtle()
new_segment.shape("square")
c = ["blue", "green", "purple", "yellow", "pink", "white"]
color1 = random.choice(c)
new_segment.color(color1)
new_segment.penup()
segment.append(new_segment)
pen.color("white")
score = score+1
pen.clear()
pen.write("SCORE = {}".format(score),
align="center",
font=("Arial", 20, "bold"))
for i in range(len(segment) - 1, 0, -1):
x = segment[i - 1].xcor()
y = segment[i - 1].ycor()
segment[i].goto(x, y)
if len(segment) > 0:
x = snake.xcor()
y = snake.ycor()
segment[0].goto(x, y)