弾の壁との判定
Posted: 2012年9月07日(金) 01:44
bullet関数というものを最後に追加し、弾が壁に当たった場合には弾を消す(shot_flag=0)ようにしました。
同じ方向でも、貫通できる壁,できない壁ができてしまいました。
新たにスペースキーを押しながら方向キーを押すことで、方向だけ変えれるようになりました。
20x20の画像を準備してコンパイルお願いします。
また、Esc以外で閉じてしまうとプロセスが残ったままなってしまうので普段はint main(void)の中に書いているのですが解決方法はありますか?
同じ方向でも、貫通できる壁,できない壁ができてしまいました。
新たにスペースキーを押しながら方向キーを押すことで、方向だけ変えれるようになりました。
20x20の画像を準備してコンパイルお願いします。
また、Esc以外で閉じてしまうとプロセスが残ったままなってしまうので普段はint main(void)の中に書いているのですが解決方法はありますか?
#include"DxLib.h"
int able_walk(struct player);//進行方向を判定する
int bullet(struct shot);//弾が壁に当たったら弾を消す。
struct player{//player構造体
int x,y;
int direction;
};
struct shot{//shot構造体
int x,y;
int direction;
};
int map[5][5]={{0,0,1,1,0},//ダンジョン形
{1,0,0,0,0},
{0,0,1,0,1},
{0,1,0,0,1},
{1,1,1,0,1}};
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
ChangeWindowMode(TRUE);
SetDrawScreen(DX_SCREEN_BACK);
if(DxLib_Init()==-1)
return -1;
int graph;
int walk_flag=0;
int shot_flag=0;
int space_flag=0;
int map_color;
int cr=GetColor(255,255,0);
int i,j;
struct shot shots;
struct player pc;//0:上向き,1:下,2:右,3:左
shots.x;
shots.y;
shots.direction;
pc.x=20;
pc.y=0;
pc.direction=1;
map_color=GetColor(255,255,255);
graph=LoadGraph("円.jpg");//20x20の円の画像
while(1){
ProcessMessage();
ClearDrawScreen();
/*-------------------------------------
キャラクターの移動処理
スペースキーを押しっぱなしのまま方向キーで
方向転換(進まずに方向転換)
---------------------------------------*/
if(pc.x%20==0&&pc.y%20==0){
walk_flag=1;
if(CheckHitKey(KEY_INPUT_UP)==1)
pc.direction=0;
else if(CheckHitKey(KEY_INPUT_DOWN)==1)
pc.direction=1;
else if(CheckHitKey(KEY_INPUT_RIGHT)==1)
pc.direction=2;
else if(CheckHitKey(KEY_INPUT_LEFT)==1)
pc.direction=3;
else if(CheckHitKey(KEY_INPUT_SPACE)==1)//スペースキーが押されているか
space_flag=1;
else {
walk_flag=0;
space_flag=0;
}
if(walk_flag==1){
if(able_walk(pc)==1)
walk_flag=0;
}
}
if((walk_flag==1)&&space_flag==0){
if (pc.direction==0)
pc.y--;
else if(pc.direction==1)
pc.y++;
else if(pc.direction==2)
pc.x++;
else if(pc.direction==3)
pc.x--;
}
//ショット
if(CheckHitKey(KEY_INPUT_W)==1&&shot_flag==0){
shots.x=pc.x;
shots.y=pc.y;
shots.direction=pc.direction;
if(pc.direction==0)
DrawBox(shots.x+7,shots.y-10,shots.x+12,shots.y,cr,TRUE);
else if(pc.direction==1)//下向き
DrawBox(shots.x+7,shots.y+20,shots.x+12,shots.y+30,cr,TRUE);
else if(pc.direction==2)//右向き
DrawBox(shots.x+20,shots.y+7,shots.x+30,shots.y+12,cr,TRUE);
else if(pc.direction==3)//左向き
DrawBox(shots.x-10,shots.y+7,shots.x,shots.y+12,cr,TRUE);
shot_flag=1;
}
if(CheckHitKey(KEY_INPUT_ESCAPE)==1)//※ゲーム終了
break;
//キャラクター描写
DrawGraph(pc.x,pc.y,graph,TRUE);
//弾描写
if(shot_flag==1){
if(shots.direction==0)
shots.y-=2;
else if(shots.direction==1)
shots.y+=2;
else if(shots.direction==2)
shots.x+=2;
else if(shots.direction==3)
shots.x-=2;
if(shots.x<0||shots.y<0||shots.x>100||shots.y>100)
shot_flag=0;
if(shots.direction==0)
DrawBox(shots.x+7,shots.y-10,shots.x+12,shots.y,cr,TRUE);
else if(shots.direction==1)//下向き
DrawBox(shots.x+7,shots.y+20,shots.x+12,shots.y+30,cr,TRUE);
else if(shots.direction==2)//右向き
DrawBox(shots.x+20,shots.y+7,shots.x+30,shots.y+12,cr,TRUE);
else if(shots.direction==3)//左向き
DrawBox(shots.x-10,shots.y+7,shots.x,shots.y+12,cr,TRUE);
if(bullet(shots)==1){
shot_flag=0;
}
}
//map描写
for(i=0;i<5;i++){
for(j=0;j<5;j++){
if(map[j][i]==1)
DrawBox(i*20,j*20,i*20+20,j*20+20,map_color,TRUE);
}
}
ScreenFlip();
}
DxLib_End();
return 0;
}
int able_walk(struct player pc)//進行方向が壁ならばエラー(1)を返す
{
if(pc.direction==0)//上
if(map[pc.y/20-1][pc.x/20]==1)
return 1;//エラー
if(pc.direction==1)//下
if(map[pc.y/20+1][pc.x/20]==1)
return 1;
if(pc.direction==2)//右
if(map[pc.y/20][pc.x/20+1]==1)
return 1;
if(pc.direction==3)//左
if(map[pc.y/20][pc.x/20-1]==1)
return 1;
return 0;//正常
}
int bullet(struct shot shots)
{
if(shots.direction==0)
if(map[shots.y/10+1][shots.x/5]==1)
return 1;//エラー(壁に衝突)
if(shots.direction==1)
if(map[shots.y/10-1][shots.x/5]==1)
return 1;
if(shots.direction==2)
if(map[shots.y/5][shots.x/10+1]==1)
return 1;
if(shots.direction==3)
if(map[shots.y/5][shots.x/10-1]==1)
return 1;
return 0;//壁にぶつかってない
}