//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit3.h"
#include "Unit2.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int shechang=40;//StrToInt(Form3->Edit3->Text);
int a=0x27,b=0,c=0,i=0;
int chang=2;
int level=1;//StrToInt(Form3->Edit2->Text);
int ti[8]={500,400,300,200,100,50,20,10};
int score=0;
String abc="";
struct weizhi
{int x;int y;} wz[100],food;
//---------------------------------------------------------------------
//----------------------------------------------------------------------
void can(int x,int y,bool z=1)
{
if(z)
Form1->Canvas->Brush->Color=clRed;
else
Form1->Canvas->Brush->Color=clYellow;
Form1->Canvas->Rectangle(x*15,y*15,(x+1)*15,(y+1)*15);
}
//--------------------------------------------------------------------------
void chu()
{
int x=rand()%18+1;
int y=rand()%38+1;
for(int i=0;i<shechang;i++)
if(wz[i].x!=x&&wz[i].y!=y)
{ food.x=x;food.y=y;
}
else
{food.x=(x+shechang)%20;food.y=(y+shechang)%40;}
}
//------------------------------------------------------------------------
bool chi(int x,int y)
{
return (x==food.x&&y==food.y);
}
//------------------------------------------------------------------------
void gameover()
{
Form1->Timer1->Enabled=false;
if(Application->MessageBoxA("try again???","game over",129)==1)
{Form1->Caption="level:1 score:0";
Form1->Timer1->Interval=ti[0];
chang=2;
wz[0].x=0;
level=1;
score=0;
wz[0].y=39;
Form1->Timer1->Enabled=true;
a=0x27;i=0; chu();
} else {Form1->Close();}
}
//----------------------------------------------------------------------
void biti()
{
abc+="level:";
abc+=level;
abc+=" score:";
abc+=score;
Form1->Caption=abc;
abc="";
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
randomize();chu(); if(Application->MessageBoxA("音乐否?","提示",129)==1)
{ OpenDialog1->Execute();
MediaPlayer1->FileName= OpenDialog1->FileName;
MediaPlayer1->Open();
MediaPlayer1->Play();}
Timer1->Enabled=true;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Repaint();
can(food.x,food.y,0);
i%=shechang;
int temp=(i+shechang-1)%shechang;
switch(a)
{
case 0x25:
{wz[i].x=wz[temp].x-1;wz[i].y=wz[temp].y;break; }
case 0x26:
{wz[i].y=wz[temp].y-1;wz[i].x=wz[temp].x;break; }
case 0x27:
{wz[i].x=wz[temp].x+1;wz[i].y=wz[temp].y;break; }
case 0x28:
{wz[i].y=wz[temp].y+1;wz[i].x=wz[temp].x;break; }
default:break;
}
for(int j=1;j<chang;j++)
{
can(wz[(i-j+shechang)%shechang].x,wz[(i-j+shechang)%shechang].y);
if(wz[i].x==wz[(i-j+shechang)%shechang].x&&wz[i].y==wz[(i-j+shechang)%shechang].y)
{ gameover(); }
}
Form1->Canvas->Brush->Color=clBlue;
Form1->Canvas->Rectangle(wz[i].x*15,wz[i].y*15,(wz[i].x+1)*15,(wz[i].y+1)*15);
if(chi(wz[i].x,wz[i].y))
{
chang++;
score+=10;
chu();
Beep();
biti();
if(chang>shechang)
{ Timer1->Enabled=false;
Application->MessageBoxA("继续.....","升级!",128);
level++;
Timer1->Interval=ti[level-1];
chang=2;
biti();
wz[0].x=0;
wz[0].y=39;
a=0x27;
i=0;
chu();
Timer1->Enabled=true;
if(level>8)
gameover();
}
}
if(wz[i].x>19||wz[i].x<0||wz[i].y>39||wz[i].y<0)
{
gameover();
}
i++;
}
//------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{ c=a;
switch(Key)
{case 0x25:
case 0x26:
case 0x27:
case 0x28:
{a=Key;if(level<7) Timer1->Interval=20;break;}
case 0x20:
{a=c;
if(Timer1->Enabled==false)
Timer1->Enabled=true;
else
Timer1->Enabled=false;break;}
default:{a=c;break;}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N1Click(TObject *Sender)
{
Timer1->Enabled=false;
Form1->Enabled=false;
Form2->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender)
{
/*Timer1->Enabled=false;
Form1->Enabled=false;
Form3->Show(); */
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
Timer1->Interval=ti[level-1];
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MediaPlayer1Notify(TObject *Sender)
{
if (MediaPlayer1->Position==MediaPlayer1->Length
&& MediaPlayer1->NotifyValue==nvSuccessful)
{
MediaPlayer1->Notify = true;
MediaPlayer1->Position = 0;
MediaPlayer1->Play();
}
}
//---------------------------------------------------------------------------