goo blog サービス終了のお知らせ 

ズブの初心者がゲーム作れるまで勉強するブログ

現在、シューティングゲームの作り方を勉強中

C++ DXライブラリ シューティングゲームソース 構造体導入

2006-03-25 15:50:12 | Weblog

#define MAX 50 //敵の数最大
#define cspeed 8 //自機移動速度
#define EBULLET_MAX 50 //敵弾数最大

#include "DxLib.h"

//プロトタイプ宣言
void Boot_Initialize();
void Play_Initialize();
void Stage_Initialize();
void Enemy_Fire(int cnt);

//変数宣言
struct stEnemy
{
 int enable;
 int x;
 int y;
};
stEnemy Enemies[MAX];

int bl[EBULLET_MAX][4]; //敵の弾

int cx=0, cy=0;
int bx=0, by=0, benable=0;

int ndir=0, dir=0;
int tspeed=0, Teki_Rest=0;

int Score=0, HighScore=0;
int Dead=0, invasion=0;
int Stage_Num=0;

int HGJiki=0;
int HGTeki=0;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
     LPSTR lpCmdLine, int nCmdShow )
{
 if( DxLib_Init() == -1 ) // DXライブラリ初期化処理
 {
   return -1;    // エラーが起きたら直ちに終了
 }


 //初期化
 Boot_Initialize();
 Play_Initialize();

 //裏画面
 SetDrawScreen( DX_SCREEN_BACK ) ;

 //*Start
 while (1)
 {
  ClsDrawScreen() ;
  WaitTimer( 1 ) ;

  //自機
  if( CheckHitKey( KEY_INPUT_LEFT ) == 1 )
  {
   cx=cx-cspeed;
   if (cx<0) cx=0;
  }

  if( CheckHitKey( KEY_INPUT_RIGHT ) == 1 )
  {
   cx=cx+cspeed;
   if (cx>639-32) cx=639-32;
  }

  if( CheckHitKey( KEY_INPUT_LCONTROL ) == 1 )
  {
   if (benable == 0) {
    benable=1;
    bx=cx+16;
    by=cy;
   }
  }
  DrawGraph( cx , cy , HGJiki , FALSE ) ;

  //自機弾
  if (benable == 1) {
   by=by-8;
   if (by<0) benable=0;
   DrawLine(bx, by, bx, by-16, GetColor(255,255,255));
  }

  //敵機
  ndir=dir;
  for (int cnt=0 ; cnt<MAX ; cnt++)
  {
   if (Enemies[cnt].enable == 1)
   {
    DrawGraph( Enemies[cnt].x, Enemies[cnt].y, HGTeki , FALSE ) ;

    Enemies[cnt].x = Enemies[cnt].x+dir;
    if (Enemies[cnt].x <= 0) ndir = tspeed;
    if (Enemies[cnt].x >= 602) ndir = -tspeed;

    //あたり判定
    if (benable && (Enemies[cnt].x < bx) && ((Enemies[cnt].x + 32) > bx) && (Enemies[cnt].y < by) && ((Enemies[cnt].y+32) > by) )
    {
     Enemies[cnt].enable = 0;
     Score = Score + 10;
     Teki_Rest = Teki_Rest - 1;

     Enemy_Fire(cnt);
    }

    //敵が接地したか
    if (Enemies[cnt].y > 448) invasion = 1;
   }
  }

  if (dir != ndir) {
   for (int cnt=0 ; cnt<MAX ; cnt++)
   {
    Enemies[cnt].y = Enemies[cnt].y + 4;
   }
  }
  dir = ndir;

  //敵弾
  for (int cnt=0 ; cnt<EBULLET_MAX ; cnt++)
  {
   if (bl[cnt][0] == 1)
   {
    bl[cnt][3] = bl[cnt][3] + 8; //移動
    DrawLine(bl[cnt][2], bl[cnt][3], bl[cnt][2], bl[cnt][3] + 16, GetColor(255,255,255)); //描画
    if (bl[cnt][3] > 480) bl[cnt][0] = 0; //画面外なら使用終了
    if ((bl[cnt][2] > cx) && (bl[cnt][2] < cx + 32) && (bl[cnt][3] > cy) && (bl[cnt][3] < cy+32)) Dead = 1; //自機との当たり判定
   }
  }

  DrawFormatString( 200, 0, GetColor(255,255,255), "Score : %d", Score);

  if (Score > HighScore) HighScore = Score;
  DrawFormatString( 0, 0, GetColor(255,255,255), "High-Score : %d", HighScore);

  // 裏画面の内容を表画面に反映させる
  ScreenFlip() ;

  if (Teki_Rest <= 10) tspeed=2;
  if (Teki_Rest <= 1) tspeed=4;
  if (Teki_Rest == 0) Stage_Initialize();
  if ( (invasion == 1) || (Dead == 1) ) Play_Initialize();

 

  // Windows システムからくる情報を処理する
  if( ProcessMessage() == -1 ) break ;

  // ESCキーが押されたらループから抜ける
  if( CheckHitKey( KEY_INPUT_ESCAPE ) == 1 ) break ;
 }

 DxLib_End() ;    // DXライブラリ使用の終了処理

 return 0 ;     // ソフトの終了
}

 

//ステージ初期化
void Stage_Initialize()
{
 Stage_Num = Stage_Num + 1;

 ZeroMemory(Enemies, sizeof(Enemies)); //敵
 ZeroMemory(bl, sizeof(bl));  //敵の弾
 
 for (int cnt=0 ; cnt<MAX ; cnt++)
 {
  Enemies[cnt].enable = 1; //利用中(0=否)
  Enemies[cnt].x = ( cnt * 64 ) % 640; //X座標
  Enemies[cnt].y = ( cnt * 64 ) / 640 * 64; //Y座標
 }
 cx=320;
 cy=440;

 benable=0;
 tspeed=1;

 Teki_Rest=MAX;
 invasion = 0;
 Dead = 0;

 ClsDrawScreen();

 DrawFormatString( 280, 240, GetColor(255,255,255), "Stage : %d", Stage_Num);

 ScreenFlip() ;
 WaitTimer( 1000 ) ;
 
 return;
}

//プレイ初期化
void Play_Initialize()
{
 Stage_Num=0;
 Score=0;
 Stage_Initialize();
 
 return;
}

//起動時の初期化
void Boot_Initialize()
{
 HighScore=74;
 Score=0;
 
 HGJiki = LoadGraph( "jiki.bmp" ) ;
 HGTeki = LoadGraph( "teki.bmp" ) ;

 ClsDrawScreen();

 return;
}

//敵が弾を発射
void Enemy_Fire(int cnt)
{
 int teki_num=cnt;

 for ( cnt=0 ; cnt<EBULLET_MAX ; cnt++)
 {
  if (bl[cnt][0] == 0) {
   bl[cnt][0] = 1;
   bl[cnt][2] = Enemies[teki_num].x+16;
   bl[cnt][3] = Enemies[teki_num].y;
   break;
  }
 }

 return;
}