...

Back to Leadership Board

Challenge #8: Collision Detection

Start Date: December 01, 2023
Submit By: 8:30 am on December 08, 2023

In this project, we will transform our animation created in Challenge 7 into a game by adding a score and collision detection.
We will cover the following:

  •     Pygame library
  •     Mouse Click Collision
  •     Object Interaction Collision)
  •     Build your game
Watch a Video Tutorial

Video Tutorial

Challenge #8: Collision Detection

Transform your animation into an interactive game! Add a scoring system with a visible score label, and implement collision detection.

Challenge Details:

  1. Open your animation Trinket created in Challenge 7. Check the links on the progress board under Project 7. If you haven't submitted Challenge 7, complete it first.
  2. Add a score and score label to the game.
  3. Implement at least one type of collision:
    • Mouse Click Collision: Introduce an element that responds to a mouse click.
    • Object Interaction Collision: Trigger a collision event when one image overlaps another image.
  4. Increase or decrease the score based on your game logic.
Too Hard? If this challenge feels too challenging, don't worry! Try your best to follow a video tutorial for step-by-step guidance.

Next
Transform Animation into the Game

Adding the Score

  • Open your animation Trinket created in Challenge 7. Check the links on the progress board under Project 7. If you haven't submitted Challenge 7, complete it first.
  • First, let's introduce a score variable and a score label.
  • Next, we need to construct our score label and display it using the blit command. Add the following code:
  • This code initialises a score variable, sets up a font for the score label, constructs the score label, and then displays it on the screen using the blit command. This way, players can easily track their scores while enjoying your game!

    Collision Detection

    Collision detection is the process in video games where the system checks if two objects, like a player and an obstacle, overlap or collide in the game world.

    As part of this project, we will define two types of collision.

  • Mouse Click Collision, which occurs when we click with the mouse on an object. If the mouse click coordinates coincide with the object coordinates, this is when a collision happens. This adds an interactive element to your game, allowing players to interact with objects through mouse clicks.
  • Object Interaction Collision, which happens when one object overlaps another. This type of collision can be triggered when the positions of two objects align, offering a different dimension of interaction within your game.
  • For the Mouse Click Collision, we will click with the mouse on the balloon. This logic should be placed inside a for loop, and we'll add a MOUSEBUTTONDOWN condition. Here's how you can implement it:

    For the Object Interaction Collision, we will move the boy up, down, left, and right, attempting to pick up the rolling ball. Once the boy's picture touches the ball picture (i.e., they overlap), this piece of code should be placed outside the for loop. Here's the code snippet:

    Prev Next
    Final Game Code

    Here's the final game code:

    Prev Next
    Submit your Challenge

    Submit your final project





    Copy your Trinket Url

  • Click Share->Link
  • Copy the link to share your code
  • Paste link into the field

  • Do you have a question?



    Prev