Mouse Event Handling
In the earlier section, we showed how the keyboard buttons can be used to move an image on the display surface. In this section, we will show you how to use mouse clicks and mouse motion to move the object on the screen. The following program moves the ball image to the left or right depending if the mouse left key or the right key is clicked respectively. The program also detects mouse motion and moves the ball as the mouse is dragged.
PROGRAM EXAMPLE: MOUSE EVENT HANDLING
Program: pygame_event_mousemotion_move_boundary_check.py
Image Name: ball.png
# Application to move the ball using mouse
# Pygame module needs to be imported before we
# can use.
import pygame # 1
# Initialize all pygame modules
pygame.init() # 2
# pygame.display.set_mode()sets up app window’s width
# and height (640 pixels wide and 480 pixels high)
# This function creates a new Surface object
screen = pygame.display.set_mode((640, 480)) # 3
# Define RGB for colors
BLACK = (0, 0, 0) # 4
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Specify ball diameter. Use it to check when the
# ball reaches boundary of the surface
ball_width = 99
# Title for the app window # 5
pygame.display.set_caption('BallMoveMouseMotionBoundaryCheck')
#
clock = pygame.time.Clock() # 6
# Load the Ball image ‘ball.png’.
ballImage = pygame.image.load('ball.png') # 7
# Starting location of the ball
x = 0 # 8
y = 0
# Define ball(x, y) function.
We will call it later in Step 12. # 9
def ball(x, y):
screen.blit(ballImage, (x, y))
#Main Loop # 10
while True:
event = pygame.event.wait() # 10a
# Check if the 'close' button (x) of the
# window is pressed
if event.type == pygame.QUIT:
# stops the application
break
###### Check if any mouse button is pressed. ######
if event.type == pygame.MOUSEBUTTONDOWN: # 10b
if event.button == 1: # Check if the LEFT mouse button is clicked
print('Left mouse button pressed', x, y)
elif event.button== 3:
# Check if the RIGHT mouse button is clicked # 10c
print('Right mouse button pressed', x, y)
# print on the console the pressed buttons,
# and the mouse position at the time of event.
###### Check for mouse motion ######
if event.type == pygame.MOUSEMOTION: # 10d
# print on the console the mouse position and
# relative movement at that time
print('Mouse motion detected', event.pos)
(x, y) = event.pos
###### check Boundary limits for Mouse Motion ##### # 10e
if x > 640 - ball_width:
x = 640 - ball_width
elif x <= 0:
x = 0
if y > 480 - ball_width:
y = 480 - ball_width
elif y <= 0:
y = 0
#
screen.fill(WHITE) # 11
ball(x, y) # 12
pygame.display.update() # 13
clock.tick(60) # 14
#
pygame.quit()
quit()
Detailed Line-byline Explanation of the Program
Step 1) through 9) are the same as in program ballMoveKeyboardBoundaryCheck.py
10) THis step is the mainloop that uses a while loop. It checks for the occurrence of any mouse events. It also checks if the ball has moved out of the surface boundaries.
10a) if event.type == pygame.QUIT:
Test if the user wants to quit the program by looking for evet type = pygame.QUIT
10b) if event.type == pygame.MOUSEBUTTONDOWN:
Test if the MOUSE Button is down. If it is down, test if it is the LEFT MOUSE button. If the LEFT MOUSE button is pressed, print “Left mouse button pressed”. Also, print the (x,y) location of the mouse when the button was pressed.
10c) elif event.button== 3:
Test if the RIGHT MOUSE is pressed. If the RIGHT MOUSE button is pressed, print “Right mouse button pressed”. Also, print the (x,y) location of where the event occurred.
10d) if event.type == pygame.MOUSEMOTION:
Test if the mouse motion occurred, that is if the mouse was dragged. Print “Mouse motion detected” and print the (x,y) location where the motion occurred. The event.pos
is the position of the mouse at the time event occurred.
10e): Test if the ball has moved beyond the boundary of the screen. If it has crossed the boundary, stop the x-coordinate or y-coordinate from going beyond the screen.
11) through14) are the same as in the program example ballMoveKeyboardBoundaryCheck.py.
Copyright © 2019 – 2021 softwareprogramming4kids.com
All Rights Reserved