on 3 December 2007
"Never judge a book by its cover" is a good maxim but we do need something to go on, especially when there are so many shiny tomes that promise so much. So what does Apress' new book from Will McGugan promise and what does it deliver?
The title is an accurate one, the concepts and tools of game programming using the Pygame library are exposed and explained along with example code, reference tables and handy tips. The cover proclaims "From Novice to Professional" (something of an aspirational slogan used on Apress' computer books). I think this is an accurate though misunderstood term and has led to some, I feel, unfair criticism of Will's book on the Internet. The word `novice' is not the same as `beginner' and I think assuming a certain level of proficiency and general programming and gaming knowledge is a mature and useful approach.
What this book is not is a straight-forward tutorial or beginners' guide to Python or game development. The back cover has a diagram that suggests that you read Apress' `Beginning Python' book before branching out to using the language for more in-depth subjects such as games development. To me that implies that you should not be starting with this book as a way to learn how to program or to learn about games programming but as a tool to take you down the next part of the road, using Python with the libraries that the book covers. That said, there are two chapters at the start of the book that serve as a refresher or introduction to Python. If you have never programmed before then you may well need to start with a book on Python and programming in general but it is not essential, depending on your goals and basic ability to grasp the subject matter as you progress through the book. For me the book is a near perfect fit. I am an experienced but out-of-date programmer who knows a fair bit about games programming in general from assembly language and BASIC programming in the 8-bit and 16-bit computing eras.
I will come back to the subject of the book's audience and place in your library later, for now let's get inside the book a bit.
Will's writing style is conversational and this creates a mature and relaxed atmosphere, giving details and explaining acronyms as he goes along rather than with box-outs or dedicated paragraphs. In this way the book manages to pack-in a lot of information without patronising or boring the reader. Despite Will being a Brit there is a predominantly US-English spelling convention used throughout (colour without the `u' and maths without the 's' for example). This may be because the publishers are US-based or they expect the largest English-speaking audience to be in the USA. It is not of great importance in the big scheme of things but it does cause a minor annoyance to me as it stilts my reading flow at times. Some books on programming will use this convention because the programming languages themselves use US-English (color is a good example again) and I am used to that so maybe I am just looking for something slightly negative to avoid seeming sycophantic here?
As I mentioned, chapters 1 and 2 deal with Python itself and cover many of the basics of programming such as data structures and loops. These chapters introduce the Python syntax and some of the relevant ideas and jargon of object-oriented programming. As an `old school' programmer these were helpful chapters for me to get to grips with the jargon used these days and within Python particularly.
Chapter 3 starts to talk about Pygame itself and includes a brief history explaining the links to, and origins of the SDL multimedia library before moving briefly onto how to install Pygame itself. By linking you to the Pygame website, which can always provide far more up-to-date and in-depth help, Will does not waste space or slow down the pace of the subject in hand. This chapter also explores the Pygame events system and some of the basic house-keeping and initialisation functions.
Chapter 4 covers the basic graphical tools available in Pygame and once again I am impressed with Will's way of introducing several small but important topics along the way without breaking the flow. With the way he introduces the idea of colour gradients and colour blending, for example, he is constantly adding new nuggets of information and setting the foundation for further knowledge as you progress through the chapter and the rest of the book. It is almost 'subversive teaching' in a way, something I respond to better than being `taught at' so well done on that count, Will.
Chapter 5 introduces movement of graphics and covers the concept of vectors, showing readers how to apply realistic physics to moving objects. Like the 3D chapters in the book this one has quite a bit of mathematics but it is not overwhelming and the concepts are quickly explained in the context that games use them. At the end of the day there is no escaping that programming requires mathematics and that games require some physics but Will keeps it relatively painless.
Chapter 6 covers user input and game control in-depth. A lot of information about this has already been gently introduced in earlier parts of the book, particularly when introducing Pygame events back in chapter 3. I was very pleased to see discussions about various types of control such as directional and rotational controls, giving depth where it is important to the subject without using space frivolously. The chapter explains keyboard, mouse and joypad/joystick reading in considerable detail.
Chapter 7 is a brief foray into artificial intelligence in games and includes a simulation program based on some ants and their behaviour. This chapter is available online as a taster for the book. AI is a really huge subject, even just within the subset of game AI but the ants simulation is a pretty interesting way of covering state-machines, one of the most useful yet economic ways of simulating AI in most games.
Chapter 8 introduces 3D graphics, starting with the illusion of parallax using standard 2D tools. There is then the next chunk of maths in the book, taking the next step from what you learned in chapter 5 and applying a third dimension. The chapter then applies this theory to practise with more faux-3D using 2D techniques.
Chapter 9 starts out with a bit more 3D mathematical theory and then links in with another example program using 2D Pygame tools before moving on to OpenGL. The basic thrust of the chapter is towards introducing and manipulating shaded polygons, the sort of 3D that was "new" when I was last programming games. This chapter is one of the heaviest in the book and I have to admit I have only skimmed chapters 9, 11 and 12 for now as my focus is still on 2D gaming.
Chapter 10 is all about sound and music and is relatively short due to the tremendous power of the Pygame sound and music tools. There is some important information about using stock sounds, music and samples as well as letting the reader know about some useful resources such as the "Audacity" sound tool. A section is dedicated to use of the stereo sound capabilities of modern sound cards for adding realism and atmosphere.
Chapter 11 is a lot lighter with the maths than the other 3D sections and concentrates on textures (the next big leap after `real' 3D objects in games that added detail). The rest of the chapter deals with models and popular formats from commercial and open source applications and applying textures to these.
Chapter 12 is the final chapter in the book proper and carries on the discussion of 3D with lighting and fog effects as well as the concept of the "skybox".
Appendix A is a reference to the Game Objects Library, a set of open source tools from Will to assist game programming with Python in 2D and 3D
Appendix B tells you how to package up your final game for the various platforms supported by Python.
Pygame is a large library and games development is a vast subject. Will does an admirable job covering most of the tools you will use in the most common game developing situations but doesn't leave you completely high and dry on all of the advanced details as he provides links to relevant sections of the Pygame documentation as well as a brief overview of parameters and functions that he doesn't go into detail about so that you can branch off and explore some of the tangents such as optimising for certain hardware and platforms. What this book does really well is to interleave a great deal of important information into the narrative so that it is relevant and timely rather than branching off into discussing boring theory in huge swathes of text. This approach does lead to a few drawbacks though.
I think the book could have benefited from some more direct explanation of several `old school' 2D concepts, for example movement and path-following for enemies (as used in classic shooters like Xenon or platform games where the baddies follow set paths) but most of this can be inferred from chapter 5's movement studies and other information around the book. Another subject I would have expected to see at least in its own section if not with a whole chapter is collision detection. Again, the subject is covered around the book but it is such an important one it would be nice to see a clear section dedicated to it. I wonder if the desire to get the 3D sections into the book affected the decisions about what to include or not in the other sections? To be fair though, a lot of what seems to be "missing" from a programming point of view should be known to any programmer who is starting to move beyond general programming and the book's content is used wisely to get the technical aspects of Pygame across.
Essentially, if you follow the book and what it does cover, you should be on the path to learning for yourself from the vast amount of information available, one book will never tell you everything.
Although this book is less self-contained than some older games programming books, it does contain a massive amount of information and certainly promotes the Open Source idea of community interaction and makes you think about solving problems for yourself while providing the technical knowledge required to get started. When creating games it is sometimes easy to fall into old grooves and ideas where perhaps a fresh approach could help, the book succeeds in providing a solid foundation of Pygame and many OpenGL concepts without imposing the author's personal preferences on the reader.
If you are a technically-minded person you could probably get along fine using this book by itself, especially if you are interested in fast, visible results and getting on with projects you have in mind. For those who need more depth such as students who are studying Python and the subject of game development and design or those seeking a gentler learning curve or a highly structured tutorial then this is probably not quite the book for you. For teachers of these students, however, this may well be a very good book as it puts many concepts very nicely into a Python/Pygame context.