Grey overlay








Mouse Events

Download It

Learn It

  • 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 Lister, 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.

Try It

  • Add the print(event) statement to your code
  • 3-1.PNG
  • Run your code with Shift-F10, then move the mouse around inside the pyGame window and press some keys on the keyboard.
  • Look in the interpreter window, located in the bottom-left corner of the pyCharm window.
  • You might see output like this:
<Event(2-KeyDown {'mod': 0, 'unicode': '', 'scancode': 56, 'key': 308})>
<Event(3-KeyUp {'mod': 0, 'key': 308, 'scancode': 56})>
  • 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'.
  • NOTE: KeyUp and KeyDown are different events. What's the difference?

Badge It

  • Silver: Copy and paste lines from the Python console in the bottom-left corner which show the events returned for the following:
    • Right mouse button clicked/released
    • Mouse wheel clicked/released
    • Mouse wheel spin up/down
    • Left mouse button pressed/released
    • Pressing the X to close the window
    • Up arrow press

Code It

  • We will make a sprite called player1 follow the mouse. Add these lines of code to your game loop.
  • Note: Make sure you use tab to double-indent the code so that it sits inside the for loop.
mousePosition = pygame.mouse.get_pos()
  • The code will look like this:


  • Look at the Python terminal window and move the mouse.
  • This is the data we will use to position the player1 sprite.
  • Modify your game loop to the following…
mousePosition = pygame.mouse.get_pos()
# print(mousePosition)   <<< UMCOMMENT THIS TO SEE POSITION DATA.
player1X = mousePosition[0]
player1Y = mousePosition[1]
player1 = pygame.draw.rect(screen, red, (player1X, player1Y, 40, 40))
  • The code will look like this:


  • Run the program with Shift-F10. What happens?
  • If you're struggling with the tasks, you can go through the notes above using this video tutorial:

Badge It

  • Gold The player1 sprite isn't in the centre of the mouse pointer. Fix this bug and comment each line of your code to explain what it does.
  • Platinum: You've now covered mouse movement. Modify your code so a different shape is drawn on left or right mouse button clicks.
  • Hint: There's a few ways to do the platinum task. You can look online and find a way to do it, or these lines could perhaps be recycled into something useful.
if pygame.mouse.get_pressed()[0] == True:
    pygame.draw.rect(screen, green, (100, 100, 100, 100))
elif pygame.mouse.get_pressed()[1] == True:
    print("You clicked a different button")