Idris Kadri
Front & Back End Developer & Animator

2D Tower Defence

Github

Languages & Tools: Android Development, Game Development, Java, LibGDX

2D Android Mobile Tower defence game. Created with the LibGDX engine using Java.


Sample Code



	

this.game = game;
getLevel = level +1; //level +1;

expGain = true;
expPoints = getLevel*100;

bodyXPosition = new ArrayList();
deadPieces = 0;

firstPlayerLife = new BoxLife[game.playerStats.getPlayerBoxes()];
secondPlayerLife = new BoxLife[(getLevel / 3) + 1];

//create cam used to follow mario through cam world
gamecam = new OrthographicCamera();

//create a FitViewport to maintain virtual aspect ratio despite screen size
gamePort = new StretchViewport(BoxOut.V_WIDTH / BoxOut.PPM , BoxOut.V_HEIGHT / BoxOut.PPM, gamecam); // initializes the type of viewport and sets its width and height aswell as it to the camera

//set gamecam position
gamecam.position.set(gamePort.getWorldWidth()/2 , gamePort.getWorldHeight()/2,0);

//shake screen
screenShake = new ScreenShake();

//create our Box2D world, setting no gravity in X, -10 gravity in Y, and allow bodies to sleep
world = new World(new Vector2(0, -10), true);
//allows for debug lines of our box2d world.
b2dr = new Box2DDebugRenderer();

creator = new B2WorldCreator(this); //this refers to the this method/constructor PlayScreen

playerPawnsList = new ArrayList();
playerKnightsList = new ArrayList();
spawnPawnList = new ArrayList();
spriteLifeBarList = new ArrayList();

pawn = new Pawn[getLevel];
enemyKnight = new Knight[getLevel/2];

int displaceX = BoxOut.V_WIDTH ;
int displaceY = 200;
for (int i = 0; i < pawn.length; i++) {
pawn[i] = new Pawn(this, displaceX, displaceY, 2, "pawn.jpg", 1000, 2, 500);
displaceX-=10;
displaceY+=10;

spriteLifeBar = new SpriteLifeBar(game.batch, "pawn");
spriteLifeBarList.add(spriteLifeBar);
spriteLifeBarList.get(spriteLifeBarList.size()-1).healthBar(pawn[i].body.getPosition().x,pawn[i].body.getPosition().y,pawn[i].getHealth());
pawnCount++;
}

for (int i = 0; i < enemyKnight.length; i++) {
enemyKnight[i] = new Knight(this, displaceX, displaceY, 2, "button2.png", 3000, 2, 500);
displaceX-=10;
displaceY+=10;

spriteLifeBar = new SpriteLifeBar(game.batch, "lol");
spriteLifeBarList.add(spriteLifeBar);
spriteLifeBarList.get(spriteLifeBarList.size()-1).healthBar(enemyKnight[i].body.getPosition().x, enemyKnight[i].body.getPosition().y, enemyKnight[i].getHealth());
enemyKnightCount++;
}

testPlayerPawn = new Pawn(this, 0, 100, 1, "playerPawn.png", game.playerStats.getPawnHealth(), game.playerStats.getPawnSpeed(), game.playerStats.getPawnStrength());
spriteLifeBar = new SpriteLifeBar(game.batch, "testPawn");
spriteLifeBarList.add(spriteLifeBar);
spriteLifeBarList.get(spriteLifeBarList.size()-1).healthBar(testPlayerPawn.body.getPosition().x,testPlayerPawn.body.getPosition().y,testPlayerPawn.getHealth());
testPawnCount++;

//life boxes for both players
for(life = 0; life < firstPlayerLife.length; life++) {
firstPlayerLife[life] = new BoxLife(this,1,20,game.playerStats.getPlayerHealth());
}
for(life = 0; life < secondPlayerLife.length; life++) {
secondPlayerLife[life] = new BoxLife(this,2,(BoxOut.V_WIDTH -20),1000 + (getLevel*100));
}


int p1DropHeight = firstPlayerLife[0].boxHeight() + 50;
int p2DropHeight = secondPlayerLife[0].boxHeight() + 50;

//players
firstPlayer = new Player(this,firstPlayerLife[0].boxWidth(),p1DropHeight,1);
secondPlayer = new Player(this,secondPlayerLife[0].boxWidth(),p2DropHeight,2);

//create our game HUD for scores/timers/level info
hud = new Hud(game.batch);

controller = new Controller(game.batch);

world.setContactListener(new WorldContactListener());

winScreen = new WinScreen(game.batch);

multiplexer = new InputMultiplexer();
multiplexer.addProcessor(controller.stage);
multiplexer.addProcessor((winScreen.stage));

gamecam.zoom = 1.0f;

Back