Using python and pygame

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:

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!

Processing, in the meantime…

Can’t even remember when was my last post 😦

A lot of things to work with, lot of really cool stuff to try and experiment with… Summer holidays, parties and relaxing… Now we’re here!

 

There was a lot of things going on. Last thing is this:

 

Thing is, this is not something very big, for you, me, but its kind of cool to do it in one hour from very very basic knowledge of Processing.

Bye!

Processing the ideas!

Hey hey, it is a bit late in the night, so I’m going to make this shorter for the sake of my sleep.

There was some time since I did last introduction in Processing. I have had many things to do, so I abandoned it, hoping I will come back to it someday in the future. Awwww, like a tale! 🙂 I almost feel sorry for me! Almost! Anyway, like a good old(young) student I don’t have time to do things I would like, or at least I don’t have enough time… Where is the Processing in this story??? Here it is!

This semester I had to do some seminar on formal languages, automata and language processors. That has to do with computational theory. I had freedom to choose subject of my seminar, and I did. It is about cellular automata. Wolfram’s rules, Rule 30 (not that you would firstly picture in your mind) and Conway’s game of life. Game of life or just Life was very fascinating to me. So fascinating I wanted to make it by myself. I did. I programmed it in C, with the lousy visualization in terminal (with 0 and 1). Then I remembered. Processing! Just the right tool for it!

But wait! I did not know the intermediate techniques for it! That did not scare me, and should not scare you. Very similar to Java. You don’t know Java? Never mind, you will learn it for a short period because it is very intuitive. With knowing the C and Java, and some basics in graphics, programming the game of life took me couple of hours.

Here it is:

Code is quite simple, and I will post it here. Since I don’t like how my wp is formatting the code, I will post link to it, when I figure out how to present it.

At the end, program is not complicated, Processing is not complicated too, but the game is very fun to set and watch.

Bye !:)