//--------------------------------------------------------------------------- #include #include #include #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "CSPIN" #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { HRGN r = CreateRoundRectRgn(0,0,Width,Height,50,50); // HRGN r = CreateEllipticRgn( 0, 0, Width, Height); SetWindowRgn(Handle,r,true); DeleteObject (r); MidiOpen(); // プロジェクト|オプションでForm2を自動生成しないようにする // Form2->Visible=false にしておく // 自動生成すると、Form1がVisible=falseにしてても // 表示されてしまう。 // // Form2 = new TForm2(this); // Form2->ShowModal(); // delete(Form2); } //--------------------------------------------------------------------------- // ゲーム開始準備 void __fastcall TForm1::Panel2Click(TObject *Sender) { // 今オープニング中だったらTimer1のイベントの関連付けを直す if(Timer1->OnTimer==Opening) { Timer1->OnTimer = Timer1Timer; if(Mode==1) Timer1->Interval = 30; else Timer1->Interval = 17; Timer1->Enabled = false; } // ステージ画面の準備 PaintBox1->Canvas->Brush->Color = clAqua; PaintBox1->Canvas->FillRect( Rect(0,0,PaintBox1->Width,PaintBox1->Height)); PaintBox1->Canvas->Brush->Color = clBlue; Label2->Caption = "Stage:"+IntToStr(lv); Panel1->Caption = "スイッチ"; // 障害物の作成 // j:障害物の隙間の高さ { int j=0; for(int i=20+Mode*20;iWidth;i+=20-3*lv+Mode*20) { // jがPaintBox1の枠をはみ出さないようにする do { j += random(160+lv*8)-80-lv*4; j=(j>0)*j; // j<0 なら j=0 if(j>PaintBox1->Height) j=PaintBox1->Height; }while(j<20 || j>PaintBox1->Height-20); j = random(PaintBox1->Height-40); PaintBox1->Canvas->FillRect(Rect(i,0,i+5,j)); PaintBox1->Canvas->FillRect( Rect(i,j+lv+50,i+5,PaintBox1->Height)); } } x=0;y=0;vy=0;ay=0.2; PaintBox1->Canvas->MoveTo(x,y); subscore=0; // Timer1->Enabled = true; Timer1Timer(NULL); } //--------------------------------------------------------------------------- // ゲームメインルーチン void __fastcall TForm1::Timer1Timer(TObject *Sender) { for(;;) { // 点の移動、スコア更新 x++; vy+=ay; y+=vy; if(Mode==1) subscore += abs(vy); else subscore += 2-abs(vy); // クリアーしたか判定 if(x>PaintBox1->Width) { // Timer1->Enabled = false; Panel1->Caption = "Stage Clear!"; Timer1->Interval += 1; lv++; score += subscore; Label1->Caption = "Score:"+IntToStr(score); break; } // ゲームオーバーか判定 if(PaintBox1->Canvas->Pixels[x][y] == clBlue || y>PaintBox1->Height || y<0) { // Timer1->Enabled = false; Panel1->Caption = "Game Over"; return; } // 点の描画 PaintBox1->Canvas->Pixels[x][y] = (TColor)(abs(vy*2)*0x001520); ShortMsg(0xc0,tone,0); ShortMsg(0x90,60+abs(vy),100); Wait(Timer1->Interval); // for(int i=0;i<100;i++); ShortMsg(0x80,60+abs(vy),0); } } //--------------------------------------------------------------------------- // 点が受ける加速度の設定 void __fastcall TForm1::Panel1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { // 加速度=−1 ay = -0.45+Mode*0.15; Panel1->Color = clLime; } //--------------------------------------------------------------------------- void __fastcall TForm1::Panel1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { // 加速度=+1 ay = 0.45-Mode*0.15; Panel1->Color = (TColor)0x00ee00; } //--------------------------------------------------------------------------- // Startパネルをボタンに見せる void __fastcall TForm1::Panel2MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { Panel2->BevelOuter = bvLowered; } //--------------------------------------------------------------------------- void __fastcall TForm1::Panel2MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { Panel2->BevelOuter = bvRaised; } //--------------------------------------------------------------------------- // ウインドウの移動の処理 void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { MousePos[2]=1; MousePos[0]=X; MousePos[1]=Y; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { // ボタンが押されてたらウィンドウを移動 if(MousePos[2]==1) { Form1->Left += X-MousePos[0]; Form1->Top += Y-MousePos[1]; } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { MousePos[2]=0; } //------------------------------------------------------- // ウィンドウの形の設定 /* void __fastcall TForm1::CreateParams(TCreateParams& Params) { TForm::CreateParams(Params); Params.Style|= WS_DLGFRAME; //立体的に表示される(サイズ変更できない) } */ //--------------------------------------------------------------------------- // オープニング void __fastcall TForm1::Opening(TObject *Sender) { // Label1の文字の色を変化させる cl+=0x001030; if(cl>0xffffff) cl=0; Label1->Font->Color = (TColor)cl; } //-------------------------------------------------------- // 初期設定 void __fastcall TForm1::FormCreate(TObject *Sender) { // 乱数の初期化(stdlib.h を手動でインクルード) randomize(); // グローバル変数の初期化 lv = 1; score = 0; cl = 0x0000ff; Label2->Caption=""; Timer1->OnTimer = Opening; Timer1->Enabled = true; } //--------------------------------------------------------------------------- // 終了 void __fastcall TForm1::Exit1Click(TObject *Sender) { midiOutReset(MidiOut); midiOutClose(MidiOut); MidiOut=0; Close(); } //--------------------------------------------------------------------------- // MIDIデバイスオープン void TForm1::MidiOpen() { MidiDev=midiOutGetNumDevs(); if(MidiDev==0) return; int id=1; UINT rc = midiOutOpen(&MidiOut,id,(DWORD)Form1->Handle,0,CALLBACK_WINDOW); if(rc!=MMSYSERR_NOERROR){ char msg[255]; midiOutGetErrorText(rc,msg,sizeof(msg)); throw Exception(AnsiString(msg)); } tone = 0; ShortMsg(0xc0,tone,0); } //-------------------------------------------------------- // 短いイベントの送信 void __fastcall TForm1::ShortMsg(DWORD msg,DWORD val1,DWORD val2) { if(MidiDev==0) return; midiOutShortMsg(MidiOut,msg | (val1<<8) | (val2<<16)); } //------------------------------------------------------ void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if(Key=='A' ) tone++; if(Key=='Z') tone--; } //--------------------------------------------------------------------------- // 割り込みありのウエイト void TForm1::Wait(unsigned long wait) { long tick=GetTickCount(); while(GetTickCount()-tickProcessMessages(); } //--------------------------------------------------------------- // 割り込みなしのウエイト void TForm1::Wait2(unsigned long wait) { unsigned long tick=GetTickCount(); while(GetTickCount()-tick