지난 학기에 짠 게임 프로그램 다시 한번 리뷰.
한달 동안 배운 내용을 가지고 간단한 게임 프로그램을 만들어보았다.
제출 이틀 전에 갈아엎고 차근차근 짜보았는데 한 두시간 헤매고 감 잡는데 성공했다.
솔직히 고생 좀 많이 했는데 에이쁠이라는 쾌거를...........하하.
아래는 그때 제출한 레포트 부가 설명이다.
레포트에 대한 부가적인 설명입니다.
기본적인 틀은 제가 처음부터 지금까지 짠 코드와 동일합니다.
게임 캐릭터가 보물과 같은 플러스 요소를 마주치면 스탯이 증가하고 위험요소를 마주치면 종료가 됩니다.
기본적인 틀은 유지하되 최근에 해리포터 소설을 읽어서 여기에서 영감을 얻어 캐릭터 이름과 세부적인 요소의 이름을 따왔습니다.
(그래서 게임 이름도 해리포터와 마법사의 돌로 정했습니다)
주인공 - 해리포터
초기 스탯 - 50
마법사의 돌 획득 시 - 스탯 +100
위험요소 - 볼드모트, 드래곤, 유니콘
1. 인터페이스
전체적인 틀만 짜준다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package report_HarryPotterGame;
public interface CharacterInter {
public void setName(String name);
public String getName();
public void setCha(char cha);
public char getCha();
public void setDistance(int distance);
public int getDistance();
public int getX();
public int getY();
public void moving();
}
|
cs |
2. 추상클래스
쉽게 생각하면 생성자를 만들어주고 인터페이스에서 선언한 것들을 구체화 시켜주면 된다.
인터페이스에서 만들어 놓은 것은 Override로 꼭 구현해 주어야 하지만 그 외의는 자유임
나는 해리포터 점수의 증감 구현 부분과 해리포터가 방해 요소와 만난 유무를 판단하는 boolean형을 추가로 구현.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package report_HarryPotterGame;
public abstract class Character implements CharacterInter{
protected String name;
protected char cha;
protected int xPos;
protected int yPos;
private int distance;
private int stat;
private int num;
public Character(String name, char cha, int xPos, int yPos, int distance) {
this.name = name;
this.cha = cha;
this.xPos = xPos;
this.yPos = yPos;
this.distance = distance;
}
@Override
public void setName(String name) {this.name = name;}
@Override
public String getName() {return name;}
@Override
public void setCha(char cha) {this.cha = cha;}
@Override
public char getCha() {return cha;}
@Override
public void setDistance(int distance) {this.distance = distance;}
@Override
public int getDistance() {return distance;}
@Override
public int getX() {return xPos;}
@Override
public int getY() {return yPos;}
@Override
public void moving() {}
//필요한 메소드 추가 구현.
public void plusStat() {stat += num;}
public int getStat(){return stat;}
public boolean collide(Character c) {
if(this.xPos == c.getX() && this.yPos == c.getY())
return true;
else return false;
}
}
|
cs |
3. Character 클래스 상속 받은 Elements.java - 방해 요소 구현 용.
처음에는 위험 요소 하나하나의 클래스를 따로 만들었지만 뒤늦게 그렇지 않아도 된다는 사실을 깨달았다.
그냥 Elements라는 클래스 하나만 만들고 메인 부분에서만 이름 위치 그냥 다 다르게 set 하면 되잖아??
ㅇㅇ..그래서 뭐.. 여기서 또 시간 좀 걸렸다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package report_HarryPotterGame;
public class Elements extends Character{
private int distance;
public Elements(String name, char cha, int xPos, int yPos, int distance) {
super(name, cha, xPos, yPos, distance);
}
@Override
public void setName(String name) {this.name = name;}
public String getName() {return name;}
public void setCha(char cha) {this.cha = cha;}
public char getCha() {return cha;}
@Override
public int getX() {return xPos;}
@Override
public int getY() {return yPos;}
@Override
public void moving() {
int now = (int)(Math.random()*5);
if (now == 1) {
xPos += distance;
}
else if (now == 0) {
yPos -= distance;
}
//범위 벗어났을 때.
if (xPos < 0) xPos = 0;
if (xPos >= 20) xPos = 19;
if (yPos < 0) yPos = 0;
if (yPos >= 20) yPos = 19;
}
}
|
cs |
4. 위와 동일하게 Character 클래스 상속받음. - 해리포터 구현 용
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package report_HarryPotterGame;
import java.util.Scanner;
public class HarryPotter extends Character{
Scanner sc = new Scanner(System.in);
private int stat;
private int num;
public HarryPotter(String name, char cha, int xPos, int yPos, int distance, int stat, int num) {
super(name, cha, xPos, yPos, distance);
this.stat = stat;
this.num = num;
}
@Override
public void setName(String name) {this.name = name;}
@Override
public String getName() {return name;}
@Override
public int getX() {return xPos;}
@Override
public int getY() {return yPos;}
public void plusStat() {stat += num;}
public int getStat() {return stat;}
@Override
public void moving() {
System.out.println("왼쪽(a) 오른쪽(d) 아래(s) 위(w)");
char c = sc.next().charAt(0);
switch (c) {
case 'a':
xPos--;
if (xPos < 0) xPos = 0;
break;
case 'd':
xPos++;
if (xPos >= 20) xPos = 20;
break;
case 's':
yPos++;
if (yPos >= 20) yPos = 20;
break;
case 'w':
yPos--;
if (yPos < 0) yPos = 0;
break;
}
}
}
|
cs |
5. run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
package report_HarryPotterGame;
public class runGame {
private static final int MAX_Y = 20;
private static final int MAX_X = 20;
private char map[][] = new char[MAX_Y][MAX_X];
private Character [] what = new Character[5];
public runGame() {
for(int y = 0; y < MAX_Y; y++)
for(int x = 0; x < MAX_X ; x++)
map[y][x] = '-';
what[0] = new HarryPotter("HarryPotter",'H',9,7,6,50,100);
what[1] = new Elements("Voldemort", 'V', 9,8,4);
what[2] = new Elements("Dragon",'D', 2,3,2);
what[3] = new Elements("Unicorn",'U',14,12,5);
what[4] = new Elements("SorcererStone",'★', 10,10,2);
}
void run() {
System.out.println("GAME : Harry Potter and Sorcerer's Stone"
+ "\n부제 : 볼드모트와 유니콘, 드래곤을 피해 마법사의 돌을 획득하라");
System.out.println("현재 해리포터의 능력치: " + what[0].getStat()
+ "\n마법사의 돌 획득 -> 능력치 +100");
update();
draw();
while(!isHarryFail()) {
clear();
for(int i = 0; i < what.length; i++)
what[i].moving();
update();
draw();
if(isFindStone()) {
what[0].plusStat();
System.out.println("마법사의 돌 발견! [해리포터 능력치 +100]");
System.out.println("현재 능력치: " + what[0].getStat());
}
}
collideWhat();
System.out.println("해리포터가 결국 "+ collideWhat()
+ " 와 마주쳤습니다.\n해리포터의 여정 끝.");
}
void update() {
for(int i = what.length-1; i>=0;i--)
map[what[i].getY()][what[i].getX()] = what[i].getCha();
}
void clear() {
for(int i =0; i<what.length; i++)
map[what[i].getY()][what[i].getX()] = '-';
}
void draw() {
System.out.println();
for(int y = 0; y < MAX_Y ; y++) {
for(int x = 0; x < MAX_X ; x++)
System.out.print(map[y][x]);
System.out.println();
}
}
boolean isHarryFail() {
if(what[0].collide(what[1]) || what[0].collide(what[2]) || what[0].collide(what[3]))
return true;
else
return false;
}
String collideWhat() {
int index = 0;
for(int i = 1; i<what.length-1;i++)
if(what[0].collide(what[i])) index = i;
return what[index].getName();
}
boolean isFindStone() {
if (what[0].collide(what[4])) return true;
else return false;
}
public static void main(String[] args) {
runGame rg = new runGame();
rg.run();
}
}
|
cs |
'Language > JAVA' 카테고리의 다른 글
파스칼의 삼각형 (0) | 2022.01.04 |
---|---|
Collector / Collection FrameWork (0) | 2021.12.12 |
베타테스트 문제 (0) | 2021.12.10 |
Multi Thread Examples (0) | 2021.11.24 |
Interface (0) | 2021.11.04 |