#2
by 参照魚 » 4年前
コマンドウインドウ上の逐次実行での作りになります。DXLib上のゲームループに置き換えるにはかなり工夫が必要になります。
コード:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int hitpoint_monster;
int hitpoint_player;
int damage_monster;
int damage_player;
int command;
srand( time( NULL ) );
while ( 1 ) {
printf( "モンスターが現れた\n" );
hitpoint_monster = rand()%50 + 50; // モンスターの体力は50-100の乱数
hitpoint_player = 100; // プレイヤーの体力は100に設定。
printf( "モンスターの体力:%d\n", hitpoint_monster );
printf( "プレイヤーの体力:%d\n", hitpoint_player );
while ( 1 ) {
// 攻撃か逃げるかは1と2で選択できる
printf( "実行するコマンドを入力してください(1:攻撃,2:逃げる):" );
scanf( "%d", &command );
if ( command == 2 ) {
printf( "モンスターから逃げました\n" );
break;
}
// 攻撃は10-30の乱数
damage_monster = rand()%20+10;
damage_player = rand()%20+10;
printf( "プレイヤーの攻撃:%dのダメージ\n", damage_player );
printf( "モンスターの攻撃:%dのダメージ\n", damage_monster );
hitpoint_monster -= damage_monster;
hitpoint_player -= damage_player;
printf( "モンスターの体力:%d\n", hitpoint_monster );
printf( "プレイヤーの体力:%d\n", hitpoint_player );
// 勝ち負けが表示できる。
if ( hitpoint_player <= 0 ||
hitpoint_monster <= 0 ) {
break;
}
}
// プログラムの終了条件:プレイヤーの体力が0
if ( hitpoint_player <= 0 ) {
break;
}
// 戦闘に勝利した時、逃げたときは新たなモンスターと戦闘するか聞く。
printf( "新たなモンスターと戦闘しますか(1:はい,2:いいえ):" );
scanf( "%d", &command );
// プログラムの終了条件:新たなモンスターと戦闘しないとき
if ( command == 2 ) {
break;
}
}
return 0;
}
コマンドウインドウ上の逐次実行での作りになります。DXLib上のゲームループに置き換えるにはかなり工夫が必要になります。
[code]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int hitpoint_monster;
int hitpoint_player;
int damage_monster;
int damage_player;
int command;
srand( time( NULL ) );
while ( 1 ) {
printf( "モンスターが現れた\n" );
hitpoint_monster = rand()%50 + 50; // モンスターの体力は50-100の乱数
hitpoint_player = 100; // プレイヤーの体力は100に設定。
printf( "モンスターの体力:%d\n", hitpoint_monster );
printf( "プレイヤーの体力:%d\n", hitpoint_player );
while ( 1 ) {
// 攻撃か逃げるかは1と2で選択できる
printf( "実行するコマンドを入力してください(1:攻撃,2:逃げる):" );
scanf( "%d", &command );
if ( command == 2 ) {
printf( "モンスターから逃げました\n" );
break;
}
// 攻撃は10-30の乱数
damage_monster = rand()%20+10;
damage_player = rand()%20+10;
printf( "プレイヤーの攻撃:%dのダメージ\n", damage_player );
printf( "モンスターの攻撃:%dのダメージ\n", damage_monster );
hitpoint_monster -= damage_monster;
hitpoint_player -= damage_player;
printf( "モンスターの体力:%d\n", hitpoint_monster );
printf( "プレイヤーの体力:%d\n", hitpoint_player );
// 勝ち負けが表示できる。
if ( hitpoint_player <= 0 ||
hitpoint_monster <= 0 ) {
break;
}
}
// プログラムの終了条件:プレイヤーの体力が0
if ( hitpoint_player <= 0 ) {
break;
}
// 戦闘に勝利した時、逃げたときは新たなモンスターと戦闘するか聞く。
printf( "新たなモンスターと戦闘しますか(1:はい,2:いいえ):" );
scanf( "%d", &command );
// プログラムの終了条件:新たなモンスターと戦闘しないとき
if ( command == 2 ) {
break;
}
}
return 0;
}
[/code]