Ok so, I am making a tower defense game (watched a tutorial on youtube to get to this point and now I’m completely stumped)
The towers, lives, minions are all working, but I don’t know how I can make the towers “attack” the minions and make the minions lose hp, does anyone know how to do this? here’s some of my code so far
this is for importing the images
//Importing Images Class
package LeagueTD;
import java.awt.*;
import javax.swing.*;
public class ac extends JPanel {
public Image back;
public Image[] ene = new Image[200];//monster
public Image[] style = new Image[200];
public Image[] tower = new Image[200];
public Image defeat;
public boolean imagesLoaded=false;
public ac(){
this.setBackground(Color.BLACK);
getImages();
}
public void getImages(){
back=new ImageIcon("back.png").getImage();
ene[0]=new ImageIcon("pinkminion.png").getImage();
style[0]=new ImageIcon("health.png").getImage();
style[1]=new ImageIcon("gold.png").getImage();
style[2]=new ImageIcon("fade.png").getImage();
tower[0]=new ImageIcon("ashe.png").getImage();
defeat=new ImageIcon("defeat.png").getImage();
imagesLoaded=true;
}
public void paintComponent(Graphics g){
if (imagesLoaded){
g.drawImage(back, 0, 0, aa.width-6, aa.height-28, null);
}
}
}
and this is for the game itself
package LeagueTD;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.*;
public class ad extends JPanel {
public int spawnRate=200;
public int maximumAmountOfMobs=200;
public int[] mobKind;
public int mobMovementSpeed=10;
public int mobPinkMinion=0;
public int mobSize=40;
public int health=30;
public int money=500;
public int shopSize=8;
public int sizeBetween=10;
public int fade=shopSize+1;
public int heldTower=0;
public int towerID[];
public int air=99;
public Rectangle[] mobs;
public Rectangle[] tower;
public Rectangle[] shop = new Rectangle[shopSize];
public Rectangle end;
//public Line2D shot;
public Point mse;
public Timer spawn;
public Timer mobMovement;
public boolean gameOver = false;
public boolean inGame = true;
public boolean holdingTower = false;
ClassLoader ldr=this.getClass().getClassLoader();
java.applet.AudioClip audio=JApplet.newAudioClip(ldr.getResource("Explosion.wav"));
public void defineObjects(){
mobKind=new int[maximumAmountOfMobs];
mobs=new Rectangle[maximumAmountOfMobs];
for (int i=0;i<mobs.length;i++){
mobs[i]=new Rectangle(-5000, -5000, 0, 0);
mobKind[i]=0;
}
for(int i=0;i<shop.length;i++){
shop[i] = new Rectangle((130)+i*72+(sizeBetween*i),12,60,80);
}
end=new Rectangle(680, aa.height/2, 100,100);
tower = new Rectangle[200];
towerID = new int[200];
for(int i=0;i<tower.length;i++){
tower[i] = new Rectangle(-5000, -5000, 0, 0);
towerID[i]=air;
}
}
public ad(ab f,ac bp){
defineObjects();
f.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getButton()==MouseEvent.BUTTON1){
if(!holdingTower){
if(shop[0].contains(mse)){
heldTower=mobPinkMinion;
holdingTower=true;
}
}
else{
for(int i=0;i<tower.length;i++){
if(towerID[i]==air){
tower[i]=new Rectangle(mse.x,mse.y, 90,90);
towerID[i]=heldTower;
i+=tower.length+1;
}
}
}
}
}
});
//Fade
f.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved (MouseEvent e){
mse=new Point(e.getX(), e.getY()-23);
boolean nope =true;
for(int i=0;i<shop.length;i++){
if (shop[i].contains(mse)){
fade=i;
nope=false;
}
}
if (nope){
fade=shopSize+1;
}
repaint();
}
});
ActionListener spawnListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
if (inGame=true){
for(int i=0;i<maximumAmountOfMobs;i++){
if (mobs[i].x <=0){
mobs[i]=new Rectangle(aa.width-750,aa.height/2-mobSize/2+55,mobSize,mobSize);
i += maximumAmountOfMobs+1;
}
}
}
repaint();
}
};
spawn=new Timer(spawnRate, spawnListener);
spawn.start();
ActionListener mobMovementListener = new ActionListener(){
public void actionPerformed(ActionEvent e){
if(inGame){
for(int i=0;i<maximumAmountOfMobs;i++){
if (end.contains(mobs[i])){
health-=1;
mobs[i]=new Rectangle(-5000, -5000, 0 ,0);
//audio.play();
checkforGameOver();
if(gameOver){
//audio.stop();
}
}
else{
mobs[i].x += 1;
}
}
}
repaint();
}
};
mobMovement=new Timer(mobMovementSpeed, mobMovementListener);
mobMovement.start();
}
public void checkforGameOver(){
if (health==0){
gameOver=true;
inGame=false;
}
}
public void paintComponent (Graphics g){
super.paintComponent(g);
if (aa.f.bp.imagesLoaded){
for (int i=0;i<mobs.length;i++){
g.drawImage(aa.f.bp.ene[mobKind[i]], mobs[i].x,mobs[i].y,mobs[i].width,mobs[i].height,null);
}
}
//Panel
g.setColor(Color.DARK_GRAY);
g.fill3DRect(0,0,aa.width,105,true);
//Health
g.setFont(new Font("Arial",Font.BOLD,20));
g.setColor(Color.WHITE);
g.drawImage(aa.f.bp.style[0],15,15,40,40,null);
g.drawString(health + "",66,42);
//Money
g.drawImage(aa.f.bp.style[1],15+2,15+48,35,35,null);
g.drawString(money + "",66,42+43);
//Towers
for(int i=0;i<tower.length;i++){
if(towerID[i] != air){
g.drawImage(aa.f.bp.tower[towerID[i]], tower[i].x-45, tower[i].y-40,tower[i].width, tower[i].height, null);
}
}
//Hand Held
if(holdingTower){
g.drawImage(aa.f.bp.tower[heldTower],mse.x-45,mse.y-40,90,90,null);
}
//Panel Component
for (int i =0; i< shop.length;i++){
g.setColor(Color.GRAY);
g.fillRect(shop[i].x,shop[i].y,shop[i].width,shop[i].height);
g.setColor(Color.WHITE);
g.drawRect(shop[i].x-2,shop[i].y-2,shop[i].width+2,shop[i].height+2);
if(i==fade){
g.drawImage(aa.f.bp.style[2],shop[i].x,shop[i].y,shop[i].width,shop[i].height,null);
}
}
g.drawImage(aa.f.bp.tower[0],15+105,12,80,80,null);
if (gameOver){
//Defeat def = new Defeat();
g.drawImage(aa.f.bp.defeat,0,0,800,600,null);
}
}
}