You are on page 1of 13

/* * To change this template, choose Tools | Templates * and open the template in the editor.

*/ import import import import java.util.Random; javax.microedition.lcdui.game.GameCanvas; javax.microedition.lcdui.*; javax.microedition.midlet.*;

/** * @author Administrator */ public class BattleCity extends MIDlet implements CommandListener{ public Display display = Display.getDisplay(this); public Canvass newgame = new Canvass(); public List menu; public Form instruction,new_game; public String[] Items = {"Instruction","New Game"}; public StringItem instruct, newGame, rules; public Command exit, ok, back; public ItemStateListener item; public int select; public BattleCity(){ CreateList(); } public void CreateList(){ menu = new List("Battle City",List.IMPLICIT); for (int i = 0; i < Items.length; i++) { menu.append(Items[i], null); } menu.setCommandListener(this); exit = new Command("Exit",Command.EXIT,1); ok = new Command("Ok",Command.OK,1); } public void startApp() { menu.addCommand(exit); menu.addCommand(ok); menu.setCommandListener(this); display.setCurrent(menu); / change to menu } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable disp) { / ok if (command == exit){ destroyApp(true); notifyDestroyed(); } else if (command == ok){ /

} else if (command == back){ display.setCurrent(menu); / return to menu form } else if (disp == menu){ int index = menu.getSelectedIndex(); if (index == 0) display.setCurrent(GameInstruction()); else display.setCurrent(GameProper()); } } public Form GameInstruction(){ instruction = new Form("Instruction"); rules = new StringItem("To play, use 2 for up, 4 for left, 6 for right, 8 for bottom. To fire use fire button.",null); back = new Command("Back",Command.BACK,1); instruction.append(rules); instruction.addCommand(back); instruction.setCommandListener(this); return instruction; } public Canvas GameProper(){ display.setCurrent(newgame); return newgame; }

public class Canvass extends GameCanvas implements Runnable, CommandListener { private Graphics gameGraphics; private int score, x, y, l, gun_l, canvas_width, canvas_height, enemies, index, gun_x, gun_y, life, front, state,firex, firey, direct, down_count; private int enemies_posX[] = new int[20]; private int enemies_posY[] = new int[20]; private int fire_x[] = new int[20]; private int fire_y[] = new int[20]; private int fire_direct[] = new int[20]; private int Egun_x[] = new int[20]; private int Egun_y[] = new int[20]; private Random random; private String keyCode =""; private int face[] = new int[20]; private int time[] = new int[20]; private int type[] = new int[20]; private int time_Limit[] = new int[20]; private boolean generateTanks, complete; private boolean setVisible[] = new boolean[20]; private boolean fired; private Alert alert; private Thread thread; public Canvass(){ super( true ); score = 0; l = 15; front = 3; state = 0; down_count = 0;

enemies = 20; / max 20 life = 10; firex = firey = -5; fired = complete = generateTanks = false; gun_l = 7; canvas_width = 180; // 180 canvas_height = 190; // 190 x = (canvas_width/2)-5; y = (canvas_height/2)+3; gameGraphics = getGraphics(); initGame(); } public void run() {

/ / implements tank commands and graphic modification while( thread == Thread.currentThread() ){ flushGraphics(); try { Thread.currentThread().sleep( 20); } catch( InterruptedException e ){ } initGame(); / initialize only background and score and line if (generateTanks == false){ generateEnemy(); / generate enemy tanks positions gun_x = x+15; gun_y = y+4; generateTanks = true; } keyPressed2(); / pressing fire button displayMyTank(); / display my tank displayEnemyTank(); / displays all enemy tanks initMoveTanks(); if (life == 0){ endGame(); } } } /

/ / /

public void commandAction(Command c, Displayable d) { if (c == exit){ destroyApp(true); notifyDestroyed();

} } public void keyPressed2(){ state = getKeyStates(); if ( ( state & FIRE_PRESSED ) != 0 ){ / OK.... if (fired == false){ fired = true; } } } protected void keyPressed(int key){ keyCode = getKeyName(key); if ("2".equals(keyCode)){ / move north gun_x = x+4; gun_y = y-7; front = 1; } if ("4".equals(keyCode)){ / move left gun_x = x-7; gun_y = y+4; front = 2; } if ("6".equals(keyCode)){ / move right gun_x = x+15; gun_y = y+4; front = 3; } if ("8".equals(keyCode)){ / move south gun_x = x+4; gun_y = y+15; front = 4; } } protected void keyReleased(int key) { keyCode = getKeyName(key); if ("2".equals(keyCode)){ / move north if (front == 1){ if (gun_y >= 18){ y = y-3; gun_y = gun_y-3; } } } if ("4".equals(keyCode)){ / move left if (front == 2){ if (gun_x >= 0){ x = x-3; / / / / / / /

gun_x = gun_x-3; } } } if ("6".equals(keyCode)){ / move right if (front == 3){ if (gun_x <= canvas_width-8){ x = x+3; gun_x = gun_x+3; } } } if ("8".equals(keyCode)){ / move south if (front == 4){ if (gun_y <= canvas_height-8){ y = y+3; gun_y = gun_y+3; } } } } public void endGame(){ alert = new Alert( " Game Over", "Your score: "+String.valueOf(score) +".", null, AlertType.WARNING ); alert.setTimeout(Alert.FOREVER); exit = new Command("Exit", Command.EXIT, 1); alert.addCommand(exit); alert.setCommandListener(this); display.setCurrent(alert, newgame); } public void comleteGame(){ alert = new Alert( " Congratulations", "Your score: "+String.valueOf(score) +".", null, AlertType.WARNING ); alert.setTimeout(Alert.FOREVER); exit = new Command("Exit", Command.EXIT, 1); alert.addCommand(exit); alert.setCommandListener(this); display.setCurrent(alert, newgame); } public void initMoveTanks(){ for(int index = 0; index < enemies; index++){ this.index = index; / setting index if (setVisible[index] == true){ / / tanks not yet hit if ((time_Limit[index] == 0)&&(type[index] == 0)){ time_Limit[this.index] = (Math.abs(random.nextInt()) % 7 0);// limited time for a process completion / / /

type[this.index] = (Math.abs(random.nextInt()) % 4)+1; / / type of move to be taken } if (time[index] < time_Limit[index]){ / if the process is still incomplete if (type[index] == 1){ / move x moveX(); } if (type[index] == 2){ e y moveY(); } if (type[index] == 4){ e fire(this.face[index], index); } if (type[index] == 3){ n turn(this.face[index], index); } time[index] = time[index]+1; / time spent for a process to complete } else{ type[index] = time[index] = time_Limit[index] = 0; / restart time, time_Limit and type of task } firing(index); if (fired == true){ fires(); / initialize firing fired = false; } enemyHit(index); / / check for hit in enemy } else{ / / hit tanks enemies_posX[index] = enemies_posY[index] = -15; //gameGraphics.setColor(200, 200, 100); //gameGraphics.drawString("hit count: "+String.valueOf(down_ count), 30, 30, 0); if ((down_count == enemies)&&(fired == false)){ // display winning alert comleteGame(); fired = true; } } Firing(this.index); / / firing algorithm } / / // tur // fir / /

// mov

} public void enemyHit(int i){ if ((direct == 1)&&(firey <= enemies_posY[i]+15)&&(firey >= enemies_ posY[i])&&(firex >= enemies_posX[i]-3)&&(firex <= enemies_posX[i]+13)){ // to north setVisible[i] = false; firex = firey = -5; score = score+30; down_count = down_count+1; } else if ((direct == 2)&&(firex <= enemies_posX[i]+15)&&(firex >= ene mies_posX[i])&&(firey >= enemies_posY[i]-3)&&(firey <= enemies_posY[i]+13)){ / / to left setVisible[i] = false; firex = firey = -5; score = score+30; down_count = down_count+1; } else if ((direct == 3)&&(firex <= enemies_posX[i]+15)&&(firex >= ene mies_posX[i])&&(firey >= enemies_posY[i]-3)&&(firey <= enemies_posY[i]+13)){ / / to right setVisible[i] = false; firex = firey = -5; score = score+30; down_count = down_count+1; } else if ((direct == 4)&&(firey <= enemies_posY[i]+15)&&(firey >= ene mies_posY[i])&&(firex >= enemies_posX[i]-3)&&(firex <= enemies_posX[i]+13)){ / / to south setVisible[i] = false; firex = firey = -5; score = score+30; down_count = down_count+1; } } public void fires(){ if (front == 1){ / move north firex = gun_x+1; firey = gun_y+1; complete = false; direct = 1; } else if (front == 2){ / move left firex = gun_x+1; firey = gun_y+1; complete = false; direct = 2; } else if (front == 3){ / move right firex = gun_x+1; firey = gun_y+1; complete = false; direct = 3; /

} else if (front == 4){ / move south firex = gun_x+1; firey = gun_y+1; complete = false; direct = 4; } displayFire(); } public void Firing(int index){ if ((index == 1)||(index == 0)){ minimize firing speed if (direct == 1){ move north if ((firey > 18)&&(complete == false)){ firey = firey-1; } else if ((firey <= 18)&&(complete == false)){ firex = firey = -5; complete = true; } } else if (direct == 2){ move left if ((firex > 0)&&(complete == false)){ firex = firex-1; } else if ((firex <= 0)&&(complete == false)){ firex = firey = -5; complete = true; } } else if (direct == 3){ move right if ((firex < canvas_width)&&(complete == false)){ firex = firex+1; } else if ((firex >= canvas_width)&&(complete == false)){ firex = firey = -5; complete = true; } } else if (direct == 4){ move south if ((firey < canvas_height)&&(complete == false)){ firey = firey+1; } else if ((firey >= canvas_height)&&(complete == false)){ firex = firey = -5; complete = true; } } displayFire(); } } public void displayFire(){ gameGraphics.setColor(255, 255, 255);

/ /

/ /

gameGraphics.fillRoundRect(firex, firey, 5, 5, 5, 5); } public void moveX(){ if (time[index] < time_Limit[index]){ if ((Egun_x[index] >= 0)&&(Egun_x[index] <= canvas_width)){ if ((face[index] == 1)&&(Egun_x[index] > 0)&&(Egun_x[index] < canvas_width-7)){ // facing right, greater than 0 and less than 179 (limits on both sides) Egun_x[index] = Egun_x[index]+1; enemies_posX[index] = enemies_posX[index]+1; } else if ((face[index] == 3)&&(Egun_x[index] > 0)&&(Egun_x[in dex] < canvas_width-7)){ // facing left, greater than 0 and less than 179 (limit s on both sides) Egun_x[index] = Egun_x[index]-1; enemies_posX[index] = enemies_posX[index]-1; } } else { time[index] = time_Limit[index]; } } } public void moveY(){ if (time[index] < time_Limit[index]){ if ((Egun_y[index] >= 18)&&(Egun_y[index] >= canvas_height - 36) ){ if ((face[index] == 2)&&(Egun_y[index] > 18)&&(Egun_y[index] < canvas_height-7)){ // facing right, greater than 0 and less than 179 (limits on both sides) Egun_y[index] = Egun_y[index]+1; enemies_posY[index] = enemies_posY[index]+1; } else if ((face[index] == 0)&&(Egun_y[index] > 18)&&(Egun_y[i ndex] <canvas_height-7)){ // facing left, greater than 0 and less than 179 (limi ts on both sides) Egun_y[index] = Egun_y[index]-1; enemies_posY[index] = enemies_posY[index]-1; } } else { time[index] = time_Limit[index]; } } } public void turn(int face, int index){ int facing = (Math.abs(random.nextInt()) % 4); / decides where tank to face if (face == 0){ / facing north tankFace(facing, index); } else if (face == 1){ / facing the right tankFace(facing, index); } / /

else if (face == 2){ / facing south tankFace(facing, index); } else if (face == 3){ / facing left tankFace(facing, index); } time[index] = time_Limit[index]; } public void tankFace(int facing, int index){ if (facing == 0){ dectates facing north this.face[index] = facing; Egun_x[index] = enemies_posX[index]+4; Egun_y[index] = enemies_posY[index]-5; } else if (facing == 1){ dectates facing right this.face[index] = facing; Egun_x[index] = enemies_posX[index]+15; Egun_y[index] = enemies_posY[index]+4; } else if (facing == 2){ dectates facing south this.face[index] = facing; Egun_x[index] = enemies_posX[index]+4; Egun_y[index] = enemies_posY[index]+15; } else if (facing == 3){ dectates facing left this.face[index] = facing; Egun_x[index] = enemies_posX[index]-5; Egun_y[index] = enemies_posY[index]+4; } }

public void fire(int face, int index){ / firing code if (face == 0){ / facing north fire_x[index] = Egun_x[index]+1; fire_y[index] = Egun_y[index]-5; fire_direct[index] = face; } else if (face == 1){ / facing right fire_x[index] = Egun_x[index]+5; fire_y[index] = Egun_y[index]+1; fire_direct[index] = face; } else if (face == 2){ / facing south fire_x[index] = Egun_x[index]+1; fire_y[index] = Egun_y[index]+6; fire_direct[index] = face; } else if (face == 3){ / facing left

/ /

fire_x[index] = Egun_x[index]-1; fire_y[index] = Egun_y[index]+1; fire_direct[index] = face; } time[index] = time_Limit[index]; } public void firing(int index){ if (fire_direct[index] == 0){ / firing to north ok.... if (fire_y[index] > 18){ fire_y[index] = fire_y[index]-2; } else if(fire_y[index] <= 18){ fire_x[index] = fire_y[index] = -5; } /

if ((fire_y[index] <= y+15)&&(fire_y[index] > y)&&(fire_x[index] >= x-4)&&(fire_x[index] <= x+15)){ // if my tank was hit life = life-1; fire_x[index] = fire_y[index] = -5; } } else if (fire_direct[index] == 1){ / / firing to right if (fire_x[index] < canvas_width){ fire_x[index] = fire_x[index]+2; } if ((fire_x[index] >= x)&&(fire_x[index] <= x+15)&&(fire_y[index ] >= y-4)&&(fire_y[index] <= y+15)){ // if my tank was hit life = life-1; fire_x[index] = fire_y[index] = -5; } } else if (fire_direct[index] == 2){ / / firing to south if (fire_y[index] < canvas_height){ fire_y[index] = fire_y[index]+2; } if ((fire_y[index] >= y)&&(fire_y[index] <= y+15)&&(fire_x[index ] >= x-4)&&(fire_x[index] <= x+15)){ // if my tank was hit life = life-1; fire_x[index] = fire_y[index] = -5; } } else if (fire_direct[index] == 3){ / / firing to left if (fire_x[index] > 0){ fire_x[index] = fire_x[index]-2; } else if(fire_x[index] <= 0){ fire_x[index] = fire_y[index] = -5; } if ((fire_x[index] > x)&&(fire_x[index] <= x+15)&&(fire_y[index] >= y-4)&&(fire_y[index] <= y+15)){ // if my tank was hit life = life-1; fire_x[index] = fire_y[index] = -5;

} } displayEnemyFire(index); } public void displayEnemyFire(int index){ gameGraphics.setColor(255, 0, 0); gameGraphics.fillRoundRect(fire_x[index], fire_y[index], 5, 5, 5, 5) ; } public void initGame(){ gameGraphics.setColor(0, 0, 0); / black background gameGraphics.fillRect(0, 0, canvas_width, canvas_height); /

gameGraphics.setColor(0, 255, 0); / / prints score and line gameGraphics.drawString("Life: "+String.valueOf(life), canvas_width - canvas_width, 2, 0); gameGraphics.drawString("Score: "+String.valueOf(score), canvas_widt h/2, 2, 0); gameGraphics.drawLine(0, 17, canvas_width, 17); } public void displayMyTank(){ gameGraphics.setColor(0, 255, 0); gameGraphics.fillRect(x, y, l, l); gameGraphics.fillRect(gun_x, gun_y, gun_l, gun_l); } public void displayEnemyTank(){ for (int i = 0; i < enemies; i++){ tankFace(face[i],i); gameGraphics.setColor(255, 255, 0); gameGraphics.fillRect(enemies_posX[i], enemies_posY[i], l, l); gameGraphics.fillRect(Egun_x[i], Egun_y[i], gun_l, gun_l); } } public void generateEnemy(){ for(int index = 0; index < enemies; index++){ / / initialize positions for 5 tanks int pos_x = (Math.abs(random.nextInt()) % (canvas_width - 15)); int pos_y = (Math.abs(random.nextInt()) % (canvas_height - 36) + 18); / / avoid my tank image from bieng overriden by enemy OK.. .... if ((pos_x >= x-20)&&(pos_y >= y-20)&&(pos_x <= x+20)&&(pos_y <= y+20)){ enemies_posX[index] = (Math.abs(random.nextInt()) % x-20); enemies_posY[index] = (Math.abs(random.nextInt()) % y)+18; if (enemies_posY[index] >= y-15){ enemies_posY[index] = enemies_posY[index]-30; } }

else{ enemies_posX[index] = pos_x; enemies_posY[index] = pos_y; } setVisible[index] = true; face[index] = (Math.abs(random.nextInt()) % 3); / initialize tank facing } } protected void hideNotify(){ thread = null; } protected void showNotify(){ random = new Random(); thread = new Thread( this ); thread.start(); } } }

You might also like