Thursday, January 26, 2023

Procedural

 The current version of Python allowd for two ways to use turtle: procedural

and Object oriented. An example of procedural, below. Note, there is one turtle

and the heavy lifting is done by a function.


import turtle
import random
# setting the initial turtle
my_screen = turtle.Screen()
my_t = turtle.Turtle()
my_t.hideturtle()
my_t.pu()
my_t.goto(0, -100)
my_t.shape('circle')
my_t.showturtle()

# the function which creates dots of various sizes and colors
def dotty(x, y):

my_t.goto(x, y)
my_colors = ['slate grey', 'crimson', 'sky blue']
my_t.color(random.choice(my_colors))
my_r = random.randrange(1, 5)
print(my_r)
my_t.turtlesize(my_r)
my_t.stamp()
my_t.hideturtle()
return

#calling the function, and updating the screen
my_screen.onclick(dotty)
my_screen.update()

#keeping the window open
my_screen.mainloop()

Tomorrow, will show an OOP use of Turtle.

No comments: