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 pressedonMouseDrag(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 bodyAll lines will be run if the condition is true
Code not indented after the
if
statement is run regardless of whether the condition is trueSome 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 previousif
statementWe can use multiple
if
statements in a row as well to test different conditionsCode will always run in either the
if
body or theelse
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