# Pygame - Collisions <!-- .element: class="r-fit-text" --> Week 16 --- ## Review  <!-- .element: style="height:400px" --> -- ### What is "state"? The current **status** of a program, stored in **variables**. <!-- .element: class="fragment" --> -- ### Where in a program do you define state variables? ```py # Import modules # Define constants # 👉🏻 Define game state variables 👈🏻 # Define functions # Initialize Pygame # Main game loop ``` <!-- .element: class="fragment" --> -- ### What are the two types of inputs in Pygame? - Discrete inputs (once-per-frame) <!-- .element: class="fragment" --> - Continuous inputs (holding down a key) <!-- .element: class="fragment" --> -- ### How do you know how fast an object is moving? Velocity = Distance / Time. <!-- .element: class="fragment" --> Pixels * FPS <!-- .element: class="fragment" --> -- ### How can we ensure an object moves the same speed on every computer? ```py [1-2|5-6] FPS = 30 clock = pygame.time.Clock() running = True while running: player_x += 10 clock.tick(FPS) ``` --- ## Collisions  <!-- .element: style="height:400px" --> -- ### What is a collision? An intersection of two objecs <!-- .element: class="fragment" -->  <!-- .element: style="height:400px" --> <!-- .element: class="fragment" --> -- ### How do we detect a collision? Checking the boundaries of two objects <!-- .element: class="fragment" -->  <!-- .element: style="height:400px" --> <!-- .element: class="fragment" --> -- ### Checking the X Dimension  <!-- .element: style="height:400px" --> -- ### Checking the Y Dimension  <!-- .element: style="height:400px" --> -- ### What does the code look like? ```py [5|1-4|6-7] pipe_r = pipe_x + PIPE_WIDTH pipe_l = pipe_x bird_r = bird_x + BIRD_SIZE / 2 bird_l = bird_x - BIRD_SIZE / 2 x_intersects = pipe_l < bird_r and pipe_r > bird_l if x_intersects: # TODO: do something ``` Why are bird & pipe dimensions calculated differently? <!-- .element: class="fragment" --> --- ## Activity: Checking Collisions! Fill out the function with the "TODO" comment in the [starter project](/2024/fall/computer-science/examples/pygame_collision_starter.py)