21. 色んな弾幕を作ろう(以降33章まで連続)。
13種類の基本的なショットを作ってみました。百聞は一見にしかず。
まずは実行してみてください。
void EnemyShotPattern1(){}
という関数で、実際に弾幕の処理をしています。
処理を
void EnemyShotPattern1()
void EnemyShotPattern2()
・・・・・
void EnemyShotPattern13()
まで13種類作ってあるので少々長くなりましたが後から順を追って説明しますので、見てください。
このサンプルでは、13種類の弾幕が放置しておけば連続で表示されます。
ショットはうたずに、プレイヤーを動かしながらみてみてください。
以下に紹介しているコードは、メニュー33章の後のリンクにあるダウンロード可能なコードと同じものです。
33章まで説明するコードを全部まとめてあり、変更点が沢山あるので、コピーするのが面倒な方は
こちらからソースを一括ダウンロードしてください。この中には適正の素材も既に入っています。
VisualSudio用のプロジェクト付なので、VisualStudioをお持ちの方はそのままslnファイルから実行してください。
(2005をお持ちの方は、2003プロジェクトなので、開いたときに出るメッセージにそって適正なものに変換してください。)
このダウンロードを行った方は以降のプログラムをコピーする必要がありません。
/* enemy.cpp */
/* 素材 ver 1.21以上必要 */
#include "DxLib.h" #include "ExternGV.h" #include <math.h> #define PI 3.141593 /* void EnemyPattern1(int i){ enemy[i].y+=1.5f; } */ void EnemyPattern2(int i){ int j; static int pattern=1; if(enemy[i].counter<50) enemy[i].y+=2.0f; if(enemy[i].counter==75){ for(j=0;j<ENEMY_TOTAL_NUM;j++) if(EnemyShot[j].flag==0) break; EnemyShot[j].mem_ex=enemy[i].x; EnemyShot[j].mem_ey=enemy[i].y; EnemyShot[j].mem_px=Player.x; EnemyShot[j].mem_py=Player.y; EnemyShot[j].counter=0; EnemyShot[j].flag=1; EnemyShot[j].pattern=pattern; EnemyShot[j].img=0; pattern++; } if(enemy[i].counter>375) enemy[i].y-=2.0f; } /* void EnemyPattern3(int i){ enemy[i].x+=sin(PI*enemy[i].counter/40.0f)*5.0f; enemy[i].y+=1.5f; } void EnemyPattern4(int i){ if(enemy[i].counter<100) enemy[i].y+=2.5f; if(enemy[i].counter>=100){ enemy[i].x+=sin(PI*(enemy[i].counter-100.0f)/160.0f)*2.5f; enemy[i].y+=sin(PI*(enemy[i].counter-20.0f )/160.0f)*2.5f; } } */ void EnemyPattern5(int i){ int j; static int pattern=11; if(enemy[i].counter<50) enemy[i].y+=3.0f; if(enemy[i].counter==75){ for(j=0;j<ENEMY_TOTAL_NUM;j++) if(EnemyShot[j].flag==0) break; EnemyShot[j].mem_ex=enemy[i].x; EnemyShot[j].mem_ey=enemy[i].y; EnemyShot[j].mem_px=Player.x; EnemyShot[j].mem_py=Player.y; EnemyShot[j].counter=0; EnemyShot[j].flag=1; EnemyShot[j].pattern=pattern; EnemyShot[j].img=2; pattern++; } if(enemy[i].counter>175) enemy[i].y-=2.0f; } void EnemyControl(){ for(int i=0;i<100;i++){ if(enemy[i].flag==1){ if(enemy[i].pattern==2) EnemyPattern2(i); if(enemy[i].pattern==5) EnemyPattern5(i); } } } void EnemyCalcDisp(){ int i; for(i=0;i<100;i++) if(enemy[i].flag==0) break; switch(counter){ case 50: case 350: case 650: case 950: case 1250: case 1550: case 1850: case 2150: case 2450: case 2750: enemy[i].pattern=2; //どういう軌道を描くか enemy[i].flag=1; //出現フラグを立てる enemy[i].counter=0;//出現して何カウント目か測るカウンター初期化 enemy[i].size=0.5f;//敵の大きさ enemy[i].range=(int)((MONSTER1_X_SIZE-60)/2.0f*enemy[i].size); //※画像サイズから余白の部分60ピクセルを引く enemy[i].x=210.0; //xの初期座標 enemy[i].y=-10.0; //yの初期座標 break; case 3050: case 3350: case 3650: enemy[i].pattern=5; //どういう軌道を描くか enemy[i].flag=1; //出現フラグを立てる enemy[i].counter=0;//出現して何カウント目か測るカウンター初期化 enemy[i].size=0.5f;//敵の大きさ enemy[i].range=(int)((MONSTER1_X_SIZE-60)/2.0f*enemy[i].size); //※画像サイズから余白の部分60ピクセルを引く enemy[i].x=210.0; //xの初期座標 enemy[i].y=-10.0; //yの初期座標 break; default: break; } int enemy_img[8]={0,1,2,3,4,3,2,1}; for(i=0;i<100;i++){ if(enemy[i].flag==1){ enemy[i].counter++; DrawRotaGraph( (int)enemy[i].x , (int)enemy[i].y , enemy[i].size , 0.0f , img_enemy1[enemy_img[counter%32/4]] , TRUE ) ; if(enemy[i].x<0.0-MONSTER1_X_SIZE/2.0*enemy[i].size || enemy[i].x>420.0+MONSTER1_X_SIZE/2.0*enemy[i].size || enemy[i].y<0.0-MONSTER1_Y_SIZE/2.0*enemy[i].size || enemy[i].y<0.0-MONSTER1_Y_SIZE/2.0*enemy[i].size) enemy[i].flag=0; } } }
/* enemy_shot.cpp */
#include "DxLib.h" #include "ExternGV.h" #include <math.h> #define PI 3.141593 #define PATTERN1SPEED 2.0f void EnemyShotPattern1(int i){ if(EnemyShot[i].counter==0){ EnemyShot[i].EnemyShots[0].flag=1; EnemyShot[i].EnemyShots[0].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[0].y=EnemyShot[i].mem_ey; EnemyShot[i].EnemyShots[0].angle = atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } EnemyShot[i].EnemyShots[0].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[0].angle); EnemyShot[i].EnemyShots[0].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[0].angle); } void EnemyShotPattern2(int i){ for(int j=0;j<10;j++){ if(EnemyShot[i].counter==20*j){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; EnemyShot[i].EnemyShots[0].angle = atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[0].angle); EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[0].angle); } } void EnemyShotPattern3(int i){ for(int j=0;j<10;j++){ if(EnemyShot[i].counter==20*j){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; EnemyShot[i].EnemyShots[j].angle = atan2(Player.y-EnemyShot[i].mem_ey,Player.x-EnemyShot[i].mem_ex); StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[j].angle); } } void EnemyShotPattern4(int i){ for(int j=0;j<10;j++){ if(EnemyShot[i].counter==10*j){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; EnemyShot[i].EnemyShots[j].angle = atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[j].angle)*(double)(0.8+0.4*j); EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[j].angle)*(double)(0.8+0.4*j); } } void EnemyShotPattern5(int i){ for(int j=0;j<21;j++){ if(EnemyShot[i].counter==7*j){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; EnemyShot[i].EnemyShots[j].angle = atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[j].angle)+(double)(j-10)/4.0f; EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[j].angle); } } void EnemyShotPattern6(int i){ for(int j=0;j<200;j++){ if(EnemyShot[i].counter==j){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; if(j==0) EnemyShot[i].EnemyShots[j].angle = atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); else EnemyShot[i].EnemyShots[j].angle = EnemyShot[i].EnemyShots[j-1].angle+0.2f; StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[j].angle); } } void EnemyShotPattern7(int i){ for(int s=0;s<10;s++){ if(EnemyShot[i].counter==10*s){ for(int j=20*s;j<20*(s+1);j++){ EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; EnemyShot[i].EnemyShots[j].flag=1; if(j==0) EnemyShot[i].EnemyShots[0].angle = atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); else EnemyShot[i].EnemyShots[j].angle = EnemyShot[i].EnemyShots[0].angle + 2.0f*PI * (double)j / 20.0f; } StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } } for(int j=0;j<200;j++){ if(EnemyShot[i].EnemyShots[j].flag==1){ EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[j].angle)*2.0f; EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[j].angle)*2.0f; } } } void EnemyShotPattern8(int i){ for(int s=0;s<10;s++){ if(EnemyShot[i].counter==10*s){ for(int j=20*s;j<20*(s+1);j++){ EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; EnemyShot[i].EnemyShots[j].flag=1; if(j==20*s) EnemyShot[i].EnemyShots[20*s].angle = atan2(Player.y-EnemyShot[i].mem_ey,Player.x-EnemyShot[i].mem_ex); else EnemyShot[i].EnemyShots[j].angle = EnemyShot[i].EnemyShots[20*s].angle + 2.0f*PI * (double)j / 20.0f; } StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } } for(int j=0;j<200;j++){ if(EnemyShot[i].EnemyShots[j].flag==1){ EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[j].angle)*2.0f; EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[j].angle)*2.0f; } } } void EnemyShotPattern9(int i){ for(int j=0;j<120;j++){ if(EnemyShot[i].counter==2*j){ EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].angle = atan2(Player.y-EnemyShot[i].mem_ey,Player.x-EnemyShot[i].mem_ex)+(double)GetRand(1500)/1000.0f-0.75f; StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } } for(int j=0;j<200;j++){ if(EnemyShot[i].EnemyShots[j].flag==1){ EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[j].angle); } } } void EnemyShotPattern10(int i){ for(int j=0;j<200;j++){ if(EnemyShot[i].counter==j){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey; if(j==0) EnemyShot[i].EnemyShots[j].angle = atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); else EnemyShot[i].EnemyShots[j].angle = EnemyShot[i].EnemyShots[j-1].angle+0.2f; StopSoundMem(sound_enemy_shot[0]); PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } EnemyShot[i].EnemyShots[j].x+=PATTERN1SPEED*cos(EnemyShot[i].EnemyShots[j].angle)+sin((double)EnemyShot[i].counter/40.0f)*2.0f; EnemyShot[i].EnemyShots[j].y+=PATTERN1SPEED*sin(EnemyShot[i].EnemyShots[j].angle)*0.7f; } } void EnemyShotPattern11(int i){ if(EnemyShot[i].counter==0){ for(int j=0;j<5;j++){ EnemyShot[i].EnemyShots[j].flag=1; if(j==0) EnemyShot[i].EnemyShots[j].angle=atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); else EnemyShot[i].EnemyShots[j].angle = EnemyShot[i].EnemyShots[0].angle+2.0f*PI*(double)j/5.0f; } PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } double old_x[5],old_y[5]; for(int j=0;j<5;j++){ old_x[j]=EnemyShot[i].EnemyShots[j].x; old_y[j]=EnemyShot[i].EnemyShots[j].y; EnemyShot[i].EnemyShots[j].angle += 2.0f*PI/500.0f; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].mem_ex+(double)EnemyShot[i].counter*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y=EnemyShot[i].mem_ey+(double)EnemyShot[i].counter*sin(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].disp_angle=atan2(EnemyShot[i].EnemyShots[j].y-old_y[j],EnemyShot[i].EnemyShots[j].x-old_x[j]); } } void EnemyShotPattern12(int i){ if(EnemyShot[i].counter==0){ EnemyShot[i].Enx[0]=EnemyShot[i].mem_ex; EnemyShot[i].Eny[0]=EnemyShot[i].mem_ey; for(int j=0;j<6;j++){ EnemyShot[i].EnemyShots[j].x=EnemyShot[i].Enx[0]; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].Eny[0]; EnemyShot[i].EnemyShots[j].flag=1; if(j==0) EnemyShot[i].EnemyShots[j].angle=atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); else EnemyShot[i].EnemyShots[j].angle = EnemyShot[i].EnemyShots[0].angle+2.0f*PI*(double)j/6.0f; } PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } if(EnemyShot[i].counter==80){ double ang[6]; EnemyShot[i].img=1; for(int s=0;s<6;s++){ ang[s]=EnemyShot[i].EnemyShots[s].disp_angle; EnemyShot[i].Enx[s]=EnemyShot[i].EnemyShots[s].x; EnemyShot[i].Eny[s]=EnemyShot[i].EnemyShots[s].y; } for(int s=0;s<6;s++){ for(int j=7*s;j<7*(s+1);j++){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].angle=ang[s]+(double)(j%7-3)/6.0f+PI; } } PlaySoundMem(sound_enemy_shot[1],DX_PLAYTYPE_BACK); } if(EnemyShot[i].counter==160){ double ang[6*7]; EnemyShot[i].img=2; for(int s=0;s<6*7;s++){ ang[s]=EnemyShot[i].EnemyShots[s].disp_angle; EnemyShot[i].Enx[s]=EnemyShot[i].EnemyShots[s].x; EnemyShot[i].Eny[s]=EnemyShot[i].EnemyShots[s].y; } for(int s=0;s<6;s++){ for(int j=7*s;j<7*(s+1);j++){ for(int t=9*j;t<9*(j+1);t++){ EnemyShot[i].EnemyShots[t].speed=GetRand(100); EnemyShot[i].EnemyShots[t].flag=1; EnemyShot[i].EnemyShots[t].angle=ang[j]+(double)(t%7-GetRand(7)-10)/6.0f+PI; } } } PlaySoundMem(sound_enemy_shot[1],DX_PLAYTYPE_BACK); } double old_x[400],old_y[400]; if(EnemyShot[i].counter<80){ for(int j=0;j<6;j++){ old_x[j]=EnemyShot[i].EnemyShots[j].x; old_y[j]=EnemyShot[i].EnemyShots[j].y; EnemyShot[i].EnemyShots[j].angle += 2.0f*PI/500.0f; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].Enx[0]+(double)EnemyShot[i].counter*1.5f*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y=EnemyShot[i].Eny[0]+(double)EnemyShot[i].counter*1.5f*sin(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].disp_angle=atan2(EnemyShot[i].EnemyShots[j].y-old_y[j],EnemyShot[i].EnemyShots[j].x-old_x[j]); } } else if(EnemyShot[i].counter<160){ for(int j=0;j<6*7;j++){ old_x[j]=EnemyShot[i].EnemyShots[j].x; old_y[j]=EnemyShot[i].EnemyShots[j].y; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].Enx[j/7]+(double)((1.5f+(double)(j%2)/3.0f)*(EnemyShot[i].counter-80))*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y=EnemyShot[i].Eny[j/7]+(double)((1.5f+(double)(j%2)/3.0f)*(EnemyShot[i].counter-80))*sin(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].disp_angle=atan2(EnemyShot[i].EnemyShots[j].y-old_y[j],EnemyShot[i].EnemyShots[j].x-old_x[j]); } } else { for(int j=0;j<6*7*9;j++){ double s; s=(double)(50+EnemyShot[i].EnemyShots[j].speed)/100.0f; old_x[j]=EnemyShot[i].EnemyShots[j].x; old_y[j]=EnemyShot[i].EnemyShots[j].y; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].Enx[j/9]+(double)((1.0f+((double)(j%3)/4.0f)/5.0f)*(EnemyShot[i].counter-160))*s*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y=EnemyShot[i].Eny[j/9]+(double)((1.0f+((double)(j%3)/4.0f)/5.0f)*(EnemyShot[i].counter-160))*s*sin(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].disp_angle=atan2(EnemyShot[i].EnemyShots[j].y-old_y[j],EnemyShot[i].EnemyShots[j].x-old_x[j]); } } } void EnemyShotPattern13(int i){ if(EnemyShot[i].counter==0){ EnemyShot[i].Enx[0]=EnemyShot[i].mem_ex; EnemyShot[i].Eny[0]=EnemyShot[i].mem_ey; for(int j=0;j<6;j++){ EnemyShot[i].EnemyShots[j].x=EnemyShot[i].Enx[0]; EnemyShot[i].EnemyShots[j].y=EnemyShot[i].Eny[0]; EnemyShot[i].EnemyShots[j].flag=1; if(j==0) EnemyShot[i].EnemyShots[j].angle=atan2(EnemyShot[i].mem_py-EnemyShot[i].mem_ey,EnemyShot[i].mem_px-EnemyShot[i].mem_ex); else EnemyShot[i].EnemyShots[j].angle = EnemyShot[i].EnemyShots[0].angle+2.0f*PI*(double)j/6.0f; } PlaySoundMem(sound_enemy_shot[0],DX_PLAYTYPE_BACK); } if(EnemyShot[i].counter==80){ double ang[10]; EnemyShot[i].img=1; for(int s=0;s<10;s++){ ang[s]=EnemyShot[i].EnemyShots[s].disp_angle; EnemyShot[i].Enx[s]=EnemyShot[i].EnemyShots[s].x; EnemyShot[i].Eny[s]=EnemyShot[i].EnemyShots[s].y; } for(int s=0;s<6;s++){ for(int j=10*s;j<10*(s+1);j++){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].EnemyShots[j].angle=ang[s]+2.0f*PI*(double)(j)/10.0f; } } PlaySoundMem(sound_enemy_shot[1],DX_PLAYTYPE_BACK); } if(EnemyShot[i].counter==160){ EnemyShot[i].img=2; for(int j=0;j<6*10;j++){ EnemyShot[i].EnemyShots[j].flag=1; EnemyShot[i].Enx[j]=EnemyShot[i].EnemyShots[j].x; EnemyShot[i].Eny[j]=EnemyShot[i].EnemyShots[j].y; EnemyShot[i].EnemyShots[j].speed=GetRand(100); EnemyShot[i].EnemyShots[j].angle=atan2(Player.y-EnemyShot[i].EnemyShots[j].y,Player.x-EnemyShot[i].EnemyShots[j].x); } PlaySoundMem(sound_enemy_shot[1],DX_PLAYTYPE_BACK); } double old_x[400],old_y[400]; if(EnemyShot[i].counter<80){ for(int j=0;j<6;j++){ old_x[j]=EnemyShot[i].EnemyShots[j].x; old_y[j]=EnemyShot[i].EnemyShots[j].y; EnemyShot[i].EnemyShots[j].angle += 2.0f*PI/500.0f; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].Enx[0]+(double)EnemyShot[i].counter*1.5f*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y=EnemyShot[i].Eny[0]+(double)EnemyShot[i].counter*1.5f*sin(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].disp_angle=atan2(EnemyShot[i].EnemyShots[j].y-old_y[j],EnemyShot[i].EnemyShots[j].x-old_x[j]); } } else if(EnemyShot[i].counter<160){ for(int j=0;j<6*10;j++){ old_x[j]=EnemyShot[i].EnemyShots[j].x; old_y[j]=EnemyShot[i].EnemyShots[j].y; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].Enx[j/10]+(double)((1.5f+(double)(j%2)/3.0f)*(EnemyShot[i].counter-80))*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y=EnemyShot[i].Eny[j/10]+(double)((1.5f+(double)(j%2)/3.0f)*(EnemyShot[i].counter-80))*sin(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].disp_angle=atan2(EnemyShot[i].EnemyShots[j].y-old_y[j],EnemyShot[i].EnemyShots[j].x-old_x[j]); } } else { for(int j=0;j<6*10;j++){ double s; s=(double)(50+EnemyShot[i].EnemyShots[j].speed)/100.0f; old_x[j]=EnemyShot[i].EnemyShots[j].x; old_y[j]=EnemyShot[i].EnemyShots[j].y; EnemyShot[i].EnemyShots[j].x=EnemyShot[i].Enx[j]+(double)(EnemyShot[i].counter-160)*s*cos(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].y=EnemyShot[i].Eny[j]+(double)(EnemyShot[i].counter-160)*s*sin(EnemyShot[i].EnemyShots[j].angle); EnemyShot[i].EnemyShots[j].disp_angle=atan2(EnemyShot[i].EnemyShots[j].y-old_y[j],EnemyShot[i].EnemyShots[j].x-old_x[j]); } } } void EnemyShotControl(){ int i; for(i=0;i<ENEMY_TOTAL_NUM;i++){ if(EnemyShot[i].flag==1){ if(EnemyShot[i].pattern==1) EnemyShotPattern1(i); if(EnemyShot[i].pattern==2) EnemyShotPattern2(i); if(EnemyShot[i].pattern==3) EnemyShotPattern3(i); if(EnemyShot[i].pattern==4) EnemyShotPattern4(i); if(EnemyShot[i].pattern==5) EnemyShotPattern5(i); if(EnemyShot[i].pattern==6) EnemyShotPattern6(i); if(EnemyShot[i].pattern==7) EnemyShotPattern7(i); if(EnemyShot[i].pattern==8) EnemyShotPattern8(i); if(EnemyShot[i].pattern==9) EnemyShotPattern9(i); if(EnemyShot[i].pattern==10) EnemyShotPattern10(i); if(EnemyShot[i].pattern==11) EnemyShotPattern11(i); if(EnemyShot[i].pattern==12) EnemyShotPattern12(i); if(EnemyShot[i].pattern==13) EnemyShotPattern13(i); } } } void EnemyShotCalcDisp(){ for(int i=0;i<ENEMY_TOTAL_NUM;i++){ if(EnemyShot[i].flag==1){ EnemyShot[i].counter++; for(int j=0;j<ENEMY_TOTAL_SHOT_NUM;j++){ if(EnemyShot[i].EnemyShots[j].flag==1){ DrawRotaGraph( (int)EnemyShot[i].EnemyShots[j].x , (int)EnemyShot[i].EnemyShots[j].y , 1.0f , EnemyShot[i].EnemyShots[j].disp_angle , img_enemy_shot[EnemyShot[i].img] , TRUE ) ; if(EnemyShot[i].EnemyShots[j].x<-20.0||EnemyShot[i].EnemyShots[j].x>440.0||EnemyShot[i].EnemyShots[j].y<-20.0||EnemyShot[i].EnemyShots[j].y>500) EnemyShot[i].EnemyShots[j].flag=0; } } } } for(int i=0;i<ENEMY_TOTAL_NUM;i++){ if(EnemyShot[i].flag==1){ for(int j=0;j<ENEMY_TOTAL_SHOT_NUM;j++){ if(EnemyShot[i].EnemyShots[j].flag==1) return; } EnemyShot[i].flag=0; } } }
/* GlobalVariable.h */
/* 定義 */ #define PLAYER_MAX_SHOT1 11 #define PLAYER_MAX_SHOT2 15 #define ENEMY_TOTAL_NUM 100 #define ENEMY_TOTAL_SHOT_NUM 400 #define MONSTER1_X_SIZE 140.0 #define MONSTER1_Y_SIZE 128.0 /* グローバル宣言 */ int counter=0; int color_white; char Key[256]; //画像ファイルハンドル int img_background[2]; int img_player[4]; int img_enemy1[5]; //玉 int img_player_shot[2]; int img_enemy_shot[20]; //各種変数 int ShotLevel=11; //音声ファイルハンドル int sound_player_shot[2]; int sound_enemy_shot[2]; int sound_enemy_death; //構造体 //プレイヤー typedef struct{ double x,y; int status,counter; } BODY_player_t; BODY_player_t Player; //敵 typedef struct{ double x,y,size; int flag,counter,hp,pattern,range; } BODY_enemy_t; BODY_enemy_t enemy[ENEMY_TOTAL_NUM];//[100] //ショット typedef struct{ double x,y; int flag; } SHOT_t; SHOT_t PlayerShot[PLAYER_MAX_SHOT1][PLAYER_MAX_SHOT2];//[11][15] typedef struct{ int flag; double x,y,angle,disp_angle,speed; } ENEMY_SHOTS_t; //敵ショット typedef struct{ int flag,pattern,counter,img; double mem_ex,mem_ey,mem_px,mem_py,Enx[60],Eny[60];//[200]; ENEMY_SHOTS_t EnemyShots[ENEMY_TOTAL_SHOT_NUM];//[200]; } ENEMY_SHOT_t; ENEMY_SHOT_t EnemyShot[ENEMY_TOTAL_NUM];//[100];
/* ExternGV.h */
/* 定義 */ #define PLAYER_MAX_SHOT1 11 #define PLAYER_MAX_SHOT2 15 #define ENEMY_TOTAL_NUM 100 #define ENEMY_TOTAL_SHOT_NUM 400 #define MONSTER1_X_SIZE 140.0 #define MONSTER1_Y_SIZE 128.0 /* extern 宣言 */ extern int counter; extern int color_white; extern char Key[256]; //画像ファイルハンドル extern int img_background[2]; extern int img_player[4]; extern int img_enemy1[5]; //玉 extern int img_player_shot[2]; extern int img_enemy_shot[20]; //各種変数 extern int ShotLevel; //音声ファイルハンドル extern int sound_player_shot[2]; extern int sound_enemy_shot[2]; extern int sound_enemy_death; //構造体 //プレイヤー typedef struct{ double x,y; int status,counter; int shot [11][15]; } BODY_player_t; extern BODY_player_t Player; //敵 typedef struct{ double x,y,size; int flag,counter,hp,pattern,range; } BODY_enemy_t; extern BODY_enemy_t enemy[100]; //ショット typedef struct{ double x,y; int flag; } SHOT_t; extern SHOT_t PlayerShot[PLAYER_MAX_SHOT1][PLAYER_MAX_SHOT2]; typedef struct{ int flag; double x,y,angle,disp_angle,speed; } ENEMY_SHOTS_t; //敵ショット typedef struct{ int flag,pattern,counter,img; double mem_ex,mem_ey,mem_px,mem_py,Enx[60],Eny[60];//[200]; ENEMY_SHOTS_t EnemyShots[ENEMY_TOTAL_SHOT_NUM];//[200]; } ENEMY_SHOT_t; extern ENEMY_SHOT_t EnemyShot[ENEMY_TOTAL_NUM];//[100];
/* main.cpp */
#include "DxLib.h" #include "GlobalVariable.h" int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){ extern void img_sound_load(); extern void initialization(); extern void SetColor(); extern void Background(); extern void PlayerShotCalc(); extern void PlayerShotDisp(); extern void PlayerControl(); extern void EnemyControl(); extern void EnemyCalcDisp(); extern void EnemyShotControl(); extern void EnemyShotCalcDisp(); extern void CollisionDetection(); extern void Background2(); extern void FpsTimeFanction(); int RefreshTime=0; // ChangeWindowMode( TRUE ) ; if( DxLib_Init() == -1 ) return -1; SetDrawScreen( DX_SCREEN_BACK ) ; //裏画面を使用する。 img_sound_load(); initialization(); SetColor(); while(ProcessMessage() == 0 && GetHitKeyStateAll(Key) == 0){ RefreshTime = GetNowCount(); //今の時間を取得 ClearDrawScreen(); //裏画面のデータを全て削除 Background(); PlayerShotCalc(); PlayerShotDisp(); PlayerControl(); EnemyControl(); EnemyCalcDisp(); EnemyShotControl(); EnemyShotCalcDisp(); CollisionDetection(); Background2(); FpsTimeFanction(); ScreenFlip() ; //裏画面データを表画面へ反映 counter++; if(Key[KEY_INPUT_ESCAPE]==1) break; //Escapeが押されたら終了 while(GetNowCount() - RefreshTime < 17);//1周の処理が17ミリ秒になるまで待つ } DxLib_End() ; return 0 ; }
/* img_sound_load.cpp */
#include "DxLib.h" #include "ExternGV.h" void img_sound_load(){ //画像ハンドル取得 img_background[0] = LoadGraph("Sh/img/back/background0.png"); img_background[1] = LoadGraph("Sh/img/back/background1.png"); img_player_shot[0] = LoadGraph("Sh/img/shot/player/1.png"); img_player_shot[1] = LoadGraph("Sh/img/shot/player/2.png"); img_enemy_shot[0] = LoadGraph("Sh/img/shot/enemy/shot1.png"); img_enemy_shot[1] = LoadGraph("Sh/img/shot/enemy/0.png"); img_enemy_shot[2] = LoadGraph("Sh/img/shot/enemy/1.png"); img_enemy1[0] = LoadGraph("Sh/img/mons/mons1_0.png"); img_enemy1[1] = LoadGraph("Sh/img/mons/mons1_1.png"); img_enemy1[2] = LoadGraph("Sh/img/mons/mons1_2.png"); img_enemy1[3] = LoadGraph("Sh/img/mons/mons1_3.png"); img_enemy1[4] = LoadGraph("Sh/img/mons/mons1_4.png"); LoadDivGraph( "Sh/img/char/player.png" , 4 , 4 , 1 , 32 , 48 , img_player ) ; //音楽ハンドル取得 sound_player_shot[0]= LoadSoundMem("Sh/sound/Player/shot.ogg"); sound_enemy_shot[0] = LoadSoundMem("Sh/sound/enemy/normalshot.ogg"); sound_enemy_shot[1] = LoadSoundMem("Sh/sound/enemy/shot3.ogg"); sound_enemy_death = LoadSoundMem("Sh/sound/enemy/death.ogg"); }
/* initial.cpp */
#include "DxLib.h" #include "ExternGV.h" void initialization(){ Player.x=200.0; Player.y=400.0; Player.counter=0; Player.status=0; for(int i=0;i<PLAYER_MAX_SHOT1;i++) for(int j=0;j<PLAYER_MAX_SHOT2;j++) PlayerShot[i][j].flag=0; for(int i=0;i<ENEMY_TOTAL_NUM;i++){ enemy[i].flag=0; EnemyShot[i].flag=0; for(int j=0;j<ENEMY_TOTAL_SHOT_NUM;j++){ EnemyShot[i].EnemyShots[j].flag=0; EnemyShot[i].EnemyShots[j].disp_angle=0; EnemyShot[i].EnemyShots[j].speed=0; } } } void SetColor(){ color_white = GetColor(255,255,255); //白色ハンドルを取得 }
DXライブラリ著作権表示
DX Library Copyright (C) 2001-2006 Takumi
Yamada.