Reading mouse buttons

Information about the currently pressed mouse buttons is provided by the pg.mouse.get_pressed() function. This function returns a tuple of three elements (an ordered triple), which are used as logical values. The tuple elements correspond to the left, middle and right mouse buttons respectively. A value of True indicates that a button is currently pressed, and False that it is not.

The example below shows how to read which mouse buttons are pressed. This is the part of the program where that happens:

 
1
pressed_mouse_button = pg.mouse.get_pressed()
2
3
if pressed_mouse_button[2]: # right button - new game
4
    (x, y) = (width//2, height//2) # return the ball to the center
5
    won, lost = False, False # the player has neither won nor lost
6
7
if pressed_mouse_button[0]: # left button - move the ball
8
    (xm, ym) = pg.mouse.get_pos() # mouse position coordinates
9
    # the ball moves away from the mouse for another half of that distance
10
    x = x - 0.5 * (xm - x)
11
    y = y - 0.5 * (ym - y)
12

(PyGame__interact_put_ball_into_box_part)

The pressed_mouse_button tuple gets three values returned by the pg.mouse.get_pressed() function. We then typically use these values in if statements. For example, if pressed_mouse_button[2] means “if right button is pressed” (0 for left, 1 for middle, and 2 for right).

Examples and tasks

Example - put the ball into the box:

While the left mouse button is held pressed, the ball moves away from the cursor. The goal is to put the ball into the red box by moving the mouse and pressing the left button. Pressing the right button returns the game to the beginning.

First, study the new_frame() function carefully, and then have a look at the other parts of the code as well. Try the program out and see if it works as you expected after reading the description.

50
 
1
import pygame as pg, pygamebg
2
width, height = 400, 400
3
canvas = pygamebg.open_window(width, height, "Put the ball in")
4
font = pg.font.SysFont("Arial", 30) # the font to display the text
5
6
r = 10 # ball size
7
(target_x, target_y) = (width//4, height//4) # target point
8
target_box = (target_x - 2*r, target_y - 2*r, 4*r, 4*r) # box around target point
9
10
(x, y) = (width//2, height//2) # ball starts from the center of the window
11
won, lost = False, False
12
13
def draw():
14
    canvas.fill(pg.Color("black")) # black background
15
    if won or lost:
16
        # the game is over, display a message
17
        poruka = "Well done!" if won else "ran away..."
18
        text_image = font.render(poruka, True, pg.Color("green"))
19
        tx = (width - text_image.get_width()) // 2
20
        ty = (height - text_image.get_height()) // 2
21
        canvas.blit(text_image, (tx, ty))
22
    else:
23
        # the game is still running, draw the box and the ball
24
        pg.draw.rect(canvas, pg.Color("red"), target_box, 3)
25
        pg.draw.circle(canvas, pg.Color("green"), (int(x), int(y)), 10)
26
27
def new_frame():
28
    global x, y, won, lost
29
    
30
    pressed_mouse_button = pg.mouse.get_pressed()
31
    if pressed_mouse_button[2]: # right button - new game
32
        (x, y) = (width//2, height//2) # return the ball to the center
33
        won, lost = False, False # the player has neither won nor lost
34
        
35
    if pressed_mouse_button[0]: # left button - move the ball
36
        (xm, ym) = pg.mouse.get_pos() # mouse position coordinates
37
        # the ball moves away from the mouse for another half of that distance
38
        x = x - 0.5 * (xm - x)
39
        y = y - 0.5 * (ym - y)
40
41
    # if the center of the ball is near the center of the target - the player wins
42
    if abs(x - target_x) < r and abs(y - target_y) < r:
43
        won = True
44
    # if the center of the ball is out the window - the player has lost
45
    if x < 0 or x > width or y < 0 or y > height:
46
        lost = True
47
    draw()
48
49
pygamebg.frame_loop(50, new_frame)
50

(PyGame__interact_put_ball_into_box)

Task - to and from the mouse:

Complete the program so that it works as shown in the example (“Play task” button).

  • When the left mouse button is pressed, the ball should move away from the mouse, as in the “put the ball into the box” example above, but not by half distance, but only by a tenth of the distance to the mouse.

  • When the left mouse button is not pressed, the ball should move closer by one tenth of the distance to the mouse (as in the “towards mouse” task in the previous lesson).

17
 
1
import pygame as pg, pygamebg
2
(width, height) = (400, 400)
3
canvas = pygamebg.open_window(width, height, "Ball following the mouse")
4
5
(x, y) = (width // 2, height // 2) # ball starts from center of the window
6
7
def new_frame():
8
    global x, y
9
10
    # ADD THE MISSING PART
11
12
    # draw a green ball on a white background
13
    canvas.fill(pg.Color("white"))
14
    pg.draw.circle(canvas, pg.Color("green"), (int(x), int(y)), 10)
15
16
pygamebg.frame_loop(50, new_frame)
17

(PyGame__interact_to_and_from_mouse)

Task - laser:

Complete the program so that it works as shown in the example (“Play task” button).

While the left mouse button is held pressed, the “laser” is on, otherwise it is off. While the laser is on, its energy decreases by 1 (but not below 0), and when it’s off the energy increases by 2 (but not over 100).

28
 
1
import pygame as pg, pygamebg
2
width, height = 400, 400
3
canvas = pygamebg.open_window(width, height, "Laser")
4
5
laser_on = False
6
energy = 25 # how full is the laser from 0 to 100
7
8
def draw():
9
    canvas.fill(pg.Color("black")) # background
10
11
    # the indicator shows how full the laser is
12
    pg.draw.rect(canvas, pg.Color("green"), (10, 10, 100, 10), 1)
13
    pg.draw.rect(canvas, pg.Color("green"), (10, 10, energy, 10))
14
15
    if laser_on:
16
        reach = (4 * energy, height - 4 * energy)
17
        pg.draw.line(canvas, pg.Color("red"), (0, height), reach, 5)
18
19
def new_frame():
20
    global energy, laser_on
21
22
    # READ THE STATE OF THE LEFT MOUSE BUTTON AND SET THE VALUES
23
    # OF THE GLOBAL VARIABLES energy, laser_on
24
25
    draw()
26
27
pygamebg.frame_loop(15, new_frame)
28

(PyGame__interact_laser)