Доброго времени суток! Написал что то похожое на Змейку, но возникла проблема: В общем, при подбирании 10-го или 15-го круга (каждый раз по разному) змейка просто проходит сквозь него. И еще, почему то функция rand() выдает постоянно одни и теже значения(после перезапуска). Помогите исправить. На форму достаточно кинуть панель, а на панель Image.
Код:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdlib>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int posRec[50][2],score = 2,lPos[50][2];
TPoint point;
//---------------------------------------------------------------------------
void __fastcall TForm1::RandPos()
{
Randomize();
point.x = rand() % ClientWidth+4;
point.y = rand() % ClientHeight+4;
if(point.x%32 != 0)
point.x = (point.x/32)*32+4;
if(point.y%12 != 0)
point.y = (point.y/12)*12+4;
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
RandPos();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
Image1->Canvas->Pen->Width = 4;
Image1->Canvas->Brush->Color = clSilver;
Image1->Canvas->Rectangle(1,1,ClientWidth-4,ClientHeight-4);
Image1->Canvas->Pen->Width = 1;
Image1->Canvas->TextOutA(ClientWidth-100,10,"ГЋГ·ГЄГЁ: "+IntToStr(score-2));
Image1->Canvas->Brush->Color = clGreen;
for(int i = 0; i<score; i++)
for(int j = 0; j<2-1; j++)
Image1->Canvas->Rectangle(posRec[i][j],posRec[i][j+1],posRec[i][j]+30,posRec[i][j+1]+10);
Image1->Canvas->Brush->Color = clRed;
Image1->Canvas->Ellipse(point.x+7,point.y,point.x+9+7,point.y+9);
Image1->Canvas->Brush->Color = clWhite;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyPress(TObject *Sender, char &Key)
{
for(int i = 0; i<score; i++)
for(int j = 0; j<score; j++)
lPos[i][j] = posRec[i][j];
if(Key == 'd' || Key == 'Гў')
{
posRec[0][0] += 32;
for(int i = 1; i<score; i++)
for(int j = 0; j<2; j++)
posRec[i][j] = lPos[i-1][j];
}
if(Key == 'a' || Key == 'Гґ')
{
posRec[0][0] -= 32;
for(int i = 1; i<score; i++)
for(int j = 0; j<2; j++)
posRec[i][j] = lPos[i-1][j];
}
if(Key == 'w' || Key == 'Г¶')
{
posRec[0][1] -= 12;
for(int i = 1; i<score; i++)
for(int j = 0; j<2; j++)
posRec[i][j] = lPos[i-1][j];
}
if(Key == 's' || Key == 'Г»')
{
posRec[0][1] += 12;
for(int i = 1; i<score; i++)
for(int j = 0; j<2; j++)
posRec[i][j] = lPos[i-1][j];
}
if(posRec[0][0]==point.x && posRec[0][1] == point.y)
{
RandPos();
score++;
}
if(posRec[0][0]>=ClientWidth || posRec[0][1] >= ClientHeight ||
posRec[0][0]<=0 || posRec[0][1]<=0)
{
ShowMessage("Game Over!");
Form1->Close();
}
//Image1->Canvas->Brush->Color = clWhite;
//Image1->Canvas->Pen->Color = clWhite;
//Image1->Canvas->Rectangle(0, 0, ClientWidth, ClientHeight);
//Image1->Canvas->Pen->Color = clBlack;
Form1->Paint();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
posRec[0][0] = 132;
posRec[0][1] = 100;
posRec[1][0] = 100;
posRec[1][1] = 100;
for(int i=2; i<50; i++)
for(int j=0; j<2; j++)
posRec[i][j] = -100;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
Application->Terminate();
}