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

Keyboard events

What am I Learning today?

I am learning how to understand the concept of Keyboard Events and how they can control the movement of an object in a game.
I am learning how to understand that Event Listeners can be used to check if an action has been performed.
I am learning how to understand that Event Handlers can be used to perform an action detected by an Event Listener.

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 3
  • Save the Powerpoint file: as your name


  • Complete Task 1 in your Powerpoint


  • Task 2 - Events Click to see more

    Events in pygame:

    All games have interaction with the user, in computing these are known as Interrupt Events.

  • The code that checks if the user has done something is called an Event Listener.
  • When a specific event is heard by the Event Listener, an action is then performed by the Event Handler code.

    If we look carefully at the template you will see that the event of the user closing the window is already handled. We'll add some more event handlers in this lesson, to make our game more interactive.

    Task 1

  • Add the print(event) statement to your code:

  • Run your code, then move the mouse around inside the pyGame window and press some keys on the keyboard.
  • Look in the shell/interpreter window, located in the bottom section of the Thonny window or in the terminal window for Python IDLE.

    You might see output like this:


    Try holding the CTRL, ALT or SHIFT keys while pushing other keys. The interpreter window output will now change slightly to show that the keys have been modified.

    KeyUp and KeyDown are different events. What's the difference?

    Task 2

    Look at the example below then copy and paste the lines from the Thonny/Python console in the bottom window which show the events returned for each of the following:


    Example:

  • Right mouse button clicked/released
  • <Event(1025-MouseButtonDown {'pos': (113, 439), 'button': 3, 'window': None})>

    <Event(1026-MouseButtonUp {'pos': (113, 439), 'button': 3, 'window': None})>


    1. Left mouse button pressed/released
    2. W key pressed
    3. Up arrow pressed

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


  • Task 3 - Keyboard Events Click to see more

    Lets first make our square we are going to move around we will do this using variables for all its values.

    Add this line of code to your program under ## your code starts here

    screen.fill(black)
    pygame.draw.rect(screen, green, (XPos, YPos, squareWidth, SquareHeight))

    Now lets put in the values for the variables for our square

    XPos = 100
    YPos = 100
    Speed = 5
    squareWidth = 20
    SquareHeight = 20

    Run your code to make sure it works


    Key Moves

    In order for us to check if a key has been pressed we need to first look for a KEY DOWN event.

    Activity

    Add this line of code to your program under ## your code starts here

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            XPos -= Speed

    Now press your left arrow key and see what happens

    Extension Task



    Add some more if statements to your code so that you can move your square up down and right using the key codes pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN


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


  • Task 4 - Adding bounderies Click to see more

    Finding the edges of the screen

    Just like last week When our square gets to the edge of our screen it disappears.

    We want the whole square to stay on the screen To do that we need to check the two different x-cordinates at either side of the screen.


    Task

    1. Set the value of Xpos or Ypos so that our square does not move whenever it gets to to the edge of the screen


    2. Can you to take into account the width of our square to keep the whole square on our screen when it is at the SCREENWIDTH and SCREENHEIGHT values

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


    Task 5 - Extend it Click to see more

    Changing speed

    Can we change the value of Speed by using a key press?

    Task

    1. Change the value of Speed by - 1 whenever we press the a key pygame.K_a
    2. Change the value of squareSpeed back to a positive number whenever another key of your choice is pressed.

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


  • Task 6 - Moving circle Click to see more

    Can you also add a moving circle using with the technique we used last lesson?


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


  • Task 7 - 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 that Event Listeners can be used to check if an action has been performed.

    • I have a basic understanding of how I can understand that Event Listeners can be used to check if an action has been performed. with a little help from my teacher
    • I can show my teacher that I can understand that Event Listeners can be used to check if an action has been performed. without their help.
    • I can understand that Event Listeners can be used to check if an action has been performed. 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