Pygame is a 2D graphics and gaming library for Python. It’s pretty nifty because it essentially gives you a blank window that you can draw any shapes or lines or images you want on it. But it doesn’t come with any UI elements like buttons, scrollbars, or check boxes. This post will go through not only creating a button class for Pygame, but also the reasoning behind why I’ve set up the code as it is. This is more of a “how to create a module other people can use” tutorial than a UI or Pygame tutorial.
This tutorial assumes you know the basics of Pygame and Python programming. If you don’t, it’s probably easy enough to follow anyway.
A button is a common user interface (UI) control that is used in many software applications. It seems simple enough: there’s a button on the window and you click on it and something happens. But there’s a lot of details we should plan out ahead of time. Remember, we want to make a generic button class so that other programmers can use this in their games and programs. Once you’ve read through the process here, you’ll be familiar with how to make your own modules for UI elements.
Designing a UI button class is good a good programming practice exercise.
The Feature List (and the Non-Feature List)
First, let’s create a list of design details for the buttons: