Hallo liebes Forum,
ich habe versucht bei meinem kleinen Snake-Skript einen Dynamischen Array einzubauen, allerdings sorgt dieser dafür, dass eine SIGSEGV Fehlermeldung kommt und sagt dass die Datei 'lclrescache.pas' in Zeile 195 diesen ausgelöst hat.
Komischerweise tritt der Fehler erst auf wenn das Array eine länge > 15 hat ... weiß einer vlt. weiter?
P.S: Hier noch das Skript:
Code: Alles auswählen
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, LCLType;
type
{ TForm1 }
TForm1 = class(TForm)
Timer1: TTimer;
Timer2: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure Timer2Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
procedure Apple;
var
Form1: TForm1;
ax: array of integer;
ay: array of integer;
ax_apple: array[1..1] of integer;
ay_apple: array[1..1] of integer;
z: integer = 3;
z_temp: integer;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var i: integer;
begin
Randomize();
ax_apple[1] := ((Random(62) + 1) * 10) + 5;
ay_apple[1] := ((Random(36) + 1) * 10) + 5;
SetLength(ax,10);
SetLength(ay,10);
for i := Low(ax) to High(ax) do
begin
ax[i] := 105 + 10 * i;
ay[i] := 105;
end;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var i: integer;
begin
case Key of
//Hoch Taste
VK_UP: begin
if not (z = 1) and not (z = 3) and not (z = 5) then
z := 1;
end;
//Rechts Taste
VK_RIGHT: begin
if not (z = 2) and not (z = 4) and not (z = 5) then
z := 2;
end;
//Unten Taste
VK_DOWN: begin
if not (z = 3) and not (z = 1) and not (z = 5) then
z := 3;
end;
//Links Taste
VK_LEFT: begin
if not (z = 4) and not (z = 2) and not (z = 5) then
z := 4;
end;
//Leertaste
VK_SPACE: begin
//Pause-Modus
if not (z = 6) and not (z = 5) then
begin
z_temp := z;
z := 6;
Timer2.Enabled := False;
end
else
//Spiel-Modus
begin
if (z = 6) then
begin
z := z_temp;
Timer2.Enabled := True;
end;
//Crash-Modus
if (z = 5) then
begin
Randomize();
for i := Low(ax) to High(ax) do
begin
ax[i] := 0;
ay[i] := 0;
end;
SetLength(ax,10);
SetLength(ay,10);
for i := Low(ax) to High(ax) do
begin
ax[i] := 105 + 10 * i;
ay[i] := 105;
end;
Apple();
z := 3;
Timer2.Enabled := True;
end;
end;
end;
end;
end;
procedure TForm1.Timer2Timer(Sender: TObject);
begin
Apple();
end;
procedure Apple;
var i: integer;
begin
//Apfel-Position neu berechnen
ax_apple[1] := ((Random(62) + 1) * 10) + 5;
ay_apple[1] := ((Random(36) + 1) * 10) + 5;
for i := Low(ax) to High(ax) do
begin
if (ax_apple[1] = ax[i]) and (ay_apple[1] = ay[i]) then
begin
ax_apple[1] := ((Random(62) + 1) * 10) + 5;
ay_apple[1] := ((Random(36) + 1) * 10) + 5;
end;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var i: integer;
begin
//Rand usw. zeichnen
Form1.Canvas.Pen.Color := clWhite;
Form1.Canvas.FillRect(0,0,650,400);
Form1.Canvas.Pen.Width := 10;
Form1.Canvas.Pen.Color := clRed;
Form1.Canvas.MoveTo(5,5);
Form1.Canvas.LineTo(645,5);
Form1.Canvas.LineTo(645,395);
Form1.Canvas.LineTo(5,395);
Form1.Canvas.LineTo(5,5);
Form1.Canvas.Pen.Color := clBlue;
Form1.Canvas.MoveTo(ax[Low(ax)],ay[Low(ax)]);
Form1.Canvas.LineTo(ax[Low(ax)],ay[Low(ax)]);
Form1.Canvas.Pen.Color := clLime;
Form1.Canvas.MoveTo(ax_apple[1],ay_apple[1]);
Form1.Canvas.LineTo(ax_apple[1],ay_apple[1]);
Form1.Canvas.Pen.Color := clBlack;
//Form1.Canvas.TextOut(20,20,'Length: ' + IntToStr(Length(ax)));
//Schlange zeichnen
for i := Low(ax) + 1 to High(ax) do
begin
Form1.Canvas.MoveTo(ax[i],ay[i]);
Form1.Canvas.LineTo(ax[i],ay[i]);
end;
//Position verändern
if not (z = 6) and not (z = 5) then
for i := High(ax) downto Low(ax) + 1 do
begin
ax[i] := ax[i - 1];
ay[i] := ay[i - 1];
end;
//Zustände abfragen
case z of
1: ay[Low(ax)] := ay[Low(ax)] - 10;
2: ax[Low(ax)] := ax[Low(ax)] + 10;
3: ay[Low(ax)] := ay[Low(ax)] + 10;
4: ax[Low(ax)] := ax[Low(ax)] - 10;
5: begin
Form1.Canvas.TextOut(300,190,'.:Crash:.');
Timer2.Enabled := False;
end;
6: Form1.Canvas.TextOut(300,190,'.:Pause:.');
end;
//Abfragen ob Kopf Schwanz berührt
if not (z = 6) and not (z = 5) then
for i := Low(ax) + 1 to High(ax) do
begin
if (ax[Low(ax)] = ax[i]) and (ay[Low(ax)] = ay[i]) then
begin
z := 5;
end;
end;
//Abfragen ob Kopf auf dem Rand ist (Kollision)
if not (z = 5) then
if (ax[Low(ax)] < 10) or (ay[Low(ax)] < 10) or (ax[Low(ax)] > 640) or (ay[Low(ax)] > 390) then
begin
z := 5;
end;
//Abfragen ob Kopf auf Apfel ist
if not (z = 5) then
if (ax[Low(ax)] = ax_apple[1]) and (ay[Low(ax)] = ay_apple[1]) then
begin
Timer2.Enabled := False;
SetLength(ax,Length(ax) + 1);
SetLength(ay,Length(ay) + 1);
Timer2.Enabled := True;
Apple();
end;
end;
end.