Text RPG 개발-2
2025. 9. 30. 21:03ㆍ개발
espace ConsoleApp;
using System.Collections.Generic;
class step7
{
static void Main()
{
//1번 문제
player player1 = new player("전사", 100);
player1.Attack();
player1.TakeDamage(30);
//2번문제
Monster slime = new Monster("슬라임", 1);
Monster goblin = new Monster("고블린", 3);
slime.DisplayInfo();
goblin.DisplayInfo();
goblin.hp = 10; //고블린 hp변경
goblin.DisplayInfo();
//3번 문제
Item sword = new Item("철검", 150);
Item potion = new Item("HP포션", 50);
potion.DisplayInfo();
sword.DisplayInfo();
sword.price = 200;
sword.DisplayInfo();
//4번문제
Skill fireball = new Skill("파이어볼", 50, 20);
Skill heal = new Skill("힐", -30, 15);
fireball.Activate();
heal.Activate();
fireball.damage = 60;
fireball.Activate();
//5번문제
Shop shop = new Shop();
shop.AddItem(new Item("HP포션",50));
shop.AddItem(new Item("MP포션",70));
shop.AddItem(new Item("철검",150));
shop.DisplayItems();
shop.items[0].price = 60;
shop.DisplayItems();
}
class player
{
public string name;
public int hp;
public player(string name, int hp)
{
this.name = name;
this.hp = hp;
}
public void Attack()
{
Console.WriteLine($"{name}이(가) 공격합니다");
}
public void TakeDamage(int damage)
{
hp -= damage;
Console.WriteLine($"{name}이(가) {damage}를 입음. 남은 HP:{hp}");
}
}
class Monster
{
public string name;
public int level;
public int hp; //필드
public Monster(string name, int level) // 생성자 추가
{
this.name = name;
this.level = level;
this.hp = 50;
}
public void DisplayInfo() // 메서드 추가
{
Console.WriteLine($"이름: {name}, 레벨: {level}, HP: {hp}");
}
}
class Item
{
public string name;
public int price; //필드 추가
public Item(string name, int price) //생성자 추가
{
this.name = name;
this.price = price; //this로 필드 초기화
}
public void DisplayInfo()
{
Console.WriteLine($"아이템: {name}, 가격: {price}G");
}
}
class Skill
{
public string name;
public int damage;
public int mpCost; //마나소모
public Skill(string name, int damage, int mpCost)
{
this.name = name;
this.damage = damage;
this.mpCost = mpCost; //필드 초기화
}
public void Activate()//매서드 추가
{
Console.WriteLine($"스킬 {name} 발동! 데미지: {damage}, MP 소모: {mpCost}");
}
}
class Shop
{
public List<Item> items; //리스트 복습..^^
public Shop()
{
items = new List<Item>(); //리스트 초기화
}
public void AddItem(Item item) //매서드 추가
{
items.Add(item);
Console.WriteLine($"상점에{item.name}아이템 추가됨");
}
public void DisplayItems()
{
Console.WriteLine("--- 상점 목록 ---");
foreach (var item in items)
{
item.DisplayInfo();
}
}
}
}



이렇게 포션 가격이 잘 변경된 것을 확인 할 수 있다.

'개발' 카테고리의 다른 글
| C# 스네이크게임 만들기 (0) | 2025.10.09 |
|---|---|
| Text RPG 개인과제 완료 (0) | 2025.10.01 |
| C#문법 종합 개인과제 [도전! 던전 탈출] (1) | 2025.09.29 |
| C#기초 공부 / void랑 class 차이점 (0) | 2025.09.28 |
| 팀프로젝트 코드 리뷰 (0) | 2025.09.26 |