Python Mouse Motion Events, Conditionals, and Helper Functions

Flight simulator attitude indicator game.

The attitude indicator (also known as an artificial horizon) shows the aircraft's relation to the horizon. From this the pilot can tell whether the wings are level (roll) and if the aircraft nose is pointing above or below the horizon (pitch).[The attitude indicator is a primary instrument for instrument flight and is also useful in conditions of poor visibility. Pilots are trained to use other instruments in combination should this instrument or its power fail. In this game the mouse location determines the role , climb and pitch.

Here is this weeks concepts summary for your reference:

Mouse Motion Events

onMouseMove and onMouseDrag

  • onMouseMove(mouseX, mouseY) is called every time the mouse is moved and not pressed

  • onMouseDrag(mouseX, mouseY) is called when the mouse is pressed and moved

Conditionals (if Statements)

if Statements

  • Runs code only if some test(or condition) is true

  • Code indented after the if statement is called the body

    • All lines will be run if the condition is true

  • Code not indented after the if statement is run regardless of whether the condition is true

  • Some possible tests:

    • (x < y): is x less than y?

    • (x <= y): is x less than or equal to y?

    • (x == y): is x equal to y?

    • (x != y): is x not equal to y?

    • (x > y): is x greater than y?

    • (x >= y): is x greater than or equal to y?

  • Be sure to use == for testing if two values are equal

Using if-else and multiple ifs

  • Code in an else body runs if the condition is false in the previous if statement

  • We can use multiple if statements in a row as well to test different conditions

  • Code will always run in either the if body or the else body but not both

Helper Functions

Helper Functions

  • We can define our own functions as well (not just ones expected to be defined like mouse events)

  • In order for these functions to run, we have to manually call them

  • They can be called from other functions once defined (ie. in our event functions)

  • A helper function is a function you write to help another function perform its work

  • We get to decide the parameters for the function

  • Functions cannot use another function’s parameters

I hope this helps.

Till next post, happy coding.

Alara

Previous
Previous

More Conditionals, Key Events, and Methods in Python

Next
Next

Python Functions, Mouse Events, and Properties