Ошибка В Игре Змейка

  • Автор темы rhino101
  • Дата начала
R

rhino101

Помогите найти ошибку. Игра змейка состоит из двух модулей (main, game), main не может найти объекты из game. Может можно объединить модули в один?

unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;

type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormPaint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure NewGame;
procedure MoveSnake;
procedure GamePlay;
procedure GameOver;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
SnakeLen, Count : Integer;
ATails : array[1..1000] of TTail;
AApple : TApple;
AHead : THead;
HeadPic, ApplePic, TailPic : TBitmap;
dir : String;

implementation

uses game;

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
MoveSnake;
Game;
Caption := 'Your Score: '+IntToStr(SnakeLen)+'.';
Refresh;
end;

procedure TForm1.NewGame;
begin
SnakeLen := 2;

ApplePic := TBitmap.Create;
ApplePic.LoadFromFile('red.bmp');
ApplePic.Transparent := True;
HeadPic := TBitmap.Create;
HeadPic.LoadFromFile('yellow.bmp');
TailPic := TBitmap.Create;
TailPic.LoadFromFile('green.bmp');

Width := 24 * (Count+1);
Height := Width;

Randomize;
AApple := TApple.Create(random(Count)*24,rand om(Count)*24,ApplePic);
AHead := THead.Create(random(Count)*24,rando m(Count)*24,HeadPic);
ATails[1] := TTail.Create(AHead.getX,AHead.getY-24,TailPic);
ATails[2] := TTail.Create(AHead.getX,AHead.getY-48,TailPic);

dir := 'Down';
Timer1.Enabled := True;
Timer1.Interval := 300;
end;

procedure TForm1.MoveSnake;
var i : Integer;
begin
for i := SnakeLen downto 2 do
begin
ATails.setX(ATails[i-1].getX);
ATails.setY(ATails[i-1].getY);
end;

ATails[1].setX(AHead.getX);
ATails[1].setY(AHead.getY);

If dir = 'Down' Then AHead.setY(AHead.getY + 24);
If dir = 'Up' Then AHead.setY(AHead.getY - 24);
If dir = 'Left' Then AHead.setX(AHead.getX - 24);
If dir = 'Right' Then AHead.setX(AHead.getX + 24);
end;

procedure TForm1.GamePlay;
var i : Integer;
begin
if (AHead.getX = AApple.getX) and (AHead.getY = AApple.getY) Then
begin
Randomize;
AApple.setX(random(Count)*24);
AApple.setY(random(Count)*24);
SnakeLen := SnakeLen + 1;
ATails[SnakeLen] := TTail.Create(ATails[SnakeLen-1].getX,ATails[SnakeLen-1].getY,TailPic);
if Timer1.Interval > 55 Then Timer1.Interval := Timer1.Interval - 5;
end;
for i := 1 to SnakeLen do
if (AHead.getX = ATails.getX) and (AHead.getY = ATails.getY) Then
GameOver;
if AHead.getX < 0 Then GameOver;
if AHead.getX > (Count+1) * 24 Then GameOver;
if AHead.getY < 0 Then GameOver;
if AHead.getY > (Count+1) * 24 Then GameOver;
end;

procedure TForm1.GameOver;
var i : Integer;
begin
Timer1.Enabled := False;
ShowMessage('GameOver__LOSER!!!');
for i := 1 to SnakeLen do

AHead.Free ;
AAple.Free ;
ATails.Free;

SnakeLen := 2;
NewGame;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered := True;
Count := 20;
Color := clBlack;
NewGame;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
begin
if (Key = VK_LEFT) and (dir <> 'Right') Then dir := 'Left';
if (Key = VK_RIGHT) and (dir <> 'Left') Then dir := 'Right';
if (Key = VK_DOWN) and (dir <> 'Up') Then dir := 'Down';
if (Key = VK_UP) and (dir <> 'Down') Then dir := 'Up';
end;

procedure TForm1.FormPaint(Sender: TObject);
var i : Integer;
begin
for i := 1 to SnakeLen do
Canvas.Draw(ATails.getX,ATails.getY,Atails.getBitmap);

Canvas.Draw(AApple.getX,AApple.getY ,AApple.getBitmap);
Canvas.Draw(AHead.getX,AHead.getY,A Head.getBitmap);
end;

end.




unit game;

interface

uses
Classes, SysUtils, Graphics;

type

{ TBase }

TBase = class
private
x : Integer;
y : Integer;
bitmap : TBitmap;
public
Constructor Create(x0,y0 : Integer; bitmap0 : TBitmap);

procedure setX(x0 : Integer);
procedure setY(y0 : Integer);
procedure setBitmap(bitmap0 : TBitmap);

function getX : Integer;
function getY : Integer;
function getBitmap : TBitmap;
end;

TApple = class (TBase)

end;

THead = class (TBase)

end;

TTail = class (TBase)

end;
implementation

uses main;

{ TBase }

constructor TBase.Create(x0, y0: Integer; bitmap0: TBitmap);
begin
x := x0;
y := y0;
bitmap := bitmap0;
end;

procedure TBase.setX(x0: Integer);
begin
x := x0;
end;

procedure TBase.setY(y0: Integer);
begin
y := y0;
end;

procedure TBase.setBitmap(bitmap0: TBitmap);
begin
bitmap := bitmap0;
end;

end.
 
S

sinkopa

Помогите найти ошибку. Игра змейка состоит из двух модулей (main, game), main не может найти объекты из game. Может можно объединить модули в один?
unit main;
Отсюда убрать
Сюда дописать
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, game;
 
Мы в соцсетях:

Обучение наступательной кибербезопасности в игровой форме. Начать игру!