Created
February 19, 2021 01:49
-
-
Save mrgarita/a6e2a5d8ffa49a572af9ed215d46c56c to your computer and use it in GitHub Desktop.
C言語:文字ベースのRPG風プログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
xRpg4kai.c RPGゲーム:イベント処理を追加する | |
CodeName = GUNDAM | |
履歴 | |
2008-12-10 初期バージョン | |
2013-02-27 リアルタイムな移動ができるようにした | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include <conio.h> | |
#define SIZE 8 /* マップサイズ */ | |
/* --- キャラクタ表示 --- */ | |
#define NOBOX 0 | |
#define BOX 1 | |
#define HERO 2 | |
#define TREASURE 3 | |
#define DOOR 4 | |
#define KEY 5 | |
#define GOAL 9 | |
/* --- キー入力 --- */ | |
#define UP 'w' | |
#define DOWN 's' | |
#define LEFT 'a' | |
#define RIGHT 'd' | |
#define QUIT 'q' | |
/* --- イベントフラグ --- */ | |
#define NONE 0 | |
#define PASSWORD 1 | |
#define OWARI 999 | |
/* --- マップ --- */ | |
int map[SIZE][SIZE] = { {1, 1, 1, 1, 1, 1, 1, 1}, | |
{1, 0, 4, 0, 1, 1, 5, 1}, | |
{1, 1, 1, 0, 1, 1, 0, 1}, | |
{1, 0, 0, 0, 1, 1, 0, 1}, | |
{1, 0, 1, 3, 0, 0, 0, 1}, | |
{1, 0, 1, 0, 1, 1, 0, 1}, | |
{1, 3, 1, 0, 0, 0, 0, 1}, | |
{1, 1, 1, 1, 1, 1, 9, 1}, | |
}; | |
/* --- キャラクタ --- */ | |
struct CHARACTER { | |
char name[80]; | |
char mobilesuit[80]; | |
char password[80]; | |
int hp, mp; | |
int gold; /* お金 */ | |
int key; /* イベントフラグ(NONE: なし PASSWORD: パスワードを手に入れた OWARI: ゲームクリア可能) */ | |
}; | |
struct CHARACTER hero; | |
int x, y, saveX, saveY; /* 現在位置、移動前の位置 */ | |
/* --- 関数 --- */ | |
void init(void); /* 初期化 */ | |
void showMap(void); /* マップ表示 */ | |
int moveHero(void); /* 主人公の移動 */ | |
void checkMap(void); /* マップ移動範囲のチェック */ | |
void checkEvent(void); /* イベントのチェック */ | |
void setHero(void); /* 主人公の位置設定 */ | |
void showParameta(void); /* 主人公のパラメタ表示 */ | |
int makeRand(int, int); /* 乱数を発生させる(最小値, 最大値) */ | |
void showTitle(void); /* タイトルを表示 */ | |
void showEnding(void); /* エンディングを表示 */ | |
/* ========== RPGメイン ========== */ | |
int main(void) | |
{ | |
init(); | |
do{ | |
checkMap(); | |
checkEvent(); | |
setHero(); | |
system("cls"); | |
showParameta(); | |
showMap(); | |
} while(moveHero()); | |
return 0; | |
} | |
/* ----- 初期化 ----- */ | |
void init(void) | |
{ | |
srand( (unsigned) time(NULL)); /* 乱数系列を初期化 */ | |
/* タイトルっぽく表示 */ | |
showTitle(); | |
/* 主人公のパラメタ設定 */ | |
strcpy(hero.name, "カミーユ・ビダン"); | |
strcpy(hero.mobilesuit, "リックディアス"); | |
strcpy(hero.password, "百式"); | |
hero.hp = 30; | |
hero.mp = 15; | |
hero.gold = 0; | |
hero.key = NONE; | |
/* 主人公の初期位置設定 */ | |
x = saveX = 4; | |
y = saveY = 4; | |
setHero(); | |
} | |
/* ----- マップ表示 ----- */ | |
void showMap(void) | |
{ | |
int i, j; | |
for (i=0; i<SIZE; i++){ | |
for (j=0; j<SIZE; j++){ | |
switch(map[i][j]){ | |
case NOBOX: | |
case KEY: | |
printf(" "); | |
break; | |
case BOX: | |
printf("■"); | |
break; | |
case HERO: | |
printf("人"); | |
break; | |
case TREASURE: | |
printf("★"); | |
break; | |
case DOOR: | |
printf("□"); | |
break; | |
case GOAL: | |
printf("?"); | |
} | |
} | |
printf("\n"); | |
} | |
} | |
/* ----- 主人公の移動 ----- */ | |
int moveHero(void) | |
{ | |
char ido[256]; | |
int key; | |
printf("\nどこに向かう? W:上 S:下 A:左 D:右 Q:終了> "); | |
while (1) { | |
if ( kbhit() ) { // キー入力がされたら | |
key = getch() ; | |
saveX = x; | |
saveY = y; | |
if(key == UP){ | |
y--; | |
return 1; | |
} | |
else if(key == DOWN){ | |
y++; | |
return 1; | |
} | |
else if(key == LEFT){ | |
x--; | |
return 1; | |
} | |
else if(key == RIGHT){ | |
x++; | |
return 1; | |
} | |
else if(key == QUIT){ | |
return 0; | |
} | |
} | |
} | |
} | |
/* ----- マップ移動範囲のチェック ----- */ | |
void checkMap(void) | |
{ | |
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || map[y][x] == BOX){ | |
x = saveX; | |
y = saveY; | |
printf("\nそっちは無理だ!..."); | |
while(1){ // キー入力待ち | |
if(kbhit()) break; | |
} | |
} | |
} | |
/* ----- イベントのチェック ----- */ | |
void checkEvent(void) | |
{ | |
int ret; | |
char inputpassword[80]; | |
switch(map[y][x]){ | |
case TREASURE: /* 宝箱のとき */ | |
printf("\n金か!"); | |
getchar(); | |
printf("%dゴールド入っていた...", ret = makeRand(10, 20) ); | |
getchar(); | |
printf("今の俺には必要ないか..."); | |
getchar(); | |
hero.gold += ret; | |
break; | |
case DOOR: /* 扉があるとき */ | |
printf("\n扉がある..."); | |
getchar(); | |
if(hero.key == NONE){ | |
printf("扉にはパスワードが掛かっているようだ..."); | |
x = saveX; | |
y = saveY; | |
} | |
else if(hero.key == PASSWORD){ | |
printf("\n%sはパスワードを打ち込んだ!", hero.name); | |
scanf("%s", inputpassword); | |
if(!strcmp(inputpassword, hero.password)){ | |
hero.key = OWARI; | |
getchar(); | |
printf("【%s】扉が音をたてて開いた!\n", hero.password); | |
getchar(); | |
printf("チッ!何もないじゃないか。\n"); | |
} | |
else{ | |
getchar(); | |
printf("パスワードが違う!!!\n"); | |
x = saveX; | |
y = saveY; | |
} | |
} | |
getchar(); | |
break; | |
case KEY: /* パスワードキーをみつけたとき */ | |
printf("\n%s?...", hero.password); | |
getchar(); | |
printf("これがパスワードか..."); | |
getchar(); | |
printf("%sはパスワードを手に入れた!", hero.name); | |
hero.key = PASSWORD; | |
getchar(); | |
break; | |
case GOAL: /* ゴール */ | |
if(hero.key == OWARI){ | |
printf("\n!?!?!!!!!\n"); | |
getchar(); | |
showEnding(); | |
} | |
else{ | |
printf("\n何か見えない力が働いている..."); | |
getchar(); | |
} | |
x = saveX; | |
y = saveY; | |
} | |
} | |
/* ----- 主人公の位置設定 ----- */ | |
void setHero(void) | |
{ | |
map[saveY][saveX] = NOBOX; /* 移動前のデータを消去 */ | |
map[y][x] = HERO; /* 移動後のデータを設定 */ | |
} | |
/* ----- 主人公のパラメタ表示 ----- */ | |
void showParameta(void) | |
{ | |
printf("%s【%s】\n", hero.name, hero.mobilesuit); | |
printf("HP/%03d MP/%03d \n%3dG", hero.hp, hero.mp, hero.gold); | |
if(hero.key == PASSWORD) printf("\t\tパスワード: %c%c*", hero.password[0], hero.password[1]); | |
printf("\n\n"); | |
} | |
/* ----- 乱数を発生させる(最小値, 最大値) ----- */ | |
int makeRand(int min, int max) | |
{ | |
return( rand() % (max - min) + min + 1); | |
} | |
/* ----- タイトルを表示 ----- */ | |
void showTitle(void) | |
{ | |
system("cls"); | |
printf("\n\n\n\n\n\n\n\n\n\n\t\t\t君は、生き延びることが出来るか?\n"); | |
getchar(); | |
} | |
/* ----- エンディングを表示 ----- */ | |
void showEnding(void) | |
{ | |
system("cls"); | |
printf("\n\n\n\n\n\t\tその後、%sがどうなったのかは誰もしらない\n", hero.name); | |
getchar(); | |
printf("\n\n\n\n\n\t\t%sの機体だけが宇宙の彼方で発見された\n", hero.mobilesuit); | |
getchar(); | |
printf("\n\n\n\n\n\n\n\t\tSee you!"); | |
getchar(); | |
system("cls"); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment