CMU: Unit 4

CS1: Unit 4 Review - More Conditionals (if-elif-else Statements)

if-elif-else Statements
  • The if statement is used to initiate conditional logic.

  • The elif (short for "else if") allows for an additional test of conditions that only executes if the initial if condition is false.

  • Multiple elif statements can follow a single if statement, although only one block of code (from the if or one of the elifs will execute.

  • It is permissible to use elif without an accompanying else, but it is not valid to use elif without first using if.

  • The block of code under the else statement runs if all previous conditions (if and any elif) are false.

  • The else clause can be interpreted as "otherwise" in common language.

Key Events: onKeyPress and onKeyRelease

  • The onKeyPress event handler is triggered when a key is pressed down.

  • The parameter key in onKeyPress represents the specific key that was pressed, allowing for conditional logic to determine which key was involved, through the use of if and elif statements.

  • The onKeyRelease event handler allows code execution when the user releases a key.

  • Arrow keys can be represented in the programming context as:

    • "left"

    • "right"

    • "up"

    • "down"

Methods: Custom Properties

  • Custom properties can be assigned to any shape.

  • Example: For a circle variable c, you can initialize a property with c.count = 0, which can later be referenced in your code.

  • Custom properties must be assigned names that are not already used by the CMU Graphics library.

  • Additionally, custom properties can be created for apps that do not specifically belong to a shape.

Shape Methods

  • toFront(): This method brings a specified shape to the front layer of other shapes.

  • toBack(): This method pushes a specific shape to the back layer, behind other shapes.

  • Difference from Functions:

    • Methods are invoked differently from standard functions: for example, to call toFront(), you would use shape.toFront() rather than toFront(shape).

    • Methods can behave like properties because their implementation may vary between different shape types.

    • Every shape possesses the methods toFront() and toBack().

  • addPoint(x, y): This method is exclusively available for polygons.

    • This method accepts the (x, y) coordinates of a point and adds that point to the defined polygon.

  • contains(x, y): This method takes two numerical arguments x and y and returns whether the specified shape includes the point defined by these coordinates, returning a boolean value of True or False.

  • shape.hits(x, y): This method returns True only if the point (x, y) lies within the borders of the shape, provided that the shape is drawn at that location.

    • If the shape's fill is set to None, any point located inside the shape will return False.

Additional Shape Methods

  • shape1.hitsShape(shape2): This method tests for a collision or intersection between two shapes.

  • shape1.containsShape(shape2): This method tests if the first shape completely encloses the second shape.

  • app.stop(): This command terminates further event handling, effectively stopping the application from processing any more events.