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:
| uses...
const farben: Array [0..2] of TColor = (clBlack, clLime, clRed); raster = 10; breite = 24; hoehe = 24;
type TForm1 = class(TForm) ... private map: Array [0..breite] of Array [0..hoehe] of Integer; kopf, richtung, futter: TPoint; laenge, punkte: Integer; ende: Boolean; bmp: TBitmap; public end; ... procedure enable_buttons(status: Boolean); procedure spiel_ende; procedure neues_futter; procedure paint_map; procedure calc_snake; public end; ... mplementation
{$R *.dfm}
procedure TForm1.enable_buttons(status: Boolean); begin Button2.Enabled := status; Button3.Enabled := status; Button4.Enabled := status; Button5.Enabled := status; end; ... procedure TForm1.spiel_ende; begin ende := True; Timer1.Enabled := False; enable_buttons(False); Button1.Enabled := True; end; ... ... procedure TForm1.neues_futter; begin futter := Point(Random(breite), Random(hoehe)); while map[futter.X, futter.Y]<>0 do futter := Point(Random(breite), Random(hoehe)); map[futter.X,futter.Y] := -1; end; ... ... procedure TForm1.paint_map; var i, j: Integer; col: TColor; begin for i := 0 to breite do for j := 0 to hoehe do begin case map[i, j] of -1: col := farben[1]; 0: col := farben[0]; else col := farben[2]; end; PaintBox1.Canvas.Brush.Color:=col; PaintBox1.Canvas.FillRect(Rect(i*raster, j*raster, (i+1)*raster, (j+1)*raster)); end; end; ... procedure TForm1.calc_snake; var i, j: Integer; begin for i := 0 to breite do for j := 0 to hoehe do if map[i, j]>0 then Dec(map[i, j]); Inc(kopf.X, richtung.X); Inc(kopf.Y, richtung.Y); if (kopf.X<0) or (kopf.X>breite) or (kopf.Y<0) or (kopf.Y>hoehe) then begin spiel_ende; ShowMessage('Sie haben den Rand berührt!'); end; if (map[kopf.X, kopf.Y]>0) and (not ende) then begin spiel_ende; ShowMessage('Sie haben sich selbst gebissen!'); end; if (kopf.X=futter.X) and (kopf.Y=futter.Y) then begin Inc(laenge); neues_futter; end; map[kopf.X, kopf.Y] := laenge; end; ... procedure TForm1.Button1Click(Sender: TObject); var i, j: Integer; begin Button1.Enabled := False; for i:=0 to breite do for j:=0 to hoehe do map[i, j] := 0; kopf := Point(1, 1); laenge := 5; richtung := Point(0, 1); map[kopf.X, kopf.Y] := laenge; neues_futter; paint_map; enable_buttons(True); ende := False; Timer1.Enabled := True; end; ... ... procedure TForm1.Timer1Timer(Sender: TObject); begin calc_snake; if not ende then paint_map; end; ... . procedure TForm1.Button2Click(Sender: TObject); begin if richtung.Y=0 then richtung := Point(0, -1); end; ... ... procedure TForm1.Button3Click(Sender: TObject); begin if richtung.X=0 then richtung := Point(-1, 0); end; ... ... procedure TForm1.Button4Click(Sender: TObject); begin if richtung.X=0 then richtung := Point(1, 0); end; ... ... procedure TForm1.Button5Click(Sender: TObject); begin if richtung.Y=0 then richtung := Point(0, 1); end; ... procedure TForm1.FormCreate(Sender: TObject); begin Randomize; end; ... |