import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
GunThread th;
ChikenThread th1 = new ChikenThread();
Container c = getContentPane();
JLabel la = new JLabel();
ImageIcon img = new ImageIcon("patrick c.png");
JLabel la1 = new JLabel(img);
JLabel la2 = new JLabel("0");
JButton JL = new JButton("다시시작");
int score=0;
int combo=0;
int time=20;
public Main() {
setTitle("사격 게임");
c.setLayout(null);
c.addKeyListener(new MyKeyListener());
c.setFocusable(true);
c.requestFocus();
c.add(la);
la.setLocation(250,450);
la.setSize(10,10);
la.setBackground(Color.red);
la.setOpaque(true);
c.add(la1);
la1.setLocation(0,0);
la1.setSize(30,30);
la1.setOpaque(true);
c.add(la2);
la2.setLocation(220,230);
la2.setSize(60,40);
la2.setFont(new Font("Impact",Font.PLAIN,30));
c.add(JL);
JL.setLocation(400,400);
JL.setSize(100,100);
JL.addActionListener(new MyActionListener());
th1.start();
setSize(500,500);
setVisible(true);
}
class MyKeyListener extends KeyAdapter {
//e.getKeyCode() 엔터 10, 스페이스 32
public void keyPressed(KeyEvent e) {
if((th==null|| th.getState()==Thread.State.TERMINATED) && e.getKeyCode()==10) {//집가서 정리
th = new GunThread();
th.start();
}
}
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
score=0;
combo=0;
time=20;
la2.setText("0"); //점수판을 0점으로 보이게 리셋
c.setFocusable(true);
c.requestFocus();
}
}
class GunThread extends Thread {
synchronized public void run() {
while(true) {
try {
sleep(time);
}catch (Exception e) {
}
int x1 = la.getX();
int y = la.getY();
la.setLocation(x1,y -5 );
if(y<0 || la.getY()<=30 && (220<=la1.getX() && la1.getX()<=260) ) {
la.setLocation(250,450);
if(score==10) {
time=10;
}
if(y>0) { //명중
combo++;
score+=combo;
la2.setText(Integer.toString(score) );
}//그 점수만큼 점수얻기
else{
combo=0;
}
return ;
//break; or 실패
}
}
}
}
class ChikenThread extends Thread {
public void run() {
while(true) {
try {
sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int x = la1.getX();
int y = la1.getY();
if(x>=500) la1.setLocation(0, y);
else la1.setLocation(x+5,y);
}
}
}
public static void main(String[] args) {
new Main();
}
}
//다음시간에 추가할거 : 사용방법 버튼 만들어서 다이얼로그 추가하기!!



