Mouse Control
PyGame tracks the mouse position, button
clicks, and movement through events and the pygame.mouse
module — essential for interactive games and user interfaces.
Learning Objectives
- 12.5.3.3 Code mouse control
Conceptual Anchor
The Touch Screen Analogy
Just like a touch screen knows where your finger is and when you tap, PyGame tracks the mouse cursor's coordinates and detects clicks. You can use this to let players aim, click buttons, drag objects, and more.
Rules & Theory
Mouse Functions
| Function | Returns | Example |
|---|---|---|
pygame.mouse.get_pos() |
(x, y) tuple — cursor position | mx, my = pygame.mouse.get_pos() |
pygame.mouse.get_pressed() |
(left, middle, right) — booleans | buttons = pygame.mouse.get_pressed() |
pygame.mouse.set_visible(bool) |
Show/hide cursor | pygame.mouse.set_visible(False) |
pygame.mouse.set_pos((x,y)) |
Move cursor to position | pygame.mouse.set_pos((400, 300)) |
Mouse Events
| Event | When It Fires | Attributes |
|---|---|---|
MOUSEBUTTONDOWN |
Button pressed | event.pos, event.button |
MOUSEBUTTONUP |
Button released | event.pos, event.button |
MOUSEMOTION |
Cursor moved | event.pos, event.rel |
Button Numbers
event.button: 1 = left, 2 = middle, 3 = right, 4 = scroll up, 5 = scroll down.
Worked Examples
1 Object Follows Mouse
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Mouse Follower")
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Get mouse position
mx, my = pygame.mouse.get_pos()
screen.fill((30, 30, 30))
# Draw circle at mouse position
pygame.draw.circle(screen, (0, 200, 255), (mx, my), 25)
# Show coordinates
font = pygame.font.SysFont("Arial", 18)
text = font.render(f"({mx}, {my})", True, (255, 255, 255))
screen.blit(text, (mx + 30, my - 10))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()2 Click to Paint Dots
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Click Painter")
clock = pygame.time.Clock()
dots = [] # store painted dots
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # left click
dots.append(event.pos)
screen.fill((255, 255, 255))
for dot in dots:
pygame.draw.circle(screen, (255, 50, 50), dot, 15)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()3 Clickable Button
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Button Demo")
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 28)
# Button rect
button = pygame.Rect(300, 250, 200, 60)
clicks = 0
running = True
while running:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if button.collidepoint(mx, my): # click on button?
clicks += 1
screen.fill((30, 30, 30))
# Change color on hover
if button.collidepoint(mx, my):
color = (80, 180, 80) # green hover
else:
color = (60, 120, 60)
pygame.draw.rect(screen, color, button, border_radius=8)
label = font.render(f"Clicks: {clicks}", True, (255, 255, 255))
screen.blit(label, (button.x + 30, button.y + 15))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()Common Pitfalls
get_pressed() vs MOUSEBUTTONDOWN Event
get_pressed() returns the current state (held down?).
MOUSEBUTTONDOWN fires once per click. For single-click actions
(buttons), use the event. For continuous actions (dragging), use get_pressed().
Tasks
Create a drawing app: hold left mouse button to draw a line trail as the mouse moves.
Create a "target practice" game: random circles appear, click them to score points.
Build a color picker: 5 colored squares at the top, click one to select the paint color, then draw with that color.
Self-Check Quiz
Q1: How do you get the current mouse position?
mx, my = pygame.mouse.get_pos() — returns (x, y) tuple.Q2: How do you detect if a button was clicked?
button_rect.collidepoint(mx, my) inside a MOUSEBUTTONDOWN event to
check if the click position is inside the button rectangle.Q3: What button number is left click?
event.button == 1
— 1=left, 2=middle, 3=right.