ページ 11

自機から弾を発射したい

Posted: 2017年7月07日(金) 22:12
by tonari
初投稿です、使い方が間違っていたらすみません。
今、自機(赤い正方形)から弾を上方向に撃つという処理を行い、画面外に出たらまた撃てるようにしたいのですが、画面外に弾が出てもまた撃てるようになりません。また発射される弾が自機の初期位置になってしまいます。さらに、弾の位置を変更しようと、更新処理の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;
}

Re: 自機から弾を発射したい

Posted: 2017年7月07日(金) 23:01
by みけCAT
弾を撃つタイミング(87行目の直後)で弾の座標を自機の座標にセットすればいいでしょう。

Re: 自機から弾を発射したい

Posted: 2017年7月08日(土) 10:45
by tonari
返信ありがとうございます。以下のように変更したところ、弾の座標が自機と同期しないで撃てるようになりました。
あとは、画面外から出た時の処理なのですが、画面から弾が出たらfalseにしてもう一度撃てるようにしたいのですが。
どのような変更を加えればよいでしょうか?(構造体が2つもいらなかったので、修正しました)

コード:

 
#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 Obj
{
	int		x;
	int     y;
	int     w;
	int	    h;
	bool flag;
	int speed;
	int color;

};

Obj me;
Obj shot;
int RED = GetColor(255, 0, 0);
int white = GetColor(255, 255, 255);

//自機初期化
void Block_Ini()
{
	me.x = 0;
	me.y = SCREEN_HEIGHT;
	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 == false)
		{
			shot.flag = true;
			shot.x = me.x + (me.w / 2);
		}
	}
}
//弾の更新
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;
}
 

Re: 自機から弾を発射したい

Posted: 2017年7月08日(土) 11:17
by みけCAT
弾を発射する時に弾のy座標も再設定するようにするといいと思います。

Re: 自機から弾を発射したい

Posted: 2017年7月08日(土) 11:26
by tonari
ありがとうございます。無事、やりたいことが実装できました