1+ import turtle
2+ import random
3+ import time
4+
5+ screen = turtle .Screen ()
6+ screen .title ("Snake" )
7+ screen .bgcolor ("black" )
8+ screen .setup (700 , 700 )
9+ screen .tracer (0 )
10+
11+ box = turtle .Turtle ()
12+ box .hideturtle ()
13+ box .color ("white" )
14+ box .pensize (3 )
15+ box .penup ()
16+ box .goto (- 300 , 300 )
17+ box .pendown ()
18+
19+ for _ in range (4 ):
20+ box .forward (600 )
21+ box .right (90 )
22+
23+ head = turtle .Turtle ()
24+ head .shape ("square" )
25+ head .color ("green" )
26+ head .penup ()
27+ head .direction = "stop"
28+
29+ food = turtle .Turtle ()
30+ food .shape ("circle" )
31+ food .color ("red" )
32+ food .penup ()
33+ food .goto (0 , 100 )
34+
35+ parts = []
36+
37+ score = 0
38+
39+ score_text = turtle .Turtle ()
40+ score_text .hideturtle ()
41+ score_text .color ("white" )
42+ score_text .penup ()
43+ score_text .goto (0 , 320 )
44+ score_text .write ("Score: 0" , align = "center" , font = ("Arial" , 18 , "normal" ))
45+
46+
47+ def move_up ():
48+ if head .direction != "down" :
49+ head .direction = "up"
50+
51+
52+ def move_down ():
53+ if head .direction != "up" :
54+ head .direction = "down"
55+
56+
57+ def move_left ():
58+ if head .direction != "right" :
59+ head .direction = "left"
60+
61+
62+ def move_right ():
63+ if head .direction != "left" :
64+ head .direction = "right"
65+
66+
67+ screen .listen ()
68+ screen .onkeypress (move_up , "Up" )
69+ screen .onkeypress (move_down , "Down" )
70+ screen .onkeypress (move_left , "Left" )
71+ screen .onkeypress (move_right , "Right" )
72+
73+
74+ def move ():
75+ if head .direction == "up" :
76+ head .sety (head .ycor () + 20 )
77+
78+ if head .direction == "down" :
79+ head .sety (head .ycor () - 20 )
80+
81+ if head .direction == "left" :
82+ head .setx (head .xcor () - 20 )
83+
84+ if head .direction == "right" :
85+ head .setx (head .xcor () + 20 )
86+
87+
88+ while True :
89+ screen .update ()
90+
91+ if (
92+ head .xcor () > 280 or
93+ head .xcor () < - 280 or
94+ head .ycor () > 280 or
95+ head .ycor () < - 280
96+ ):
97+ time .sleep (1 )
98+
99+ head .goto (0 , 0 )
100+ head .direction = "stop"
101+
102+ for p in parts :
103+ p .goto (1000 , 1000 )
104+
105+ parts .clear ()
106+
107+ score = 0
108+
109+ score_text .clear ()
110+ score_text .write (
111+ f"Score: { score } " ,
112+ align = "center" ,
113+ font = ("Arial" , 18 , "normal" )
114+ )
115+
116+ if head .distance (food ) < 15 :
117+ x = random .randint (- 14 , 14 ) * 20
118+ y = random .randint (- 14 , 14 ) * 20
119+
120+ food .goto (x , y )
121+
122+ new_part = turtle .Turtle ()
123+ new_part .shape ("square" )
124+ new_part .color ("lightgreen" )
125+ new_part .penup ()
126+
127+ parts .append (new_part )
128+
129+ score += 1
130+
131+ score_text .clear ()
132+ score_text .write (
133+ f"Score: { score } " ,
134+ align = "center" ,
135+ font = ("Arial" , 18 , "normal" )
136+ )
137+
138+ for i in range (len (parts ) - 1 , 0 , - 1 ):
139+ x = parts [i - 1 ].xcor ()
140+ y = parts [i - 1 ].ycor ()
141+ parts [i ].goto (x , y )
142+
143+ if len (parts ) > 0 :
144+ parts [0 ].goto (head .xcor (), head .ycor ())
145+
146+ move ()
147+
148+ for p in parts :
149+ if p .distance (head ) < 20 :
150+ time .sleep (1 )
151+
152+ head .goto (0 , 0 )
153+ head .direction = "stop"
154+
155+ for s in parts :
156+ s .goto (1000 , 1000 )
157+
158+ parts .clear ()
159+
160+ score = 0
161+
162+ score_text .clear ()
163+ score_text .write (
164+ f"Score: { score } " ,
165+ align = "center" ,
166+ font = ("Arial" , 18 , "normal" )
167+ )
168+
169+ time .sleep (0.1 )
0 commit comments