(1)Hero.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Hero
{
public int hp;
public int maxHp;
//생성자
public Hero()
{
this.hp = 10;
this.maxHp = this.hp;
}
//남은체력
public void HitDamage(int damage, Action<int, int> callback)
{
this.hp -= damage;
callback(this.hp, this.maxHp);
}
//죽었니 살았니
public void HitDamage(int damage, Action<bool> callback) {
this.hp -= damage;
callback(this.hp <= 0);
}
}
}
(2)App.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class App
{
//생성자
public App()
{
Hero hero = new Hero();
hero.HitDamage(3, (hp, maxhp) => {
Console.WriteLine("{0}/{1}", hp, maxhp);
});
hero.HitDamage(3, (isDie) => { Console.WriteLine("isDie: {0}", isDie); });
}
}
}
결과
'K-digital traning > C#프로그래밍' 카테고리의 다른 글
주말과제... (1) | 2023.07.31 |
---|---|
대리자 연습 - 데이터매니저 로드 (0) | 2023.07.27 |
대리자 연습 - Hero 이동 (0) | 2023.07.27 |
가짜 인벤토리 만들기 (0) | 2023.07.26 |
2차원 배열 연습 (0) | 2023.07.25 |