PDA

Click to See Complete Forum and Search --> : Collision Detection!!!!!!!


Donny
Feb 23rd, 2001, 02:13 PM
Please help How would i add collision detection to this code so that when the moving rectangle hits the other rectangle The words You Are the winner come up on the screen please help....

code:


import java.awt.* ;
import java.applet.Applet ;

public class Try1 extends Applet implements Runnable
{
private int maxWidth , maxHeight ;
int X = 50;
int Y = 50;
int AX = 100;
int AY = 100;
private int imageHeight , imageWidth ;
boolean left = false;
boolean right = false;
boolean up = false;
boolean down = false;
boolean win = false, loose = false;
public void init()
{
setBackground( Color.yellow );
maxWidth = 200 ;
maxHeight = 200 ;
imageHeight = 10 ;
imageWidth = 10 ;


Thread animator = new Thread( this );
animator.start();
}

public void paint( Graphics g )
{
g.drawRect( X , Y , imageWidth , imageHeight );
g.drawRect( AX, AY , 10 , 10 );
if (win)
{
g.drawString("U are the winner", 40, 40);
}
}


public boolean isFocusTraversable(){
return true;
}

public boolean keyDown(Event e, int key)
{
if (key == Event.LEFT) {
left = true;
right = false;
up = false;
down = false;
}
if (key == Event.RIGHT) {
right = true;
left = false;
up = false;
down = false;
}
if (key == Event.UP) {

up = true;
right = false;
left = false;
down = false;
}
if (key == Event.DOWN) {

down = true;
right = false;
up = false;
left = false;

}
return true;
}


public void run()
{
while ( true )
{
try
{
Thread.sleep( 20 );
}
catch ( InterruptedException e )
{
}


if (up){
Y -= 3 ;
repaint();
}

if (down){
Y += 3 ;
repaint();
}

if (right){
X += 3 ;
repaint();
}

if (left){
X -= 3 ;
repaint();
}
if ( ( X + imageWidth ) >= maxWidth )
{
X = maxWidth - imageWidth -1;
}
else if ( X <= 0 )
{
X = 0;
}

if ( ( Y + imageHeight ) >= maxHeight )
{
Y = maxHeight - imageHeight - 1;
}
else if ( Y <= 0 )
{
Y = 0;
}
}
}

} // close class


code end:


PLEASE HELP !!!!