unit SnakeA_maXbox4; {€€€ based on a python game on raspi3 and baseline from Julian Rausch €€€} //#sign:max: MAXBOX8: 27/12/2018 10:51:33 //TODO onclose event with destroy timers ,#locs:1177 //TODO highscore list and pause timers during game, mixmode for panels interface {uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus, ExtCtrls, StdCtrls, ComCtrls, SnakeHighscore, Spin, SnakeHighscoreList, SnakeOptionen; } type TForm1 = TForm; //) var Spielfeld: TPaintBox; Timer: TTimer; StartMenu: TMainMenu; Start1: TMenuItem; NeuesSpiel1: TMenuItem; Spielbeenden1: TMenuItem; PunkteEdit: TEdit; PunkteLabel: TLabel; GeschTB: TTrackBar; GeschwLabel: TLabel; Highscore1: TMenuItem; Optionen1: TMenuItem; SpezProBar: TProgressBar; FutterTimer: TTimer; PunkteTimer: TTimer; GameOverButton: TButton; Countdown: TTimer; CountdownLabel: TLabel; procedure NeuesSpiel1Click(Sender: TObject); procedure FeldZeichnen; procedure SpielEnde; procedure FutterNeu; procedure NeuesSpiel; procedure SpezFutterNeu; procedure SchlangeBerrechnen; procedure Highscore; procedure FormCreate(Sender: TObject); procedure SnakeFormClose(Sender: TObject; var Action: TCloseAction); procedure TimerTimer(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure onpanelClick(Sender: TObject); procedure GeschTBChange(Sender: TObject); procedure Highscore1Click(Sender: TObject); procedure Optionen1Click(Sender: TObject); procedure FutterTimerTimer(Sender: TObject); procedure FormActivate(Sender: TObject); procedure PunkteTimerTimer(Sender: TObject); procedure GameOverButtonClick(Sender: TObject); procedure Spielbeenden1Click(Sender: TObject); procedure CountdownTimer(Sender: TObject); //private { Private-Deklarationen } //public { Public-Deklarationen } //end; var Form1: TForm1; Feld: array of array of Integer; //Das Feld als Koordinate eine -1 steht für Futter // eine 0 für ein leeres und > 0 für ein Teil der Schlange, wobei die Zahl 1 das ende der Schlage ist Farbe: array [0..4] of TColor; //= (clBlack, clLime, clRed, clRed, clBtnFace); Kopf,Richtung : TPoint; // Position des Kopfes und die Richtung (0,1) steht für runter //(0/-1) für hoch (1/0) für rechts und (-1/0) für links Futter: array of TPoint; //Position des Futters SpezFutter : TPoint; SpeFutter:Boolean; SpeTime:integer; laenge,Futterzahl ,Punkte, CountdownI:integer; //länge der Schlage; Anzahl des Futters was liegt raster: integer; //= 10; //Wie viele Pixel ein Feld groß ist hoehe : integer; //= 30; // Die höhe des Spielfelds in Feldern breite: integer; //= 60; // Die breite des Spielfelds in Feldern feldhoehe, feldbreite:integer; start: Boolean; //= false; //Gibt an, ob ein Spiel "läuft" Tastendruck:array [1..2] of TPoint; //unit SnakeHighscoreList; type TForm3 = TForm; var Form3: TForm3; //unit SnakeOptionen; type TForm4 = TForm; var BreiteSE: TSpinEdit; HoeheSE: TSpinEdit; BreiteLabel: TLabel; HoeheLabel: TLabel; SchlangeColB: TColorBox; SchlangeLabel: TLabel; HintergColB: TColorBox; Label1: TLabel; RandCB: TComboBox; RandLabel: TLabel; AnpassenChB: TCheckBox; procedure TForm4BreiteSEChange(Sender: TObject); procedure TForm4HoeheSEChange(Sender: TObject); procedure TForm4SchlangeColBChange(Sender: TObject); procedure TForm4HintergColBChange(Sender: TObject); procedure TForm4FormClose(Sender: TObject; var Action: TCloseAction); procedure TForm4Form4Activate(Sender: TObject); procedure TForm4RandCBChange(Sender: TObject); //private { Private-Deklarationen } //public { Public-Deklarationen } //end; var Form4: TForm4; //(( todo aenderung : Boolean; Randoffen : Boolean; // = false; Wahrheitswert : Boolean; //unit SnakeHighscore; type TForm2 = TForm; var //TODO: form2 and 4 setting Form2: TForm2; //ztodo SpeicherListe : TStrings; Clicken:Boolean; //= True; TextS : string; Highscored: array [1..10] of integer; HighscoreName: array [1..10] of string; Platz: integer; implementation //Begin------------------------------unit SnakeOptionen; ------------------- procedure buildSnakeOptionenForm; begin Form4:= TForm4.create(self) BreiteSE:= TSpinEdit.create(form4) // inits on form activate event hoeheSE:= TSpinEdit.create(form4) with form4 do begin Setbounds(557,218, 384, 308) Caption := 'Snake Options' Color := clBtnFace Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -11 Font.Name := 'MS Sans Serif' Font.Style := [] OldCreateOrder := False OnActivate := @TForm4Form4Activate OnClose := @TForm4FormClose PixelsPerInch := 96 show; //TextHeight := 13 end; BreiteLabel:= TLabel.create(self) with breitelabel do begin parent:= form4 setBounds(40, 40, 97, 33) AutoSize := False Caption := 'Count of Field Width:' Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -12 Font.Name := 'Calibri' Font.Style := [] ParentFont := False WordWrap := True end; HoeheLabel:= TLabel.create(form4) with hoehelabel do begin parent:= form4 Left := 40 Top := 104 Width := 97 Height := 33 AutoSize := False Caption := 'Count of Field Height' Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -12 Font.Name := 'Calibri' Font.Style := [] ParentFont := False WordWrap := True end; SchlangeLabel:= TLabel.create(self) with schlangelabel do begin parent:= form4; Left := 240 Top := 48 Width := 97 Height := 33 AutoSize := False Caption := 'Change of Snakecolor' Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -12 Font.Name := 'Calibri' Font.Style := [] ParentFont := False WordWrap := True end; Label1:= TLabel.create(form4) with label1 do begin parent:= form4 Left := 240 Top := 112 Width := 97 Height := 33 AutoSize := False Caption := 'Change of Backgroundcolor' Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -12 Font.Name := 'Calibri' Font.Style := [] ParentFont := False WordWrap := True end; RandLabel:= TLabel.create(form4) with randlabel do begin parent:= form4 Left := 32 Top := 184 Width := 121 Height := 33 AutoSize := False Caption := 'Border like a Wall?' Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -12 Font.Name := 'Calibri' Font.Style := [] ParentFont := False WordWrap := True end; // BreiteSE:= TSpinEdit.create(form4) with breitese do begin parent:= form4 Left := 40 Top := 72 Width := 89 Height := 22 MaxValue := 0 MinValue := 0 TabOrder := 0 Value := 24 OnChange := @TForm4BreiteSEChange end; //object HoeheSE: TSpinEdit // hoeheSE:= TSpinEdit.create(form4) with hoehese do begin parent:= form4 Left := 40 Top := 136 Width := 89 Height := 22 MaxValue := 0 MinValue := 0 TabOrder := 1 Value := 24 OnChange := @Tform4HoeheSEChange end; SchlangeColB:= TColorBox.create(form4) with schlangecolb do begin parent:= form4 Left := 240 Top := 80 Width := 97 Height := 22 Selected := clRed //ItemHeight := 16 TabOrder := 2 // OnChange := @TForm4SchlangeColBChange end; // object HintergColB: TColorBox HintergColB:= TColorBox.create(form4) with hintergcolb do begin parent:= form4 Left := 240 Top := 144 Width := 97 Height := 22 //ItemHeight := 16 TabOrder := 3 //TCustomColorBox(hintergcolb).OnChange := @TForm4HintergColBChange end; RandCB:= TComboBox.create(form4) with randcb do begin parent:= form4 Left := 40 Top := 216 Width := 89 Height := 21 ItemHeight := 13 ItemIndex := 1 TabOrder := 4 Text := 'Yes' OnChange := @TForm4RandCBChange Items.add('No') Items.add('Yes') end; AnpassenChB:= TCheckBox.create(self) with anpassenchb do begin parent:= form4 Left := 144 Top := 72 Width := 81 Height := 49 //Caption := 'Gr'#246#223'e Maximal anpassen' Caption := 'Size to fit to Maximum' Checked := True Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -12 Font.Name := 'Calibri' Font.Style := [] ParentFont := False State := cbChecked TabOrder := 5 //WordWrap := True end; // } end; procedure TForm4BreiteSEChange(Sender: TObject); var i:integeR; begin if(BreiteSe.Value > 10) and (not Wahrheitswert) then begin Wahrheitswert := true; Breite := BreiteSE.Value; if(Feldbreite div (BreiteSE.Value+1) < 20)then BreiteSE.Value :=Feldbreite div 20-1; Breite := BreiteSE.Value; if (AnpassenChB.State = cbChecked)then HoeheSE.Value := Trunc(Breite * (feldhoehe / feldbreite)); hoehe := HoeheSE.Value; BreiteSE.Value := Breite; aenderung := true; SetLength(Feld,breite+2); For i:= 0 to breite Do SetLength(Feld[i],hoehe +2); Wahrheitswert:= false; end; end; procedure TForm4HoeheSEChange(Sender: TObject); var i :integer; begin if(HoeheSE.Value > 10) and (not Wahrheitswert) then begin Wahrheitswert:= true; if(Feldhoehe div (HoeheSE.Value+1) < 20) then HoeheSE.Value:=(Feldbreite div 20)-1; Hoehe := HoeheSE.Value; if (AnpassenChB.State = cbChecked) then BreiteSE.Value := Trunc(Hoehe * (feldbreite / feldhoehe)); breite :=BreiteSE.Value; aenderung := true; SetLength(Feld,breite+2); For i:= 0 to breite Do SetLength(Feld[i],hoehe +2); Wahrheitswert:= false end; end; procedure TForm4SchlangeColBChange(Sender: TObject); begin if(SchlangeColB.Selected <> Farbe[0])then Farbe[2]:= SchlangeColB.Selected; end; procedure TForm4HintergColBChange(Sender: TObject); begin if(HintergColB.Selected <> Farbe[2]) and (HintergColB.Selected <> Farbe[1]) and (HintergColB.Selected <> Farbe[3])then Farbe[0]:=HintergColB.Selected; end; procedure TForm4FormClose(Sender: TObject; var Action: TCloseAction); begin HintergColB.Selected:=Farbe[0]; SchlangeColB.Selected:=Farbe[2]; start:= true; writeln('form snake options closed') end; procedure TForm4Form4Activate(Sender: TObject); begin Randoffen := False; // from main form to test //Width = 1025 //Height = 553 default from main form feldbreite:= 1025 feldhoehe:= 553 hoehe := 30; // Die höhe des Spielfelds in Feldern breite:= 60; // Die breite des Spielfelds in Feldern writeln('settings form activated') BreiteSE.Value :=Breite; HoeheSE.Value :=Hoehe; end; procedure TForm4RandCBChange(Sender: TObject); begin case RandCB.ItemIndex of 0: RandOffen := true; 1: RandOffen := false; end; end; //End ------------------------------unit SnakeOptionen; ------------------- procedure TForm2Speichern; var pFile : Textfile; sPath : string; sText : string; begin sPath:= ExtractFilePath(ParamStr(0)) + '\Highscore.txt'; sText:= HighscoreName[1]+ #13 + #10 + IntToStr(Highscored[1]) + #13 + #10 + HighscoreName[2]+ #13 + #10 + IntToStr(Highscored[2]) + #13 + #10 + HighscoreName[3]+ #13 + #10 + IntToStr(Highscored[3]) + #13 + #10 + HighscoreName[4]+ #13 + #10 + IntToStr(Highscored[4]) + #13 + #10 + HighscoreName[5]+ #13 + #10 + IntToStr(Highscored[5]) + #13 + #10 + HighscoreName[6]+ #13 + #10 + IntToStr(Highscored[6]) + #13 + #10 + HighscoreName[7]+ #13 + #10 + IntToStr(Highscored[7]) + #13 + #10 + HighscoreName[8]+ #13 + #10 + IntToStr(Highscored[8]) + #13 + #10 + HighscoreName[9]+ #13 + #10 + IntToStr(Highscored[9]) + #13 + #10 + HighscoreName[10]+ #13 + #10 + IntToStr(Highscored[10]); AssignFile(pFile,sPath); ReWrite(pFile); //Writeln(pFile,sText); CloseFile(pFile); end; procedure TForm2EintrageButtonClick(Sender: TObject); var i,j :integer; cFile : Textfile; cPath : string; begin cPath := ExtractFilePath(ParamStr(0)) + '\Highscore2.txt'; if FileExists(cPath) then begin AssignFile(cFile,cPath); Reset(cFile); For i:= 1 to 10 Do begin //Readln(cFile, HighscoreName[i]); //Readln(cFile, Highscored[i]); end; CloseFile(cFile); end; i:=0; repeat i:= i+1 until Punkte > Highscored[i]; For j:= 1 to 11-i Do begin Highscored[11-j]:= Highscored[10-j]; HighscoreName[11-j]:=HighscoreName[10-j] end; Highscored[i] := Punkte; //HighscoreName[i] := NameEdit.Text; Form2.Visible := false; TForm2Speichern; end; procedure setbitmaptoPaintBox(Sender: TObject); var Bmp: TBitmap; begin //if OpenPictureDialog1.Execute then begin Bmp := TBitmap.Create; try Bmp.LoadFromFile(exepath+'examples\citymax.bmp'); spielfeld.Canvas.Draw(0, 0, Bmp); finally Bmp.Free; end; end; end; //{$R *.dfm} -----------------build @main form procedure buildSnakeMainForm; begin Form1:= TForm1.create(self) with form1 do begin setBounds(203,237, 1087, 700) Caption := 'Snake Bite 2019 - maXbox4' Color := clBtnFace color:= clgreen; Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -11 Font.Name := 'MS Sans Serif' Font.Style := [] Menu := StartMenu OldCreateOrder := False OnActivate := @FormActivate OnCreate := @FormCreate OnKeyDown := @FormKeyDown //OnMouseDown:= @MouseDown doublebuffered:= true; onclose:= @snakeFormClose; PixelsPerInch := 96 Show; //TextHeight := 13 end; //object Spielfeld: TPaintBox Spielfeld:= TPaintBox.create(form1) with spielfeld do begin parent:= form1 Left := 21 Top := 56 Width := 1025 Height := 553 OnMouseDown:= @MouseDown end; //setbitmaptoPaintBox(form1) PunkteLabel:= TLabel.create(form1) with punktelabel do begin parent:= form1 Left := 860 Top := 18 Width := 60 Height := 22 AutoSize := False Caption := 'Score:' Font.Charset := DEFAULT_CHARSET Font.Color := clRed; //clWindowText Font.Height := -13 Font.Name := 'MS Sans Serif' Font.Style := [] Font.size:= 15 ParentFont := False end; GeschwLabel:= TLabel.create(form1) with geschwlabel do begin parent:= form1 Left := 336 Top := 608 Width := 161 Height := 49 AutoSize := False Caption := 'Set the speed of snake with + and - or m and n'; Font.Charset := DEFAULT_CHARSET Font.Color := clWindowText Font.Height := -12 Font.Name := 'Calibri' Font.Style := [] ParentFont := False WordWrap := True end; with TLabel.create(form1) do begin parent:= form1 Left := 1021 Top := 440 Width := 22 Height := 25 Caption := 'T' Font.Charset := DEFAULT_CHARSET Font.Color := clBlue; //clWindowText Font.size:= 24 onclick:= @onpanelclick; end; with TLabel.create(form1) do begin parent:= form1 Left := 1021 Top := 480 Width := 22 Height := 26 Caption := 'V' Font.Charset := DEFAULT_CHARSET Font.Color := clBlue; //clWindowText Font.size:= 24 onclick:= @onpanelclick; end; with TLabel.create(form1) do begin parent:= form1 Left := 1000 Top := 455 Width := 22 Height := 26 Caption := '<' Font.Charset := DEFAULT_CHARSET Font.Color := clBlue; //clWindowText Font.size:= 25 onclick:= @onpanelclick; end; with TLabel.create(form1) do begin parent:= form1 Left := 1045 Top := 455 Width := 22 Height := 26 Caption := '>' Font.Charset := DEFAULT_CHARSET Font.Color := clBlue; //clWindowText Font.size:= 25 onclick:= @onpanelclick; end; CountdownLabel:= TLabel.create(form1) with countdownlabel do begin parent:= form1 Left := 528 Top := 280 Width := 15 Height := 82 Color := clWindowText Font.Charset := DEFAULT_CHARSET Font.Color := clred; //clWindow Font.Height := -67 Font.Name := 'Calibri' Font.Style := [] ParentColor := False ParentFont := False Transparent := True end; PunkteEdit:= TEdit.create(form1) with punkteedit do begin parent:= form1 Left := 928 Top := 16 Width := 70 Height := 20 color:= clyellow; font.color:= clBlue; font.size:= 16; Enabled := False TabOrder := 0 end; GeschTB:= TTrackBar.create(form1) with geschtb do begin parent:= form1 Left := 24 Top := 600 Width := 305 Height := 33 Enabled := False Max := 21 Position := 16 TabOrder := 1 OnChange := @GeschTBChange end; SpezProBar:= TProgressBar.create(form1) with spezprobar do begin parent:= form1 setBounds(45, 18, 370, 24) Max := 122 //backcolor color:= clblue; TabOrder := 2 end; GameOverButton:= TButton.create(form1) with gameoverbutton do begin parent:= form1 setbounds(480, 248, 121, 121) Caption := 'Game Over - New Game?' Font.Charset := ANSI_CHARSET Font.Color := clWindowText Font.Height := -16 Font.Name := 'Calibri' Font.Style := [fsBold, fsItalic] ParentFont := False TabOrder := 3 Visible := False WordWrap := True OnClick := @GameOverButtonClick end; Timer:= TTimer.create(form1) with timer do begin Enabled := False Interval := 111 OnTimer := @TimerTimer end; StartMenu:= TMainMenu.create(form1) //startmenu.Left := 24; Start1:= TMenuItem.create(startmenu) Start1.Caption := 'Start...' StartMenu.items.add(start1) NeuesSpiel1:= TMenuItem.create(startmenu) NeuesSpiel1.Caption := '&New_Game' NeuesSpiel1.OnClick := @NeuesSpiel1Click; StartMenu.items.add(neuesspiel1) Optionen1:= TMenuItem.create(startmenu) optionen1.Caption := 'Settings' optionen1.OnClick := @Optionen1Click StartMenu.items.add(optionen1) { object Highscore1: TMenuItem Caption := 'Highscore' OnClick := Highscore1Click end } Spielbeenden1:= TMenuItem.create(startmenu) Spielbeenden1.Caption := '&Close_Game' Spielbeenden1.OnClick := @Spielbeenden1Click StartMenu.items.add(spielbeenden1) FutterTimer:= TTimer.create(form1) with futtertimer do begin Enabled := False Interval := 500 OnTimer := @FutterTimerTimer //Top := 24 end; PunkteTimer:= TTimer.create(form1) with punktetimer do begin Enabled := False Interval := 2000 OnTimer := @PunkteTimerTimer //Left := 24 //Top := 24 end; Countdown:= TTimer.create(form1) with countdown do begin Enabled := False Interval := 600 OnTimer := @CountdownTimer //Left := 56 end; end; procedure NeuesSpiel1Click(Sender: TObject); begin Farbe[0]:= clBlack; Farbe[1]:= clLime; Farbe[2]:= clRed; Farbe[3]:= clBlue; Farbe[4]:= clBtnFace; raster:= 10; //Wie viele Pixel ein Feld groß ist hoehe := 30; // Die höhe des Spielfelds in Feldern breite:= 60; // Die breite des Spielfelds in Feldern start:= false; //Gibt an, ob ein Spiel "läuft" //, , , , ); Timer.Enabled := false; start := false; CountdownI := 6; Countdown.Interval:=1; CountdownLabel.Caption:= '5'; Countdown.Enabled:= true; NeuesSpiel; GameOverButton.Visible:= false; CountdownLabel.Transparent:= true; FeldZeichnen; end; procedure NeuesSpiel; var i,j:integer; begin //spielfeld.repaint; Spielfeld.Canvas.Brush.Color:=clBtnFace; Spielfeld.Canvas.FillRect(Rect(0, 0, Spielfeld.Width, Spielfeld.Height)); spielfeld.repaint; GameOverButton.Visible := false; SetLength(Feld,breite+2); For i:= 0 to breite Do SetLength(Feld[i],hoehe +2); For i:= 0 to breite Do For j:=0 to hoehe Do Feld[i][j]:=0; kopf:= Point(Random(breite-3)+2,Random(hoehe-3)+2); PunkteTimer.Enabled := false; laenge := 5; richtung := Point(0,1); Futterzahl := 0; Punkte:=0; Feld[kopf.X][kopf.Y]:=laenge; FutterNeu; FeldZeichnen; end; procedure FeldZeichnen; var i,j:integer; col : TColor; begin if aenderung then begin if(Futter[1].X > breite) or (Futter[1].Y > hoehe) then FutterNeu; aenderung := false; Spielfeld.Canvas.Brush.Color:=clBtnFace; Spielfeld.Canvas.FillRect(Rect(0, 0, Spielfeld.Width, Spielfeld.Height)); end; raster := Spielfeld.Width div (breite+1); if(Spielfeld.Height div (hoehe+1) < raster)then raster:=Spielfeld.Height div (hoehe+1); if(GameOverButton.Top <>Spielfeld.Top + (hoehe * raster) div 2 - GameOverButton.Height div 2)then GameOverButton.Top := Spielfeld.Top + (hoehe * raster) div 2 - GameOverButton.Height div 2; if(GameOverButton.Left <> Spielfeld.Left + (breite * raster) div 2 - GameOverButton.Width div 2)then GameOverButton.Left := Spielfeld.Left + (breite * raster) div 2 - GameOverButton.Width div 2; if(CountdownLabel.Top <> Spielfeld.Top + (hoehe * raster) div 2 - CountdownLabel.Height div 2)then CountdownLabel.Top := Spielfeld.Top + (hoehe * raster) div 2 - CountdownLabel.Height div 2; if(CountdownLabel.Left <> Spielfeld.Left + (breite * raster) div 2 - CountdownLabel.Width div 2)then CountdownLabel.Left := Spielfeld.Left + (breite * raster) div 2 - CountdownLabel.Width div 2; For i:=0 to breite Do For j:=0 to hoehe Do begin case feld[i][j] of -3: col := farbe[4]; -2: col := farbe[3]; -1: col := farbe[1]; 0: col := farbe[0]; else col := farbe[2]; end; Spielfeld.Canvas.Brush.Color:=col; Spielfeld.Canvas.FillRect(Rect(i*raster, j*raster, (i+1)*raster, (j+1)*raster)); end; end; procedure FutterNeu; begin PunkteEdit.Text := IntToStr(Punkte); Futterzahl := Futterzahl+1; SetLength(Futter,Futterzahl+1); Futter[Futterzahl]:=Point(Random(breite+1),Random(hoehe+1)); while feld[futter[Futterzahl].X][futter[Futterzahl].Y]<>0 do Futter[Futterzahl] := Point(Random(breite+1), Random(hoehe+1)); //feld[futter[Futterzahl].X],futter[Futterzahl].Y] := -1; feld[futter[Futterzahl].X][futter[Futterzahl].Y] := -1; end; procedure SpezFutterNeu; begin PunkteEdit.Text := IntToStr(Punkte); if(SpeFutter = false) then begin FutterTimer.Interval := (Breite+Hoehe)+Timer.Interval div 2 - 20; FutterTimer.Enabled:=true; SpeTime:=0; SpeFutter:=true; SpezFutter:=Point(Random(breite+1),Random(hoehe+1)); while feld[SpezFutter.X][SpezFutter.Y]<>0 do SpezFutter := Point(Random(breite+1), Random(hoehe+1)); feld[SpezFutter.X][SpezFutter.Y]:= -2; end; end; procedure Highscore; var i:integer; begin i := 0; repeat i := i +1; if(Punkte > Highscored[i])then begin Platz:= i; Clicken:=true; //Form2.Visible := true; end; until (i=10) or (Punkte > Highscored[i]); if(Punkte <= Highscored[10])then ShowMessageBig(Texts); end; procedure SchlangeBerrechnen; var i,j,k:integer; begin if (not RandOffen) then if ((kopf.X+richtung.X<0) or (kopf.X+richtung.X>breite) or (kopf.Y+richtung.Y<0) or (kopf.Y+richtung.Y>hoehe)) and (start) then begin TextS:='Sorry, you touched the borderline...!'; SpielEnde; end; for i := 0 to breite do for j := 0 to hoehe do if feld[i][ j]>0 then feld[i][j]:= feld[i][j]-1; if (RandOffen) then begin if ((kopf.X+richtung.X<0) or (kopf.X+richtung.X>breite) or (kopf.Y+richtung.Y<0) or (kopf.Y+richtung.Y>hoehe)) and (start) then begin if (kopf.X+richtung.X<0)then kopf.X := breite; if (kopf.X+richtung.X>breite)then kopf.X := 0; if (kopf.Y+richtung.Y>hoehe)then kopf.Y := 0; if (kopf.Y+richtung.Y<0)then kopf.Y := hoehe; end else begin kopf.X:=kopf.X+richtung.X; kopf.Y:=kopf.Y+richtung.Y; end; end else begin kopf.X:=kopf.X+richtung.X; kopf.Y:=kopf.Y+richtung.Y; end; if (feld[kopf.X][kopf.Y]>0) and (start) then begin TextS:='Sie haben sich selbst gebissen, eating yourself!'; SpielEnde; end; if(start)then For i:=0 to Futterzahl Do begin if (kopf.X=futter[i].X) and (kopf.Y=futter[i].Y) then begin if(RandOffen)then k :=1; if (not RandOffen) then k:=0; laenge:= laenge+1; // Feld[futter[i].X,futter[i].Y]:=0; Feld[futter[i].X][futter[i].Y]:=0; writeln('debug: collision detected...at ' +itoa(kopf.x)+':'+itoa(kopf.y)) Punkte:= Punkte+15-6*k; PunkteEdit.Text := IntToStr(Punkte); spielfeld.canvas.font.color:= clyellow; //random(clred) spielfeld.canvas.font.size:= 28; spielfeld.canvas.TextOut(kopf.X,kopf.Y + 2, 'Points!'); For j:= i to Futterzahl-1 Do //found bug -1! Futter[j] := Futter[j+1]; Futterzahl := Futterzahl -1; if(Futterzahl = 0)then FutterNeu; //} end; //} end; if(start)then if (kopf.X=SpezFutter.X) and (kopf.Y=SpezFutter.Y) then begin if(RandOffen)then k :=1; if (not RandOffen)then k:=0; laenge:= laenge+1; SpezProBar.Position:= 0; SpeTime:=0; Feld[SpezFutter.X][SpezFutter.Y]:=0; Punkte:= Punkte+50-15*k; spielfeld.canvas.TextOut(kopf.X, kopf.Y + 2, 'Super Score Points!'); FutterTimer.Enabled:=false; SpeFutter:=false; PunkteEdit.Text:=IntToStr(Punkte); end; if start then feld[kopf.X][kopf.Y] := laenge; end; procedure SpielEnde; begin Richtung := Point(0,0); GameOverButton.Visible := true; PunkteTimer.Enabled := false; start:=false; Timer.Enabled := false; FutterTimer.Enabled:=false; SpezProBar.Position:=0; Highscore; //showmessageBIG} GameOverButton.setfocus; if fileExists(exepath+'examples\citymax.bmp') then setbitmaptoPaintBox(form1); end; //TODO change assignfile procedure FormCreate(Sender: TObject); var cFile : Textfile; cPath : string; i : integeR; begin cPath := ExtractFilePath(ParamStr(0)) + '\Highscore2.txt'; if FileExists(cPath) then begin AssignFile(cFile,cPath); Reset(cFile); For i:= 1 to 10 Do begin //Readln(cFile, HighscoreName[i]); //Readln(cFile, Highscored[i]); end; CloseFile(cFile); end; randomize; //default //Width = 1025 //Height = 553 Feldhoehe := Spielfeld.Height; Feldbreite:= Spielfeld.Width; end; procedure TimerTimer(Sender: TObject); begin FeldZeichnen; if(CountdownI > 0)then start := false; if start then begin SchlangeBerrechnen; PunkteTimer.Interval := (Timer.Interval * 50+15 + (breite + hoehe) * 4); if(Random(500+GeschTB.Position*5)=15) and (Futterzahl < 5) then FutterNeu; if(Random(100+GeschTB.Position*3)=34)then SpezFutterNeu; Tastendruck[2].X := Tastendruck[1].X; Tastendruck[2].Y := Tastendruck[1].Y; Tastendruck[1] := Point(0,0); end; end; procedure MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var MausPos: TPoint; key: word; begin GetCursorPos(MausPos); writeln(IntToStr(MausPos.x)); writeln(IntToStr(MausPos.y)); //up arrow 38 if Button = mbLeft then if(Y > Richtung.X) and (Richtung.X=1) then begin //Tastendruck[1]:= Richtung; Richtung:= Point(0,0); writeln('uuuuuuuuupppp') key:= 38; FormKeyDown(self, key, []) //showmessage(itoa(kopf.y)+' '+itoa(y)) end; //*) //right arrow 39 ! if Button = mbLeft then if(Y > Richtung.Y) and (Richtung.Y=-1) then begin //Tastendruck[1] := Richtung; Richtung:= Point(1,0); end; //} //down arrow 40 if Button = mbLeft then if(Y > Richtung.X) and (Richtung.X=-1) then begin //Tastendruck[1]:= Richtung; writeln('doooooowwwwwwwwwwnnp') Richtung:= Point(0,1); end; // left arrow 37 ! if Button = mbLeft then if(Y > Richtung.Y) and (Richtung.X=0) then begin //Tastendruck[1] := Richtung; Richtung:= Point(-1,0); end; end; procedure onPanelClick(Sender: TObject); var key: word; begin case TLabel(sender).caption of 'T': key:= 38; 'V': key:= 40; '<': key:= 37; '>': key:= 39; end; FormKeyDown(self, key, []) end; procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin Tastendruck[2].X := Tastendruck[1].X; Tastendruck[2].Y := Tastendruck[1].Y; Tastendruck[1] := Point(0,0); if (Key = $6B ) or (Key = $4D) then GeschTB.Position := GeschTB.Position+1; if (Key = $6D ) or (Key = $4E) then GeschTB.Position := GeschTB.Position-1; if(Key = 38) and (Richtung.Y=0) and (Tastendruck[2].Y <> 1) then begin Tastendruck[1] := Richtung; Richtung:= Point(0,-1); end; if(Key = 39) and (Richtung.X=0) and (Tastendruck[2].X <> -1) then begin Tastendruck[1] := Richtung; Richtung:= Point(1,0); end; if(Key = 40) and (Richtung.Y=0) and (Tastendruck[2].Y <> -1) then begin Tastendruck[1] := Richtung; Richtung:= Point(0,1); end; if(Key = 37) and (Richtung.X=0) and (Tastendruck[2].X <> 1) then begin Tastendruck[1] := Richtung; Richtung:= Point(-1,0); end; end; procedure {TForm1.}GeschTBChange(Sender: TObject); var i,Time:integer; begin Time:=0; For i:=0 to GeschTB.Position Do Time := Time +i; Timer.Interval:= 232 - Time; end; procedure {TForm1.}Highscore1Click(Sender: TObject); begin Form3.Visible:=true; if (CountdownI > 0) then begin Spielfeld.Canvas.Brush.Color:=clBtnFace; Spielfeld.Canvas.FillRect(Rect(0, 0, Spielfeld.Width, Spielfeld.Height)); Countdown.Enabled := false; CountdownLabel.Caption := ''; CountdownLabel.Transparent:= true; end; start := false; end; procedure Optionen1Click(Sender: TObject); begin Form4.Visible := true; if (CountdownI > 0) then begin Spielfeld.Canvas.Brush.Color:=clBtnFace; Spielfeld.Canvas.FillRect(Rect(0, 0, Spielfeld.Width, Spielfeld.Height)); Countdown.Enabled := false; CountdownLabel.Caption := ''; CountdownLabel.Transparent:= true; end; start := false; end; procedure FutterTimerTimer(Sender: TObject); begin if(start) then begin SpeTime:= SpeTime+3; SpezProBar.Position := SpeTime; if(SpeTime> 126) then begin SpezProBar.Position:= 0; SpeFutter:=false; Feld[SpezFutter.X][SpezFutter.Y]:= 0; FutterTimer.Enabled:= false; end; end; end; procedure FormActivate(Sender: TObject); begin //Form2.Visible := false; //Form3.Visible := false; Form4.Visible := false; writeln('snake main form activated') end; procedure PunkteTimerTimer(Sender: TObject); var i:integer; begin if(start) then begin if(RandOffen)then i :=3 else i:=2; Punkte := Punkte + ((laenge div 3 +1) div ((breite + laenge) div 19 + 1) div i); PunkteEdit.Text := IntToStr(Punkte); end; end; procedure GameOverButtonClick(Sender: TObject); begin Timer.Enabled := false; start := false; CountdownI := 4; Countdown.Interval:=1; CountdownLabel.Caption := '3'; Countdown.Enabled := true; NeuesSpiel; GameOverButton.Visible := false; CountdownLabel.Transparent := true; FeldZeichnen; end; procedure SnakeFormClose(Sender: TObject; var Action: TCloseAction); begin //Spielbeenden1Click(Self) { FutterTimer.free PunkteTimer.free Countdown.free; ProcessMessagesON; Form4.Free; } Action := caFree //form1.close; } writeln('buildSnakeMainForm and 4 timers freed') end; procedure {TForm1.}Spielbeenden1Click(Sender: TObject); begin //Application.Terminate; //Form1.Close; //Form2.Close; //Form3.Close; timer.free; FutterTimer.free PunkteTimer.free Countdown.free; ProcessMessagesON; Form4.Close; //Action := caFree form1.close; writeln('buildSnakeMainForm and 4 timers closed') end; procedure CountdownTimer(Sender: TObject); var i,j:integer; col : TColor; begin Countdown.Interval := 600; if(CountdownI > 1) then GameOverButton.Visible := true; CountdownI := CountdownI -1; CountdownLabel.Caption := IntToStr(CountdownI); if(CountdownI >0) then GameOverButton.Visible := false; if(CountdownI = 0) then begin CountdownLabel.Caption := ''; Countdown.Enabled := false; Timer.Enabled := true; PunkteTimer.Enabled := true; start:= true; FeldZeichnen; //Voice2('lets go for Snake') end; FeldZeichnen; end; begin //@main rain processmessagesOFf; buildSnakeOptionenForm; buildSnakeMainForm; writeln(getHostname) writeln(itoa(feldbreite)+' : '+itoa(feldhoehe)) End.