Browse Source

main class fixes

added image files
fixed class constructors
pull/1/head
jrtechs 8 years ago
parent
commit
ac60cfcddc
4 changed files with 76 additions and 22 deletions
  1. +2
    -1
      .gitignore
  2. +74
    -21
      src/tanks/Tanks.java
  3. BIN
      src/tanks/bullet.png
  4. BIN
      src/tanks/player.png

+ 2
- 1
.gitignore View File

@ -1 +1,2 @@
/nbproject/private/
/nbproject/private/
/build/

+ 74
- 21
src/tanks/Tanks.java View File

@ -7,6 +7,7 @@
package tanks; package tanks;
import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
@ -36,11 +37,14 @@ public class Tanks
//constructor //constructor
public Tanks() public Tanks()
{ {
frame=new JFrame("Tanks project"); frame=new JFrame("Tanks project");
frame.setSize(500,500); frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newGame();
key=new KeyListener() key=new KeyListener()
{ {
@Override @Override
@ -65,7 +69,7 @@ public class Tanks
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);
@ -89,14 +93,29 @@ public class Tanks
public void actionPerformed(ActionEvent e) public void actionPerformed(ActionEvent e)
{ {
p.move(); p.move();
for(Bullet b: bullets)
for(int i = 0; i < bullets.size(); i ++)
{ {
b.move();
try
{
bullets.get(i).move();
}
catch(Exception ex)
{
}
} }
for(Enemy en: enemy)
for(int i = 0; i < enemy.size(); i++)
{ {
en.move();
try
{
enemy.get(i).move();
}
catch(Exception ex)
{
}
} }
panel.repaint(); panel.repaint();
} }
@ -106,6 +125,18 @@ public class Tanks
move.start(); move.start();
} }
void newGame()
{
bullets = new ArrayList<Bullet>();
enemy = new ArrayList<Enemy>();
p = new Player();
wave = new Wave();
}
public static void main(String[] arguments)
{
Tanks game = new Tanks();
}
/* /*
@ -121,8 +152,20 @@ public class Tanks
*/ */
private class Player extends Living private class Player extends Living
{ {
Turret t;
boolean up,down,left,right;
private Turret t;
private boolean up,down,left,right;
public Player()
{
speed = 5;
x = frame.getWidth()/2;
y = frame.getHeight()/2;
health = 100;
this.imageLocation = "player.png";
super.loadImage();
width = 50;
height = 50;
}
void move() void move()
{ {
@ -130,17 +173,17 @@ public class Tanks
{ {
super.move(-1); super.move(-1);
} }
else if(down==true)
if(down==true)
{ {
super.move(1); super.move(1);
} }
else if(left==true)
if(left==true)
{ {
super.direction +=5;
super.direction -=5;
} }
else if(right==true)
if(right==true)
{ {
super.direction-=5;
super.direction+=5;
} }
} }
void updateDir(KeyEvent e, boolean pressed) void updateDir(KeyEvent e, boolean pressed)
@ -149,22 +192,18 @@ public class Tanks
if(id== KeyEvent.VK_UP) if(id== KeyEvent.VK_UP)
{ {
up=pressed; up=pressed;
move();
} }
else if(id==KeyEvent.VK_DOWN) else if(id==KeyEvent.VK_DOWN)
{ {
down=pressed; down=pressed;
move();
} }
else if(id==KeyEvent.VK_LEFT) else if(id==KeyEvent.VK_LEFT)
{ {
left=pressed; left=pressed;
move();
} }
else if(id==KeyEvent.VK_RIGHT) else if(id==KeyEvent.VK_RIGHT)
{ {
right=pressed; right=pressed;
move();
} }
else if(id==KeyEvent.VK_SPACE) else if(id==KeyEvent.VK_SPACE)
{ {
@ -199,6 +238,9 @@ public class Tanks
height = 30; height = 30;
health = 10; health = 10;
isAlive=true; isAlive=true;
speed = 3;
imageLocation = "player.png";
super.loadImage();
} }
//uses super to move player if collision then removes zombie and player //uses super to move player if collision then removes zombie and player
//takes damage //takes damage
@ -235,6 +277,8 @@ public class Tanks
y = e.y; y = e.y;
direction = e.direction; direction = e.direction;
speed = 10; speed = 10;
imageLocation = "bullet.png";
super.loadImage();
} }
//Moving the bullet //Moving the bullet
@ -243,11 +287,11 @@ public class Tanks
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 > frame.getHeight())
if(x < 0 || x > frame.getWidth())
{ {
bullets.remove(this); bullets.remove(this);
} }
if (y < 0 || y > frame.getWidth())
if (y < 0 || y > frame.getHeight())
{ {
bullets.remove(this); bullets.remove(this);
} }
@ -257,11 +301,15 @@ public class Tanks
{ {
if(enemyBullet == false) if(enemyBullet == false)
{ {
boolean collided = this.checkCollision(bullets.get(i));
boolean collided = this.checkCollision(enemy.get(i));
if(collided) if(collided)
{ {
enemy.get(i).takeDamage(); enemy.get(i).takeDamage();
bullets.remove(i);
bullets.remove(this);
if(!enemy.get(i).isAlive)
{
enemy.remove(enemy.get(i));
}
} }
} }
} }
@ -333,6 +381,11 @@ public class Tanks
spawn.start(); spawn.start();
} }
public void draw(Graphics g)
{
g.setColor(Color.yellow);
g.fillRect(0, 0, 500, 500);
}
//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

BIN
src/tanks/bullet.png View File

Before After
Width: 25  |  Height: 25  |  Size: 1.4 KiB

BIN
src/tanks/player.png View File

Before After
Width: 50  |  Height: 50  |  Size: 3.2 KiB

Loading…
Cancel
Save