Mobile Phones, Space Aliens and Dinosaurs
By seapegasus on Jun 10, 2006
Many will agree that it's more motivating to learn a new programming language by either implementing something immediately useful (like Ramon Ramos' modules) — or something immediately funny. So during JavaOne I bought this nice book J2ME Games with MIDP2 by Carol Hamer. (J2ME is now renamed to Java ME by the way.) Have a look at the amazon.com reviews, and I think you will totally agree with the comments stating that Java ME games are one of the last areas where you still can write a complete game on your own without a team of designers and storyboard specialists.
Some words about the book: It assumes you already know Java and just shows the differences and mentions best practices. The content itself is solid and useful, even if it's often annoying to read dozens of pages of source code with only a few jotted-down comments between the lines.
Speaking of which, you can download the source code and the sample projects can be imported into NetBeans IDE with the Mobility Pack installed. When you create a new project, select Mobile > Import Wireless Toolkit Project, and there you are. The first game (Maze) works right away, the tumbleweed game however has a fixed screensize and does not run on the emulator because the emulator's screen is too big... Go figure. I can't just comment out the screen size test to make it run, and I have not yet found out if I can change the emulators screensize (anybody know?), and I don't want to rewrite the whole thing to make one little sample file fit. I'd rather spend the time on writing my own game.
So on Sunday I just took the maze game as a skeleton and started playing with the
javax.microedition.lcdui libraries. One of the big differences between Java SE and ME is: On Mobiles, you don't use swing libraries. The alternative, LCDUI, is very limited, but well, it's not called microedition for nothing. When you use New > Visual MIDlet to create a MIDlet, you'll get to see the Mobility Pack's famous visual designer. If you drop a PNG image file into the the same directory with the MIDlet, you can select and display it, e.g. on a splashscreen. There's still a lot to find out for me.
My game-to-be can already draw some pretty fancy lines and colored rectangles to their correct positions on the screen, and I already have a class that contains the randomized game map in an int array. (I needed to use some crazy
g.drawString(Integer.toString(myMaze.getTileFromMaze(i,j)),20+i\*17,30+j\*17,g.TOP|g.LEFT); line just to print the array to the screen and test whether it was correctly generated. Is there no easier way to do System.out.println()? Need to investigate.) Map generation probably still needs some optimizing (it runs in zero time on the emulator. On a 1.6 Ghz CPU... But how many Ghz does my mobile phone have? You see what I mean.) I also found out that there is no filled polygon routine in
javax.microedition.lcdui.Graphics, but you can replace that by drawing lines, and filling the space in between with several fillTriangle()s.
Now nothing can stop me, Muahahahaha! 8-) The greatest mobile card game ever! Including first-person 3D racing! And space aliens! And dinosaurs!! All at the same time! Or maybe not. ;-) Let's see what I come up with. :-P