ch07-pag161

THE MOUSE IS an extremely versatile tool. Mice are simple to use and make interaction with the computer via GUIs very straightforward.

In this adventure, you’ll learn how to use the mouse with Python and PyGame. You’ll learn how to check for mouse clicks and find the position of the mouse pointer. By building on these basic things, you’ll create some cool programs including a moving target game, a mesh drawer and a trail that follows the mouse around the window.

The mouse position uses coordinates to determine its position like other PyGame programs. Values for the coordinates are most frequently represented by x and y variables. The top-left corner has the coordinates 0, 0, with these x and y values increasing as the mouse moves farther to the right and downwards.

Getting the Mouse Position

Let’s dive into using the mouse with Python and PyGame.

This first program will get the current position of the mouse and display it. The program is meant to show you how to get the mouse position; it doesn’t do much else.

In this program, you press the left button on ...

Get Adventures in Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.