Microsoft PowerPoint - 14주차 강의자료

Similar documents
gnu-lee-oop-kor-lec10-1-chap10

10장.key

Microsoft PowerPoint - 04-UDP Programming.ppt

11장.key

<4D F736F F F696E74202D20C1A63138C0E520C0CCBAA5C6AE20C3B3B8AE28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

<4D F736F F F696E74202D20C1A63139C0E520B9E8C4A120B0FCB8AEC0DA28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

9장.key

PowerPoint Presentation

<4D F736F F F696E74202D20C1A63233C0E520B1D7B7A1C7C820C7C1B7CEB1D7B7A1B9D628B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Java Programing Environment

Microsoft Word - java18-1-final-answer.doc

<4D F736F F F696E74202D20C1A63230C0E520BDBAC0AE20C4C4C6F7B3CDC6AE203128B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

강의자료

자바GUI실전프로그래밍2_장대원.PDF

05-class.key

No Slide Title

제8장 자바 GUI 프로그래밍 II

PowerPoint Presentation

public class FlowLayoutPractice extends JFrame { public FlowLayoutPractice() { super("flowlayout Practice"); this. Container contentpane = getcontentp

Microsoft PowerPoint - 06-Chapter09-Event.ppt

PowerPoint 프레젠테이션

<4D F736F F F696E74202D20C1A63236C0E520BED6C7C3B8B428B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint Presentation

rmi_박준용_final.PDF

슬라이드 1

<4D F736F F F696E74202D205B4A415641C7C1B7CEB1D7B7A1B9D65D3130C0E5C0CCBAA5C6AEC3B3B8AE>

Contents. 1. PMD ㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍ 2. Metrics ㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍ 3. FindBugs ㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍ 4. ㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍㆍ

PowerPoint 프레젠테이션

Chap12

PowerPoint 프레젠테이션

JAVA PROGRAMMING 실습 08.다형성

PowerPoint 프레젠테이션

JMF3_심빈구.PDF

PowerPoint 프레젠테이션

ch09

Spring Data JPA Many To Many 양방향 관계 예제

JAVA 프로그래밍실습 실습 1) 실습목표 - 메소드개념이해하기 - 매개변수이해하기 - 새메소드만들기 - Math 클래스의기존메소드이용하기 ( ) 문제 - 직사각형모양의땅이있다. 이땅의둘레, 면적과대각

iii. Design Tab 을 Click 하여 WindowBuilder 가자동으로생성한 GUI 프로그래밍환경을확인한다.

중간고사

Microsoft PowerPoint - Java7.pptx

PowerPoint Presentation

슬라이드 1

Cluster management software

PowerPoint Presentation

PowerPoint Presentation

슬라이드 1

Microsoft PowerPoint - ÀÚ¹Ù08Àå-1.ppt

(Microsoft PowerPoint - java2-lecture6.ppt [\310\243\310\257 \270\360\265\345])

PowerPoint 프레젠테이션

Microsoft PowerPoint - java1-lecture10.ppt [호환 모드]

5장.key

02 C h a p t e r Java

Microsoft PowerPoint - EEL2 Lecture10 -Swing and Event Handling.pptx

01-OOPConcepts(2).PDF

Microsoft PowerPoint - java2-lecture6.ppt [호환 모드]

쉽게 풀어쓴 C 프로그래밍

Microsoft Word - java19-1-final-answer.doc

자바 프로그래밍

(8) getpi() 함수는정적함수이므로 main() 에서호출할수있다. (9) class Circle private double radius; static final double PI= ; // PI 이름으로 로초기화된정적상수 public

오버라이딩 (Overriding)

Microsoft PowerPoint - java1-lab5-ImageProcessorTestOOP.pptx

09-interface.key

Design Issues

JAVA PROGRAMMING 실습 05. 객체의 활용

Microsoft PowerPoint - Java-03.pptx

PowerPoint Presentation

제11장 프로세스와 쓰레드

11-GUI.key

쉽게 풀어쓴 C 프로그래밍

12-file.key

PowerPoint 프레젠테이션

쉽게 풀어쓴 C 프로그래밊

JTable과 MVC(Model-View-Controller) 구조 - 모델-뷰-컨트롤러구조는데이터의저장과접근에대한제공은모델이담당하고, 화면표시는뷰, 이벤트의처리는컨트롤러가하도록각역할을구분한구조이다. 즉, 역할의분담을통하여상호간의영향을최소화하고각요소의독립성을보장하여독자

Java ...

쉽게 풀어쓴 C 프로그래밍

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

PowerPoint Presentation

신림프로그래머_클린코드.key

K&R2 Reference Manual 번역본

JAVA PROGRAMMING 실습 02. 표준 입출력

슬라이드 1

Spring Boot/JDBC JdbcTemplate/CRUD 예제

15 홍보담당관 (언론홍보담당) 김병호 ( 金 秉 鎬 ) 16 (행정담당) 박찬해 ( 朴 鑽 海 ) 예산담당관 17 (복지행정담당) 이혁재 ( 李 赫 在 ) 18 (보육담당) 주사 이영임 ( 李 泳 任 ) 기동근무해제. 19 (장애인담당) 박노혁 ( 朴 魯 爀 ) 기동

쉽게 풀어쓴 C 프로그래밍

PowerPoint Presentation

gnu-lee-oop-kor-lec06-3-chap7

@OneToOne(cascade = = "addr_id") private Addr addr; public Emp(String ename, Addr addr) { this.ename = ename; this.a

Mobile Service > IAP > Android SDK [ ] IAP SDK TOAST SDK. IAP SDK. Android Studio IDE Android SDK Version (API Level 10). Name Reference V

fundamentalOfCommandPattern_calmglow_pattern_jstorm_1.0_f…

class Sale void makelineitem(productspecification* spec, int qty) SalesLineItem* sl = new SalesLineItem(spec, qty); ; 2. 아래의액티비티다이어그램을보고 Java 또는 C ++,

PowerPoint Presentation

비긴쿡-자바 00앞부속

Microsoft PowerPoint - CSharp-10-예외처리

Microsoft Word - java19-1-midterm-answer.doc

[ 그림 8-1] XML 을이용한옵션메뉴설정방법 <menu> <item 항목ID" android:title=" 항목제목 "/> </menu> public boolean oncreateoptionsmenu(menu menu) { getme

Network Programming

Contents Contents 2 1 Abstract 3 2 Infer Checkers Eradicate Infer....

JUNIT 실습및발표

(Microsoft PowerPoint - LZVNQBAJWGTC.ppt [\310\243\310\257 \270\360\265\345])

10-Java Applet

Transcription:

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();