본문 바로가기
프로그래밍/C 언어(코딩)

C언어] RPG게임만들기-6 (아이템 생성)

by 곰나나 2024. 1. 2.

[RPG게임만들기]

RPG게임만들기 아이템 만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>  // strcpy 함수를 사용하기 위한 헤더 추가
 
//& 플레이어 구조체 정의
typedef struct {
    //& 멤버 변수(플레이어 정보 설정)
    int HP;
    int MP;
    double physicalAttack;
    double magicalAttack;
    int strength;
    int agility;
    int luck;
    int magicPower;
    double defense;
    double resistance;
    double speed;
    double critical;
} Player;
 
//& 적(몬스터) 구조체 정의
typedef struct {
    //& 멤버 변수 (몬스터 정보 설정)
    int grade;
    int HP;
    int MP;
    double physicalAttack;
    double magicalAttack;
    int strength;
    int agility;
    int luck;
    int magicPower;
    double defense;
    double resistance;
    double speed;
    double critical;
} Monster;
 
//& 아이템 유형 열거형 정의
typedef enum {
    WEAPON,
    HELMET,
    ARMOR,
    GLOVES,
    SHOES,
    EARRING,
    RING,
    BRACELET
} ItemType;
 
//& 아이템 구조체 정의
typedef struct {
    ItemType type;
    char name[50];  // 아이템 이름 추가
    int attack;
    int defense;
    int resistance;
    int strength;
    int agility;
    int magicPower;
    int luck;
    int speed;
    int price;
} Item;
 
//& 아이템 유형 문자열 배열
const char* itemTypeNames[] = { "무기""투구""갑옷""장갑""신발""귀걸이""반지""팔찌" };
 
//& 플레이어 속성 계산 함수
void calculateAttributes(Player* player)
{
    //& 물리 공격력 = (기본 1)+(힘 * 3) / 10.0
    player->physicalAttack = (1+ (player->strength * 3/ 10.0;
 
    //& 마법 공격력 = (기본0.5)+(마력 * 2) / 8.0
    player->magicalAttack = (0.5+ (player->magicPower * 2/ 8.0;
 
    //& 방어력 = 5 + (힘 / 15)
    player->defense = 5 + (player->strength / 15.0);
 
    //& 저항력 = 3 + (마력 / 20)
    player->resistance = 3 + (player->magicPower / 20.0);
 
    //& 스피드 = 1 * (민첩 / 5)
    player->speed = 1 * (player->agility / 5.0);
 
    //& 크리티컬 = 0 * (행운 / 2)
    player->critical = 0 * (player->luck / 2.0);
}
 
//& 몬스터 속성 계산 함수
void calculateMonsterAttributes(Monster* monster)
{
    //& 물리 공격력 = (힘 * 1) / 10.0
    monster->physicalAttack = (monster->strength * 5/ 10.0;
 
    //& 마법 공격력 = (마력 * 0.2) / 8.0
    monster->magicalAttack = (monster->magicPower * 2/ 8.0;
 
    //& 방어력 = (등급 * 3) + (힘 / 15)
    monster->defense = (monster->grade * 3+ (monster->strength / 15.0);
 
    //& 저항력 = (등급 * 1) + (마력 / 20)
    monster->resistance = (monster->grade * 1+ (monster->magicPower / 20.0);
 
    //& 스피드 = 1 * (민첩 / 5)
    monster->speed = 1 * (monster->agility / 5.0);
 
    //& 크리티컬 = 0 * (행운 / 2)
    monster->critical = 0 * (monster->luck / 2.0);
}
 
//& 아이템 생성 함수
Item createItem(ItemType type, const char* name, int attack, int defense, int resistance,
    int strength, int agility, int magicPower, int luck,
    int speed, int price)
{
    Item newItem;
    newItem.type = type;
    strcpy_s(newItem.name, sizeof(newItem.name), name);  // 이름 설정
    newItem.attack = attack;
    newItem.defense = defense;
    newItem.resistance = resistance;
    newItem.strength = strength;
    newItem.agility = agility;
    newItem.magicPower = magicPower;
    newItem.luck = luck;
    newItem.speed = speed;
    newItem.price = price;
 
    return newItem;
}
 
//& 플레이어 정보 표시 함수
void displayPlayerInfo(Player* player)
{
    printf("\n=== 플레이어 정보 ===\n");
    printf("HP: %d\n", player->HP);
    printf("MP: %d\n", player->MP);
    printf("물리 공격력: %lf\n", player->physicalAttack);
    printf("마법 공격력: %lf\n", player->magicalAttack);
    printf("방어력: %lf\n", player->defense);
    printf("저항력: %lf\n", player->resistance);
    printf("스피드: %lf\n", player->speed);
    printf("크리티컬: %lf\n", player->critical);
}// displayPlayerInfo
 
//& 몬스터 정보 표시 함수
void displayMonsterInfo(Monster* monster)
{
    printf("\n=== 몬스터 정보 ===\n");
    printf("몬스터 등급 : %d\n", monster->grade);
    printf("HP: %d\n", monster->HP);
    printf("MP: %d\n", monster->MP);
    printf("물리 공격력: %lf\n", monster->physicalAttack);
    printf("마법 공격력: %lf\n", monster->magicalAttack);
    printf("방어력: %lf\n", monster->defense);
    printf("저항력: %lf\n", monster->resistance);
    printf("스피드: %lf\n", monster->speed);
    printf("크리티컬: %lf\n", monster->critical);
}//displayMonsterInfo
 
//& 아이템 정보 표시 함수
void displayItemInfo(Item* item)
{
    printf("\n=== 아이템 정보 ===\n");
    printf("이름: %s\n", item->name);
    printf("유형: %s\n", itemTypeNames[item->type]);
    printf("공격력: %d\n", item->attack);
    printf("방어력: %d\n", item->defense);
    printf("저항력: %d\n", item->resistance);
    printf("힘: %d\n", item->strength);
    printf("민첩: %d\n", item->agility);
    printf("마력: %d\n", item->magicPower);
    printf("행운: %d\n", item->luck);
    printf("스피드: %d\n", item->speed);
    printf("가격: %d\n", item->price);
}// displayItemInfo
 
//& 몬스터 공격 함수
void monsterAttack(Player* player, Monster* monster)
{
    printf("몬스터가 플레이어를 공격합니다!\n");
 
    int isPhysicalAttack = rand() % 2// 0 또는 1 중 랜덤 선택
 
    if (isPhysicalAttack)
    {
        //& 몬스터가 물리 공격을 선택한 경우
        int monsterPhysicalDamage = monster->physicalAttack - player->defense;
        if (monsterPhysicalDamage < 0)
        {
            monsterPhysicalDamage = 0;
        }
        player->HP -= monsterPhysicalDamage;
        printf("플레이어에게 %d만큼의 물리 피해를 입혔습니다.\n", monsterPhysicalDamage);
    }
    else
    {
        //& 몬스터가 마법 공격을 선택한 경우
        int monsterMagicalDamage = monster->magicalAttack - player->resistance;
        if (monsterMagicalDamage < 0)
        {
            monsterMagicalDamage = 0;
        }
        player->HP -= monsterMagicalDamage;
        printf("플레이어에게 %d만큼의 마법 피해를 입혔습니다.\n", monsterMagicalDamage);
    }
}//monsterAttack
 
//& 전투 함수
void battle(Player* player, Monster* monster)
{
    int choice;
    int physicalDamage, magicalDamage; // 변수 초기화 추가
 
    do {
        printf("\n=== 전투 메뉴 ===\n");
        printf("1. 공격\n");
        printf("2. 마법\n");
        printf("3. 도망\n");
        printf("선택: ");
        scanf_s("%d"&choice);
 
        system("cls"); // 화면 지우기
 
        printf("[%d] 등급 몬스터 등장\n", monster->grade);
 
        switch (choice)
        {
        case 1// 공격
            printf("플레이어가 몬스터를 공격합니다!\n");
            physicalDamage = player->physicalAttack - monster->defense / 10;
            if (physicalDamage < 0)
            {
                physicalDamage = 0;
            }
            monster->HP -= physicalDamage;
            printf("몬스터에게 %d만큼의 물리 피해를 입혔습니다.\n", physicalDamage);
            break;
        case 2// 마법
            printf("플레이어가 몬스터에게 마법을 사용합니다!\n");
            magicalDamage = player->magicalAttack - monster->resistance / 5;
            if (magicalDamage < 0)
            {
                magicalDamage = 0;
            }
            monster->HP -= magicalDamage;
            printf("몬스터에게 %d만큼의 마법 피해를 입혔습니다.\n", magicalDamage);
            break;
        case 3// 도망
            printf("도망쳤습니다. 전투를 종료합니다.\n");
            return;
        default:
            printf("잘못된 선택입니다. 다시 선택하세요.\n");
        }
 
        //& 몬스터 공격 함수 
        monsterAttack(player, monster);
 
        // 플레이어와 몬스터의 HP 출력
        printf("플레이어 HP: %d, 몬스터 HP: %d\n", player->HP, monster->HP);
 
        // 전투 종료 조건 확인
        if (player->HP <= 0)
        {
            printf("플레이어가 전투에서 패배했습니다.\n");
            return;
        }
        else if (monster->HP <= 0)
        {
            printf("몬스터를 처치했습니다. 전투에서 승리했습니다!\n");
            return;
        }
    } while (1);
}//battle
 
//& 사냥터 함수
void hunt(Player* player, int minGrade, int maxGrade)
{
    printf("=== 사냥터에 입장했습니다. ===\n");
 
    //& 몬스터 등급은 minGrade부터 maxGrade 사이의 랜덤 값
    int monsterGrade = rand() % (maxGrade - minGrade + 1+ minGrade;
 
    //& 몬스터 생성 및 초기화
    Monster monster;
    monster.grade = monsterGrade;
    monster.HP = 80 + monster.grade * 20;
    monster.MP = 40 + monster.grade * 10;
    monster.strength = monster.grade * 5;
    monster.agility = monster.grade * 5;
    monster.luck = monster.grade * 5;
    monster.magicPower = monster.grade * 2;
    calculateMonsterAttributes(&monster);
 
    //& 전투 실행
    battle(player, &monster);
}//hunt
 
int main()
{
    //& 난수 발생을 위한 시드 설정
    srand((unsigned int)time(NULL));
 
    //& 플레이어 구조체 생성 및 초기화
    Player player;
    player.HP = 100;
    player.MP = 50;
    player.strength = 10;
    player.agility = 10;
    player.luck = 10;
    player.magicPower = 5;
    calculateAttributes(&player);
 
    //& 선택 변수 선언
    int choice;
    int minGrade, maxGrade;
 
    do {
        printf("\n=== 메뉴 ===\n");
        printf("1. 플레이어 정보 표시\n");
        printf("2. 사냥터 선택\n");
        printf("3. 종료\n");
        printf("선택: ");
        scanf_s("%d"&choice);
 
        switch (choice)
        {
        case 1:
            displayPlayerInfo(&player);
            break;
        case 2:
            printf("=== 사냥터 선택 ===\n");
            printf("1. 하급 사냥터 (1~3 등급)\n");
            printf("2. 중급 사냥터 (4~9 등급)\n");
            printf("3. 고급 사냥터 (10~19 등급)\n");
            printf("4. 종료\n");
            printf("선택: ");
            scanf_s("%d"&choice);
 
            switch (choice)
            {
            case 1:
                minGrade = 1;
                maxGrade = 3;
                hunt(&player, minGrade, maxGrade);
                break;
            case 2:
                minGrade = 4;
                maxGrade = 9;
                hunt(&player, minGrade, maxGrade);
                break;
            case 3:
                minGrade = 10;
                maxGrade = 19;
                hunt(&player, minGrade, maxGrade);
                break;
            case 4:
                printf("프로그램을 종료합니다.\n");
                break;
            default:
                printf("잘못된 선택입니다. 다시 선택하세요.\n");
            }
            break;
        case 3:
            printf("프로그램을 종료합니다.\n");
            break;
        case 7:
            //& 아이템 생성 및 초기화
            Item weapon = createItem(WEAPON, "Test 무기"350010000050);
            Item helmet = createItem(HELMET, "Test 헷멧"0530000030);
 
            //& 아이템 정보 출력
            displayItemInfo(&weapon);
            displayItemInfo(&helmet);
        default:
            printf("잘못된 선택입니다. 다시 선택하세요.\n");
        }
    } while (choice != 3);
 
    return 0;
}// main
cs

아이템 생성 함수 추가
아이템은 이름, 유형, 공격력, 방어력, 자항력, 힘, 민첩, 마력, 행운, 스피드, 판매 가격 등 옵션
추후 유형에 맞는 옵션만 표시

유형은 "무기", "투구", "갑옷", "장갑", "신발", "귀걸이", "반지", "팔찌" 추가

728x90