C#/수업 내용

마린 메딕으로 치유하기 (class 이용)

Bueong_E 2023. 1. 5. 12:11
반응형
SMALL

App 실행단 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study04
{
    class App
    {
        //생성자
        public App()
        {
            Marine marine0 = new Marine("marine0", 60, 5);
            Marine marine1 = new Marine("marine0", 60, 30);

            marine0.Hit(marine0, marine1);

            Medic medic0 = new Medic(5.86f,50,200); //치료량,에너지(50/200)
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);
            medic0.Heal(marine0);



            //marine0 40/60
            Console.WriteLine("{0} {1}/{2}", marine0.name, marine0.hp, marine0.maxHp);

            //치료 할때마다 에너자를 5씩 사용한다.
            //medic0 45/200
        }
    }
}

마린

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study04
{
    class Marine
    {
        //맴버 변수 
         public string name;
         public int maxHp;
         public float hp;
         public int damage = 5;

        //생성자 "메서드
        public Marine(string name, int myHp, int damage)
        {
            //인스턴스 생성되고 생성자가 호출된다
            //this : 클래스의 현재 인스턴스
            //이름을 계속 남기고 싶다면 미리 한정자의 맴버 변수를 지정해주고 값을 저장시킨다.(인스턴스가 살아있는 동안 지속적으로 사용한다면.)
            this.name =  name;
            this.maxHp = myHp;
            this.hp = this.maxHp;
            this.damage = damage;
            Console.WriteLine("{0}이 생성되었습니다.{1}/{2}", name, myHp, maxHp);
           
        }
        //맴버 메서드
        public void Creat()
        {
   
        }
        public void Move()
        {
       
        }
        public void Attack(Marine target)
        {
 
            Console.WriteLine("{0} -> {1}을 공격 합니다", name, target.name);
            target.Hit(this,this);
            
            //target.Hit();
        }
        public void Hit(Marine target, Marine attacker)
        {

            Console.WriteLine(this.name);
            target.hp = maxHp - attacker.damage;
            Console.WriteLine("피해량 -{0}", attacker.damage);
            Console.WriteLine("{0} 남은 체력 {1}/{2}", target.name, target.hp, maxHp);

        }
    }
}

메딕

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study04
{
    class Medic
    {
        float healPow;
        int maxEnergy;
        int energy;
        

        //생성자
        public Medic(float healPow , int energy , int maxEnergy)
        {
            this.healPow = healPow;
            this.energy = energy;
            this.maxEnergy = maxEnergy;
            Console.WriteLine("메딕이 생성되었습니다.");
            Console.WriteLine("힐파워 : {0} 에너지 : {1}/{2})", this.healPow , this.energy, this.maxEnergy);

        }
        public void Heal(Marine target)
        {
            if (this.energy > 0)
            {
               
                target.hp = target.hp + this.healPow;
                this.energy = this.energy - 5;
                if (target.hp < target.maxHp)
                {

                    Console.WriteLine("힐을 사용하였습니다.  {0}의 체력 {1}/{2}", target.name, target.hp, target.maxHp);
                    Console.WriteLine("치유량 {0}", this.healPow);
                    Console.WriteLine("남은 에너지 {0}/{1}", this.energy, this.maxEnergy);
                }
                else if (target.hp > target.maxHp)
                {
                    if (this.energy == 0)
                    {
                        Console.WriteLine("에너지가 부족합니다.");
                    }
                    target.hp = target.maxHp;
                    Console.WriteLine("힐을 사용하였습니다. {0}의 체력 {1}/{2}", target.name, target.hp, target.maxHp);
                    Console.WriteLine("치유량 {0}", this.healPow);
                    Console.WriteLine("더이상 치유할수 없습니다.");
                    Console.WriteLine("남은 에너지 {0}/{1}", this.energy, this.maxEnergy);

                }
            }
            else if(this.energy == 0)
            {
                this.energy = 0;
                Console.WriteLine("에너지가 부족합니다.");
                Console.WriteLine("에너지가 부족합니다.");
                Console.WriteLine("남은 에너지 {0}/{1}", this.energy, this.maxEnergy);


            }


        }
    }
}

 

결과물

반응형
LIST