Hi
I have a simple mini game who has a cannon, that cannon shot bullets (Rectangle) to enemies stored in a arrayList. I must code into Bullet class the collision method but i have no idea.
I have search examples but all of them has two rectangles…
This is my Bullet class
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
public class Balas implements Runnable
{
int x, y;
int balaX, balaY;
Rectangle balas, r1;
Boolean colisao= false;
private Jogo jogo;
private ArrayList<Alvos> bugs;
public Balas()
{
x= 305;
y= 355;
}
public void desenho(Graphics g)
{
//Bullet color
g.setColor(Color.pink);
//x and y position and height and width of bullet
g.fillRect(balas.x, balas.y, balas.width, balas.height);
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int getBalaX()
{
return balaX;
}
public int getBalaY()
{
return balaY;
}
@Override
public void run()
{
try
{
while(true)
{
//colisao();
Thread.sleep(5);
}
}
catch(Exception e){System.err.println(e.getMessage());
}
}
}
This is my cannon class
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class Canhao implements Runnable
{
int x, y, DirecaoX;
boolean disparar, tiro = false;
static Balas bl = new Balas();
ArrayList<Alvos> bugs;
ArrayList<Rectangle> bal = new ArrayList<>();
public Canhao()
{
x= 350;
y= 355;
}
public void desenho(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(x, y, 40, 10);
g.fillRect(x+18, y-7, 4, 7);
if(tiro)
{
bl.desenho(g);
}
}
public void movimento()
{
x += DirecaoX;
if(x<=0)
{
x=0;
}
if(x>=555)
{
x=555;
}
}
public void setDirecaoX(int xdir)
{
DirecaoX= xdir;
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_LEFT){
setDirecaoX(-1);
}
if(keyCode == KeyEvent.VK_RIGHT)
{
setDirecaoX(1);
}
if(keyCode == KeyEvent.VK_SPACE)
{
if(bl.balas==null)
{
disparar=true;
}
if(disparar)
{
bl.balaY= y-7;
bl.balaX= x+18;
bl.balas= new Rectangle(bl.balaX, bl.balaY, 15, 10);
tiro = true;
}
}
}
public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_LEFT)
{
setDirecaoX(0);
}
if(keyCode == KeyEvent.VK_RIGHT)
{
setDirecaoX(0);
}
if(keyCode == KeyEvent.VK_SPACE)
{
disparar=false;
if(bl.balas.y <=-5)
{
bl.balas=new Rectangle(0,0,0,0);
tiro = false;
disparar=true;
}
}
}
public void atirar()
{
if(tiro)
{
bl.balas.y--;
}
}
public void haColisao()
{
}
@Override
public void run()
{
Thread Balas = new Thread(bl);
Balas.start();
try
{
while(true)
{
atirar();
movimento();
//haColisao(bugs, bl);
Thread.sleep(5);
}
}
catch(Exception e){System.err.println(e.getMessage());}
}
}
My enemies class
package canvasgmc;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
public class Alvos extends Objetos
{
private Image img;
static Balas bl = new Balas();
public Alvos(int posX, int posY, int altura, int largura, String jog)
{
super(posX, posY, altura, largura);
this.img=Toolkit.getDefaultToolkit().getImage(jog);
}
@Override
void desenho(Graphics g)
{
g.drawImage(img, getPosX(), getPosY(), getAltura(), getLargura(), null);
}
}
The arrayList is created in the main class
private ArrayList<Alvos> bugs = new ArrayList<>();
//constructor
bugs.add(new Alvos(50, 50 , 70, 70,"imagens/bug.gif"));
bugs.add(new Alvos(145, 50 , 70, 70,"imagens/bug1.gif"));
Can someone show me how can i achieve the collision and remove enemies from the arrayList?
Thanks