I have a problem with this code and I do not know what is causing it.
// The "ArrowKeys" class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ArrowKeys2 extends Applet implements KeyListener, Runnable
{
int x = 50, dx, y = 50, dy, a = 0, score, total, locx, locy;
double rand, location;
char m;
boolean get = false;
private Image dbImage;
private Graphics dbg;
Thread thread;
Image img, tile, background, target;
Graphics backg;
// Place instance variables here
public void init ()
{
rand = Math.random ();
location = Math.random () * 1000;
locx = (int) Math.round (location);
location = Math.random () * 1000;
locy = (int) Math.round (location);
img = getImage (getDocumentBase (), "bug1right.gif");
if (rand > .63)
{
target = getImage (getDocumentBase (), "flblue.gif");
score = 5;
}
else if (rand > .33)
{
target = getImage (getDocumentBase (), "flred.gif");
score = 2;
}
else
{
target = getImage (getDocumentBase (), "flyellow.gif");
score = 1;
}
//background = getImage (getDocumentBase (), "hedgelarge.gif");
addKeyListener (this);
/*
tile = createImage (1280, 690);
backg = tile.getGraphics ();
backg.setColor (Color.white);
*/
// Place the body of the initialization method here
} // init method
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 (20);
}
catch (InterruptedException e)
{
}
if (dx == -2)
{
// If you hit an edge, reverse the direction
img = getImage (getDocumentBase (), "bug1left.gif");
for (int i = 0 ; i < 500000 ; i++)
{
}
repaint ();
if (x <= locx + 50 && x >= locx)
{
get = true;
}
}
else if (dx == 2)
{
img = getImage (getDocumentBase (), "bug1right.gif");
for (int i = 0 ; i < 500000 ; i++)
{
}
repaint ();
if (x >= locx && x <= locx + 50)
{
get = true;
}
}
else if (dy == -2)
{
img = getImage (getDocumentBase (), "bug1up.gif");
for (int i = 0 ; i < 500000 ; i++)
{
}
repaint ();
if (y >= locy + 50 && y >= locy)
{
get = true;
}
}
else if (dy == 2)
{
img = getImage (getDocumentBase (), "bug1down.gif");
for (int i = 0 ; i < 500000 ; i++)
{
}
repaint ();
if (y <= locy + 50 && y >= locy)
{
get = true;
}
}
if ((x + dx < 0) || (x + dx > getSize ().width))
{
dx = 0;
}
if ((y + dy < 0) || (y + dy > getSize ().height))
{
dy = 0;
}
//calculate the new position
x += dx;
y += dy;
if (get = true)
{
get = false;
total += score;
rand = Math.random ();
location = Math.random () * 1000;
locx = (int) Math.round (location);
location = Math.random () * 1000;
locy = (int) Math.round (location);
if (rand > .63)
{
target = getImage (getDocumentBase (), "flblue.gif");
score = 5;
}
else if (rand > .33)
{
target = getImage (getDocumentBase (), "flred.gif");
score = 2;
}
else
{
target = getImage (getDocumentBase (), "flyellow.gif");
score = 1;
}
}
// reset thread priority
Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
}
} // while (true)
public void paint (Graphics g)
{
//backg.drawImage(background, 0, 0, this);
g.drawImage (target, locx, locy, 50, 50, this);
g.drawImage (img, x, y, 50, 50, this);
g.drawString ("Score: " + total, 10, 10);
// backg.drawImage( background, 0, 0, this );
// Place the body of the drawing method here
} // paint method
public void keyPressed (KeyEvent e)
{
int key = e.getKeyCode ();
if (key == KeyEvent.VK_RIGHT)
{
//backg.drawImage (background, 0, 0, 1280, 960, this);
dx = 2;
dy = 0;
x = x + dx;
}
else if (key == KeyEvent.VK_LEFT)
{
//backg.drawImage (img, 0, 0, 1280, 960, this);
dx = -2;
dy = 0;
x = x + dx;
}
else if (key == KeyEvent.VK_UP)
{
//backg.drawImage (img, 0, 0, 1280, 960, this);
dy = -2;
dx = 0;
y = y + dy;
}
else if (key == KeyEvent.VK_DOWN)
{
//backg.drawImage (img, 0, 0, 1280, 960, this);
dy = 2;
dx = 0;
y = y + dy;
}
}
public void keyReleased (KeyEvent e)
{
}
public void keyTyped (KeyEvent e)
{
}
public void update (Graphics g)
{
// initialize doublebuffers
if (dbImage == null)
{
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);
}
} // ArrowKeys class
I added in the collision between the x and locx and the y and locy.
Thanks for any help.