Class mathod
Console(콘솔)/실습 2019. 10. 2. 16:46메소드를 이용한 스타크래프트 예시
마린을 생성한후 마린1과 마린2를 싸우게한다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Syntax39 { class Program { static void Main(string[] args) { new App(); } } }
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Syntax39 { class App { public App() { Tower tower=new Tower(); tower.name = "배럭스"; Console.WriteLine(tower.name); tower.MakeUnit("마린1",10,100); tower.MakeUnit("마린2",10,100); Unit unit1=new Unit("마린1",10,100); Unit unit2 = new Unit("마린2",10,100); unit1.Attack(unit2); unit2.Hit(unit1); } } }
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Syntax39 { class Tower { public string name; public Tower() {
} public void MakeUnit(string name, int damage,int hp) { Unit unit=new Unit(name,damage,hp); unit.name = name; Console.WriteLine(name+"(이)가 생성되었습니다."); } } }
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Syntax39 { class Unit { public string name; public int hp; public int damage; public int maxhp;
public Unit(string name,int hp, int damage) { this.name = name; this.hp = hp; this.damage = damage; this.maxhp = this.hp;
} public void Attack(Unit taget) { this.hp = hp - this.damage; Console.WriteLine($"{this.name}(이)가 {taget.name}(을)를 공격하였습니다.");
} public void Hit(Unit taget) { this.hp = hp - this.damage; Console.WriteLine($"{this.name}(이)가 {taget.name}에게 공격당하였습니다."); Console.WriteLine($"{this.name}의 체력은 {this.hp}입니다."); } } }
|
'Console(콘솔) > 실습' 카테고리의 다른 글
UIPanel 제작 (0) | 2019.10.14 |
---|---|
인벤토리에 아이템 넣고 빼기 (0) | 2019.10.10 |
class mathod (0) | 2019.10.02 |
Claas를 이용한 버거 만들기 (0) | 2019.10.01 |
method문 사용 (0) | 2019.09.30 |