Make your own free website on Tripod.com

Java Gaming for the desktop.

SpaceOddysy BIT, Mesra Middle-Earth SCJP Java Gaming Blog My Personal Blog

 !   Space Oddysy: A java shoot-em-up game.

Space Oddysy: Chapter 6

With everything finally done now we need to code the game engine. This class is by necessity larger than usual, so, bear with me for making this chapter larger.

The game logic is coded in the Space class.
public class Space implements Serializable{
The important variables are:
.............
int chanceRogueComes=20;//determines how often a new enemy is spawned.lower value means heigher chance
int chanceRogueFires=100;//determines how often a enemy ships will fire.lower value means heigher chance
int chancePowerUpGenerated=400;//determines how often a powerup is generated.lower value means heigher chance
int chanceSpecialShipComes=30;
List levelData;//this list contains integers determining chances of spawning of different type of enemies
List levelPowerUpData;//List containing which power up be generated in which stage
final int lives=10;//lives given to hero
final int stageSize=20;//kill this many enemies to advance to next stage
int sumOfAllEnemies; //it is sum of enemies of all levels kept for optimisation
int sumOfAllPowerUps;//small optimisation
boolean bossSpawnedInThisStage=false;

......................
int stage;//current stage
int heroDeaths=0;//ships i have lost
int enemiesKilled=0;//enemies i have killed
HeroShip hero=new HeroShip(UW.Rand(0, UW.xSize),UW.ySize-20,UW.xSpeed,UW.ySpeed,100,100,100,true,true);
HeroData heroData=new HeroData();

.....................................
These are bunch of variables conataining data which influences the way the game progresses. Next, we define a list containing the various ships and the weapons.
.......................
List rogues=java.util.Collections.synchronizedList(new LinkedList());//list of enemy ships
List myWeapons=java.util.Collections.synchronizedList( new LinkedList());//list of my weapons
List enemyWeapons=java.util.Collections.synchronizedList(new LinkedList());//list of enemy weapons
List bossShip=java.util.Collections.synchronizedList(new LinkedList());//list of special enemyships
List powerUps=java.util.Collections.synchronizedList(new LinkedList());//powerUps
List friends=java.util.Collections.synchronizedList(new LinkedList());//reinforcements i have called
List specialShips=java.util.Collections.synchronizedList(new LinkedList());//special ships i have called
Next we define some JLabels to display the current game condition.
JLabel shipsLeftL=new JLabel("Ships left: "+(lives-heroDeaths));
JLabel moneyL=new JLabel("Money: "+heroData.money);
JLabel bulletsLeftL=new JLabel("Bullets: "+hero.bullets);
JLabel minesLeftL=new JLabel("Mines: "+hero.mines);
JLabel friendsL=new JLabel("Reinforcements: "+hero.friends);
Finally, we keep an array to get the input from the user.
private MouseListener[] arr;
private MouseMotionListener[] arr1;
private MouseWheelListener[] arr2;
private ActionMap am;
boolean splashScreenMode=false;

The next step is to allow the ships and the bullets move., to periodically allow the game to send the current game state to the screen class to be drawn, to check if game is over and to check if the player cleared a stage.
Timer movement=new Timer(23,new ActionListener() {//move every ship and let enemy ships attack
Timer special=new Timer(80,new ActionListener() {
Timer paint=new Timer(80,new ActionListener() {//repaint the screen
Timer stageUpp=new Timer(100,new ActionListener() {//check if stage is complete

Now we initialise the variables and start the timers.
public Space(){//initilise everything
stage=1;
levelData=new ArrayList();
levelPowerUpData=new ArrayList();
setStage(stage);
addKeyBindings();
addMouse();
rogues.add(addRogues2());
rogues.add(addRogues2());
rogues.add(addRogues2());
s.artifacts.addAll(rogues);
s.artifacts.addAll(bossShip);
s.artifacts.add(hero);
movement.start();
paint.start();
stageUpp.start();
checkGameOver.start();
sumOfAllEnemies=getIntFromList(levelData.size());
sumOfAllPowerUps=getIntFromPowerUpList(levelPowerUpData.size());
Sounds.backgroundMusic();
initSplashScreen(UW.createImageIcon("../images/initial-Splash1.gif","splashScreen"));
}

After that we create utility methods to create random enemies, power ups etc depending on stage. We write methods to check collision between ships, weapons etc. These are called by their respective timers. Next we write the methods to get input from the user and to allow the game to change to shop mode and splashscreen mode. Lastly in the main metod we get a JFrame and put our game inside it. Ho! Our game is done.

Get the downdoads.
Back to index.
Java Gaming blog.