Pygame and mouse events

MOUSEBUTTONDOWM – MOUSEBUTTONUP

Let’s see how to check when the mouse button is pressed. Not only we will check the mouse button on the left, but the middle one, the right one and also the when mousewheel will be pushed up or down.

 

Risultati immagini per mouse icon png

Initialize the window

First

  • we start pygame with pygame.init(),
  • init the font
  • create a font object in Comin Sans MS, 14 of size
  • then we create the clock object to control the frame rate
  • we create the SURFACE object (screen)
  • we give the window a title with set_caption method of display
import pygame

# Initialize pygame and the screen
pygame.init()
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 14)
clock = pygame.time.Clock() # frame rate
screen = pygame.display.set_mode((400, 400)) 
pygame.display.set_caption('Mouse events')

Then we create a little function to render the text

def update_text(text, color=(0, 255, 255)):
    text = text.encode()
    mf = myfont.render(text, True, color)
    screen.blit(mf, (10, 10))

In the main function we start with some variable for colors, text etc.

def main():
    c = 0
    tc = (c,c,c)
    text = "'http://pythonprogramming.altervista.org'"
    color = (255,255,255)
    update_text(text, color=color)
    loop = 1
    CORAL = (128,0,0)
    DARKGREEN = (0,255,128)
    COLOR = CORAL

The interesting part is the loop where we get the input of the user and we show different text for every event

    while loop:
        screen.fill(COLOR)
        clock.tick(60)
        events = pygame.event.get()
        update_text(text, color=color)
        for event in events:
            if event.type == pygame.QUIT:
                loop = 0
            elif event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.MOUSEBUTTONUP:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    action, color = "pressed", (0,255, 255)
                elif event.type == pygame.MOUSEBUTTONUP:
                    action, color = "released", (255, 64, 64)
                if event.button == 4:
                    print("MOUSEWHEEL UP")
                    action = "MOUSEWHEEL UP"
                    COLOR = CORAL
                if event.button == 5:
                    print("MOUSEWHEEL DOWN")
                    action = "MOUSEWHEEL DOWN"
                    COLOR = DARKGREEN


                text = f'button {event.button} {action} in the position {event.pos}'
                print(text)
        pygame.display.update()
    pygame.quit()

This is the whole code:

import pygame

# Initialize pygame and the screen
pygame.init()
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 14)
clock = pygame.time.Clock() # frame rate
screen = pygame.display.set_mode((400, 400)) 
pygame.display.set_caption('Mouse events')

def update_text(text, color=(0, 255, 255)):
    text = text.encode()
    mf = myfont.render(text, True, color)
    screen.blit(mf, (10, 10))


def main():
    c = 0
    tc = (c,c,c)
    text = "'http://pythonprogramming.altervista.org'"
    color = (255,255,255)
    update_text(text, color=color)
    loop = 1
    CORAL = (128,0,0)
    DARKGREEN = (0,255,128)
    COLOR = CORAL
    while loop:
        screen.fill(COLOR)
        clock.tick(60)
        events = pygame.event.get()
        update_text(text, color=color)
        for event in events:
            if event.type == pygame.QUIT:
                loop = 0
            elif event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.MOUSEBUTTONUP:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    action, color = "pressed", (0,255, 255)
                elif event.type == pygame.MOUSEBUTTONUP:
                    action, color = "released", (255, 64, 64)
                if event.button == 4:
                    print("MOUSEWHEEL UP")
                    action = "MOUSEWHEEL UP"
                    COLOR = CORAL
                if event.button == 5:
                    print("MOUSEWHEEL DOWN")
                    action = "MOUSEWHEEL DOWN"
                    COLOR = DARKGREEN


                text = f'button {event.button} {action} in the position {event.pos}'
                print(text)
        pygame.display.update()
    pygame.quit()

main()

This is how the window appears together with the messages on the console.

In this video you see the window in action:

To be continued…

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.