Hello!
I need to get some collision detection going in my code but I have no idea where to start!
can anybody find out how to put this in?
the detection will stop drwaing monstors if the bullet hits them
// The "SpaceShooter" class.
//import java.applet.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.*;
import java.applet.AudioClip;
public class SpaceShooter extends Applet
implements ActionListener, KeyListener, Runnable
{
//All the global varibles
int x1 = 50, y1 = 50, x = 10, dx, y = 10, dy, a = 0, by, bx = 95, mx = 510, my = 20;
private Image dbImage;
private Graphics dbg;
Thread thread, thread2;
Image img, tile, background, rocketBullet, bullet, credit, sam, mon1, mon2, mon3, mon4, mon5, mon6, mon7;
Graphics backg;
boolean triMove = false;
int collide = 0, instruction = 1, counter = 0;
// Place instance variables here
public void init ()
{
//Imports all the pictures
img = getImage (getDocumentBase (), "Shoot.gif");
bullet = getImage (getDocumentBase (), "bullet.gif");
background = getImage (getDocumentBase (), "space.jpg");
credit = getImage (getDocumentBase (), "Credits.jpg");
sam = getImage (getDocumentBase (), "Samus.jpg");
mon1 = getImage (getDocumentBase (), "mon1.gif");
mon2 = getImage (getDocumentBase (), "mon2.gif");
mon3 = getImage (getDocumentBase (), "mon3.gif");
mon4 = getImage (getDocumentBase (), "mon4.gif");
mon5 = getImage (getDocumentBase (), "mon5.gif");
mon6 = getImage (getDocumentBase (), "mon6.gif");
mon7 = getImage (getDocumentBase (), "mon8.gif");
addKeyListener (this);
// Place the body of the initialization method here
} // init method
public void actionPerformed (ActionEvent e) //When user clicks action (below)is preformed
{
}
//Starting here, the following lines of code make my bullet finally loop!
public void start ()
{
thread = new Thread (this);
thread.start ();
}
public void stop ()
{
thread.stop ();
}
public void run ()
{
Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);
while (true)
{
// Delay for 20 milliseconds
try
{
Thread.sleep (2);
}
catch (InterruptedException e)
{
}
bx = dx;
by = dy;
//If you hit an edge, reverse the direction
/*if ((x1 + dx < 0) || (x1 + dx > getSize ().width))
{
dx = 0;
}
if ((y1 + dy < 0) || (y1 + dy > getSize ().height))
{
dy = 0;
}*/
//Find the new bullet coordinates
x1 += bx;
y1 += by;
//Allows the user to fire another missle
if (counter == 1)
{
x1 = 50;
y1 = y + 8;
counter = 0;
}
//Collision
if (x1 == 685 && y1 == 20)
{
collide = collide + 1;
}
repaint ();
// reset thread priority
Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
} // while (true)
}
public void paint (Graphics g)
{
//Draws all the pictures
if (instruction == 1)
{
g.drawImage (credit, 0, 0, 750, 500, this);
}
if (instruction != 1)
{
if (collide >= 0)
{
g.drawImage (background, 0, 0, 750, 500, this);
g.drawImage (img, x, y, 70, 43, this);
g.drawImage (sam, mx + 350, my + 150, 100, 100, this);
g.drawImage (mon1, mx, my, 50, 50, this);
g.drawImage (mon1, mx + 175, my + 20, 50, 50, this);
g.drawImage (mon2, mx + 50, my + 210, 50, 50, this);
g.drawImage (mon2, mx + 175, my + 260, 50, 50, this);
g.drawImage (mon3, mx + 100, my + 85, 50, 50, this);
g.drawImage (mon3, mx + 200, my + 185, 50, 50, this);
g.drawImage (mon4, mx + 25, my + 110, 50, 50, this);
g.drawImage (mon4, mx + 300, my + 85, 50, 50, this);
g.drawImage (mon5, mx - 25, my + 210, 50, 50, this);
g.drawImage (mon5, mx - 50, my - 10, 50, 50, this);
g.drawImage (mon6, mx - 10, my + 310, 50, 50, this);
g.drawImage (mon6, mx - 70, my + 60, 50, 50, this);
g.drawImage (mon7, mx + 10, my + 410, 73, 43, this);
g.drawImage (mon7, mx + 150, my + 360, 73, 43, this);
if (collide == 1)
{
g.drawImage (background, 0, 0, 750, 500, this);
g.drawImage (img, x, y, 70, 43, this);
g.drawImage (sam, mx + 350, my + 150, 100, 100, this);
g.drawImage (mon1, mx + 175, my + 20, 50, 50, this);
g.drawImage (mon2, mx + 50, my + 210, 50, 50, this);
g.drawImage (mon2, mx + 175, my + 260, 50, 50, this);
g.drawImage (mon3, mx + 100, my + 85, 50, 50, this);
g.drawImage (mon3, mx + 200, my + 185, 50, 50, this);
g.drawImage (mon4, mx + 25, my + 110, 50, 50, this);
g.drawImage (mon4, mx + 300, my + 85, 50, 50, this);
g.drawImage (mon5, mx - 25, my + 210, 50, 50, this);
g.drawImage (mon5, mx - 50, my - 10, 50, 50, this);
g.drawImage (mon6, mx - 10, my + 310, 50, 50, this);
g.drawImage (mon6, mx - 70, my + 60, 50, 50, this);
g.drawImage (mon7, mx + 10, my + 410, 73, 43, this);
g.drawImage (mon7, mx + 150, my + 360, 73, 43, this);
}
//Draws the bullet
if (triMove == true)
{
g.drawImage (bullet, x1, y1, 40, 10, this);
}
}
}
//Place the body of the drawing method here
} //paintmethod
public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode ();
if (key == KeyEvent.VK_UP)
{
by = -15;
y = y + by;
}
else if (key == KeyEvent.VK_DOWN)
{
by = 15;
y = y + by;
}
else if (key == KeyEvent.VK_SPACE)
{
counter = counter + 1;
dx = 2;
x1 = x1 + dx;
triMove = true;
}
else if (key == KeyEvent.VK_ENTER)
{
instruction = instruction + 1;
}
}
public void keyReleased (KeyEvent e)
{
}
public void keyTyped (KeyEvent e)
{
}
public void update (Graphics g)
{
// initialize doublebuffers
dbImage = createImage (this.getSize ().width, this.getSize ().height);
dbg = dbImage.getGraphics ();
// save background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);
// draw foreground on background
dbg.setColor (getForeground ());
paint (dbg);
// Now indicate ready drawn picture Offscreen on the right screen
g.drawImage (dbImage, 0, 0, this);
}
} // SpaceShooter class