//シューティングゲーム サンプルプログラム 15 クラス
//ライブラリ宣言
#include "DxLib.h"
//定数の宣言---------------------------------------------------------
#define MINI_RANGE_X 160 //画面の大きさと位置 横最小座標
#define MINI_RANGE_Y 0 //画面の大きさと位置 横最小座標
#define MAX_RANGE_X 480 //画面の大きさと位置 横最大座標
#define MAX_RANGE_Y 480 //画面の大きさと位置 横最大座標
#define ENEMY_SIZE 50
//-------------------------------------------------------------------
//**クラス**********************************************************
//シューティング用プレイヤー移動クラス-----------------------------------
class MovePlane
{
private:
//変数
int player_position_x; //プレイヤー座標X
int player_position_y; //プレイヤー座標Y
int PLAYER_SIZE; //プレイヤー画像の大きさ
int PLAYER_MOVE_SPEED; //1ターンあたりの移動ドット数
int MINI_MOVE_PLAYER_RANGE_X; //プレイヤー移動範囲最小X
int MINI_MOVE_PLAYER_RANGE_Y; //プレイヤー移動範囲最小Y
int MAX_MOVE_PLAYER_RANGE_X; //プレイヤー移動範囲最大X
int MAX_MOVE_PLAYER_RANGE_Y; //プレイヤー移動範囲最大Y
int player_graph; //プレイヤー画像ハンドル
public:
MovePlane() //初期値
{
player_position_x=320;
player_position_y=450;
PLAYER_SIZE=50;
PLAYER_MOVE_SPEED=4;
MINI_MOVE_PLAYER_RANGE_X=160;
MINI_MOVE_PLAYER_RANGE_Y=0;
MAX_MOVE_PLAYER_RANGE_X=480;
MAX_MOVE_PLAYER_RANGE_Y=480;
player_graph=LoadGraph("Player.png");
}
//関数
//変数の出力
getposition_x() { return player_position_x; } //プレイヤー座標Xを参照する
getposition_y() { return player_position_y; } //プレイヤー座標Yを参照する
//[解説] ジョイスティック入力によってX座標Y座標を更新する
int inputAndUpdate();
//]解説] 移動範囲補正処理(範囲外にX座標Y座標を更新しない)
int reviseRange();
//自機の表示
int drawPlane(); //描画
};
//----------------------------------------------------------------
//シューティング用 弾 発射 移動 クラス
class PlaneGun
{
private:
int f_player_gun_flag; //弾が発射されているか フラグ
int player_gun_position_x; // 弾の位置 X座標
int player_gun_position_y; // 弾の位置 Y座標
int player_gun_graph; // 弾画像
int GUN_MOVE_SPEED; //弾の1ターンあたりの移動量(ドット)
int GUN_SIZE; //キャラのサイズ
public:
PlaneGun() //初期値
{
f_player_gun_flag=FALSE;
player_gun_position_x=10;
player_gun_position_y=10;
player_gun_graph=LoadGraph("Gun.png");
GUN_MOVE_SPEED=10;
GUN_SIZE=12;
}
//関数
//変数の出力
getposition_x() { return player_gun_position_x; } //プレイヤー座標Xを参照する
getposition_y() { return player_gun_position_y; } //プレイヤー座標Yを参照する
//弾の発射と移動
//[ 引数 ] 弾発射位置 X座標 Y座標
int inputAndUpdate(int player_position_x,int player_position_y);
//弾の表示
int drawGun(); //描画
};
//----------------------------------------------------------------
//**クラス**********************************************************
//構造体-----------------------------------------------------------
//敵用 構造体 型宣言
struct enemy_set
{
int graph; //画像ハンドル(車のハンドルと同じ意味。操作する部分)
int position_x; //座標X
int position_y; //座標Y
};
//-----------------------------------------------------------------
//WinMain関数
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
// DXライブラリの設定
// SetOutApplicationLogValidFlag(TRUE);
//SetGraphMode(640,480,16);
ChangeWindowMode( TRUE ) ;
if( DxLib_Init()==-1) return-1;
// SetMouseDispFlag( TRUE ) ;
SetDrawScreen(DX_SCREEN_BACK);
//クラスのオブジェクト(実体)を作る
MovePlane Player;
PlaneGun Gun;
//変数の宣言と初期化----------------------------------------------
int background_graph=LoadGraph("BackGroundGraph.png"); //背景
enemy_set m_enemy[2]= //敵 構造体 2体
{
LoadGraph("Enemy.png") , 240 , 100,
LoadGraph("Enemy.png") , 400 , 100
} ;
//-------------------------------------------------------------------
while(ProcessMessage()==0)
{
//**メインループ ********************************************************
//ジョイスティック&キー入力 キャラクターの移動
Player.inputAndUpdate();
//弾の発射 移動
Gun.inputAndUpdate(Player.getposition_x(),Player.getposition_y());
//画像描画
ClsDrawScreen() ;
DrawGraph(MINI_RANGE_X,MINI_RANGE_Y, background_graph,TRUE);
Gun.drawGun();
Player.drawPlane();
DrawGraph( m_enemy[0].position_x-ENEMY_SIZE/2,
m_enemy[0].position_y-ENEMY_SIZE/2, m_enemy[0].graph,TRUE);
DrawGraph( m_enemy[1].position_x-ENEMY_SIZE/2,
m_enemy[1].position_y-ENEMY_SIZE/2, m_enemy[1].graph,TRUE);
ScreenFlip();
//**メインループ **************************************************
// ESCキーが押されたらループから抜ける
if(CheckHitKey(KEY_INPUT_ESCAPE)==TRUE)break;
}
//終了処理
DxLib_End();
return 0;
}
//自作関数---------------------------------------------------------
//**クラス*****************************************************
//**飛行機 移動 クラス*****************************************
//ジョイスティック&キー 移動入力
//[解説] ジョイスティック入力によってX座標Y座標を更新する
int MovePlane::inputAndUpdate()
{
int joypad_state=GetJoypadInputState(DX_INPUT_KEY_PAD1);
if((joypad_state&PAD_INPUT_UP)!=0) player_position_y-=PLAYER_MOVE_SPEED;
if((joypad_state&PAD_INPUT_DOWN)!=0) player_position_y+=PLAYER_MOVE_SPEED;
if((joypad_state&PAD_INPUT_LEFT)!=0) player_position_x-=PLAYER_MOVE_SPEED;
if((joypad_state&PAD_INPUT_RIGHT)!=0) player_position_x+=PLAYER_MOVE_SPEED;
reviseRange();
return 0;
}
//-----------------------------------------------------------------
//]解説] 移動範囲補正処理(範囲外にX座標Y座標を更新しない)
int MovePlane::reviseRange()
{
if(player_position_x>MAX_MOVE_PLAYER_RANGE_X-PLAYER_SIZE/2)
player_position_x=MAX_MOVE_PLAYER_RANGE_X-PLAYER_SIZE/2;
if(player_position_x<MINI_MOVE_PLAYER_RANGE_X+PLAYER_SIZE/2)
player_position_x=MINI_MOVE_PLAYER_RANGE_X+PLAYER_SIZE/2;
if(player_position_y>MAX_MOVE_PLAYER_RANGE_Y-PLAYER_SIZE/2)
player_position_y=MAX_MOVE_PLAYER_RANGE_Y-PLAYER_SIZE/2;
if(player_position_y<MINI_MOVE_PLAYER_RANGE_Y+PLAYER_SIZE/2)
player_position_y=MINI_MOVE_PLAYER_RANGE_Y+PLAYER_SIZE/2;
return 0;
}
//-----------------------------------------------------------------
//自機を表示する
int MovePlane::drawPlane()
{
DrawGraph(player_position_x-PLAYER_SIZE/2,
player_position_y-PLAYER_SIZE/2, player_graph,TRUE);
}
//**飛行機 機銃 クラス*****************************************
//-----------------------------------------------------------------
//]解説] ジョイスティック入力によって発射。弾の移動処理(X座標Y座標を更新する)
int PlaneGun::inputAndUpdate(int player_position_x,int player_position_y)
{
int joypad_state=GetJoypadInputState(DX_INPUT_KEY_PAD1);
if(((joypad_state& PAD_INPUT_A) !=0)&&(f_player_gun_flag==FALSE))
{
f_player_gun_flag=TRUE;
player_gun_position_x= player_position_x ;
player_gun_position_y= player_position_y ;
}
if(f_player_gun_flag==TRUE)player_gun_position_y-=GUN_MOVE_SPEED;;
if(player_gun_position_y<1) f_player_gun_flag=FALSE;
return f_player_gun_flag;
}
//-----------------------------------------------------------------
//弾を表示する
int PlaneGun::drawGun()
{
if(f_player_gun_flag==TRUE)DrawGraph(player_gun_position_x-GUN_SIZE/2,
player_gun_position_y-GUN_SIZE/2,player_gun_graph,TRUE);
}
//-----------------------------------------------------------------
|