Menu

Code

Timings:- 6:30 PM
------------
Game.java
------------
import java.awt.*;
import java.awt.event.*;

class Game
{
Frame f;
static Label timer,score;
static Button target,tank,bullet;
Game()
{
f=new Frame("MyFrame");
timer=new Label("60");
score=new Label("0");
timer.setFont(new Font("Arial",1,18));
score.setFont(new Font("Arial",1,18));
tank=new Button();
tank.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tank.setEnabled(false);
BulletThread b=new BulletThread();
b.start();
}
});
target=new Button();
bullet=new Button();
timer.setBounds(10,360,40,40);
score.setBounds(350,360,40,40);
target.setBounds(10,30,100,20);
tank.setBounds(180,360,40,40);
bullet.setBounds(195,350,10,10);
f.setLayout(null);
f.add(timer);
f.add(tank);
f.add(target);
f.add(score);
f.add(bullet);
f.setVisible(true);
f.setSize(400,400);
TargetThread t1=new TargetThread();
t1.start();
TimerThread t2=new TimerThread();
t2.start();
}
public static void main(String ar[])
{
Game g=new Game();
}
}
------------------------------------
TargetThread.java
------------------------------------
import java.awt.*;
class TargetThread extends Thread
{
public void run()
{
Button b=Game.target;
while(true)
{
int x=b.getX();
if(x>=400)
x=0;
b.setBounds(x+10,30,100,20);
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
}
}
}
--------------------------------------
TimerThread.java
--------------------------------------
import java.awt.*;
class TimerThread extends Thread
{
public void run()
{
Label l=Game.timer;
for(int i=60;i>=0;i--)
{
l.setText(""+i);
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
}
}
}
}
---------------------------------------
BulletThread.java
---------------------------------------
import java.awt.*;
class BulletThread extends Thread
{
public void run()
{
Button b=Game.bullet;
Button t=Game.target;
Button b1=Game.tank;
Label l=Game.score;
for(int i=350;i>=0;i-=10)
{
b.setBounds(195,i,10,10);
if((b.getX()>=t.getX() && b.getX()<=t.getX()+100)&&(b.getY()>=t.getY()&&b.getY()<=t.getY()+20))
{
int a=Integer.parseInt(l.getText());
a++;
l.setText(""+a);
break;
}
try
{
Thread.sleep(10);
}
catch(Exception e)
{
}
}
b.setBounds(195,350,10,10);
b1.setEnabled(true);
}
}