Holidays are gone, tiredness not

It’s been a while, since last post…

A lot of work through holidays, if I could call it that way. Holidays for people around me, and for me, it was like a extended school period.

Anyway, I’ve been doing some rather neat things regarding my point of interest. Since I’m about to finish master studies at my faculty, or I hope I will asap, I’ve been wasting my time on couple of last exams. To make it short, there was a subject called Internet Programming, about JAVA web technologies, and for the project I did something I firstly doubled I could.

Main goal was to connect my idea of programming XMPP client, with GlassFish application server. It was very frustrating experience, since there is no systematic documentation about various Java technologies online. So, I had to use forums, and strongly rely on my intuition about some things. I have used GlassFish 3.1.2 server, with NetBeans 7.2 IDE and Jabberwocky framework for implementing XMPP components. It took me about two weeks to make it work, since CDI has been bugging me for a week. Like I said, documentation was very poor, and community very small. I’d like to thank to a man/team developing this framework, since they tried to help me. In the end, I did full install on virtual machine with java 7 jdk packages, and it worked without any problems. For developing console based client, I have used Smack Java library, and developing the client was a peace of cake, in comparing to the Jabberwocky component. But, it worked splendid at project presentation. Not to mention that professor did not even look at the code 🙂

Thing I’ve been working for a couple of days now is Python/PyGame simulation for another subject, which exam I’m going to take tomorrow. Simulation is about satellite/space ship orbiting a planet. Since that subject lectures finished in January, I did first satisfying version in February, but it had some peculiar errors in calculations (numerical), so I’ve abandoned it for some time. This is the video of it:

The second version, which I completed couple of days ago, had those errors fixed, but I did remove all eye cookie, just for the sake of debugging speed. This is the second version:

As you can see, there are no fancy panels, but having fixed all the errors, I’ve “repaired all system panels, and restore it to it full capacity” 🙂 and added some eye cookie, like velocity vector, gravitational force vector and radius vector. I hope it is finished, for good 🙂 Here is the final version:

Since this is based on numeric calculations, there is some calculation error, based on how short time period is. In this case, time period is 10^-2. To make things more serious, I’ve made some tests, without graphic interface and exported them and draw some charts in PyPlot. In, lets say, first 300 steps of iteration, error is slight, and you can see even on graphic representation. If you make 1000 iterations, and take 300 steps at the end, you can see how numeric orbit is changed through time, in comparing to a orbit which is drawn regarding analytic formula. Here is the chart for 300 steps, form 699th until 999th.

Blue line is orbit drawn by coordinates based on iterative process. Green one is based on 100% correct coordinates based on analytic formula of ellipse. Reducing time period to 10^-4, errors are slight even for 20 000 iteration steps. Of course, you have to find balance between wanted precision and time needed to finish calculations.

This is what I’ve been doing lately. Maybe its interesting?

Computer simulation in pygame

Today, we are going to … I’m going to talk about … I’m going to present you a computer simulation. It’s not just any simulation, it is a simulation of body acceleration in gravitational field. Wow, physics! Yes! 😎

In particular, it simulates how space shuttle should be accelerating near planet, and use it’s gravitational field to accelerate. I’m not a physician, so I won’t talk about physics theory, but it is good to know something about Newton’s three laws, Newton’s gravitation law (or formula of gravity),  Kepler’s laws, and conservation of energy. Ok, the last would be nice to know for analytic solution of body’s trajectory in gravitational field of a planet. Unless you are physician, or mathematician, you should use numerical solution for solving simulation problems, because it can be good enough 🙂 Enough, it you do your best do reduce error. In this simulation, that could be done reducing time step for numerical formula. Since reducing time step would shorten the path that object traverses, animation would be veeeery slow. I guess I could overcome this problem, but it is very appealing right now, or  appealing enough 🙂

The simulation lets you set body’s initial coordinates and initial velocity, when dragging mouse. When you release the left mouse button, body starts to move, by numerical formula, and you get yourself an animation 🙂 Real eye cookie 😎 You can turn on statistics monitor, and also a body’s velocity vector in it’s little coordinate system in down right place of the simulation. You can also change planets. All 9. You can say, well Pluto is no longer a planet. Ok, that’s true, but I have found a png images of a planets and Pluto’s was there, so I added it in 🙂 Yey, my simulation has 9 planets! Anyway, simulation has configuration file, where you can change masses of planets, since my masses are not very realistic, not even for comparing between two planets in solar system.

Software used for this simulation is Python 2.7 and PyGame library.

You can download it here.

Bye!

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!