In this conclusion to a six-part series on the Python programming language, you'll learn about a number of modules in the standard library that are useful when writing games in Python. This article is excerpted from chapter two of the book Beginning Game Development with Python and Pygame: From Novice to Professional, written by Will McGugan (Apress; ISBN: 9781590598726).
Ueful Modules for Games
The standard library contains a large number of modules, but you will only use a few of them in game. Let’s go over some of the more commonly used modules.
Math Module
People are often surprised when I tell them I’m not very good at math. “But you are a computer programmer!” they exclaim. “Exactly,” I tell them. “I get the computer to do the math for me.” Basic math is built into Python; you can add, subtract, and multiply without importing a special module. But you do need themath module for more advanced functions—the kind of thing you would find on a scientific calculator. See Table 2-5 for a few of them.
Table 2-5. Some Functions in the math Module
Function
Description
Example
sin
Returns the sine of a number, in radians
sin(angle)
cos
Returns the cosine of a number, in radians
cos(angle)
tan
Returns the tangent of a number, in radians
tan(angle)
ceil
Returns the largest integer greater than or equal to a number
ceil(3.4323)
fabs
Returns the absolute value (without the sign) of number
fabs(–2.65)
floor
Returns the largest integer less than or equal to a number
floor(7.234)
pi
The value of pi
pi*radius**2
Let’s use themathmodule to calculate the area of a circle, given its radius. If you remember from school, the formula for this is pi times the radius squared, where pi is a magic number that equals 3.14 something. Fortunately Python has a better memory for numbers than me, and you can rely on it having a more accurate representation of pi. It’s such a simple function we will use the interactive interpreter:
>>> from math import * >>> def area_of_circle(radius): ... return pi*radius**2 ... >>> area_of_circle(5) 78.539816339744831
Because themathmodule has just a few small functions, we are lazy and import everything to the current module, just to use pi. We then define a very trivial function that takes the radius and returns the area of the circle. To test it, we calculate the area of a circle with a radius of 5 units, which turns out to be a little over 78.5 units squared.
Datetime Module
Thedatetime module has a number of functions and classes that deal with date and time. You can use it to query the time of your PC’s internal clock and to calculate time differences between dates. It may sound like a simple task, because we often do mental calculations about dates, but it can get a little complicated when you think about leap years and time zones! Fortunately we can rely on the work of some smart programmers and have Python do this effortlessly. In thedatetime module is a class of the same name. Let’s use it to find the current time:
>>> from datetime import datetime >>> the_time = datetime.now() >>> the_time.ctime() 'Thu Dec 28 15:35:26 2006'
After importing thedatetimeclass from thedatetime module, we call the functionnowto return adatetimeobject with the current time. The functionnowis what is called a static method because you use it on a class rather than an object created with that class. Once we have the current date and time stored inthe_time, we call thectimemethod, which returns a friendly representation of the time as a string. Obviously it will return a different result when you run it.
So what use is finding the time in a game? Well, you may want to store a time stamp with saved games and high scores. You could also link the game with the current time of day, so it is bright and sunny at midday but dark and gloomy if you play it in the evening. Have a look at Table 2-6 for some of the things you can find in thedatetimemodule.