//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define canvas_w 600
#define canvas_h 600
#define grid_w 10
#define grid_h 10
TForm1 *Form1;
using namespace std;
struct snake{
int head;
int length;
int eat;
};
snake snake;
const width = canvas_w/grid_w;
const height = canvas_h/grid_h;
TColor color[3] = {clWhite, clBlack, clRed};
Graphics::TBitmap *bitmap[3] = {NULL};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Panel1->ClientWidth = canvas_w;
Panel1->ClientHeight = canvas_h;
Image1->Align = alClient;
Image1->Stretch = true;
snake.head = 6;
snake.length = 20;
snake.eat = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
for(int i =0; i<3; i++)
{
bitmap[i] = new Graphics::TBitmap();
bitmap[i]->Width = grid_w;
bitmap[i]->Height = grid_h;
bitmap[i]->Canvas->Brush->Color = color[i];
bitmap[i]->Canvas->FillRect(Rect(0, 0, width, height));
}
initial();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
delete [] bitmap; //release
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::initial()
{
Image1->Canvas->Brush->Color = color[0]; //clear
Image1->Canvas->FillRect(Rect(0, 0, canvas_w, canvas_h));
Image1->Canvas->Draw(canvas_w/2, canvas_h/2, bitmap[2]); //food
for(int i= 0; i< snake.length; i++) //snake
Image1->Canvas->Draw(i*grid_w, canvas_h-2*grid_h, bitmap[1]);
Label2->Caption = 0; //score
}
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
if(Key == VK_UP)
Label2->Caption = 100;
}