With the processing power with mobile devices today and in particular mobile phones, you can-with a little effort, patience and eager-implement interesting games in Java for cell phones.
The "cell" of today is like a computer with lots of peripherals integrated camera, keyboard, screen, cell phone, external card reader, wifi, gps, gsm, etc. And all this in the palm of your hand.
Some of these mobile devices bring proprietary operating systems, others use some version of Windows, Linux or Symbian OS. But there is one characteristic that most have in common: Java. Obviously not the full power version of Java that is installed on our desktop, laptop or server. This is a version adapted for mobile devices which do not have gigabytes of RAM and multiple CPUs with clock frequencies of the order of Giga Hetrz.
Surprisingly, Java Micro Edition (JME, AKA J2ME) has the characteristics that make it a very interesting platform to develop applications and especially games. And the latter are the ones who are going to explore.
First a little theory
Given the number of mobile devices with different characteristics-all-be invented "skills profiles" for MIDP Mobile Information Device Profile. Depending on the version of MIDP, the device will have more or less skills: communication, presentation, audio, etc..
MIDP addition, there is another specification called CLDC Connected Limited Device Configuration for, which defines low-level capabilities of the device, for example, if you have the ability to handle floating-point number.
Is based on CLDC MIDP to form the Java ME platform. The idea is to develop more compatible platform as possible. Today, most phones support MIDP 2.0 and CLDC 1.1, but if we restrict ourselves to CLDC 1.0 better. The Motorola V360 is an example of mobile phone with MIDP 2.0 and CLDC 1.0.
Then when we start to develop, we have to choose which version of MIDP and CLDC going to use. Oracle On page (http://java.sun.com/javame/technology/index.jsp), you can find more information and the SDK (http://java.sun.com/javame/downloads/index. jsp).
Well, we have the SDK installed, we trust our IDE configured. Now what? To schedule! Well, not so fast. How do you program a game? This is something we have been doing IT since years ago, we have no very clear. The Internet is a lot of documentation, but the really good is scarce.
First of all, we will see is the skeleton of a pseudo-game-in:
process user input update the state of play screen drawing goto 1
Patron saint of the computer, sorry for the goto.
There is nothing revealing in these 4 lines, but what if you can start seeing some problems. For example, this game will run at different speeds depending on the power of the cell. Something is clearly not desirable. Obviously we want our game to run at the same speed in any phone, regardless of the power of it.
For this, we make a small change:
process user input update the state of play screen drawing wait X milliseconds goto 1
Now varying X, we can vary the speed of the game. Easy ... no? Rests with the user to choose X by, for example, the menu the game. But X could not be determined automatically? Better yet, why X could not be determined dynamically at every turn of the cycle? Sure. Now we see:
ciclos_por_segundo = 30 milisegundos_por_ciclo = 1000 / ciclos_por_segundo t_inicial = now () process user input update the state of play screen drawing t_ciclo = now () - t_inicial if (t_ciclo <milisegundos_por_ciclo) then wait (milisegundos_por_ciclo - t_ciclo) milliseconds goto 3
Note: The function now () returns the current time in milliseconds, something like that done by System.currentTimeMillis () in Java.
What you do is, take the time it takes a full cycle (4, 5 and 6). If that time is less than the amount of time we established for each cycle 1 and 2, so we expect this difference in time. Thus, all the same and delay cycles waiting time is dynamically adjusted.
In this way we perform as well between different power cell. This method can be improved by implementing breaks cycles (frame skipping) when the cell is very low power, but we run the risk that the game becomes unplayable. Thus, the game run slower in low-power cell, and that all the time will be consumed in paragraphs 4, 5 and 6 and never enter standby.
It's amazing how many interesting things that can be done using this simple technique.
In practice
We will implement, with this technique, the "hello world" of the games: PONG by one.
It's like an Arkanoid, but without the bricks. True, not much fun.
The Java implementation of the main loop would look something like:
CICLOS_POR_SEGUNDO final int = 30; MILISEGUNDOS_POR_CICLO final int = 1000 / CICLOS_POR_SEGUNDO; while (true) { long tInicial = System.currentTimeMillis (); procesarEntradaDelUsuario (); actualizarEstadoDelJuego (); dibujarLaPantalla (); long tCiclo = System.currentTimeMillis () - tInicial; if (tCiclo <MILISEGUNDOS_POR_CICLO) { Thread.sleep (MILISEGUNDOS_POR_CICLO - tCiclo); } }
Note that so far there is nothing done particularly for the PONG. It will be particularly procesarEntradaDelUsuario functions (), actualizarEstadoDelJuego () and dibujarLaPantalla ().
The first has the responsibility to take user input and change something in the game, in this case would be the position of the palette. Yes, the palette is the green rectangle .. have to have imagination
The second must update the internal state of the game. In this game, this function has two objectives: a) move the ball and b) if the ball goes down, then the game ends with the fateful "Game Over".
The third and final feature, you must take the state of the game and draw on the screen.
The source code can be downloaded from here: pong-scr.tar . In this implementation, is never lost. When the ball goes down, appears again in the middle of the screen. A good exercise would be to add life, to increase the speed of the ball, giving acceleration to the palette, improve graphics, add ladrillitos, sounds, and name it powerups marketer (not Arkanoid, clear
) And then, fame!
Enjoy!

