Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE
Responsive image

Game 2 - Collisions

What am I Learning today?

I am learning how to understand and use variables to change to reflect/keep track of the state (position, colour etc) of a game object
I am learning how to understand and use selection, i.e. using if-else, if-elif, to branch the flow of an action.

Knowledge Organiser

×

Lesson

Task 1 - Getting Started Click to see more

You are going to screenshot all of your work into a Powerpoint file.

To download the PowerPoint by clicking on the image below

To save your powerpoint go to:

  • File explorer
  • -> Year 9 Common Area :S
  • -> You can save here
  • -> Computer Science
  • -> Term 3
  • -> Lesson 5
  • Save the Powerpoint file: as your name


  • Complete Task 1 in your Powerpoint


  • Task 2 - Target Practice Click to see more

    Activity 1 - Creating the moving crosshair sprite

    Complete the following:

    1. Start a new file with a blank template
    2. Save it as target.py
    3. Right click on the image below and save it in your Computer Science folder.
    4. Save it as crosshair.png
    5. Add the following code into your variables area.
    6. ##### Your Variables go here ####
      image_crosshair = pygame.image.load("crosshair.png")
      image_crosshair_sizeXY = [50, 50]
      crosshair_position = [300, 100]
      image_crosshair = pygame.transform.scale(image_crosshair, image_crosshair_sizeXY)
      pygame.mouse.set_visible(False)
    7. Add the following code into your code starts here area.
    8.     #### your code starts here ####
          screen.fill(black)
           crosshair = screen.blit(image_crosshair, crosshair_position)
           mouse_position = pygame.mouse.get_pos()
           crosshair_position = mouse_position[0], mouse_position[1]
           new_X_position = mouse_position[0] - (image_crosshair_sizeXY[0] / 2)
           new_Y_position = mouse_position[1] - (image_crosshair_sizeXY[1] / 2)
           crosshair_position = new_X_position, new_Y_position

    Do not forget to indent your code



  • When you are finished screenshot your working code into your Learning Journal


  • Task 3 - Adding a Target Click to see more

  • we will create another sprite using a basic shape called target. We will then detect if player1 has collided with the target.
  • First we need a variable to hold the x and y position of the target.

    Activity 2

    1. Add the following code into our variables area.
    2. targetXY = [200, 200]
    3. Next, we will draw the target sprite onto the screen.
    4. Add the following lines of code into the ## your code starts here ## area.
    5. target = pygame.draw.circle(screen, red, (300, 100), 20)
    6. Then we will check for a collision.
    7. if crosshair.colliderect(target):
          print("Hit!")

    Run the code, and see what happens?


  • When you are finished screenshot your working code into your Learning Journal


  • Task 4 - Moving Target Click to see more

    Printing 'hit' in the console is not very exciting. Let's change the code so the target disappears and reappears at a random x and y position when touched by the player sprite.

    Activity 3

    The first this we need to do is import the random library functions

    1. On line 4 of your code add the following line of code:
    2. import random
    3. Next, we can use a new function called: random.randint() to create random numbers for the x and y postions of the target. Comment out the print("hit") line you wrote previously by placing a hash symbol (#) in front of it.
    4. Then add the following lines underneath. Don't forget that they're part of the IF block, so will need to be indented with the tab key three times.
    5. targetXY[0] = random.randint(0, SCREENWIDTH)
      targetXY[1] = random.randint(0, SCREENHEIGHT)
    6. Finally change the line
    7. target = pygame.draw.circle(screen, red, (300, 100), 20)
    8. to:
    9. target = pygame.draw.circle(screen, red, targetXY, 20)

      Run the code to test it, and see what happens?


    10. When you are finished screenshot your working code into your Learning Journal


    Task 5 - Improve it - Target Practice Click to see more

  • There are a number of improvements we can make.
  • It would be better if the user had to collide with the target and click the mouse to score a point. We could add this by changing our collision code slightly.
  • Task 3

    Complete the following:

    1. Add another IF statement inside the game loop to force the user to click on the target.
    2. if pygame.mouse.get_pressed()[0] == 1:
          
      if crosshair.colliderect(target):

      The IF statement is to be added above our first IF statement ensure it is indented correctly.


  • When you are finished screenshot your working code into your Learning Journal


  • Task 6 - Extension - Firelock Click to see more

  • Our final improvement is to remove the firelock bug we have introduced.Which means the player can hold down the mouse button constantly, so that they just have to touch the target
  • To fix this we have to add a boolean flag variable to prevent the player firing again until the mouse button has been released.
  • Task 4

    Complete the following:

    1. Create the fireLock variable and set to: 0 in our variables section
    2. fireLock = 0
    3. Next, we'll need to set: fireLock to: 1 when the mouse button is pushed, by modifying our IF statement from before a little bit.
    4. if pygame.mouse.get_pressed()[0] == 1 and fireLock == 0: fireLock = 1
    5. Finally, we'll need to make it so that we only reset: fireLock to 0 when the mouse button is released. We do this by adding another event listener to our event loop.
    6. if event.type == pygame.MOUSEBUTTONUP:
          fireLock = 0

  • When you are finished screenshot your working code into your Learning Journal


  • Task 7 - Extend it - Target Practice Click to see more

    Now you have a working crosshiair and a moving target see if you can add the following elements to your game:

    1. A score which is written on the screen and increases every time you click a target
    2. Make the targets move around the screen insead of being stationary
    3. Have more than one target with different colours giving different point values

  • When you are finished screenshot your working code into your Learning Journal


  • Task 8 - Lesson Review/Homework Click to see more


    Summing it all up

    Lets look at the learning outcomes and decide which one best describes our current level of understanding :

    Tick the one you feel is closest to your level

    Learning Outcomes I need to learn how to understand and use selection, i.e. using if-else, if-elif, to branch the flow of an action.

    • I have a basic understanding of how I can understand and use selection, i.e. using if-else, if-elif, to branch the flow of an action. with a little help from my teacher
    • I can show my teacher that I can understand and use selection, i.e. using if-else, if-elif, to branch the flow of an action. without their help.
    • I can understand and use selection, i.e. using if-else, if-elif, to branch the flow of an action. independently and I can also explain it to others and can complete any extension tasks I am given.

    🠜 Now update your learning objectivesClick on the Assessment image



    My Notes: Game Development

    Student_Comment_3 not found

    Task Notes/Comments - Add here Click to see more

    Comments/Notes

    Copyright © 2013 - 2026 LearnITwithMrC