import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame{
ImageIcon PlayerTank = new ImageIcon("tank.png");
ImageIcon SuTank = new ImageIcon("STank.png");
ImageIcon bullet = new ImageIcon("bullet.png");
ImageIcon explode = new ImageIcon("fire.png");
JLabel PlayerT = new JLabel(PlayerTank);
JLabel STank = new JLabel(SuTank);
JLabel PlayerB = new JLabel(bullet);
JLabel TankExplode = new JLabel(explode);
STank tankS = new STank();
PBullet Bullet = new PBullet();
int startC=0;
boolean flag=true;
Container c = getContentPane();
public Main(){
int tankSize=64;
int bulletSize=32;
setTitle("Shooting Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLayout(null);
PlayerT.setLocation(256,490);
PlayerT.setSize(tankSize,tankSize);
c.add(PlayerT);
PlayerB.setLocation(280,458);
PlayerB.setSize(16,32);
c.add(PlayerB);
STank.setLocation(515,0);
STank.setSize(64,64);
c.add(STank);
TankExplode.setSize(64,64);
TankExplode.setVisible(false);
c.add(TankExplode);
setSize(600,600);
setVisible(true);
tankS.start();
c.addKeyListener(new MyKeyListener());
c.setFocusable(true);
c.requestFocus();
}
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e) {
char keyChar= e.getKeyChar();
if(keyChar == '\n') {
if(startC==0) Bullet.start();
else Bullet.run();
startC++;
}
}
}
class STank extends Thread{
public void run(){
int Tx,Ty;
Tx=STank.getX();
Ty=STank.getY();
while(flag==true) {
STank.setLocation(Tx,Ty);
if(Tx<-60) {
STank.setLocation(515,0);
Tx=515;
Ty=0;
}
try {
Thread.sleep(20);
}catch(InterruptedException e) {
return;
}
Tx-=5;
c.repaint();
}
TankExplode.setLocation(STank.getX(),STank.getY());
TankExplode.setVisible(true);
try {
Thread.sleep(2500);
}catch(InterruptedException e) {
return;
}
TankExplode.setVisible(false);
flag=true;
STank.setLocation(515,0);
}
}
class PBullet extends Thread{
public void run() {
int Bx=PlayerB.getX(),By=PlayerB.getY();
while(By>-16) {
PlayerB.setLocation(Bx,By);
if(Bx>=STank.getX() && Bx<STank.getX()+50 && By<=STank.getY()+45) {
flag=false;
}
try {
Thread.sleep(20);
}catch(InterruptedException e) {
return;
}
By-=5;
c.repaint();
}
PlayerB.setLocation(280,458);
return ;
}
}
public static void main(String[] args) {
new Main();
}
}



