블로그 정리하다 발견
주석달때 누구한테 설명하는것마냥 존나 자세하게 달아놓는 버릇이 있는데 저때도 그랬었네..ㅋ
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Pkmshooting
{
public static void main(String[]args)
{
JFrame f1 = new JFrame("슈팅게임 실험1.0");
f1.setBounds(1,1,679,500);
f1.setLayout(null);
pkmstembox th1 = new pkmstembox();
cheker th4 = new cheker(th1);
emvst th2 = new emvst(th1,th4);
otst1 th3 = new otst1(th1,th4);
Thread TTT3 = new Thread(th3);
ms ms1 = new ms(th1,1,th2);
ms ms2 = new ms(th1,2,th2);
ms ms3 = new ms(th1,3,th2);
th1.vvv4.addMouseListener(ms1);
th1.vvv5.addMouseListener(ms2);
th1.vvv6.addMouseListener(ms3);
f1.add(th1.vvv1);
f1.add(th1.vvv2);
TTT3.start();/*팬텀의 인공지능을 담당하는 쓰레드를 스타트시켰다*/
f1.add(th1.vvv3);
f1.add(th1.vvv4);
f1.add(th1.vvv5);
f1.add(th1.vvv6);
f1.add(th1.vvv7);
f1.add(th1.vvv8);
f1.add(th1.vvv9);
f1.add(th1.vvv10);
f1.setVisible(true);
}
}
class pkmstembox
{
JLabel vvv1;
JLabel vvv2;
JLabel vvv3;
JLabel vvv4;
JLabel vvv5;
JLabel vvv6;
JLabel vvv7;
JLabel vvv8;
JButton vvv9;
JButton vvv10;
int hp1=500; /*프테라의 hp를 나타내는 변수*/
int ehp1=500; /*팬텀의 hp를 나타내는 변수*/
int gameseter=0;
int y1 =2; /*프테라의 위치를 나타내는 변수로써3:맨 위 2:가운데 1:맨 아래 로 구성되있다,*/
int y2 =2; /*팬텀의 위치를 나타내는 변수로써3:맨 위 2:가운데 1:맨 아래 로 구성되있다,*/
int ein=0; /*팬텀의 제거 여부를 나타내는 변수, 1:제거됨 0:아직 제거되지 않음*/
int in=0; /*프테라의 제거 여부*/
int theY1=303; /*프테라의 y좌표값을 나타내는 변수*/
int theY2=315; /*팬텀의 y좌표값을 나타내는 변수*/
public pkmstembox()
{
ImageIcon AAA1 = new ImageIcon("C:/jenjenEMG/프테라1.PNG");
JLabel em1 = new JLabel(AAA1);
em1.setSize(94,84);
em1.setLocation(580,303);
ImageIcon AAA2 = new ImageIcon("C:/jenjenEMG/팬텀1.PNG");
JLabel em2 = new JLabel(AAA2);
em2.setSize(74,75);
em2.setLocation(10,315);
ImageIcon AAA3 = new ImageIcon("C:/jenjenEMG/들판 배경1.PNG");
JLabel em3 = new JLabel(AAA3);
em3.setSize(679,218);
em3.setLocation(1,248);
ImageIcon AAA4 = new ImageIcon("C:/jenjenEMG/위이동버튼1.PNG");
JLabel em4 = new JLabel(AAA4);
em4.setSize(53,43);
em4.setLocation(410,1);
ImageIcon AAA5 = new ImageIcon("C:/jenjenEMG/아래이동버튼1.PNG");
JLabel em5 = new JLabel(AAA5);
em5.setSize(53,43);
em5.setLocation(480,1);
ImageIcon AAA6 = new ImageIcon("C:/jenjenEMG/공격버튼1.PNG");
JLabel em6 = new JLabel(AAA6);
em6.setSize(53,43);
em6.setLocation(550,1);
ImageIcon AAA7 = new ImageIcon("C:/jenjenEMG/파동탄1.PNG");
JLabel em7 = new JLabel(AAA7);
em7.setSize(134,120);
em7.setLocation(800,1); /*맨 처음에는 안보이는곳에 설정했다*/
ImageIcon AAA8 = new ImageIcon("C:/jenjenEMG/섀도볼1.PNG");
JLabel em8 = new JLabel(AAA8);
em8.setSize(90,102);
em8.setLocation(800,1);
JButton hps1 = new JButton(); /*프테라의의 hp를 표시하는 컴포넌트*/
hps1.setSize(hp1,30);
hps1.setLocation(1,45);
JButton ehps1 = new JButton(); /*팬텀의의 hp를 표시하는 컴포넌트*/
ehps1.setSize(ehp1,30);
ehps1.setLocation(1,80);
vvv1 =em1;
vvv2 =em2;
vvv3 =em3;
vvv4 =em4;
vvv5 =em5;
vvv6 =em6;
vvv7 =em7;
vvv8 =em8;
vvv9 =ehps1;
vvv10 =hps1;
}
public void Locater1(int num1,int num2,int num3) /*vvv2와 8의 setLocation의 호출을 담당하는 메써드로, 셋 로케이션을 직접 호출해줘도 되지만, 이런식으로 메써드를 만들어주면 코드도 깔끔해지기도 하지만 최대 장점은, 이미지를 옮길때 공통된 조건이 있을경우(여기에서는 ein의 값이 0일때) 그 조건을 쉽게 첨가할수있게 해준다 매개변수로 하여금 기능을 세부하게 분리시킬수도 있다, 매개변수를 사용해도 좋지만, 참조변수가 있다면 변수를 직접 참조해도 된다, num3을 매개로 받아 이동시킬 컴포넌트를 구분하고 있지만 그냥 각 컴포넌트마다 전용 Locater 메써드를 정의해줘도 된다, 그렇게하면 프로그램 진행속도는 약간 빨라질수도 있는데 미세한 차의이니 편한쪽을 선택*/
{
if(ein==0)
{
if(num3==2)
{
vvv2.setLocation(num1,num2);
return;
}
if(num3==8)
{
vvv8.setLocation(num1,num2);
return;
}
}
}
public void Locater2(int num1,int num2)
{
if(in==0)
{
vvv7.setLocation(num1,num2);
return;
}
}
public void water(int num1)
{
try{
Thread.sleep(num1);
}
catch (Exception e1){
}
}
}
class ms implements MouseListener
{
int mssn1; /*마우스 리스터의 종류를 구분하는 변수*/
pkmstembox mssth1 = null;
emvst mssth2 = null;
public ms(pkmstembox th1,int n1,emvst th2)
{
mssth1 = th1;
mssn1 =n1;
mssth2 = th2;
}
public void mouseClicked(MouseEvent e)
{
if(mssn1==1)
{
if(mssth1.y1<3)
{
mssth1.y1+=1;
mssth1.theY1-=70;
mssth1.vvv1.setLocation(580,mssth1.theY1);
System.out.println(mssth1.y1);
}
}
if(mssn1==2)
{
if(mssth1.y1>1)
{
mssth1.y1-=1;
mssth1.theY1+=70;
mssth1.vvv1.setLocation(580,mssth1.theY1);
System.out.println(mssth1.y1);
}
}
if(mssn1==3)
{
mssth1.vvv4.setLocation(800,1);
mssth1.vvv5.setLocation(800,1);
mssth1.vvv6.setLocation(800,1); /*연결된 컴포넌트를 아예 치워버리는 방식으로 공격 중첩시의 동기화문제를 해결했다*/
Thread TTT2 = new Thread(mssth2);
TTT2.start(); /*동기화 처리를 해주지 않으면 공격 이펙트가 중복되면서 이상한 현상을 보인다, 꼭 동기화처리를 해야한다*/
return; /*EDT 쓰레드에서 탈출*/
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
}
class emvst implements Runnable
{
pkmstembox emvststh1;
cheker emvststh4;
public emvst(pkmstembox th1,cheker th4)
{
emvststh1=th1;
emvststh4=th4;
}
public void run()
{
emvststh1.vvv7.setLocation(500,emvststh1.theY1);
emvststh1.water(200);
emvststh1.Locater2(400,emvststh1.theY1);
emvststh1.water(200);
emvststh1.Locater2(300,emvststh1.theY1);
emvststh1.water(200);
emvststh1.Locater2(200,emvststh1.theY1);
emvststh1.water(200);
emvststh1.Locater2(100,emvststh1.theY1);
emvststh1.water(200);
emvststh1.Locater2(1,emvststh1.theY1);
if(emvststh1.y1==emvststh1.y2&&emvststh1.hp1>0&&emvststh1.in==0)
{
emvststh1.ehp1-=200;
emvststh1.vvv9.setSize(emvststh1.ehp1,30);
System.out.println(emvststh1.ehp1);
emvststh4.chek1();
}
emvststh1.water(200);
emvststh1.vvv7.setLocation(800,1);
if(emvststh1.in==0)
{
emvststh1.vvv4.setLocation(410,1);
emvststh1.vvv5.setLocation(480,1);
emvststh1.vvv6.setLocation(550,1);
}
return;
}
}
class otst1 implements Runnable
{
pkmstembox otst1sth1 = null;
cheker otst1sth4;
int pt1 =1; /*팬텀의 공격 패턴을 나타내는 변수*/
public otst1(pkmstembox th1,cheker th4)
{
otst1sth1 = th1;
otst1sth4 =th4;
}
public void run()
{
while(otst1sth1.ein==0) /*ein의 값이 1이 되버린다 하더래도 진행중인 루프는 계속되므로 불안정한 장면이 연출될수있다, 이 조건식을 기입하는거 이외의 처리를 따로 해주어야만 한다, 즉 이미지를 이동시키기 위해 호출하는 Locater 메써드에서 ein을 참조한 조건을 한개 더 제시하였다*/
{
if(pt1==1)
{
if(otst1sth1.theY2!=315&&otst1sth1.ein==0)
{
otst1sth1.theY2=315;
otst1sth1.y2=2;
otst1sth1.Locater1(10,315,2);
}
otst1sth1.water(500);
otst1sth1.Locater1(80,otst1sth1.theY2,8); /*ehp가 0이되어 팬텀이 제거될경우 이 쓰레드 이외의 emvst에서도 setLocation을 사용하게되는데, 그것에 대한 적절한 동기화 처리를 반드시 해주어야 한다*/
otst1sth1.water(200);
otst1sth1.Locater1(180,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(280,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(380,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(480,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(580,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(800,otst1sth1.theY2,8);
if(otst1sth1.y2==otst1sth1.y1&&otst1sth1.ehp1>0)
{
otst1sth1.hp1-=150;
otst1sth1.vvv10.setSize(otst1sth1.hp1,30);
otst1sth4.chek2();
}
pt1=2; /*패턴을 변경함*/
}
if(pt1==2)
{
if(otst1sth1.ein==0)
{
otst1sth1.theY2-=70;
otst1sth1.y2=3;
otst1sth1.Locater1(10,otst1sth1.theY2,2);
otst1sth1.water(500);
otst1sth1.Locater1(80,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(180,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(280,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(380,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(480,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(580,otst1sth1.theY2,8);
otst1sth1.water(200);
otst1sth1.Locater1(800,otst1sth1.theY2,8);
if(otst1sth1.y2==otst1sth1.y1&&otst1sth1.ehp1>0)
{
otst1sth1.hp1-=80;
otst1sth1.vvv10.setSize(otst1sth1.hp1,30);
otst1sth4.chek2();
}
}
pt1=1; /*패턴을 변경함*/
}
}
}
}
class cheker
{
pkmstembox chekersth1;
public cheker(pkmstembox th1)
{
chekersth1=th1;
}
public synchronized void chek1()
{
if(chekersth1.ehp1<1&&chekersth1.ein==0) /*적의 hp가 1미만이고, 적이 아직 제거되지 않았을때*/
{
if(chekersth1.gameseter==0)
{
chekersth1.ein=1;
chekersth1.gameseter=1;
chekersth1.vvv7.setLocation(800,1);
chekersth1.vvv8.setLocation(800,1);
chekersth1.water(500);
chekersth1.vvv2.setLocation(15,chekersth1.theY2);
chekersth1.water(50);
chekersth1.vvv2.setLocation(-20,chekersth1.theY2);
chekersth1.water(50);
chekersth1.vvv2.setLocation(-50,chekersth1.theY2);
chekersth1.water(50);
chekersth1.vvv2.setLocation(800,1);
System.out.println("팬텀은 쓰러졌다!");
chekersth1.water(300);
System.out.println("사용자 승리!");
return;
}
if(chekersth1.gameseter==1) /*이 조건문을 위의 조건문보다 먼저 넣고, 반환하도록 시켜도 된다!*/
{
System.out.println("ㅇㅇ");
}
}
}
public synchronized void chek2()
{
if(chekersth1.hp1<1&&chekersth1.in==0)
{
if(chekersth1.gameseter==0)
{
chekersth1.in=1;
chekersth1.gameseter=1;
chekersth1.vvv8.setLocation(800,1);
chekersth1.vvv7.setLocation(800,1);
chekersth1.water(500);
chekersth1.vvv1.setLocation(590,315);
chekersth1.water(30);
chekersth1.vvv1.setLocation(620,315);
chekersth1.water(30);
chekersth1.vvv1.setLocation(690,315);
chekersth1.water(30);
chekersth1.vvv1.setLocation(750,315);
System.out.println("프테라는 쓰러졌다!");
chekersth1.water(300);
System.out.println("사용자 패배");
return;
}
if(chekersth1.gameseter==1)
{
System.out.println("ㅇㅇ");
}
}
}
}
고딩때 자바로 게임 개발이라니 지금 유니티로 개발하면 날라 다니겠네.
이런 종류의 스압은 예상 못했는데
주석이 정말 친절하군요?