Python Functions, Mouse Events, and Properties

Moving Shapes with Functions and Mouse events Creative Task -Sep 2021

This week we are learning how to define functions, use shape properties and use mouse events . As we learn more, the results started to get more dynamic. Once again we will code using CMU Academy IDE (Integrated Development Environment). CMU CS Academy is an online, graphics-based computer science curriculum taught in Python provided by Carnegie Mellon University. They create novel, world-class Computer Science education forthe classroom —and it’s entirely free. You can sign up for it here: CMU CS Academy

Alternatively, To get started in Python, I also recommend getting PyCharm: https://www.jetbrains.com/pycharm/

In each blog post, I will go over some examples, creative tasks that I created, related code and summarize the key concepts and syntax we learned .

Now here’s how I created this animated Python code :

Now let’s go over some key concepts we learned in this topic.

Indent the body of a function by 4 spaces

  • Function parameters can be different types, including numbers, strings (like 'hello'), and booleans (True or False)

  • You can call the same function multiple times

  • Python has rules for legal function names

    • Upper and lower case are different, so sayHi is not the same as sayhi

    • Names must start with letters

    • Names can contain digits, but they cannot start with a digit

    • Names cannot contain spaces, newlines, or punctuation

    • Python reserves some special words that you cannot use for function names

  • Legal names aren’t necessarily good names.

    • Good names are short and clear

    • Good names are camelCased

  • We can use pass to tell Python to do nothing for that function

Multiple Parameters

  • Parameters and arguments must match -- the same number, in the same order

  • When calling a function with no parameters, still use parentheses like: beHappy()

Mouse Events

Mouse Press Events

  • The onMousePress(mouseX, mouseY) function is called every time the canvas is clicked.

    • The name of the function must match

    • We can name the parameters whatever we want, but they will represent the x and y coordinate of where the mouse was pressed on the canvas

Using and Debugging Events

  • Using print properly:

    • Use print to help you understand and debug your code

    • The console is for programmers, not users

    • Delete your print calls when you no longer need them

  • Use the control or ctrlkey to access the inspector

  • Top-level code is any code not inside a function

    • Runs exactly once before any event is called

  • A little Math

    • Use + to add, - to subtract, * to multiply, / to divide

Globals

  • To modify a shape after creating it, store the shape in a global variable

    • You can get and set the properties of that shape in any function

Mouse Release Events

  • onMouseRelease(mouseX, mouseY) is called every time the mouse is released

    • It is most common to use mouse release events to modify existing shapes

Properties

General Shape Properties

  • There are many shape properties that you can get and set. Default values are in parentheses next to the property name:

    • Position Properties

      • left , right , top , bottom , centerX , centerY

    • Size Properties

      • width , height

    • Alignment Properties

      • align

        • Can not be changed after the shape is initialized

    • Fill and Border Properties

      • fill ('black'), border (None), borderWidth (2), opacity (100), dashes (False)

    • Other Properties

      • rotateAngle (0), visible (True)

    • Changing Properties

      • Use += to add, -= to subtract, *= to multiply, /= to divide

Shape-Specific Properties

  • Some shapes have additional properties just for them

    • Circle Properties

      • radius

    • RegularPolygon Properties

      • radius , points

    • Star Properties

      • radius , points , roundness

    • Line Properties

      • x1 , y1 , x2 , y2 , dashes (False), lineWidth (2)

    • Label Properties

      • value , font ('arial'), size (12), bold (False), italic (False)

  • App Background Property: app.background

I hope this helps.

Till next time.

Alara

Previous
Previous

Python Mouse Motion Events, Conditionals, and Helper Functions

Next
Next

Creating Drawings in Python