ぷよぷよっぽいゲーム開発2日目

今日やったこと

こんな感じのソースコードを書いた

#include "DxLib.h"
#include<iostream>
#include<vector>

typedef std::pair<int, int> pair;
std::vector<pair> pair_vec;

//test2
int field[6][12];
int rensa[6][12];


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
			 LPSTR lpCmdLine, int nCmdShow )
{
	//フルスクリーンの表示をやめる
	ChangeWindowMode(true);
	
	// DXライブラリ初期化
	if( DxLib_Init() == -1 ) return -1 ;
	
	
	//画面の解像度などを設定
	SetGraphMode( 640 , 480 , 16 ) ;
	
	
	//画像ハンドルの宣言
	int StageHandle,KiroHandle,AkaHandle,AoHandle,MidoriHandle,BatuHandle;
	
	
	//ステージ画像の読み込み
	StageHandle=LoadGraph("stage2.png");
	//ぷよの画像の読み込み
	KiroHandle=LoadGraph("kiro.png");
	AkaHandle=LoadGraph("aka.png");
	AoHandle=LoadGraph("ao.png");
	MidoriHandle=LoadGraph("midori.png");
	//その他画像の読み込み
	BatuHandle=LoadGraph("batu.png");
	
	//ステージ画像のサイズを取得
	int stagew,stageh;
	GetGraphSize(StageHandle,&stagew,&stageh);
	//ステージ画像を拡大
	double stager=(double)480.0/stageh;
	stagew=stagew*stager;
	stageh=stageh*stager;
	//ステージの左端の位置
	int a=150;
	
	
	//ぷよ画像のサイズを取得
	int puyow,puyoh;
	GetGraphSize(AkaHandle,&puyow,&puyoh);
	//ぷよ画像を拡大
	double puyor=(double)stagew/6.0/puyow;
	puyow=puyow*puyor;
	puyor=(double)stageh/12.0/puyoh;
	puyoh=puyoh*puyor;
	
	
	//その他の画像の処理
	int batuw,batuh;
	GetGraphSize(BatuHandle,&batuw,&batuh);
	double batur=(double)puyow/batuw;
	batuw=batur*batuw;
	batuh=batur*batuh;
	
	
	//ステージの各列の高さ
	int hi[6];
	for(int i=0;i<6;i++)
	  hi[i]=11*puyoh;
	
	
	//ぷよの初期位置
	int inix=a+2*puyow;
	int iniy=-puyoh;
	
	
	//ぷよの位置の変数
	int x,y;
	
	//ぷよの色
	int c;
	srand((unsigned int)time(NULL));
	
	//ぷよに関するフラグ
	int puyoflag=0;
	int puyoxflag=0;
	
	
	//test2
	for(int i=0;i<6;i++){
	  for(int j=0;j<12;j++){
	    field[i][j]=0;
	  }
	}
	
	//描画先を裏画面に設定
	SetDrawScreen( DX_SCREEN_BACK ) ;
	
	
	while(1){
	  //画面をクリア
	  ClearDrawScreen() ;
	
	  //ステージ画像の処理
	  {
	    //ステージ画像を拡大して表示
	    DrawExtendGraph(a,0,stagew+a-0.5,stageh,StageHandle,false);
	    DrawExtendGraph(a+2*puyow,0,a+2*puyow+batuw,batuh,BatuHandle,true);
          }
	  
	  //ぷよの処理
	  {
	     //ぷよを操作し終わったら新しいぷよを降らせる
	     if(puyoflag==0){
	       x=inix;
	       y=iniy; 
	       //ぷよの色をランダムに決定
	       c=rand()%4;
	       switch(c){                    
	         case 0:c=AoHandle;break;
		 case 1:c=AkaHandle;break;
		 case 2:c=KiroHandle;break;
		 case 3:c=MidoriHandle;break;
	       }
	       
	       DrawExtendGraph(x,y,x+puyow,y+puyoh,c,true);
	       puyoflag=1;
	     }
	     
	     else{
	       //キーボードから入力に応じてぷよを動かす
	       if( CheckHitKey( KEY_INPUT_LEFT ) == 1 ){
	         if(puyoxflag==0){
		   if(hi[(x-puyow-a)/puyow]<y)x=x;
		   else{
	             x-=puyow;
		     puyoxflag=7;
		   }
		 }
	       }
	       if( CheckHitKey( KEY_INPUT_RIGHT ) == 1 ){
	         if(puyoxflag==0){
		   if(hi[(x+puyow-a)/puyow]<y)x=x;
		   else{
	             x+=puyow;
		     puyoxflag=7;
		   }
		 }
	       }
	       if( CheckHitKey( KEY_INPUT_DOWN ) == 1 ) y += 12 ;
	       //壁際の処理
	       if(x<a)x=a;
	       if(x>a+5*puyow)x=a+5*puyow;
	       
	       //着地したら操作フラグをしまう
	       if(y>hi[(x-a)/puyow]){
	         y=hi[(x-a)/puyow];
		 field[(x-a)/puyow][y/puyoh]=c;
		 hi[(x-a)/puyow]-=puyoh;
		 puyoflag=0;
		 
		 
		 
	       }
	       
	       
	       //ぷよを横方向に小刻みに動かすための処理
	       if(puyoxflag!=0)
	         puyoxflag--;
		 
	     //操作中のぷよの描画 
	     DrawExtendGraph(x,y,x+puyow,y+puyoh,c,false);

	     //堆積したぷよの描画
	       for(int i=0;i<6;i++){
	         for(int j=0;j<12;j++){
		   if(field[i][j]==0)continue;
	             DrawExtendGraph(i*puyow+a,puyoh*j,i*puyow+a+puyow,puyoh*j+puyoh,field[i][j],false);
	         }
	       }
	       
	       
	    //表画面への描画
	    ScreenFlip();
	       
	    }
	     
	     //自由落下
	     y+=1;
	   } 
	  
	  
	  //ゲームオーバー
	  if(hi[2]==-puyoh){
	    break;
	  }  
	  
	  
	  
	  //仮の終了処理
	  {
	    // Windows特有の面倒な処理をDXライブラリにやらせる
	    // -1 が返ってきたらループを抜ける
	    if( ProcessMessage() < 0 ) break ;
	    // もしESCキーが押されていたらループから抜ける
	    if( CheckHitKey( KEY_INPUT_ESCAPE ) ) break ;
	  }
	  
	}
	
	//ゲームオーバーの処理
	DrawString(0,0,"ゲームオーバー",GetColor( 255 , 0 , 0 )) ;
	ScreenFlip();
	WaitTimer(5*100);

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

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

進捗

  • 乱数を使って降って来るぷよの色をランダムにした。
  • Screenflip();の場所を変えることで以前でていた画面のちらつきをおさえることができた(うまくいった理由は考え中)
  • ぷよを操作する部分にif文を書き加えることで、ぷよの上にぷよを乗せる挙動がうまくいくようになった
  • pair型vectorの変数がなかなかうまく宣言できなかった(使っているコンパイラが古いのか?)。上のソースコードの冒頭部分のように回りくどい感じで宣言したらできた。でもこの型の変数はまだ使っていない。連鎖を実装するときに使いそうな気がする。

懸念点

連鎖を実装する方法がまだ思いつかない。

実行ファイル

Debug.zip - Google ドライブ
windows用のexeファイルが格納されたzipファイルがダウンロードできます。未完成です。