본문 바로가기

PROJECT/포켓몬

[토이/자바] 포켓몬 게임 만들기(6) : 포켓몬

 

포켓몬 게임을 하기 위해서는 포켓몬 데이터가 존재해야 한다. 우선 크게 포켓몬으로 정의내릴 수 있는 class가 필요하고, 그 중에서 나의 포켓몬만 골라서 재정의할 수 있는 자식 class가 필요하다.

 

Monster class

인스턴스 변수/생성자

포켓몬 번호, 이름, 타입, 레벨, 체력, 공격력, 방어력, 스피드, 스킬로 구성되어 있다.

getter 메서드들은 자동 생성했고 너무 길어서 첨부는 생략한다.

public class Monster {
	int mnum;
	String name;
	String type;
	int level;
	int hp;
	int atk;
	int def;
	int speed;
	String skill;
	Monster(){}
	Monster(int mnum, String name, String type, int level, int hp, int atk, int def, int speed, String skill){
		this.mnum = mnum;
		this.name = name;
		this.type = type;
		this.level = level;
		this.hp = hp;
		this.atk = atk;
		this.def = def;
		this.speed = speed;
		this.skill = skill;
	}

 

 

포켓몬 객체 생성

포켓몬 데이터로 사용할 객체들을 선언해주었다. static 초기화블럭 안에 넣어서 게임이 실행될 때 자동으로 객체를 생성해주는 방식을 사용했다. static 초기화블럭은 main 메서드 실행 전에 우선적으로 단 한번만 실행되므로 초기화 블럭 안에 넣어서 불러오는 방식을 선택했다.

public static Monster gm1;
	public static Monster gm2;
	public static Monster gm3;
	public static Monster gm4;
	public static Monster wm5;
	public static Monster wm6;
	public static Monster wm7;
	public static Monster wm8;
	public static Monster fm9;
	public static Monster fm10;
	public static Monster fm11;
	public static Monster fm12;
	public static Monster dm13;
	public static Monster dm14;
	public static Monster dm15;
	public static Monster dm16;
	static {
		//풀타입 Grass 
		gm1 = new Monster(1, "이상해씨", "풀", 1, 300, 50, 20, 50, "덩쿨채찍");
		gm2 = new Monster(2, "캐터피", "풀", 1, 300, 50, 20, 50, "실뿜기");
		gm3 = new Monster(3, "니드런", "풀", 1, 300, 50, 20, 50, "뿔치기");
		gm4 = new Monster(4, "탕구리", "풀", 1, 300, 50, 20, 50, "뼈다귀치기");
		//물타입 Water
		wm5 = new Monster(5, "꼬부기", "물", 1, 300, 50, 20, 50, "덩쿨채찍");
		wm6 = new Monster(6, "브이젤", "물", 1, 300, 50, 20, 50, "덩쿨채찍");
		wm7 = new Monster(7, "마린", "물", 1, 300, 50, 20, 50, "덩쿨채찍");
		wm8 = new Monster(8, "샤미드", "물", 1, 300, 50, 20, 50, "덩쿨채찍");
		//불타입 Fire
		fm9 = new Monster(9, "파이리", "불", 1, 300, 50, 20, 50, "덩쿨채찍");
		fm10 = new Monster(10, "윈디", "불", 1, 300, 50, 20, 50, "덩쿨채찍");
		fm11 = new Monster(11, "암멍이", "불", 1, 300, 50, 20, 50, "덩쿨채찍");
		fm12 = new Monster(12, "포니타", "불", 1, 300, 50, 20, 50, "덩쿨채찍");
		//드래곤타입 Dragon
		dm13 = new Monster(13, "망나뇽", "드래곤", 1, 300, 50, 20, 50, "덩쿨채찍");
		dm14 = new Monster(14, "한카리아스", "드래곤", 1, 300, 50, 20, 50, "덩쿨채찍");
		dm15 = new Monster(15, "마기라스", "드래곤", 1, 300, 50, 20, 50, "덩쿨채찍");
		dm16 = new Monster(16, "삼삼드래", "드래곤", 1, 300, 50, 20, 50, "덩쿨채찍");

		System.out.println("ai몬 생성");
	}

 

 

울부짖는 소리

포켓몬은 말을 하는 대신 자신의 이름을 소리로 낸다

추가로 toSting 메서드는 이름으로 반환하도록 했다

public String toSting() {
		return name;
	}
	
	public String howl(ArrayList<Monster> my_mons) {
		int a = (int)(Math.random()*3+1);
		int b = (int)(Math.random()*(my_mons.size()-1));
		String c = "";
		switch(a) {
		case 1 : 
			c = my_mons.get(b).getMname()+" : "+my_mons.get(b).getMname()+"~!!";
			break;
		case 2 : 
			c = my_mons.get(b).getMname()+" : "+my_mons.get(b).getMname()+"~?";
			break;
		case 3 : 
			c = my_mons.get(b).getMname()+" : "+"크아앙";
			break;
		default : 
			return c;
		}
		return c;
	}

 

 

배틀 시스템

포켓몬 간의 전투는 포켓몬 게임에서 가장 기초적이고 핵심적인 부분이다

1. 스피드가 더 빠른 몬스터가 선공권을 가진다

2. satk(special attack) 급소 때리기. 20% 확률로 급소를 때릴 수 있고 이때는 데미지가 두배로 들어간다

3. 데미지 공식은 (현재체력) = (현재체력)+(방어력/2)-(공격력)이다.

4. 체력이 먼저 0 이하로 내려간 쪽이 패배 판정

5. 승리하면 승리한 쪽의 메세지를 출력한다

	void battle(Monster a, Monster b) throws InterruptedException {
		int ahp = a.hp;
		int bhp = b.hp;
		int satk = 0;
		System.out.println(a.name+"(이/가) "+b.name+"에게 싸움을 걸어왔다!");
		Thread.sleep(1000);
		while(ahp>0 || bhp>0) {
			if(a.speed > b.speed) {
				System.out.println(a.name+"의 "+a.skill+"!");
				Thread.sleep(1000);
				bhp = bhp + b.def/2 - a.atk;
				satk = (int)(Math.random()*5)+1;
				if(satk==1) {
					bhp = bhp + b.def/2 - a.atk;
					System.out.println("!!!!급소에 맞았다!!!!");
					Thread.sleep(1000);
					System.out.println((bhp>0)?b.name+"의 체력이"+bhp+" 남았다!":b.name+"(이/가) 쓰러졌다!");
					Thread.sleep(1000);
				}else {
					System.out.println((bhp>0)?b.name+"의 체력이"+bhp+" 남았다!":b.name+"(이/가) 쓰러졌다!");
					Thread.sleep(1000);
				}
				if(ahp<=0 || bhp<=0) {break;}
				System.out.println(b.name+"의 "+b.skill+"!");
				Thread.sleep(1000);
				ahp = ahp + a.def/2 - b.atk;
				satk = (int)(Math.random()*5)+1;
				if(satk==1) {
					ahp = ahp + a.def/2 - b.atk;
					System.out.println("!!!!급소에 맞았다!!!!");
					Thread.sleep(1000);
					System.out.println((ahp>0)?a.name+"의 체력이"+ahp+" 남았다!":a.name+"(이/가) 쓰러졌다!");
					Thread.sleep(1000);
				}else {
					System.out.println((ahp>0)?a.name+"의 체력이"+ahp+" 남았다!":a.name+"(이/가) 쓰러졌다!");
					Thread.sleep(1000);
				}
				if(ahp<=0 || bhp<=0) {break;}
			}else {
				System.out.println(b.name+"의 "+b.skill+"!");
				Thread.sleep(1000);
				ahp = ahp + a.def/2 - b.atk;
				satk = (int)(Math.random()*5)+1;
				if(satk==1) {
					ahp = ahp + a.def/2 - b.atk;
					System.out.println("!!!!급소에 맞았다!!!!");
					Thread.sleep(1000);
					System.out.println((ahp>0)?a.name+"의 체력이"+ahp+" 남았다!":a.name+"(이/가) 쓰러졌다!");
					Thread.sleep(1000);
				}else {
					System.out.println((ahp>0)?a.name+"의 체력이"+ahp+" 남았다!":a.name+"(이/가) 쓰러졌다!");
					Thread.sleep(1000);
				}
				if(ahp<=0 || bhp<=0) {break;}
				System.out.println(a.name+"의 "+a.skill+"!");
				Thread.sleep(1000);
				bhp = bhp + b.def/2 - a.atk;
				satk = (int)(Math.random()*5)+1;
				if(satk==1) {
					bhp = bhp + b.def/2 - a.atk;
					System.out.println("!!!!급소에 맞았다!!!!");
					System.out.println((bhp>0)?b.name+"의 체력이"+bhp+" 남았다!":b.name+"(이/가) 쓰러졌다!");
					Thread.sleep(1000);
				}else {
					System.out.println((bhp>0)?b.name+"의 체력이"+bhp+" 남았다!":b.name+"(이/가) 쓰러졌다!");
					Thread.sleep(1000);
				}
				if(ahp<=0 || bhp<=0) {break;}
			}
		}
		Thread.sleep(1000);
		System.out.println((ahp>bhp)?a.name+"승리":b.name+"승리");
		System.out.println("===배틀이 종료되었습니다===");
	}

 

 

 

MyMonster class

나의 포켓몬을 분류하는 클래스

Monster class를 상속받으며 추가된 인스턴스 변수로는 친밀도와 피로도가 있다

 

나의 포켓몬 List에 포켓몬을 추가하는 addMyMonster 메서드를 보유하고 있다

class MyMonster extends Monster{
	User owner = new User();
	String ownername = owner.my_name;
	int friendly;
	int tired;
	MyMonster(){}
	MyMonster(int unum, String ownername, int mnum, String name, String type, int hp, int atk, int def, int speed, String skill, int friendly, int tired){
		super();
		this.friendly = friendly;
		this.tired = tired;
	}
	void addMyMonster(ArrayList<MyMonster>my_mons, MyMonster a) {
		my_mons.add(a);
	}
	public String toString() {
		return name;
	}
	public String Info() {
		return "이름:"+name+" 어버이:"+ownername+" 타입:"+type+"\n레벨:"+level+" 친밀도:"+friendly+" 피로도:"+tired;
	}
	public String deepInfo() {
		return "체력:"+hp+" 공격력:"+atk+" 방어력:"+def+"스피드:"+speed+"\n스킬:"+skill;
	}