Top positive review
31 people found this helpful
An excellent introduction to Pygame and 2D game creation
on March 17, 2012
"Making Games with Python and Pygame" is the second in a series of tutorial books from Al Sweigart. Knowing that it can often be a somewhat tedious affair, Sweigart takes the approach that it's easier to learn a new computer language if it's applied to something fun. His first book, "Invent Your Own Computer Games with Python", put this to great effect as it lead the reader through learning basic Python by creating several small and simple games. His latest book continues this concept with more complex games and introduces the Pygame library for manipulating graphics, animation and sound. Well, re-introduces Pygame. The "Invent" book had a chapter on Pygame but it really needs its own book, hence this one.
"Making Games with Python and Pygame" is divided up into ten chapters. Over the course of the book, you will learn to create twelve games and every one of them is fun and very useful in the concepts that they teach. Make no mistake, this is not a book on basic Python syntax or just using the Pygame library. This book teaches you how to make actual games with a nice level of "professional" touches. I was surprised by a number of the hidden gems that were included in the book.
Each chapter follows the same basic formula. First is a brief description of the game that will be created and how it works. Next, is the full source code for the game. Then, he goes through the code, section by section, with a thorough explanation of what the code does and why it does it. At the end of the chapter, is often ideas for expanding the game and a link to "buggy" versions of the game that you can use to practice debugging techniques. The buggy versions can all be downloaded and each version tells you exactly what is wrong with the code. In fact, the book makes several references to supplemental material to be found on the book's website.
One thing about the chapters that I really appreciated was the way that they are organized. Each section of code has its own subchapter heading. This makes it very easy to find information on that section or, more importantly, the concept that section represents. These subchapters are also incorporated into the main table of contents. Another really great idea is that the book's website includes a "diff" tool where you can compare the source code that you typed in to the actual code for the games. This is extremely helpful if you get stuck on a bug and can't figure out what you did differently from the code itself. Combine this with the "buggy" versions available online and you can learn a lot. For example, comparing the working source code to a version where the game character isn't displayed on the screen as expected. This is the first time that I've seen this concept applied and it's a wonderful tool.
Here is a quick breakdown of the chapters and the topics that they cover:
Chapter 1 introduces you to the intent of the book and a few other preliminaries such as getting Python and Pygame installed.
Chapter 2 covers the basics of the game loop, game states, and general concepts such as managing the screen, graphic surfaces (canvases), basic drawing, animation and sound manipulation, and more. It also explains how to manage how fast the screen is drawn (refresh rates and frames per second). These are all very important ideas to understand before you can tackle the games themselves and Sweigart does an excellent job of leading you through them.
Chapter 3 introduces you to your first game, the classic "Memory" puzzle. The purpose of this chapter is to give you some experience with a real game loop and refresh you on your basic Python (conditions, loops, etc), as well as coordinate systems. It also introduces you to some nice additional features - basic animations for starting and ending the game.
Chapter 4 has you creating a Sliding Numbers puzzle. Key concepts such as keyboard and mouse input, screen rendering and animation are used here. I really want to emphasize something that is introduced in this chapter: "flair". Most beginner books that describe how to make games just teach you the basic mechanics of the game itself. Several of the chapters in this book teach you how to add some nice flair to your games. Chapter 4 has you create a nice little animation when the game is restarted. Rather than simply redrawing the board in a reset state, it has you play back every move in reverse, so it looks like the puzzle is being unshuffled. It might sound like a small thing but it helps to add a level of "professionalism" to the game. Especially for a beginner, it helps to give them the feeling that they have created something special. Little touches like this are spread throughout the remaining chapters.
Chapter 5 is built around a Simon-type (copy me) game. For those that may not remember Simon, its an electronic game that has four large buttons, each a different color, that light up and play a sound in sequence. The player then has to push each button in the same sequence. The longer you play, the longer the sequences get. This chapter focuses on the use of sound and controlling it, as well as using time as a feature. It also focuses on a bit of flair called an "attractor". An attractor is often used when a game is sitting idle. This is very common in arcade coin-op games where they will play an animation or sound clip to get your attention. You'll also see it used in games where you control a character on-screen (a la Mario Brothers or Braid) and let them sit idle for a bit and they start to get "bored". They'll start tapping their toes or looking around, maybe whistle... Those actions are all possible with the attractor logic and "idle state" taught in this chapter.
Chapter 6 uses the classic Snake game, here called "Wormy", to teach simple collision detection and mapping concepts. While not explicitly stated in the chapter, his approach to tracking the snake on the playing field and rendering it is very similar to managing levels and drawing their maps onscreen in RPGs. This concept is expanded on later in Chapter 9. Text rotation is also introduced for the title screen.
Chapter 7 expands on what has been learned so far with a Tetris clone. Here we're getting into complex shape manipulation, variable timing for user movements, more collision and rotation techniques and UI layout. A critical element of this chapter's game is that it is played in real time. Anyone that has played Tetris will be all to familiar with how frantic things can get in the later stages. The author takes the reader through some basic algorithms for increasing the complexity as the player progresses. Like Wormy in the previous chapter, on-screen elements are kept track of by using an array to store an object's boundaries, but it works and it works very well. Using the concepts learned here, you can track a complex object in space and move it around with some interaction with the environment (e.g. walls and other pieces). This is revisited again in Chapter 9. A strong emphasis on game logic is the underlying theme of this section and it helps prepare the reader to create more complex games.
Chapter 8 brings autonomous characters into the mix as you create a simple variation of Katamari Damacy. You basically take control of a squirrel that runs around eating other smaller squirrels while avoiding being eaten by larger ones. As you consume the other objects, you increase in size. There are some huge concepts in this chapter such as sprite manipulation, camera manipulation and viewports (essential for side-scrollers, which is alluded to with a reference to Super Mario World). This basically allows you to create games that have very large playing areas while only showing a small section at a time. You also learn how to do some basic sprite movement to make it appear as if your character is jumping/bouncing with the introduction of the Sine mathematical function. Also nice to have if you want to throw a ball up in the air and have it come back down realistically. Not content to show you how to merely move the screen's camera around, a little bit of flair is introduced that gives the player's character some "slack" where he can move on the screen before the camera moves, as well as brief invulnerability when the player first spawns.. Again, just a nice thing to see in a book directed towards beginners.
Chapter 9 is not the last chapter but it is the one that ties everything together in one really nice setup. Here the game is Star Pusher, a clone of Sokoban. This is an isometric game using a tile-set and externally stored maps, actual sprites, collision detection, and more graphic techniques. Upon completion of this chapter, you will have just about everything you could need to build a 2D game.
Chapter 10 finishes things up with four additional games that you can use to reinforce what you have learned and discover new techniques. The games included are variants of Othello, Flood It, Connect Four, and Bejeweled. Of special importance here is the introduction of basic A.I. concepts in Othello and Connect Four.
There are a few things that I would have liked to have seen included. It would have been great to have an actual side-scroller game, since I know that a lot of people are interested in that. Manipulation of background images with basic parallaxing would have been useful in that context, too. Greater use of canvases for more complex UIs would be helpful for people interested in creating RPGs or RTSs. Seeing a little more done with sounds and sound caching (similar to Chapter 9's caching of sprites) would also have been welcome. Speaking of sprites, sprite animation would also be nice. It's one thing to move a character around on the screen. It's a whole 'nother world to see him dance. A demonstration of using multiple screens (beyond a game over one) is another thing that I would have liked to have seen. Lastly, I would have preferred if the concept of basic class objects was introduced as a data structure instead of a dictionary type, as used in Chapter 9. Maybe for the "Making Advanced Games..." book?
One other thing to keep in mind is that this is not a Python tutorial and you will need a reference on hand for both Python and the Pygame library. If you are new to Python, you will definitely want to start with his other book first. Also, much has been said about the fact that this book is available online for free. While that is true, there is a lot to be said for having the physical book in front of you. Al has done a great job with this book and should be rewarded. If you opt for the online version, please, at the very least, consider hitting the PayPal donation link on his site.
I've been a professional software developer and architect for almost 30 years and have used various methods to teach programming to colleagues, friends, and kids. I've found structuring the lessons around games to be a very effective approach. I found Al's first book, "Invent Your Own Computer Games with Python", to be a very useful starter in learning Python. My daughter (who was nine years old at the time) used the first book to learn Python with great success. From it, she came away with the ability to create a small-scale RPG involving a certain well-known teen wizard (yes, that one) that very quickly became an elaborate adventure game. "Making Games with Python and Pygame" has allowed her to more fully flesh out that first game and she is excitedly making plans for a slew of new games. It's great to see her enthusiastic about programming and even better that it didn't involve any coaxing from me. She basically did it all on her own.
If she can do it, so can you.
[Note: A friend of mine asked if I was a close personal friend of the author, based on my username. No, that's a reference to "Weird" Al Yankovic, whose fans are known as "A Warm, Close Personal Friend of Al"].