Pygame

Grey overlay

Pink

Green

Blue

Cream

Liliac

Purple

Yellow

Objectives

Developing Programming and Development (learning strand), specifically:

  • Understand and be able to use Interrupt Events to interact with a game.
  • Understand that Event Listeners can be used to check if an action has been performed.
  • Understand that Event Handlers can be used to perform an action detected by an Event Listener.

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 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:
<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 the lines from the Thonny/Python console in the bottom window 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.
screen.fill(black)
mousePosition = pygame.mouse.get_pos()
print(mousePosition)
  • The code will look like this:

3-2.PNG

  • Look at the Thonny/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:
screen.fill(black)
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:

3-3.PNG

  • 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")