Hi there everyone.
I'm wanting to make a simple Snake game where the speed of a block (image) goes around by pressing up, down, left and right. I would also like it to go faster every 10 seconds so it gets harder as time progresses.
Here is the code I have, although it hits the top and doesn't go any further, I would like it to hit the side and display a message (eg for ondestroy) about "Game Over".
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TMainForm = class(TForm)
ImageSnake: TImage;
BtUp: TButton;
BtDown: TButton;
BtLeft: TButton;
BtRight: TButton;
TimerVert: TTimer;
TimerHorz: TTimer;
procedure BtUpClick(Sender: TObject);
procedure BtLeftClick(Sender: TObject);
procedure BtDownClick(Sender: TObject);
procedure BtRightClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TimerHorzTimer(Sender: TObject);
procedure TimerVertTimer (Sender: TObject);
private
{ Private declarations }
FSnakeSpeed : integer;
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.FormCreate(Sender: TObject);
begin
FSnakeSpeed := 6; //speed you want the snake to travel at
TimerHorz.Tag := 1;
TimerVert.Tag := 1;
TimerHorz.Enabled := false;
TimerVert.Enabled := false;
end;
procedure TMainForm.BtUpClick(Sender: TObject);
begin
TimerHorz.Enabled := false;
TimerVert.Enabled := false;
TimerVert.Tag := -1; //minus
TimerVert.Enabled := true;
TimerVertTimer(nil); //calls the timer's OnTimer event
end;
procedure TMainForm.BtDownClick(Sender: TObject);
begin
TimerHorz.Enabled := false;
TimerVert.Enabled := false;
TimerVert.Tag := 1; //plus
TimerVert.Enabled := true;
TimerVertTimer(nil); //calls the timer's OnTimer event
end;
procedure TMainForm.BtLeftClick(Sender: TObject);
begin
TimerHorz.Enabled := false;
TimerVert.Enabled := false;
TimerHorz.Tag := -1; //minus
TimerHorz.Enabled := true;
TimerHorzTimer(nil);
end;
procedure TMainForm.BtRightClick(Sender: TObject);
begin
TimerHorz.Enabled := false;
TimerVert.Enabled := false;
TimerHorz.Tag := 1; //plus
TimerHorz.Enabled := true;
TimerHorzTimer(nil);
end;
procedure TMainForm.TimerHorzTimer(Sender: TObject);
var
x: integer;
begin
x := ImageSnake.Left +(FSnakeSpeed *TimerHorz.Tag);
if x > ClientWidth then
x := 1
else
if x < 1 then
x := ClientWidth;
ImageSnake.Left := x;
end;
procedure TMainForm.TimerVertTimer(Sender: TObject);
var y: integer;
begin
y := ImageSnake.Top +(FSnakeSpeed *TimerVert.Tag);
if y > ClientHeight then
y := 1
else
if y < 1 then
y := ClientHeight;
ImageSnake.Top := y;
end;
end.
If anyone can make it so when it hits the side, it doesn't roll around and make it so the game is over, that would be awesome. Thanks :)
Thanks
Michael