しかし、ClearDrawScreen関数を使用しているせいで、弾を撃つたびに弾以外のものがすべて消えてしまいます。
原因は分かっているのですが、どうしていいかわかりません。
ご回答よろしくおねがいします。
下にソースを載せておきます。
申し訳ないのですが、73行目?でjpg画像を取り込んでいるので、適当に20x20の画像をつくって実行してください。
#include"DxLib.h"
int able_walk(int,int,int);//進行方向を判定する
void shot(int,int,int);//弾の軌道
int map[5][5]={{0,0,1,1,0},//ダンジョン形
{0,1,0,0,0},
{0,1,1,0,1},
{0,0,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 x=0,y=0,cir_color;//円の座標と色
int direction=1;//0:上向き,1:下,2:右,3:左
int graph;
int walk_flag=0;
int map_color;
int i,j,m;
cir_color=GetColor(0,0,255);
map_color=GetColor(255,255,255);
while(1){
ProcessMessage();
ClearDrawScreen();
/*-------------------------------------
キャラクターの移動処理
---------------------------------------*/
if(x%20==0&&y%20==0){
walk_flag=1;
if(CheckHitKey(KEY_INPUT_UP)==1)
direction=0;
else if(CheckHitKey(KEY_INPUT_DOWN)==1)
direction=1;
else if(CheckHitKey(KEY_INPUT_RIGHT)==1)
direction=2;
else if(CheckHitKey(KEY_INPUT_LEFT)==1)
direction=3;
else
walk_flag=0;
if(walk_flag==1){
if(able_walk(x,y,direction)==1)
walk_flag=0;
}
}
if(walk_flag==1){
if (direction==0)
y--;
else if(direction==1)
y++;
else if(direction==2)
x++;
else if(direction==3)
x--;
}
//ショット
if(CheckHitKey(KEY_INPUT_W)==1)
shot(x,y,direction);
//キャラクター描写
graph=LoadGraph("円.jpg");//20x20の円の画像
DrawGraph(x,y,graph,TRUE);
//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();
}
WaitKey();
DxLib_End();
return 0;
}
int able_walk(int x,int y,int direction)//進行方向が壁ならばエラー(1)を返す
{
if(direction==0)//上
if(map[y/20-1][x/20]==1)
return 1;//エラー
if(direction==1)//下
if(map[y/20+1][x/20]==1)
return 1;
if(direction==2)//右
if(map[y/20][x/20+1]==1)
return 1;
if(direction==3)//左
if(map[y/20][x/20-1]==1)
return 1;
return 0;//正常
}
void shot(int x,int y,int direction)//弾の軌道を描写する関数
{
SetDrawScreen(DX_SCREEN_BACK);
int cr=GetColor(255,255,0);
int i;
for(i=0;i<40;i++){
ProcessMessage();
ClearDrawScreen();
if(direction==0){//上向き
DrawBox(x+7,y-10,x+13,y-1,cr,TRUE);
y--;
}
else if(direction==1){//下向き
DrawBox(x+7,y+20,x+13,y+29,cr,TRUE);
y++;
}
else if(direction==2){//右向き
DrawBox(x+20,y+7,x+29,y+13,cr,TRUE);
x++;
}
else if(direction==3){//左向き
DrawBox(x-10,y+7,x-1,y+13,cr,TRUE);
x--;
}
ScreenFlip();
}
}