import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
public class Main extends JFrame {
ImageIcon imgpla = new ImageIcon("최종전투기.png");
JLabel l = new JLabel(imgpla);
ImageIcon imgsky = new ImageIcon("밤하늘.png");
JLabel sky = new JLabel(imgsky);
ImageIcon imgal = new ImageIcon("말벌.png");
JLabel al = new JLabel(imgal);
ImageIcon imgal2 = new ImageIcon("파리 (1).png");
JLabel al2 = new JLabel(imgal2);
JLabel b_count = new JLabel();
int cnt=50;
boolean u=false,d=false,le=false,ri=false,sp=false;
Container c;
class P extends Thread {//Player
private int x=220;
private int y=370;
public void run() {
c.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP)
u = false;
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
ri = false;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
d = false;
if(e.getKeyCode() == KeyEvent.VK_LEFT)
le = false;
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP) {
u = true;
if(ri==true) x+=5;
if(le==true) x-=5;
y-=5;
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
ri = true;
if(u==true) y-=5;
if(d==true) y+=5;
x+=5;
}
if(e.getKeyCode() == KeyEvent.VK_DOWN) {
d = true;
if(ri==true) x+=5;
if(le==true) x-=5;
y+=5;
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
le = true;
if(u==true) y-=5;
if(d==true) y+=5;
x-=5;
}
}
});
while(true) {
l.setLocation(x,y);
l.repaint();
}
}
}
class P_Attack extends Thread { //쓰레드마다 총알이 한개씩
ImageIcon imgbul = new ImageIcon("총알.png");
JLabel b = new JLabel(imgbul);
private int x;
private int y;
public P_Attack(int x, int y) {
//System.out.println("총알 스레드 생성!");
this.x=x;
this.y=y;
b.setSize(2,12);
b.setLocation(x,y);
c.add(b);
//sky.setSize(550,750);
//sky.setLocation(0,0);
//c.add(sky);
}
public void run() {
cnt--;
b_count.setFont(new Font("DungGeunMo", Font.PLAIN, 20));
b_count.setForeground(Color.WHITE);
b_count.setText("YOU HAVE "+cnt+" BULLET TO SHOT.");
b_count.repaint();
while(true) {
if(cnt<=0) {
cnt=1;
b.setVisible(false);
b_count.setText("YOU DONT'T HAVE ANY BULLET.");
System.out.println("Reroad...");
c.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if(e.getKeyChar() == 'r') {
cnt=50;
}
}
public void keyReleased(KeyEvent e) {}
});
}
if(al.getX()<=x && al.getX()+24>=x+2 || al.getY()<=y && al.getY()-24>=y-12) {
System.out.println("충돌");
}
try {
sleep(10);
b.setLocation(x,y);
y-=10;
//System.out.println(x+", "+y);
if(y<=0) {
b.setVisible(false);
return;
}
// 총알 쓰레드를 언제 멈출지
} catch (InterruptedException e1) {
return;
}
b.repaint();
}
}
}
class A1 extends Thread {
private int x=120;
private int y=0;
public void run() {
while(true) {
y++;
if(y>=500) {
al.setLocation(120,0);
}
try {
sleep(10);
al.setLocation(x,y);
} catch (InterruptedException e1) {
return;
}
al.repaint();
}
}
}
class A2 extends Thread {
private int x=240;
private int y=0;
public void run() {
while(true) {
y++;
if(y>=500) {
al2.setLocation(240,0);
}
try {
sleep(10);
al2.setLocation(x,y);
} catch (InterruptedException e1) {
return;
}
al2.repaint();
}
}
}
class A3 extends Thread {
public void run() {
while(true) {
}
}
}
public Main() {
setTitle("피융");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c = getContentPane();
c.setLayout(null);
b_count.setFont(new Font("DungGeunMo", Font.PLAIN, 20));
b_count.setForeground(Color.WHITE);
b_count.setText("YOU HAVE 50 BULLET TO SHOT.");
c.setBackground(Color.BLACK);
l.setSize(32,32);
l.setLocation(220,370);
al.setSize(24,24);
al.setLocation(120,0);
al2.setSize(24,24);
al2.setLocation(240,0);
b_count.setSize(300,15);
b_count.setLocation(100,30);
c.add(l);
c.add(al);
c.add(al2);
c.add(b_count);
c.requestFocus();
c.setFocusable(true);
setSize(400,500);
setVisible(true);
A1 th3 = new A1();
th3.start();
A2 th4 = new A2();
th4.start();
if(al.getY()>=400) {
th3.start();
th4.start();
}
P th1 = new P();
th1.start();
//스페이스 눌릴때마다 th2 생성하고 시작
c.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE) {
P_Attack th2 = new P_Attack(l.getX()+16,l.getY());
th2.start();
}
}
public void keyPressed(KeyEvent e) {}
});
}
public static void main(String[] args) {
new Main();
}
}