11import turtle
2- import time
32import random
3+ import time
44
55screen = turtle .Screen ()
6- screen .title ("Classic Snake Game " )
6+ screen .title ("Snake" )
77screen .bgcolor ("black" )
8- screen .setup (width = 600 , height = 600 )
8+ screen .setup (700 , 700 )
99screen .tracer (0 )
1010
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+
1123head = turtle .Turtle ()
12- head .speed (0 )
1324head .shape ("square" )
1425head .color ("green" )
1526head .penup ()
16- head .goto (0 , 0 )
1727head .direction = "stop"
1828
1929food = turtle .Turtle ()
20- food .speed (0 )
2130food .shape ("circle" )
2231food .color ("red" )
2332food .penup ()
24- food .goto (100 , 100 )
33+ food .goto (0 , 100 )
2534
26- segments = []
35+ parts = []
2736
2837score = 0
2938
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" ))
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" ))
3745
3846
39- def go_up ():
47+ def move_up ():
4048 if head .direction != "down" :
4149 head .direction = "up"
4250
4351
44- def go_down ():
52+ def move_down ():
4553 if head .direction != "up" :
4654 head .direction = "down"
4755
4856
49- def go_left ():
57+ def move_left ():
5058 if head .direction != "right" :
5159 head .direction = "left"
5260
5361
54- def go_right ():
62+ def move_right ():
5563 if head .direction != "left" :
5664 head .direction = "right"
5765
5866
59- def move ():
60- x = head .xcor ()
61- y = head .ycor ()
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+
6273
74+ def move ():
6375 if head .direction == "up" :
64- head .sety (y + 20 )
76+ head .sety (head . ycor () + 20 )
6577
6678 if head .direction == "down" :
67- head .sety (y - 20 )
79+ head .sety (head . ycor () - 20 )
6880
6981 if head .direction == "left" :
70- head .setx (x - 20 )
82+ head .setx (head . xcor () - 20 )
7183
7284 if head .direction == "right" :
73- head .setx (x + 20 )
85+ head .setx (head . xcor () + 20 )
7486
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" )
8087
8188while True :
8289 screen .update ()
8390
8491 if (
85- head .xcor () > 290 or
86- head .xcor () < - 290 or
87- head .ycor () > 290 or
88- head .ycor () < - 290
92+ head .xcor () > 280 or
93+ head .xcor () < - 280 or
94+ head .ycor () > 280 or
95+ head .ycor () < - 280
8996 ):
9097 time .sleep (1 )
98+
9199 head .goto (0 , 0 )
92100 head .direction = "stop"
93101
94- for segment in segments :
95- segment .goto (1000 , 1000 )
102+ for p in parts :
103+ p .goto (1000 , 1000 )
104+
105+ parts .clear ()
96106
97- segments .clear ()
98107 score = 0
99- pen .clear ()
100- pen .write (
108+
109+ score_text .clear ()
110+ score_text .write (
101111 f"Score: { score } " ,
102112 align = "center" ,
103- font = ("Arial" , 20 , "normal" )
113+ font = ("Arial" , 18 , "normal" )
104114 )
105115
106- if head .distance (food ) < 20 :
107- x = random .randint (- 280 , 280 )
108- y = random .randint (- 280 , 280 )
116+ if head .distance (food ) < 15 :
117+ x = random .randint (- 14 , 14 ) * 20
118+ y = random .randint (- 14 , 14 ) * 20
119+
109120 food .goto (x , y )
110121
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 )
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 )
117128
118129 score += 1
119- pen .clear ()
120- pen .write (
130+
131+ score_text .clear ()
132+ score_text .write (
121133 f"Score: { score } " ,
122134 align = "center" ,
123- font = ("Arial" , 20 , "normal" )
135+ font = ("Arial" , 18 , "normal" )
124136 )
125137
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 )
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 )
130142
131- if len (segments ) > 0 :
132- x = head .xcor ()
133- y = head .ycor ()
134- segments [0 ].goto (x , y )
143+ if len (parts ) > 0 :
144+ parts [0 ].goto (head .xcor (), head .ycor ())
135145
136146 move ()
137147
138- for segment in segments :
139- if segment .distance (head ) < 20 :
148+ for p in parts :
149+ if p .distance (head ) < 20 :
140150 time .sleep (1 )
151+
141152 head .goto (0 , 0 )
142153 head .direction = "stop"
143154
144- for seg in segments :
145- seg .goto (1000 , 1000 )
155+ for s in parts :
156+ s .goto (1000 , 1000 )
146157
147- segments .clear ()
158+ parts .clear ()
148159
149160 score = 0
150- pen .clear ()
151- pen .write (
161+
162+ score_text .clear ()
163+ score_text .write (
152164 f"Score: { score } " ,
153165 align = "center" ,
154- font = ("Arial" , 20 , "normal" )
166+ font = ("Arial" , 18 , "normal" )
155167 )
156168
157- time .sleep (0.1 )
158-
159- screen .mainloop ()
169+ time .sleep (0.1 )
0 commit comments