1
+ import turtle as t
2
+ import random as rd
3
+
4
+ t .bgcolor ('yellow' )
5
+
6
+ caterpillar = t .Turtle ()
7
+ caterpillar .shape ('square' )
8
+ caterpillar .speed (0 )
9
+ caterpillar .penup ()
10
+ caterpillar .hideturtle ()
11
+
12
+ leaf = t .Turtle ()
13
+ leaf_shape = ((0 ,0 ),(14 ,2 ),(18 ,6 ),(20 ,20 ),(6 ,18 ),(2 ,14 ))
14
+ t .register_shape ('leaf' , leaf_shape )
15
+ leaf .shape ('leaf' )
16
+ leaf .color ('green' )
17
+ leaf .penup ()
18
+ leaf .hideturtle ()
19
+ leaf .speed ()
20
+
21
+ game_started = False
22
+ text_turtle = False
23
+ text_turtle = t .Turtle ()
24
+ text_turtle .write ('Press SPACE to start' , align = 'center' , font = ('Arial' , 18 , 'bold' ))
25
+ text_turtle .hideturtle ()
26
+
27
+ score_turtle = t .Turtle ()
28
+ score_turtle .hideturtle ()
29
+ score_turtle .speed (0 )
30
+
31
+ def outside_window ():
32
+ left_wall = - t .window_width ()/ 2
33
+ right_Wall = t .window_width ()/ 2
34
+ top_wall = t .window_height ()/ 2
35
+ bottom_wall = - t .window_height ()/ 2
36
+ (x ,y ) = caterpillar .pos ()
37
+ outside = x < left_wall or x > right_Wall or y > top_wall or y < bottom_wall
38
+ return outside
39
+
40
+ def game_over ():
41
+ caterpillar .color ('yellow' )
42
+ leaf .color ('yellow' )
43
+ t .penup ()
44
+ t .hideturtle ()
45
+ t .write ('GAME OVER !' , align = 'center' , font = ('Arial' , 30 , 'normal' ) )
46
+
47
+ def display_score (current_score ):
48
+ score_turtle .clear ()
49
+ score_turtle .penup ()
50
+ x = (t .window_width ()/ 2 ) - 70
51
+ y = (t .window_height ()/ 2 ) - 70
52
+ score_turtle .setpos (x ,y )
53
+ score_turtle .write (str (current_score ), align = 'right' , font = ('Arial' , 40 , 'bold' ))
54
+
55
+ def place_leaf ():
56
+ leaf .hideturtle ()
57
+ leaf .setx (rd .randint (- 200 ,200 ))
58
+ leaf .sety (rd .randint (- 200 ,200 ))
59
+ leaf .showturtle ()
60
+
61
+ def start_game ():
62
+ global game_started
63
+ if game_started :
64
+ return
65
+ game_started = True
66
+
67
+ score = 0
68
+ text_turtle .clear ()
69
+
70
+ caterpillar_speed = 2
71
+ caterpillar_length = 3
72
+ caterpillar .shapesize (1 ,caterpillar_length ,1 )
73
+ caterpillar .showturtle ()
74
+ display_score (score )
75
+ place_leaf ()
76
+
77
+ while True :
78
+ caterpillar .forward (caterpillar_speed )
79
+ if caterpillar .distance (leaf ) < 20 :
80
+ place_leaf ()
81
+ caterpillar_length = caterpillar_length + 1
82
+ caterpillar .shapesize (1 ,caterpillar_length ,1 )
83
+ caterpillar_speed = caterpillar_speed + 1
84
+ score = score + 10
85
+ display_score (score )
86
+ if outside_window ():
87
+ game_over ()
88
+ break
89
+
90
+ def move_up ():
91
+ if caterpillar .heading () == 0 or caterpillar .heading () == 180 :
92
+ caterpillar .setheading (90 )
93
+
94
+ def move_down ():
95
+ if caterpillar .heading () == 0 or caterpillar .heading () == 180 :
96
+ caterpillar .setheading (270 )
97
+
98
+ def move_left ():
99
+ if caterpillar .heading () == 90 or caterpillar .heading () == 270 :
100
+ caterpillar .setheading (180 )
101
+
102
+ def move_right ():
103
+ if caterpillar .heading () == 90 or caterpillar .heading () == 270 :
104
+ caterpillar .setheading (0 )
105
+
106
+ t .onkey (start_game ,'space' )
107
+ t .onkey (move_up ,'Up' )
108
+ t .onkey (move_right ,'Right' )
109
+ t .onkey (move_down ,'Down' )
110
+ t .onkey (move_left ,'Left' )
111
+ t .listen ()
112
+ t .mainloop ()
0 commit comments