/
caterpillar2.0.py
147 lines (122 loc) · 4.13 KB
/
caterpillar2.0.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import random
import turtle
turtle.bgcolor("yellow")
caterpillar = turtle.Turtle()
caterpillar.shape("square")
caterpillar.color("red")
caterpillar.speed(0)
caterpillar.penup()
caterpillar.hideturtle()
caterpillar2 = turtle.Turtle()
caterpillar2.shape("square")
caterpillar2.color("blue")
caterpillar2.speed(0)
caterpillar2.penup()
caterpillar2.hideturtle()
leaf = turtle.Turtle()
leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14))
turtle.register_shape("leaf", leaf_shape)
leaf.shape("leaf")
leaf.color("green")
leaf.penup()
leaf.hideturtle()
leaf.speed(0)
game_started = False
text_turtle = turtle.Turtle()
text_turtle.write("Press SPACE to start", align="center", font=("Arial", 16, "bold"))
text_turtle.hideturtle()
score_turtle = turtle.Turtle()
score_turtle.hideturtle()
score_turtle.speed(0)
def outside_window():
left_wall = -turtle.window_width() / 2
right_wall = turtle.window_width() / 2
top_wall = turtle.window_height() / 2
bottom_wall = -turtle.window_height() / 2
(x, y) = caterpillar.pos()
outside = x < left_wall or x > right_wall or y < bottom_wall or y > top_wall
return outside
def game_over():
caterpillar.color("yellow")
caterpillar2.color("yellow")
leaf.color("yellow")
turtle.penup()
turtle.hideturtle()
turtle.write("GAME OVER!", align="center", font=("Arial", 30, "normal"))
def display_score(current_score):
score_turtle.clear()
score_turtle.penup()
x = (turtle.window_width() / 2) - 50
y = (turtle.window_height() / 2) - 50
score_turtle.setpos(x, y)
score_turtle.write(str(current_score), align="right", font=("Arial", 40, "bold"))
def place_leaf():
leaf.ht()
leaf.setx(random.randint(-200, 200))
leaf.sety(random.randint(-200, 200))
leaf.st()
def start_game():
global game_started
if game_started:
return
game_started = True
score = 0
text_turtle.clear()
caterpillar_speed = 2
caterpillar_length = 3
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar.showturtle()
caterpillar2.shapesize(1, caterpillar_length, 1)
caterpillar2.setheading(180)
caterpillar2.showturtle()
display_score(score)
place_leaf()
while True:
caterpillar.forward(caterpillar_speed)
caterpillar2.forward(caterpillar_speed)
if caterpillar.distance(leaf) < 20 or leaf.distance(caterpillar2) < 20:
place_leaf()
caterpillar_length += 1
caterpillar.shapesize(1, caterpillar_length, 1)
caterpillar2.shapesize(1, caterpillar_length, 1)
caterpillar_speed += 1
score += 10
display_score(score)
if outside_window(caterpillar) or outside_window(caterpillar2):
game_over()
break
def move_up():
if caterpillar.heading() == 0 or caterpillar.heading == 180:
caterpillar.setheading(90)
def move_down():
if caterpillar.heading() == 0 or caterpillar.heading == 180:
caterpillar.setheading(270)
def move_left():
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
caterpillar.setheading(180)
def move_right():
if caterpillar.heading() == 90 or caterpillar.heading() == 270:
caterpillar.setheading(0)
def caterpillar2_move_up():
if caterpillar2.heading() == 0 or caterpillar2.heading == 180:
caterpillar2.setheading(90)
def caterpillar2_move_down():
if caterpillar2.heading() == 0 or caterpillar2.heading == 180:
caterpillar2.setheading(270)
def caterpillar2_move_left():
if caterpillar2.heading() == 90 or caterpillar2.heading() == 270:
caterpillar2.setheading(180)
def caterpillar2_move_right():
if caterpillar2.heading() == 90 or caterpillar2.heading() == 270:
caterpillar2.setheading(0)
turtle.onkey(start_game, "space")
turtle.onkey(move_up, "Up")
turtle.onkey(move_right, "Right")
turtle.onkey(move_down, "Down")
turtle.onkey(move_left, "Left")
turtle.onkey(caterpillar2_move_up, "w")
turtle.onkey(caterpillar2_move_right, "d")
turtle.onkey(caterpillar2_move_down, "s")
turtle.onkey(caterpillar2_move_left, "a")
turtle.listen()
turtle.mainloop()