import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
import java.lang.*;
public class rocketship3 extends Applet implements ActionListener
{
//Declare buttons and images
Button btnFLeft, btnFRight, btnUp, btnDown;
Image imgF1, asteroid, explode;
int Rocket, Move[] = new int [5];
int l;
int r;
int h;
int w, z, n;
int x, y, a, b, c, d, k, e, f;
Label lblPoints;
int amount;
public void init ()
{
lblPoints = new Label ("SCORE: " + amount + " Points");
//Load the rocket images
imgF1 = getImage (getCodeBase (), "F1.JPG");
asteroid = getImage (getCodeBase (), "asteroid.JPG");
explode = getImage (getCodeBase (), "explode.JPG");
btnFRight = new Button ("Right");
btnFLeft = new Button ("Left");
//Show action button
add (lblPoints);
setLayout (null);
add (btnFRight);
btnFRight.setActionCommand ("Right");
btnFRight.addActionListener (this);
btnFRight.setBounds (465, 650, 40, 30);
lblPoints.setBackground (Color.red);
lblPoints.setBounds (300, 15, 200, 40);
add (btnFLeft);
btnFLeft.setActionCommand ("Left");
btnFLeft.addActionListener (this);
btnFLeft.setBounds (425, 650, 40, 30);
}
public void actionPerformed (ActionEvent e)
{
String ACTION = e.getActionCommand ();
//Random Variables in Moving the rocket
if (ACTION.equals ("Right"))
{
r = r + 50;
}
if (ACTION.equals ("Left"))
{
l = l + 50;
}
repaint ();
}
public void paint (Graphics g)
{
//Background
setBackground (Color.black);
/* timer = new Timer(speed, this);
timer.setInitialDelay(pause);
timer.start();
*/
g.drawImage (imgF1, r - l, 550, 90, 90, this);
y = y + 5;
b = b + 6;
d = d + 4;
g.drawImage (asteroid, x, y, e + 50, e + 50, this);
g.drawImage (asteroid, a, b, f + 50, f + 50, this);
g.drawImage (asteroid, c, d, k + 50, k + 50, this);
repaint ();
if (y > 650)
{
y = 0;
x = ((int) (1000 * Math.random ()));
e = ((int) (50 * Math.random ()) + 10);
amount = (amount + 1);
lblPoints.setText ("SCORE: " + amount + " Points");
}
if (b > 650)
{
b = 0;
a = ((int) (1000 * Math.random ()));
f = ((int) (50 * Math.random ()) + 10);
amount = (amount + 1);
lblPoints.setText ("SCORE: " + amount + " Points");
}
if (d > 650)
{
d = 0;
c = ((int) (1000 * Math.random ()));
k = ((int) (50 * Math.random ()) + 10);
amount = (amount + 1);
lblPoints.setText ("SCORE: " + amount + " Points");
}
repaint ();
try
{
//do what you want to do before sleeping
Thread.currentThread ().sleep (25); //sleep for 1000 ms
//do what you want to do after sleeptig
}
catch (InterruptedException ie)
{
//If this thread was intrrupted by nother thread
}
for (int w = 1 ; w <=100 ; w++)
if ((r - l == x + w) && (y == 550) || (r - l == x - w) && (y == 550))
{
y = 0;
x = ((int) (1000 * Math.random ()));
amount = (amount - 100);
g.drawImage (explode, r-l, 550, 60, 60, this);
lblPoints.setText ("SCORE: " + amount + " Points");
}
else if (( r - l == a + w) && (b==550) || (r - l == a - w) && (b == 550))
{
b = 0;
a = ((int) (1000 * Math.random ()));
amount = (amount - 100);
g.drawImage (explode, r-l, 550, 60, 60, this);
lblPoints.setText ("SCORE: " + amount + " Points");
}
else if (( r - l == c + w) && (d==550) || (r - l == c - w) && (d == 550))
{
d = 0;
c = ((int) (1000 * Math.random ()));
amount = (amount - 100);
g.drawImage (explode, r-l, 550, 60, 60, this);
lblPoints.setText ("SCORE: " + amount + " Points");
}
}
}