Java 로만드는 Monster 잡기게임예제이해 2014. 12. 2
게임화면및게임방법 기사초기위치 : (0,0) 아이템 10 개랜덤생성 몬스터 10 놈랜덤생성 Frame 하단에기사위치와기사파워출력방향키로기사이동아이템과몬스터는고정종료버튼클릭하면종료
Project 구성 GameMain.java GUI 환경설정, Main Method 게임객체램덤위치에생성 Event Lister 정의및등록 GameMap.java pj 게임맵관련필드정의 GameObject.java Human.java 주인공 ( 기사 ) 클래스 Item.java 에너지충전시키는아이템클래스 Monster.java 몬스터클래스
GameMap.java public class GameMap { public static final int WIDTH=500, HEIGHT=500, MAX_WIDTH=WIDTH, MAX_HEIGHT=HEIGHT+80; public static final String strgametitle = "< Monster 잡기 > "; 500 580 80
GameObject.java java import java.util.random; public class GameObject { public String sname; public int ix, iy; public int ipower; public static final int WIDTH=60, HEIGHT=20; public GameObject() { public GameObject(String s) { this.sname = s; Random r = new Random(); this.ix = (r.nextint(gamemap.widththis.width)/10*10); this.iy = (r.nextint(gamemap.heightthis.height)/10*10); public int getx() { return this.ix; public int gety() { return this.iy; public void setx(int x) { this.ix = x; public void sety(int y) { this.iy = y; public int getpower() { return this.ipower; public String getname() { public void setpower(int power) { return this.sname; this.ipower = power;
Human.java public class Human extends GameObject { public static final int MOVING_UNIT=10; public Human() { public Human(String s) { super(s); public Human(String s, int power, int x, int y) { super.sname = s; super.ipower = power; super.ix = x; super.iy = y; public void move(int x, int y){ if ( (super.ix+x) >= 0 && (super.ix+x) <= (GameMap.WIDTH-GameObject.WIDTH) ) super.ix += x; if ( (super.iy+y) >= 0 && (super.iy+y) <= (GameMap.HEIGHT-GameObject.HEIGHT) ) super.iy += y;
Item.java public class Item extends GameObject { public static final int iitemnum = 10; public static String stritemname[] = {" 물약 ", " 고기 ", " 식빵 ", " 치즈 ", " 맥주 ", " 김치 ", " 집밥 ", " 버거 ", " 콜라 ", " 피자 "; public static int iitempower[] = {10203040501020304050; {10,20,30,40,50,10,20,30,40,50; public Item() { public Item(String s) { super(s); public Item(String s, int power) { super(s); super.ipower = power;
Monster.java public class Monster extends GameObject { public static final int imonsternum = 10; public static String strmonstername[] = {" 전갈 ", " 해골 ", " 저그 ", " 액션가면 ", " 뱀파 ", " 마귀 ", " 귀신 ", " 유령 ", " 라바 ", " 짱구 "; public static int imonsterpower[] = {70,60,70,-30,90,100,80,60,70,80,90,-70; 30901008060708090 70; public Monster() { public Monster(String s) { super(s); public Monster(String s, int power) { super(s); super.ipower = power;
import java.util.scanner; import java.awt.event.*; import java.awt.*; import javax.swing.*; GameMain.java - 1 public class GameMain extends JFrame { public JPanel GameGround, GameMessage; // 기사, 몬스터, 아이템객체변수 public Human Knight; public Monster[] GameMonster; public Item[] GameItem; // 기사, 몬스터, 아이템을 GUI에나타내기위한 JLabel 변수 public JLabel KnightLabel, MonsterLabel[], ItemLabel[]; public JLabel KnightInfo1, KnightInfo2; public JTextField KnightLocation, i KnightPower; public JButton exit;
GameMain.java - 2 GameMain() { // Frame 생성 settitle(gamemap.strgametitle); G l ) setdefaultcloseoperation(jframe.exit_on_close); setlayout(null); GameGround = new JPanel(); GameMessage = new JPanel(); GameGround.setLayout(null); GameMessage.setLayout(null); GameGround.setBounds(0,0,GameMap.MAX_WIDTH, GameMap.HEIGHT); System.out.printf("GameGround: %d, %d \n", GameGround.getWidth(), GameGround.getHeight()); GameMessage.setBounds(0,GameMap.HEIGHT,GameMap.MAX_WIDTH, 80); System.out.printf("GameMessage: %d, %d \n",gamemessage.getwidth(), GameMessage.getHeight()); GameGround.setBackground(Color.WHITE); GameMessage.setBackground(Color.LIGHT_GRAY); setsize(gamemap.max_width, MAX GameMap.MAX_HEIGHT); MAX
GameMain.java - 3 // 주인공 (Human) 객체생성 & Console에결과출력 Knight = new Human(" 기사 ", 100, 0, 0); System.out.printf(" 파워가 %d 인 %s 가무덤에서나왔습니다. \n", Knight.iPower, ip Knight.sName); System.out.printf("%s의위치는 (%3d, %3d) 입니다. \n\n", Knight.sName, Knight.getX(), Knight.getY()); // Monster 객체생성 & Console 에결과출력 GameMonster = new Monster[Monster.iMonsterNum]; for (int i=0; i<monster.imonsternum; i++) { GameMonster[i] = new Monster(Monster.strMonsterName[i], Monster.iMonsterPower[i]); System.out.printf("%s 이생성되었고, 위치는 (%3d, %3d) 입니다. \n", GameMonster[i].sName, GameMonster[i].getX(), GameMonster[i].getY()); // Item 객체생성 & Console 에결과출력 GameItem = new Item[Item.iItemNum]; for (int i=0; i<item.iitemnum; i++) { GameItem[i] = new Item(Item.strItemName[i], Item.iItemPower[i]); System.out.printf("%s이생성되었고, 위치는 (%3d, %3d) 입니다. \n",gameitem[i].sname, GameItem[i].getX(), GameItem[i].getY());
GameMain.java - 4 // 주인공 (Human) JLable 객체생성및 Frame에 Add KnightLabel = new JLabel(Knight.sName); KnightLabel.setLocation(Knight.getX(),Knight.getY()); KnightLabel.setSize(GameObject.WIDTH,GameObject.HEIGHT); KnightLabel.setForeground(Color.BLUE); GameGround.add(KnightLabel); // Monster JLable 객체생성및 Frame 에 Add MonsterLabel = new JLabel[Monster.iMonsterNum]; for (int i=0; i<monster.imonsternum; i++) { MonsterLabel[i] = new JLabel(GameMonster[i].sName); MonsterLabel[i].setLocation(GameMonster[i].getX(),GameMonster[i].getY()); MonsterLabel[i].setSize(GameObject.WIDTH,GameObject.HEIGHT); MonsterLabel[i].setForeground(Color.RED); GameGround.add(MonsterLabel[i]); // Item JLable 객체생성및 Frame 에 Add ItemLabel = new JLabel[Item.iItemNum]; for (int i=0; i<item.iitemnum; i++) { ItemLabel[i] = new JLabel(GameItem[i].sName); ItemLabel[i].setLocation(GameItem[i].getX(), GameItem[i].getY()); ItemLabel[i].setSize(GameObject.WIDTH,GameObject.HEIGHT); ItemLabel[i].setForeground(Color.GREEN); tf l GameGround.add(ItemLabel[i]);
GameMain.java - 5 // 기사위치를 Textbox에출력 KnightInfo1 = new JLabel(" 기사위치 : (0,0)"); KnightInfo1.setLocation(10,20); KnightInfo1.setSize(150,20); GameMessage.add(KnightInfo1); // 기사에너지를 Textbox에출력 KnightInfo2 = new JLabel(" 기사파워 : 100"); KnightInfo2.setLocation(210,20); KnightInfo2.setSize(100,20); GameMessage.add(KnightInfo2); // 종료버튼 exit = new JButton(" 종료 "); exit.setlocation(400,15); exit.setsize(80,30); GameMessage.add(exit); add(exit); GameGround.addKeyListener(new GameKeyListener()); exit.addactionlistener(new GameActionListener()); add(gamemessage); add(gameground); setresizable(false); setvisible(true); GameGround.requestFocus();
GameMain.java - 6 // Key Event class GameKeyListener extends KeyAdapter { public void keypressed(keyevent e){ int keycode = e.getkeycode(); switch (keycode){ case KeyEvent.VK_UP : Knight.move(0,-Human.MOVING_UNIT); break; case KeyEvent.VK_DOWN : Knight.move(0,+Human.MOVING_UNIT); break; case KeyEvent.VK_LEFT: Knight.move(-Human.MOVING_UNIT,0); break; case KeyEvent.VK_RIGHT: Knight.move(+Human.MOVING_UNIT,0); break; default: return; KnightLabel.setLocation(Knight.getX(), Knight.getY()); System.out.printf("%s가 (%3d, %3d) 로이동했습니다. \n", Knight.sName, Knight.getX(), Knight.getY()); KnightInfo1.setText(" 기사위치 : ("+Knight.getX()+","+Knight.getY()+")");
GameMain.java - 7 // 기사가아이템을획득할수있는지확인, 아이템에너지흡수및아이템제거 for (int i=0; i<item.iitemnum; i++) { if (GameItem[i]!= null ) if ( (Knight.getX() tx() == GameItem[i].getX()) tx()) && (Knight.getY() ty() == GameItem[i].getY())) ty())) { int power = Knight.getPower() + GameItem[i].getPower(); Knight.setPower(power); KnightInfo2.setText(" 기사파워 : "+Knight.getPower()); System.out.printf("%s 가 %s를획득했습니다.\n", Knight.sName, GameItem[i].sName); System.out.printf("%s의에너지가 %d가되었습니다.\n", Knight.sName, power); GameItem[i]=null; GameGround.remove(ItemLabel[i]); // 기사가몬스터와싸움 for (int i=0; i<monster.imonsternum; i++) { if (GameMonster[i]!= null ) if ( (Knight.getX() == GameMonster[i].getX()) && (Knight.getY() == GameMonster[i].getY())) { int power = Knight.getPower() - GameMonster[i].getPower(); Knight.setPower(power); KnightInfo2.setText(" 기사파워 : "+Knight.getPower()); System.out.printf("%s가 %s와싸웁니다.\n", Knight.sName, GameMonster[i].sName); System.out.printf("%s 의에너지가 %d 가되었습니다.\n", Knight.sName, power); GameMonster[i]=null; GameGround.remove(MonsterLabel[i]);
GameMain.java - 8 class GameActionListener implements ActionListener { public void actionperformed(actionevent e) { JButton b = (JButton)e.getSource(); if(b.gettext().equals(" 종료 ")) System.exit(0); public static void main(string[]args){ new GameMain();