Mouse events

In the “switch” example we have shown how we can react in a program when the user presses a mouse button. Although for the user a click seems like a single action, we have seen that for the computer it is a sequence of events that starts with an event of the type pg.MOUSEBUTTONDOWN.

In the following examples and tasks, we will use three types of mouse-generated events:

Event objects whose type is pg.MOUSEBUTTONDOWN also contain some additional information, such as:

Some of the additional event data contained in pg.MOUSEMOTION event objects are:

Click processing - exercises

You may not have noticed that in the “switch” program from the previous lesson, the light can be turned on and off by clicking any mouse button. This is because the same type of event is generated for each mouse button, and we did not check which button was pressed when the event occurred.

Task - left button as a switch:

Copy the “switch” program here, then modify it so that the light can be switched on and off only with the left mouse button.

Hint: Use event.button data.

Task - three switches:

Use parts of the “switch” program and create a program that simulates the work of three switches, as shown in the example.

../_images/Shema3_Off.png ../_images/Shema3_On.png ../_images/SwitchOff.png ../_images/SwitchOn.png ../_images/BulbOff.png ../_images/BulbOn.png

Other mouse events

As it was mentioned at the beginning of this lesson, a program can also respond to mouse button release and mouse motion events. To do that, it is necessary to compare the value of event.type with the constants pg.MOUSEBUTTONUP and pg.MOUSEMOTION. The following are tasks where you can try this out.

Task - drawing lines:

Complete the program so that it can draw straight lines, as in the example.

Task - drawing lines with deletion:

Copy the program for drawing lines below, then add an ability to delete all lines with a right-click.

Hint: To distinguish between left and right mouse buttons in the program, the event.button data must be used again. The code in the handle_event function should now look something like this:

Task - dragging:

The following program shows how to allow the user of the program to drag objects.

Try the program out (drag the apples into the basket) and try to understand it, then answer the questions below.

../_images/apple.png ../_images/basket.png ../_images/drag_scene.png

    Q-56: What is the i_apple variable in the program?

  • the index of the apple we are drawing
  • Try again
  • the index of the apple we are dragging
  • Correct
  • total number of apples
  • Try again
  • the number of apples remaining on the tree
  • Try again
    Pair the checks in the program with their meaning. Try again!
  • if mouse_is_on_image(event.pos, basket_pos, basket_image):
  • whether the apple should be deleted
  • if mouse_is_on_image(event.pos, apple_positions[i]
  • whether the user "took" the apple
  • if len(apple_positions) == 0:
  • whether the game is over
  • if i_apple >= 0:
  • whether a drag is ongoing

    Q-57: How do we distinguish between dragging and plain mouse movement in a program?

  • we read if a mouse button is down during movement
  • This is not a convenient way, since the button can be pressed in an empty space (the user did not "take" the object to be dragged)
  • dragging is a separate type of event
  • No, there is no such type of event
  • when plain moving the mouse, the index of the "apple we are dragging" is -1
  • Correct