Mechraf

Currently in /2019/01/27/pen-chase/


Pen Chase

The turtle module has a lot of functions. These functions, if combined in specific ways, can produce cool results. Other python modules like time, math and Tkinter can be combined to produce even better results. In this post, we will encounter more turtle functions that expose us more to the art of programming and the turtle module. The complete script is shown at the end. You can always download the python (.py) file and run it in your chosen python environment.

The Paint Script

Part One

This script is part of the turtle demo. It’s quite easy and straightforward. It’s amazing how a few lines of code could produce a very simple paint program. We first start by importing turtle and defining the pen’s states.

def switchupdown(x=0, y=0):
    if pen()["pendown"]:
        end_fill()
        up()
    else:
        down()
        begin_fill()

It’s necessary that the switchupdown() function have the arguments x and y. The program starts with the pen in the up state. So invariably, the else part of the if statement would be run first.

Part Two

The if part can then be run, which fills any polygon that has been formed, The next part is the change color function.

def changecolor(x=0, y=0):
    global colors
    colors = colors[1:]+colors[:1]
    color(colors[0])

This function defines what happens when the middle mouse button is clicked. The colors follow the new list formatting given. Note that this formatting means that the original colors list is cut from the ‘index 1’ element (which is the second element) to the last element on the list. Then that is added to the ‘index 0’ (the first element) element. A more technical explanation is this shorrt code.

A little outside code

colors = [2, 4, 7, 9, 2, 5]
    colors = colors[1:] + colors[:1]
    print("sum of formatted lists = ",colors)
    
    
    x = [4, 7, 9, 2, 5]
    y = [2]
    print("proof sum of two lists = ", x+y)

The output is this:

sum of formatted lists =  [4, 7, 9, 2, 5, 2]
proof sum of two lists =  [4, 7, 9, 2, 5, 2]

Part Three

The third code block starts with the main function. A few normal functions like shape(), width() and shapesize() are called. The pen color now obeys the changecolor() function in Part Two. Then the previously defined switchupdown function is called.

def main():
    global colors
    shape("turtle")
    resizemode("user")
    shapesize(.5)
    width(3)
    colors=["red", "green", "blue", "yellow"]
    color(colors[0])
    switchupdown()
    onscreenclick(goto, 1)
    onscreenclick(changecolor,2)
    onscreenclick(switchupdown, 3)
    return "EVENTLOOP"

Now, a function, onscreenclick is used to collect mouse input fro

Final Part

if __name__ == "__main__":
    help(switchupdown())
    msg = main()
    print(msg)
    mainloop()

The Complete Script

from turtle import *

def switchupdown(x = 0, y = 0):
    if pen()["pendown"]:
        end_fill()
        up()
    else:
        down()
        begin_fill()

def changecolor(x=0, y=0):
    global colors
    colors = colors[1:]+colors[:1]
    color(colors[0])

def main():
    global colors
    shape("turtle")
    resizemode("user")
    shapesize(.5)
    width(3)
    colors=["red", "green", "blue", "yellow"]
    color(colors[0])
    switchupdown()
    onscreenclick(goto, 1)
    onscreenclick(changecolor,2)
    onscreenclick(switchupdown, 3)
    return "EVENTLOOP"


if __name__ == "__main__":
    help(switchupdown())
    msg = main()
    print(msg)
    mainloop()

You can download the file from this github page

https://github.com/Vicradon/Mecharaf/blob/master/paint.py

I’ve combined all the explanation (pseudocode). Just go through it if you get lost.

The Pseudocode

1. First, a function is defined which defines the following conditions:
a. If the pen is down (that’s when the Right Mouse Button is clicked) stop filling the polygon, then take the pen up
b. else if the pen is up (when the Left Mouse Button is clicked, or when the script is finally executed),
bring the pen down and start filling the polygon

2. Then, the changecolor function is defined. This begins by transforming the colors variable to global.
Note that the variable colors is assigned to a list of colors (“red”, “green”, “blue”, “yellow”)

3. The main() method here contains all the necessary functions and definitions.
It’s a good practice to have a main() function. At the end of your script, you
then call the main() method in an if statement.

4. The last three functions in the main() method
a.onscreenclick(goto, 1)
b.onscreenclick(changecolor,2)
c.onscreenclick(switchupdown, 3)

are input functions. The onscreenclick function itself is defined by the number designation.
“1” is the left mouse button, “2” is the middle mouse button and “3” is the right mouse button.
If two consecutive lines are drawn and pen is down, when the RMB is clicked, the polygon is filled.
Try it yourself.

5. The script ends with the if statement. the msg, in this case, is the __name__ which was assigned to the “__main__”

Last Words

That was quite a python script. We will encounter scripts that are more involved as we progress. Do well to run this script on your python environment. If you are using Pydroid, you must long press the turtle which in this case is a circle, to activate pendown(). Long pressing again activates penup(). Swiping over the turtle (or pen) toggles the color. The whole script comes along with pydroid, you just need to find it.