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