기초JAVA 18강 - 게임만들기 발표

Whatever·2021년 10월 5일
0

기초 JAVA

목록 보기
18/26

내가 만든 게임 : Chicken Farm

public class Owner {

//돈(상태),레벨
int money;
int level;
int exp;
int chickenfood;
Items[] eggBox; //계란 담을 보관함
Items[] chickenBox;
int count; //계란 수 세는 변수

//모이를 가지고 있음 -> 닭은 모이를 먹어야 계란을 생성(모이--,계란++) -> 
//계란을 생성하면 주인이 계란을 아이템배열에 저장 -> 아이템이 꽉차면 팔아야한다는 메세지
//팔고 money 증가 -> 모이가 떨어지면 구매해야한다는 메세지 -> 모이는 하나에 10원

//생성자
Owner(){
	money = 0;
	level = 1;
	chickenfood = 10;
	eggBox = new Items[100];
	chickenBox = new Items[100];
	
}


//먹이주기
void feed(){
	--chickenfood;
	System.out.println("🐔에게 모이를 줬습니다.");
	
	if(chickenfood < 0){
		chickenfood = 0;
	}
	
}



//아이템창 - egg, 모이를 가지고 있는 창
void showInfo(){
    
	System.out.println("=====================================");
	System.out.println("현재 돈 💰: " + money + "원");
	System.out.println("보유한 모이 🥔: " + chickenfood + "개");
	countEggs();
	System.out.println("보유한 계란 수 🥚: " + count +  "개"); //내가 보유한 닭 추가하면 좋겠음
	System.out.println("=====================================");
	}

void countEggs(){
	count = 0;
	for(int i = 0; i < eggBox.length;i++){
		if(eggBox[i] != null){
			count++;
		}
	
	}	
}

void sellEggs(){//어떻게 만들어야하지??
// for(int i = 0; i < o.eggBox.length;i++){
// if("Egg".equals(o.eggBox[i].name)){
// o.eggBox[i] = null;
// o.money += 50; //총 gain이 얼마인지 더하기
// }
// }
//계란이 eggBox에 있으면 있는 만큼 팔아버리기/
int input1 = 0;
int input2 = 0;

	System.out.println("계란은 하나에 50원에 팔 수 있습니다, 계란을 파시겠습니까?");
	stop();
	System.out.println("1.예\t2.아니오");
	input1 = ScanUtil.nextInt();
	
	switch(input1) {
	case 1 :
		outer: for (int i = 0; i < eggBox.length; i++) {
			if (eggBox[i] != null) {
				eggBox[i] = null;
				money += 50;
				count--;
				System.out.println("🐔가 슬퍼합니다.");
				stop();
				System.out.println("계란을 팔았습니다. 현재 잔액은 " + money + "원 입니다.");
				stop();
				System.out.println("현재 계란 개수는 " + count + "개 입니다.");
				stop();
				if (count > 0) {
					sell: while (true) {
						System.out.println("계란을 더 팔까요? 1.예\t2.아니오");
						input2 = ScanUtil.nextInt();
						switch (input2) {
						case 1:
							break sell;
						case 2:
							break outer;
						default:
							break outer;
						}
					}
				}
			}
		}
	break;
	case 2 :
		break;
	default:
		break;

	}
}
void stop(){
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}

}

}

public class Chicken{

//레벨
String name;
int exp;
int level;
//상태
boolean status;
//영양상태?
String nutrition; 

Chicken(){
	nutrition = "허약한 상태";
	level = 1;
}


//모이먹기
void eatChickenFood(Owner o){
	o.feed();
}
//알 낳기
Egg makeEgg(){
  return new Egg();
}
//알 품기
Chicken beChicken(){
	return new Chicken();
}
//경험치 쌓기
void expUp(int exp){
	this.exp += exp;
	System.out.println(name + "의 경험치가 " + exp + " 올랐습니다.");
		while(this.exp >= 50){
	
		levelUp();
		this.exp -= 50;
		
		
	}
}

//레벨업
void levelUp(){
	level++;
	System.out.println("🐣🐣🐣 LEVEL UP 🐣🐣🐣");
	changeNutirition();
	if(level == 2){
		System.out.println("🐔의 상태가 "+ nutrition + "로 바뀌었습니다!!");
	}
	if(level == 5){
		System.out.println("🐔의 상태가 "+ nutrition + "로 바뀌었습니다!!");
	}
	if(level == 7){
		System.out.println("🐔의 상태가 "+ nutrition + "으로 바뀌었습니다!!");
	}
	if(level == 10){
		System.out.println("🐔의 상태가 "+ nutrition + "으로 바뀌었습니다!!");
	}
}
//닭 상태창 - 상태(기분), 경험치, 레벨
void showChickenInfo(){
	System.out.println("=====================================");
	System.out.println("🐓의 레벨 : " + level );
	System.out.println("🐓의 경험치 : " + exp + " / 50" );
	System.out.println("🐓의 영양상태: "+ nutrition );
	System.out.println("🐓의 기분: "+ showStatus() );
	System.out.println("=====================================");
}
//기분변경

void switchStatus(){
	status = !status;
}
String showStatus(){
	if(status){
		return "지금 🐔은 기분이 좋습니다.";
	}else{
		return"🐔은 기분이 안 좋습니다.";
	}
}

//영양상태 변경
void changeNutirition(){
	switch(level){
	case 2:
		nutrition = "보통 상태";
		
		break;
	case 5:
		nutrition = "튼튼한 상태";
		
		break;
	case 7:
		nutrition = "경도비만";
		
		break;
	case 10:
		nutrition = "뒤룩뒤룩";
		
		break;
	default :
		break;
	}
}
}


public class Egg extends Items{

Egg(){
	super("Egg",50);
}

//부화하기

}

public class GameRunning {
Owner o;
Chicken c;
Egg e;

GameRunning(){
    o = new Owner();
    c = new Chicken();

}
public static void main(String[] args) {

GameRunning game = new GameRunning();
game.start();
}


void start(){
    System.out.println("당신은 Chicken Farm🐓의 주인이 되었습니다.");
    stop();

c.name = nameMaking();
System.out.println(c.name + "에게 모이를 주면 달걀을 얻을 수 있습니다.");
stop();
System.out.println("모이는 10개가 제공되며, 이후에는 구매해야 합니다.");
stop();
stop();
int input = 0;
while(true){
	System.out.println("1.나의 재산보기 2."+ c.name + "의 정보보기 3."
    + c.name + "에게 모이주기 4.계란 팔기 5.모이 사기");
	input = ScanUtil.nextInt();
	switch(input){
	case 1://나의 재산보기
		o.showInfo();
		stop();
		break;
		
	case 2:	//닭 정보보기
		System.out.println(c.name + "의 정보를 봅니다.");
		stop();
		c.showChickenInfo();
		break;
		
	case 3://닭에게 모이주기	
		if(o.chickenfood <= 0){
			noChickenFood();
			break;
		}else{
		c.eatChickenFood(o);
		e = c.makeEgg();
		countSecond();
		stop();
		getEgg(o,c,e);
		stop();
		c.expUp(10);
		stop();
		//계란 = 10개가 넘으면 팔아야 합니다.
		break;}
		
	case 4://계란 팔기
		o.countEggs();
		
		if (o.count == 0){
			System.out.println("계란이 없습니다. 먼저 계란을 모아주세요.");
			stop();
		}else{
			o.sellEggs();
			c.status = false;
			stop();
		}
		break;
		
	case 5://모이 사기
		buyChickenFood();
		break;
	default :
		break;
	}
	
}
}
//모이사기
void buyChickenFood(){
    System.out.println(c.name + " 이(가) 먹을 모이를 삽니다. 모이는 1개에 10원 입니다.");
    stop();
    if(o.money < 10){
        System.out.println("돈이 부족합니다.");
        return;
    }
    buy : while(true){
        stop();
        System.out.println("몇 개를 살까요? 입력해주세요.");
    int input = 0;
        input = ScanUtil.nextInt();
        if(o.money < input * 10){
            System.out.println("돈이 부족합니다.");
        }else{
        o.chickenfood += input;
        o.money -= input * 10;
        System.out.println(input + "개의 모이를 샀습니다.");
        stop();
        System.out.println("🐔가 기뻐합니다.");
        c.status = true;
        stop();
        break buy;
        }
    }
}

//계란 수거
void getEgg(Owner o, Chicken c, Egg e){
    for(int i = 0; i <o.eggBox.length;i++){
        if(o.eggBox[i] == null){
            o.eggBox[i] = c.makeEgg();
            System.out.println("🥚");
            stop();
            System.out.println("계란을 1개 얻었습니다!");
            break;
        }
    }


}
//닭 이름지어주기
String nameMaking(){
    System.out.println("당신이 키울 닭의 이름을 입력해주세요.");
    return ScanUtil.nextLine();
}

//1초 멈추기
void stop(){
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

void countSecond(){
    stop();
    System.out.println(3);
    stop();
    System.out.println(2);
    stop();
    System.out.println(1);
}


//모이없어 notice
void noChickenFood(){
    System.out.println("모이가 없습니다. 모이를 구매하세요.");
}
}

0개의 댓글

관련 채용 정보