| Why Program Games in Java? |
- Active online community
- API's available for any purpose:
- Ability to reach people through web browser Applets
Don't think that's important? Consider
this: Runescape, an online
MMORPG, runs in an Applet and has a
larger market share than
the following games:
| Starting a Game: Tips and Tricks |
| int width
= 200, height = 100; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
This image, prior to Java 5, will not be accelerated
|
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice().getDefaultConfiguration(); BufferedImage image = gc.createCompatibleImage(width, height,Transparency.TRANSLUCENT); |
Use a timing method to keep the game running smooth, ie GAGETimer
Example of a run loop:
| //these
variables will be used to help keep the framerate constant AdvancedTimer at = new AdvancedTimer(); long tick = 0; at.start(); long ticksPerFrame = AdvancedTimer.getTicksPerSecond()/game.getFPS(); //these variables will be used to keep an average value of the frames //per second of this game long lastTime = System.currentTimeMillis(); int paints = 0;
while (game.isRunning()) { |
| boolean
keysPressed = new boolean[256]; public void keyPressed(KeyEvent e) { keysPressed[e.getKeyCode()] = true; } public void keyReleased(KeyEvent e) { keysPressed[e.getKeyCode()] = false; } |
In your run loop:
| private
void checkKeys() { //horizontal motion if (keysPressed[KeyEvent.VK_LEFT]) player.setDx(-Ship.VELOCITY); else if (keysPressed[KeyEvent.VK_RIGHT]) player.setDx(Ship.VELOCITY); else player.setDx(0); //vertical motion if (keysPressed[KeyEvent.VK_UP]) player.setDy(-Ship.VELOCITY); else if (keysPressed[KeyEvent.VK_DOWN]) player.setDy(Ship.VELOCITY); else player.setDy(0); if (keysPressed[KeyEvent.VK_SPACE]) if (player.canFire()) playerShots.add(player.fire()); } |
- Specified in GameFrameLoader and GameAppletLoader
| Games written in Java: |