Have you ever heard about Jaql and Jackal Queenston? Very nice electronic music 🙂 Here’s one:
Anyway, now that you are listening and maybe enjoying, we can continue. Reason why I have started to make game in python using pygame module is because some homework. More precisely, I gave a introductory lecture on pygame module at one of my faculty subjects. It is about computer simulations. Final goal, is, I guess, to make some nice computer simulation using python with numpy module. Of course making it visually attractive by drawing it with pygame module. Viewing from the physics point of view, I think the simplest and attractive is simulation of a trajectory of a projectile. That is what I took and made this pocket tanks clone in python/pygame. I won’t go in physics about calculating positions of a projectile, you can read about it on http://en.wikipedia.org/wiki/Trajectory_of_a_projectile. The thing I’m about to speak, is how to make functional, or fairly functional game by your hands.
Now, lets get techy 😎
You will have to make sprites using pygame.sprite module. Sprites have update method and be added in sprites group, but we won’t do that, because it is not necessary. Simple sprite will be just fine.
Events. Pygame has a couple of ways to give you access to events. One way is through pygame.events.get() which returns a list of events. If you put that in a for in loop, you can access one and each event, and scan for mouse button down, key down etc. Of course, scanning for events has to be in engine loop, which can be simulated with a while true loop. Now, making a really simple graphics will do just fine with all introductory python/pygame tutorials. Initializing pygame module, setting up screen size, objects (sprites), while true loop for events scanning and making all visible. But, if your game needs something like modes, where you will be drawing different things according to the current mode, you will either put all that in while loop and make it very, very bad looking, or you can divide modes (scenarios) in methods. I did the second. Game has start, play and pause mode.
Start mode draws welcome text and there is not much in it.
Play mode is where you get all the fun. Drawing stats, rotating cannons, cannonball and terrain line, and above all scanning for events. Yep, the Q is now, how did I scan events, right? Or maybe not… 😐 But I’ll tell you anyway. I did scanning in while true loop and then call method for play mode with events list as it argument. Something like this:
while True:
…
events = pygame.events.get()
if mode == ‘play’:
play_mode(screen,events)
…
I did try to make for in loop for event scanning in the each of mode functions, but it kind of bugged. Really bugged and very unstable. So I did as I showed you.
This may not be the smartest way, though, because some events can be lost while interpreting all that code in play_mode function, for example. Luck with this game is that it is not very intense in user input and this way of event scanning is doing the job. I guess some user input intense game, like collaborative shooting for 2 player will really need better way of getting all input events.
As I said, it is manageable to do it by your bare hands, but I did use some already programmed functions for collisions for example. There is one fine written class for animated sprite, which I used for all sprites, cannon and cannonball.
The links are:
- http://shinylittlething.com/2009/07/21/pygame-and-animated-sprites/
- http://pygame.org/wiki/IntersectingLineDetection
I think you’ll find them very helpful, if you are new to pygame.
That’s it, I wish you to enjoy making games in python/pygame 🙂 Bye!