Skip to content

Commit a4b026f

Browse files
committed
Watch this Moving Turtle
1 parent 7c2004f commit a4b026f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

MovingTurtle.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import turtle as t
2+
import random as ri
3+
4+
def inside_window():
5+
left_limit = (-t.window_width() / 2) + 100
6+
right_limit = (t.window_width() / 2) - 100
7+
top_limit = (t.window_height() / 2) - 100
8+
bottom_limit = (-t.window_height() / 2) + 100
9+
(x,y) = t.pos()
10+
inside = left_limit < x < right_limit and bottom_limit < y < top_limit
11+
return inside
12+
13+
def move_turtle():
14+
if inside_window():
15+
angle = ri.randint(0,180)
16+
t.right(angle)
17+
t.forward(200)
18+
else:
19+
t.backward(200)
20+
21+
t.shape('turtle')
22+
t.fillcolor('green')
23+
t.bgcolor('black')
24+
t.speed('slow')
25+
t.pensize(2)
26+
27+
28+
while True:
29+
move_turtle()

0 commit comments

Comments
 (0)