//今回のプログラムは当たり判定のほかにプログラムがコメントアウトされているところがあります。 //その部分は追加資料で解説していきたいと思いますのでとりあえずのところおいておいてください。 #include"DxLib.h" #include #define WIDTH 640 #define HEIGHT 480 #define ENEMY 20 #define ITEM 30 #define BULLET 200 //自機のstruct struct S_player{ int x,y; //座標 int vx,vy; //座標方向の速度 int hp; //hit point int score; //得点 int size_x;//x軸方向の大きさ int size_y;//y軸方向の大きさ }; //敵のstruct struct S_enemy{ int x,y; int vx,vy; int hp; int t; //その敵が作られてからのフレーム数を格納するメンバ int size_x; int size_y; }; //弾のstruct struct S_bullet{ double x,y; double vx,vy; int hp; //hpが0なら画面上に存在していない、1なら存在している int atk; //弾の攻撃力 int size_x; int size_y; }; ////アイテムのstruct //struct S_item{ // int x,y; // int vx,vy; // int hp; //hpが0なら画面上に存在していない、1なら存在している // int size_x; // int size_y; //}; //背景のstruct struct S_background{ int x,y; int vy; int size_x; int size_y; }; //画像のstruct struct S_image{ int img_player; //自機の画像を格納 int img_enemy; //敵の画像 int img_bullet; //弾の画像 int img_item; //アイテム int img_gameover; //ゲームオーバー画面 int img_title; //タイトル画面 int img_background; //背景 }; enum e_flag{ //列挙型 enum ここでは title, //title=0 gameplay, //gameplay=1 gameover, //gameover=2 }; //の定数を定義していると考える //グローバル変数の宣言 S_player player; //自機のstructをインスタンス化   S_enemy enemy[ENEMY]; //敵のstructを S_bullet playerbullet[BULLET]; //弾のstructを自機の弾用に S_bullet enemybullet[ENEMY][BULLET]; //弾のstructを敵の弾用に //S_item item[ITEM]; //アイテムのstructを S_background background; //背景のstructを S_image imglist; //画像のstructを e_flag flag; //e_flag型の変数(中身がtitle,gameplay,gameoverの3つにしかならない変数) char key[256]; //キーボードの入力状況を格納するための変数 int white=GetColor(255,255,255); //画面に表示する文字の色を格納 //初期化用関数 int initialize(){ //playerの初期化 player.hp=100; player.score=0; player.size_x=50; player.size_y=50; player.x=(WIDTH - player.size_x) / 2; player.y=HEIGHT - player.size_y; player.vx=10; player.vy=10; //playerbulletの初期化 for(int i=0;i WIDTH) player.x = WIDTH - player.size_x; if(player.y<0) player.y=0; if(player.y + player.size_y > HEIGHT) player.y = HEIGHT - player.size_y; } //敵を動かす関数 void move_enemy(){ //すべての敵 for(int i=0;iHEIGHT){ enemy[i].hp=0; } } } } //自機の弾を動かす関数 void move_playerbullet(){ //すべての弾 for(int i=0;i HEIGHT+enemybullet[i][j].size_y||enemybullet[i][j].x < 0 || enemybullet[i][j].x > WIDTH || enemybullet[i][j].x + playerbullet[i].size_x < 0 ){ enemybullet[i][j].hp=0; } } } } } ////アイテムを動かす関数 //void move_item(){ // //すべてのアイテム // for(int i=0;i HEIGHT) 【穴埋め】=0; // // } // } //} //背景を動かす関数 void move_background(){ //x,y方向の速度を現座標に加え,背景の大きさでループさせる background.y = (background.y + background.vy) % background.size_y; } //敵を作る関数 void create_enemy(){ //0〜30の乱数で0だったら if(GetRand(30)==0){ //すべての敵から for(int i=1;i 【穴埋め】 && 【穴埋め】 < enemy[i].y + enemy[i].size_y && playerbullet[j].y + playerbullet[j].size_y > 【穴埋め】 ){ 【穴埋め】=0; //弾の存在を消し enemy[i].hp -= playerbullet[j].atk; //敵のhpから弾のatk分減らして if(enemy[i].hp<0) enemy[i].hp=0; //敵のhpがマイナスにならないように /*if(enemy[i].hp==0) 【穴埋め】 //敵を倒したらアイテムを作る*/ } } } } } } //あたり判定、敵の弾と自機 void judge_enemybullet_to_player(){ //すべての敵の for(int i=0;i player.x && enemybullet[i][j].y < 【穴埋め】 && 【穴埋め】 > player.y ){ 【穴埋め】=0; //弾の存在を消し player.hp -= enemybullet[i][j].atk; //自機のhpから弾のatk分減らして if(player.hp<0) player.hp=0; //自機のhpがマイナスにならないように } } } } } //あたり判定、自機と敵 void judge_player_to_enemy(){ //すべての敵の for(int i=0;i