1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294:
| unit schlange;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type TForm1 = class(TForm) Timer1: TTimer; procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure FormActivate(Sender: TObject); procedure Timer1Timer(Sender: TObject); private public end;
TZeiger = ^TListenElement; TListenElement = object next: TZeiger; image: TLabel; constructor init(pX,pY: integer; pNext: TZeiger); end; TListe = class head, tail: TZeiger; direction: string; constructor init; procedure bewege(move: string; verlaengern: boolean); function suchen(pX,pY: integer): boolean; end;
TFutterZeiger = ^TFutterElement;
TFutterElement = object image: TLabel; gefressen: boolean; next: TFutterZeiger; constructor init; end;
TFutter = class head, tail: TFutterZeiger; constructor init; function anzahl: word; procedure erstelle; function suche(pX,pY: integer): boolean; end;
var Form1: TForm1;
implementation
constructor TListenElement.init; begin self.next:= pNext; self.image:= TLabel.Create(Form1); self.image.Parent:= Form1; self.image.Left:= pX; self.image.top:= pY; self.image.Color:= clred; self.image.Width:= 10; self.image.Height:= 10; end;
constructor TListe.init; var i,x: integer; neu: TZeiger; begin self.direction:= 'right'; x:= 100; new(neu, init(x,200,nil)); tail:= neu; head:= neu; x:= x + 10; for i:= 2 to 10 do begin new(neu, init(x,200,nil)); head.next:= neu; head:= head.next; x:= x + 10; end; end;
procedure TListe.bewege; var neu: TZeiger; begin if move = 'up' then begin if verlaengern then begin if head.image.Top - 10 < 0 then new(neu, init(head.image.Left,Form1.ClientHeight - 10,nil)) else new(neu, init(head.image.Left,head.image.Top - 10,nil)); head.next:= neu; head:= neu; end else begin if head.image.Top - 10 < 0 then tail.image.Top:= Form1.ClientHeight - 10 else tail.image.Top:= head.image.Top - 10; tail.image.Left:= head.image.Left; end; end; if move = 'right' then begin if verlaengern then begin if head.image.Left + 10 + 10 > Form1.ClientWidth then new(neu, init(0,head.image.Top,nil)) else new(neu, init(head.image.Left + 10,head.image.Top,nil)); head.next:= neu; head:= neu; end else begin if head.image.Left + 10 + 10 > Form1.ClientWidth then tail.image.Left:= 0 else tail.image.Left:= head.image.Left + 10; tail.image.Top:= head.image.Top; end; end; if move = 'down' then begin if verlaengern then begin if head.image.Top + 10 + 10 > Form1.ClientWidth then new(neu, init(head.image.Left,0,nil)) else new(neu, init(head.image.Left,head.image.Top + 10,nil)); head.next:= neu; head:= neu; end else begin if head.image.Top + 10 + 10 > Form1.ClientHeight then tail.image.Top:= 0 else tail.image.Top:= head.image.Top + 10; tail.image.Left:= head.image.Left; end; end; if move = 'left' then begin if verlaengern then begin if head.image.Left - 10 < 0 then new(neu, init(Form1.ClientWidth - 10,head.image.Top,nil)) else new(neu, init(head.image.Left - 10,head.image.Top,nil)); head.next:= neu; head:= neu; end else begin if head.image.Left - 10 < 0 then tail.image.Left:= Form1.ClientWidth - 10 else tail.image.Left:= head.image.Left - 10; tail.image.Top:= head.image.Top; end; end; if not verlaengern then begin head.next:= tail; tail:= tail.next; head.next.next:= nil; head:= head.next; end; if suchen(head.image.Left, head.image.Top) then Form1.Timer1.Enabled:= false; end;
function TListe.suchen; var hilf: TZeiger; zaehler: word; begin suchen:= false; zaehler:= 0; hilf:= tail; while hilf <> nil do begin if (hilf.image.Left = pX) and (hilf.image.Top = pY) then inc(zaehler); hilf:= hilf.next; end; if zaehler > 1 then suchen:= true; end;
constructor TFutterElement.init; begin self.image:= TLabel.Create(Form1); self.image.Parent:= Form1; self.image.Color:= clblue; self.image.Width:= 4; self.image.Height:= 4; self.image.Visible:= true; self.next:= nil; self.gefressen:= false; end;
constructor TFutter.init; var i: word; neu: TFutterZeiger; begin new(neu, init); tail:= neu; head:= neu; for i:= 2 to 5 do begin new(neu, init); head.next:= neu; head:= head.next; end; end;
function TFutter.anzahl; var anz: word; hilf: TFutterZeiger; begin anz:= 0; hilf:= head; while hilf <> nil do begin if hilf.image.Visible then inc(anz); hilf:= hilf.next; end; anzahl:= anz; end;
procedure TFutter.erstelle; var hilf: TFutterZeiger; begin hilf:= head; if hilf.image.Visible then begin while hilf.image.Visible and (hilf.next <> nil) do hilf:= hilf.next; if not hilf.image.Visible then begin hilf.image.Left:= (random(Form1.ClientWidth) div 10 * 10) + 3; hilf.image.Top:= (random(Form1.ClientHeight) div 10 * 10) + 3; hilf.image.Visible:= true; end; end else begin hilf.image.Left:= (random(Form1.ClientWidth) div 10 * 10) + 3; hilf.image.Top:= (random(Form1.ClientHeight) div 10 * 10) + 3; hilf.image.Visible:= true; end; end;
function TFutter.suche; var hilf: TFutterZeiger; begin hilf:= head; suche:= false; while hilf <> nil do begin if hilf.image.Visible and (hilf.image.Left = pX) and (hilf.image.Top = pY) then hilf.image.Visible:= false; hilf:= hilf.next; end; end;
{$R *.dfm}
var schlange1: TListe; Futter1: TFutter;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if key = 27 then self.Close; if (key = VK_Up) and not (schlange1.direction = 'down') then schlange1.direction:= 'up'; if (key = VK_Right) and not (schlange1.direction = 'left') then schlange1.direction:= 'right'; if (key = VK_Down) and not (schlange1.direction = 'up') then schlange1.direction:= 'down'; if (key = VK_Left) and not (schlange1.direction = 'right') then schlange1.direction:= 'left'; end;
procedure TForm1.FormActivate(Sender: TObject); begin randomize; schlange1:= TListe.Create; schlange1.init; Futter1:= TFutter.Create; Futter1.init; end;
procedure TForm1.Timer1Timer(Sender: TObject); var zufall: integer; begin
Futter1.erstelle;
end;
end. |