O'Reilly logo

Adventures in Python by Craig Richardson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required