Browse Source

Had to do some format changes becacuse the ammount of empty space drove me crazy. Also changed useless code and fixed a bug where you could shoot when the game hadnt started yet.

pull/5/head
Michael 6 years ago
parent
commit
64d4e83a38
1 changed files with 72 additions and 175 deletions
  1. +72
    -175
      src/tanks/Tanks.java

+ 72
- 175
src/tanks/Tanks.java View File

@ -48,8 +48,7 @@ public class Tanks
newGame(); newGame();
key=new KeyListener()
{
key=new KeyListener() {
@Override @Override
public void keyTyped(KeyEvent e) public void keyTyped(KeyEvent e)
{ {
@ -88,17 +87,14 @@ public class Tanks
panel = new JPanel() panel = new JPanel()
{ {
protected void paintComponent(Graphics g)
{
protected void paintComponent(Graphics g) {
//Paint Wave //Paint Wave
wave.draw(g); wave.draw(g);
for(Bullet b: bullets)
{
for(Bullet b: bullets) {
b.draw(g); b.draw(g);
} }
for(Enemy en: enemy)
{
for(Enemy en: enemy) {
en.draw(g); en.draw(g);
} }
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
@ -112,29 +108,25 @@ public class Tanks
g.drawString("Wave: " + wave.waveNum + " Ammo: "+ p.ammo + " Kills: " + wave.kills +" Health: " + p.health + " Time: " + wave.timeCount, 50, fheight + 50); g.drawString("Wave: " + wave.waveNum + " Ammo: "+ p.ammo + " Kills: " + wave.kills +" Health: " + p.health + " Time: " + wave.timeCount, 50, fheight + 50);
//Pausing the game //Pausing the game
if(wave.gameMode == 3)
{
if(wave.gameMode == 3) {
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.setFont(new Font("Arial" , 1, 40)); g.setFont(new Font("Arial" , 1, 40));
g.drawString("PAUSED - PRESS ESC TO CONTINUE" , 75, fheight/2); g.drawString("PAUSED - PRESS ESC TO CONTINUE" , 75, fheight/2);
} }
if(wave.gameMode == 2)
{
if(wave.gameMode == 2) {
//On screen
g.setColor(Color.RED); g.setColor(Color.RED);
g.setFont(new Font("Arial" , 1, 40)); g.setFont(new Font("Arial" , 1, 40));
g.drawString("You Died" , fwidth/2 - 100, fheight/2); g.drawString("You Died" , fwidth/2 - 100, fheight/2);
//In black bar
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.setFont(new Font("Arial" , 1, 25)); g.setFont(new Font("Arial" , 1, 25));
g.drawString("Press enter to play", 50, fheight + 100); g.drawString("Press enter to play", 50, fheight + 100);
} }
else if(wave.gameMode ==0)
{
else if(wave.gameMode ==0) {
g.drawString("Press enter to play", 50, fheight + 100); g.drawString("Press enter to play", 50, fheight + 100);
} }
} }
}; };
frame.add(panel); frame.add(panel);
@ -146,29 +138,12 @@ public class Tanks
if(wave.gameMode == 1) if(wave.gameMode == 1)
{ {
p.move(); p.move();
for(int i = 0; i < bullets.size(); i ++)
{
try
{
bullets.get(i).move();
}
catch(Exception ex)
{
}
for(int i = 0; i < bullets.size(); i ++) {
bullets.get(i).move();
} }
for(int i = 0; i < enemy.size(); i++)
{
try
{
enemy.get(i).move();
}
catch(Exception ex)
{
}
for(int i = 0; i < enemy.size(); i++) {
enemy.get(i).move();
} }
} }
wave.waveCheck(); wave.waveCheck();
@ -181,24 +156,17 @@ public class Tanks
void newGame() void newGame()
{ {
bullets = new ArrayList<Bullet>();
enemy = new ArrayList<Enemy>();
bullets = new ArrayList<>();
enemy = new ArrayList<>();
p = new Player(); p = new Player();
wave = new Wave(); wave = new Wave();
} }
public static void main(String[] arguments)
{
Tanks game = new Tanks();
}
/* /*
Sub-classes that require the fields in tanks Sub-classes that require the fields in tanks
*/ */
/** /**
* the player class which extends the living class * the player class which extends the living class
* a player has a turret and an int kills * a player has a turret and an int kills
@ -294,55 +262,37 @@ public class Tanks
t.y = y; t.y = y;
} }
void updateDir(KeyEvent e, boolean pressed)
{
void updateDir(KeyEvent e, boolean pressed) {
int id=e.getKeyCode(); int id=e.getKeyCode();
if(id== KeyEvent.VK_UP)
{
if(id== KeyEvent.VK_UP) {
up=pressed; up=pressed;
} }
else if(id==KeyEvent.VK_DOWN)
{
else if(id==KeyEvent.VK_DOWN) {
down=pressed; down=pressed;
} }
else if(id==KeyEvent.VK_LEFT)
{
else if(id==KeyEvent.VK_LEFT) {
left=pressed; left=pressed;
} }
else if(id==KeyEvent.VK_RIGHT)
{
else if(id==KeyEvent.VK_RIGHT) {
right=pressed; right=pressed;
} }
else if(id==KeyEvent.VK_A)
{
else if(id==KeyEvent.VK_A) {
rleft = pressed; rleft = pressed;
} }
else if(id==KeyEvent.VK_D)
{
else if(id==KeyEvent.VK_D) {
rRight = pressed; rRight = pressed;
} }
else if(id==KeyEvent.VK_R && wave.gameMode ==1)
{
else if(id==KeyEvent.VK_R && wave.gameMode ==1) {
ammo=10; ammo=10;
} }
else if(id==KeyEvent.VK_SPACE)
{
if(pressed)
{
if (shoot)
{
else if(id==KeyEvent.VK_SPACE && wave.gameMode !=0) {
if(pressed) {
if (shoot) {
shoot(); shoot();
shoot=false; shoot=false;
} }
else
{
}
} }
} }
} }
void shoot() void shoot()
{ {
@ -418,32 +368,25 @@ public class Tanks
} }
//Moving the bullet //Moving the bullet
public void move()
{
public void move() {
super.move(-1); super.move(-1);
//Checks if the bullet goes off screen, if so... it get removed //Checks if the bullet goes off screen, if so... it get removed
if(x < 0 || x > fwidth)
{
if(x < 0 || x > fwidth) {
bullets.remove(this); bullets.remove(this);
} }
if (y < 0 || y > fheight)
{
if (y < 0 || y > fheight) {
bullets.remove(this); bullets.remove(this);
} }
//Checks for collision with enemies, enemy takes damage and byllet is removed //Checks for collision with enemies, enemy takes damage and byllet is removed
for(int i = 0; i < enemy.size(); i++)
{
if(enemyBullet == false)
{
for(int i = 0; i < enemy.size(); i++) {
if(enemyBullet == false) {
boolean collided = this.checkCollision(enemy.get(i)); boolean collided = this.checkCollision(enemy.get(i));
if(collided)
{
if(collided) {
enemy.get(i).takeDamage(); enemy.get(i).takeDamage();
bullets.remove(this); bullets.remove(this);
if(!enemy.get(i).isAlive)
{
if(!enemy.get(i).isAlive) {
enemy.remove(enemy.get(i)); enemy.remove(enemy.get(i));
wave.kills++; wave.kills++;
} }
@ -455,12 +398,10 @@ public class Tanks
if(enemyBullet) if(enemyBullet)
{ {
boolean collided = this.checkCollision(p); boolean collided = this.checkCollision(p);
if(collided)
{
if(collided) {
p.takeDamage(); p.takeDamage();
bullets.remove(this); bullets.remove(this);
if(!p.isAlive)
{
if(!p.isAlive) {
wave.setGameMode(2); wave.setGameMode(2);
} }
} }
@ -476,10 +417,8 @@ public class Tanks
* turn left, 1 will cause * turn left, 1 will cause
* it to turn right * it to turn right
*/ */
private class Turret extends RotationalElement
{
public Turret(RotationalElement e)
{
private class Turret extends RotationalElement {
public Turret(RotationalElement e) {
width = 50; width = 50;
height = 50; height = 50;
x = e.x; x = e.x;
@ -489,22 +428,16 @@ public class Tanks
imageLocation = "turret.png"; imageLocation = "turret.png";
super.loadImage(); super.loadImage();
} }
public void shoot()
{
//bullets.add(new Bullet (this));
}
public void rotate(int e)
{
public void rotate(int e) {
super.direction = super.direction + 5*e; super.direction = super.direction + 5*e;
} }
} }
/* /*
A version of the emeny tank that arrives at some point
*/ */
private class Tank extends Enemy private class Tank extends Enemy
{ {
Bullet b; Bullet b;
Turret t; Turret t;
Timer tim; Timer tim;
@ -526,48 +459,32 @@ public class Tanks
t.imageLocation = "enemyTurret.png"; t.imageLocation = "enemyTurret.png";
t.loadImage(); t.loadImage();
al = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(getAlive())
{
b = new Bullet(t);
b.enemyBullet=true;
bullets.add(b);
}
}
al = e -> {
if(getAlive()) {
b = new Bullet(t);
b.enemyBullet=true;
bullets.add(b);
}
}; };
tim = new Timer(3500,al); tim = new Timer(3500,al);
tim.start(); tim.start();
} }
public void draw (Graphics g)
{
public void draw (Graphics g) {
super.draw(g); super.draw(g);
t.draw(g); t.draw(g);
} }
public void move()
{
if(this.distToPlayer(p)>250)
{
public void move() {
if(this.distToPlayer(p)>250) {
direction = angleToPlayer(p); direction = angleToPlayer(p);
super.move(-1); super.move(-1);
if(this.checkCollision(p))
{
if(this.checkCollision(p)) {
p.takeDamage(); p.takeDamage();
this.takeDamage(); this.takeDamage();
if(isAlive==false)
{
if(isAlive==false) {
enemy.remove(this); enemy.remove(this);
} }
if(!p.isAlive)
{
if(!p.isAlive) {
wave.setGameMode(2); wave.setGameMode(2);
} }
} }
@ -579,7 +496,7 @@ public class Tanks
} }
private class Wave extends DrawableElement
private class Wave extends DrawableElement
{ {
/**fields time is continous while playing, gameMode(1=playing, /**fields time is continous while playing, gameMode(1=playing,
* 2=dead, 3=paused. spawntime keeps a countdown until next spawn, * 2=dead, 3=paused. spawntime keeps a countdown until next spawn,
@ -603,31 +520,19 @@ public class Tanks
super.loadImage(); super.loadImage();
//actionlistener calls spawn every 2 seconds //actionlistener calls spawn every 2 seconds
ActionListener z = new ActionListener()
{
ActionListener z = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e)
{
if(gameMode == 1)
{
public void actionPerformed(ActionEvent e) {
if(gameMode == 1) {
spawnZombie(); spawnZombie();
} }
} }
}; };
ActionListener ta = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(gameMode ==1)
spawnTank();
}
ActionListener ta = e -> {
if(gameMode ==1)
spawnTank();
}; };
//action listener increments time //action listener increments time
@ -636,33 +541,26 @@ public class Tanks
spawnZombie.start(); spawnZombie.start();
ActionListener t = new ActionListener() ActionListener t = new ActionListener()
{ {
@Override @Override
public void actionPerformed(ActionEvent e)
{
if(gameMode==1)
{
public void actionPerformed(ActionEvent e) {
if(gameMode==1) {
timeCount++; timeCount++;
} }
} }
}; };
time = new Timer(1000,t); time = new Timer(1000,t);
time.start(); time.start();
} }
public void setGameMode(int newGameMode)
{
public void setGameMode(int newGameMode) {
gameMode = newGameMode; gameMode = newGameMode;
if(gameMode == 2)
{
if(gameMode == 2) {
//Player has died, ending the game //Player has died, ending the game
time.stop(); time.stop();
//move.stop(); //move.stop();
spawnZombie.stop(); spawnZombie.stop();
spawnTank.stop(); spawnTank.stop();
} }
else if(gameMode == 4)
{
else if(gameMode == 4) {
//Player has won //Player has won
time.stop(); time.stop();
//move.stop(); //move.stop();
@ -670,17 +568,13 @@ public class Tanks
spawnTank.stop(); spawnTank.stop();
} }
} }
//spawn method checks if spawn timer ==0 and if so then spawns an //spawn method checks if spawn timer ==0 and if so then spawns an
//enemy //enemy
public void spawnTank()
{
public void spawnTank() {
enemy.add(new Tank()); enemy.add(new Tank());
spawnedTanks++; spawnedTanks++;
} }
public void spawnZombie()
{
public void spawnZombie() {
enemy.add(new Zombie()); enemy.add(new Zombie());
spawnedZombies++; spawnedZombies++;
} }
@ -740,7 +634,6 @@ public class Tanks
if(kills == 60) if(kills == 60)
{ {
//Game over //Game over
} }
break; break;
} }
@ -754,5 +647,9 @@ public class Tanks
spawnTank.setDelay(4000); spawnTank.setDelay(4000);
} }
} }
//Main method
public static void main(String[] arguments) {
Tanks game = new Tanks();
}
} }

Loading…
Cancel
Save