今、自機(赤い正方形)から弾を上方向に撃つという処理を行い、画面外に出たらまた撃てるようにしたいのですが、画面外に弾が出てもまた撃てるようになりません。また発射される弾が自機の初期位置になってしまいます。さらに、弾の位置を変更しようと、更新処理のshot.xに自機の座標を入れると
自機の移動と弾の移動が同期してしまいます。どのような処理を行えばよいですか?
#include "DxLib.h"
#define SCREEN_WIDIH 1000
#define SCREEN_HEIGHT 900
//キーの実装
int key[256];
int gpUpdatekey()
{
char tmpkey[256]; //現在のキーの入力状態を格納
GetHitKeyStateAll(tmpkey); //すべてのキーの入力状態を得る
for (int i = 0; i < 256; ++i)
{
if (tmpkey[i] != 0)
{
++key[i];
}
else
{
key[i] = 0;
}
}
return 0;
}
struct Block
{
int x;
int y;
int w;
int h;
bool flag;
int speed;
int color;
};
struct SHOT
{
int x;
int y;
int w;
int h;
bool flag;
int speed;
int color;
};
Block me;
SHOT shot;
int RED = GetColor(255, 0, 0);
int white = GetColor(255, 255, 255);
//自機初期化
void Block_Ini()
{
me.x = 0;
me.y = 900;
me.w = 50;
me.h = 50;
me.flag = true;
me.color = RED;
me.speed = 5;
}
//弾の初期化
void Shot_Ini()
{
shot.x = me.x + (me.w / 2);
shot.y = me.y;
shot.speed = 10;
shot.color = white;
shot.flag = false;
}
//自機更新
void Block_Update()
{
if (me.flag == true)
{
if (key[KEY_INPUT_RIGHT] > 0 && me.x + me.w <= SCREEN_WIDIH)
{
me.x += me.speed;
}
if (key[KEY_INPUT_LEFT] > 0 && me.x >= 0)
{
me.x -= me.speed;
}
if (key[KEY_INPUT_Z] == 1)
{
shot.flag = true;
}
}
}
//弾の更新
void Shot_Update()
{
if (shot.flag == true)
{
shot.y -= shot.speed;
}
if (shot.y <= 0 && shot.flag == true)
{
shot.flag = false;
}
}
//描画
void Obj_Draw()
{
if (shot.flag == true)
{
DrawCircle(shot.x, shot.y, 2, shot.color, true);
}
if (me.flag == true)
{
DrawBox(me.x, me.y - me.h, me.x + me.w, me.y, me.color, true);
}
}
//エントリーポイント
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
ChangeWindowMode(1), DxLib_Init(), SetDrawScreen(DX_SCREEN_BACK); //ウィンドウモード変更と初期化と裏画面設定
//ウインドウサイズ(1000*900)
SetGraphMode(SCREEN_WIDIH, SCREEN_HEIGHT, 32);
//自機の初期化処理
Block_Ini();
Shot_Ini();
// while(裏画面を表画面に反映、メッセージ処理、画面クリア、キーの更新)
while(ScreenFlip() == 0 && ProcessMessage() == 0 && ClearDrawScreen() == 0 && gpUpdatekey() == 0) //無限ループ
{
//ここにゲームの処理を描く------------------------------------------------
Block_Update();
Shot_Update();
Obj_Draw();
//----------------------------------------------------------------------
}
DxLib_End(); // DXライブラリ終了処理
return 0;
}