Cooking up code everyday

{Opening Files}
TIntegerFile=File of integer; {<- Under Arrays}
var filebuffer:TIntegerFile;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{Under OnTSaveClick}
var Row,Col,Temp:Integer;
Begin
If SaveMazeAs Dialog.Execute then
begin {If valid file entered}
CurMazeFile:=SaveMazeAsDialog.FileName;
AssignFile(Filebuffer,CurMazeFile);
Rewrite(FileBuffer);
Write(FileBuffer,OrcGuys[Orc],KnightRow,KnightCol); {SavesObject} For Row:= 1 to NumRow do {Location}
For Col := 1 to NumCol do
Begin
Temp:=MazeMap[(Row,Col)]; {Saves Walls}
Write(FileBuffer,Temp);
End;
MainFormDC.Caption:='Edit'+CurMazeFile);
CloseFile(File,Buffer);
End;
End;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{Under OnTOpenClick}
var Row,Col,Temp:Integer;
Begin
If OpenMazeDialog.Execute then
begin {If valid file entered}
CurMazeFile:=OpenMazeDialog.FileName;
AssignFile(Filebuffer,CurMazeFile);
Reset(FileBuffer);
Read(FileBuffer,OrcGuys[Orc],KnightRow,KnightCol); {Reads Objects} For Row:= 1 to NumRow do {Location}
For Col := 1 to NumCol do
Begin
MazeMap[Row,Col]:=Temp; {Reads Walls}
Read(FileBuffer,Temp);
End;
MainFormDC.Caption:='Edit'+CurMazeFile);
CloseFile(File,Buffer);
End;
End;




Demolition code
implementation
uses Splash, Instruct;
{$R *.DFM}
const
NUMROWS = 10; NUMCOLS = 16;
BOXHEIGHT = 40; BOXWIDTH = 40;
GRASS = 0; ROCK = 1; WATER = 2;
MainCursor = 3; crDragSpider = 4;
crDragHill = 5; crWorkAnt = 6;
crDragAnt = 7;
Editing = 8;
Running = 9;
Left=1;
Right=2;
Up=3;
Down=4;
type TIntegerFile= file of integer;
var
Dir:integer;
MazeImage: array[1..NUMROWS, 1..NUMCOLS] of TImage; MazeMap: array[1..NUMROWS, 1..NUMCOLS] of integer; SpiderImage: array [1..5] of TImage; SpiderRow: array [1..5] of integer; SpiderCol: array [1..5] of integer; HillRow: integer; HillCol: integer; AntRow: integer; AntCol: integer; FileBuffer: TIntegerFile; CurMazeFile: string; Level: integer; Edit: boolean; procedure TGameForm.Exit1Click(Sender: TObject); var SaveResult: integer; begin if edit then begin SaveResult:=MessageDlg('You Maze is not saved, Do you want to save it know? ',mtWarning,mbYesNoCancel,0); if SaveResult=mrYes then SaveMazeOptionClick(SaveMazeOption); GameForm.Close; end; end; procedure TGameForm.FormShow(Sender: TObject); begin SplashScreen.Free; end; procedure TGameForm.FormCreate(Sender: TObject); var Row, Col, Spider: integer; begin Edit:= False; CurMazeFile:=''; Screen.Cursors[ MainCursor ] := LoadCursorFromFile('c:\ZB378\MazeGame\AntEye.ani' ); Cursor := MainCursor; Screen.Cursors[ crDragSpider ] := LoadCursorFromFile('c:\ZB378\MazeGame\SpiderDrag.ani' ); Screen.Cursors[ crDragHill ] := LoadCursorFromFile('c:\ZB378\MazeGame\DumpTruck.ani' ); Screen.Cursors[ crWorkAnt ] := LoadCursorFromFile('c:\ZB378\MazeGame\WorkAnt.ani' ); Screen.Cursors[ crDragAnt ] := LoadCursorFromFile('c:\ZB378\MazeGame\AntSpin.ani' ); for Row:= 1 to NUMROWS do for Col:= 1 to NUMCOLS do begin MazeImage [Row,Col]:= TImage.Create(GameForm); MazeImage [Row,Col].Parent:= GameForm; MazeImage [Row,Col].Name:= 'MazePiece'+ IntToStr(Col+(Row-1)* NUMCOLS); MazeImage [Row,Col].AutoSize:= True; MazeImage [Row,Col].Top:= (Row-1)* BOXHEIGHT; MazeImage [Row,Col].Left:= (Col-1)* BOXWIDTH; MazeImage [Row,Col].Picture:= GrassImage.Picture; MazeImage [Row,Col].OnClick:= MyMazeClick; MazeImage [Row,Col].OnDragDrop:= MyMazeDragDrop; MazeImage [Row,Col].OnDragOver:= MyMazeDragOver; MazeMap[Row,Col] := GRASS; end; for Spider:= 1 to 5 do begin SpiderImage [Spider]:= TImage.Create(GameForm); SpiderImage [Spider].Parent:= GameForm; SpiderImage [Spider].Name:= 'Spider'+ IntToStr(Spider); SpiderImage [Spider].AutoSize:= True; SpiderRow [Spider]:= 1; SpiderCol [Spider]:= Spider; SpiderImage [Spider].Top:= 0; SpiderImage [Spider].Left:= (Spider-1)* BOXWIDTH; SpiderImage [Spider].Picture:= SpidRight.Picture; SpiderImage [Spider].DragMode:= dmAutomatic; SpiderImage [Spider].Transparent:= True; SpiderImage [Spider].DragCursor := crDragSpider; if Spider > 1 then SpiderImage [Spider].Visible := False else SpiderImage [Spider].Visible := True; end; HillImage.BringToFront; HillImage.DragCursor:= crDragHill; AntImage.BringToFront; AntImage.DragCursor:= crDragAnt; AntRow:= 1; AntCol:= 7; HillRow:= 1; HillCol:= 6; Level:= 1; end; procedure TGameForm.MyMazeClick(Sender: Tobject); var Row, Col, PieceNum: integer; begin Edit:= True; with (Sender as TImage)do begin PieceNum:= StrToInt(Copy(Name,10,3)); Row:= (PieceNum-1) div NUMCOLS +1; Col:= (PieceNum-1) mod NUMCOLS +1; if MazeMap[Row,Col]= ROCK then begin Picture:= WaterImage.Picture; MazeMap[Row,Col]:= WATER; end else if MazeMap[Row,Col]= WATER then begin Picture:= GrassImage.Picture; MazeMap[Row,Col]:= GRASS; end else begin Picture:= RockImage.Picture; MazeMap[Row,Col]:= ROCK; end; end; {with} NewMazeOption.Enabled:=True; end; {MyMazeClick} procedure TGameForm.MyMazeDragDrop(Sender,Source:Tobject; X,Y: integer); var Spider, PieceNum: integer; begin Edit:= True; {Source is object being dropped. Sender is object being dropped onto} with (Sender as TImage) do begin PieceNum:= StrToInt(Copy(Name,10,3)); if (Source as TImage).Name[1]='S' then begin Spider:= StrToInt(Copy((Source as TImage).Name,7,1)); SpiderRow[Spider]:= (PieceNum-1) div NUMCOLS+1; SpiderCol[Spider]:= (PieceNum-1) mod NUMCOLS+1; end else if (Source as TImage).Name[1]='A' then begin AntRow:= (PieceNum-1) div NUMCOLS+1; AntCol:= (PieceNum-1) mod NUMCOLS+1; end else begin HillRow:= (PieceNum-1) div NUMCOLS+1; HillCol:= (PieceNum-1) mod NUMCOLS+1; end; (Source as TImage).Top:= Top; (Source as TImage).Left:= Left; end; NewMazeOption.Enabled:=True; end; Procedure TGameForm.MyMazeDragOver(Sender, Source: Tobject;X,Y: integer; State: TDragState; var Accept: boolean); var Row, Col, PieceNum: integer; begin with (Sender as TImage)do begin PieceNum:= StrToInt(Copy(Name,10,3)); Row:= (PieceNum-1) div NUMCOLS +1; Col:= (PieceNum-1) mod NUMCOLS +1; if MazeMap[Row,Col]= GRASS then Accept:= True else Accept:= False; end; NewMazeOption.Enabled:=True; end; procedure TGameForm.ShowMaze(Mode: integer); var Row, Col, Sp: integer; begin for Row:= 1 to NUMROWS do for Col:= 1 to NUMCOLS do begin if Mode= Editing then MazeImage [Row,Col].Enabled:= True else MazeImage [Row,Col].Enabled:= False; if MazeMap[Row,Col] = GRASS then MazeImage [Row,Col].Picture:= GrassImage.Picture else if MazeMap[Row,Col] = ROCK then MazeImage [Row,Col].Picture:= RockImage.Picture else if MazeMap[Row,Col] = WATER then MazeImage [Row,Col].Picture:= WaterImage.Picture; end; For Sp:= 1 to 5 do begin if Mode= Editing then SpiderImage [Sp].Enabled:= True else SpiderImage [Sp].Enabled:= False; SpiderImage [Sp].Top:= (SpiderRow[Sp]-1)* BOXHEIGHT; SpiderImage [Sp].Left:= (SpiderCol[Sp]-1)* BOXWIDTH; end; AntImage.Top:= (AntRow -1)* BOXHEIGHT; AntImage.Left:= (AntCol -1)* BOXWIDTH; HillImage.Top:= (HillRow -1)* BOXHEIGHT; HillImage.Left:= (HillCol -1)* BOXWIDTH; if Mode= Editing then begin AntImage.DragMode:= dmAutomatic; HillImage.DragMode:= dmAutomatic; End else if Mode= Running then begin AntImage.DragMode:= dmManual; HillImage.DragMode:= dmManual; end; end; procedure TGameForm.OneSpiderClick(Sender: TObject); begin Level:= 1; SpiderImage [1].Visible:= True; SpiderImage [2].Visible:= False; SpiderImage [3].Visible:= False; SpiderImage [4].Visible:= False; SpiderImage [5].Visible:= False; end; procedure TGameForm.TwoSpidersClick(Sender: TObject); begin Level:= 2; SpiderImage [1].Visible:= True; SpiderImage [2].Visible:= True; SpiderImage [3].Visible:= False; SpiderImage [4].Visible:= False; SpiderImage [5].Visible:= False; end; procedure TGameForm.ThreeSpidersClick(Sender: TObject); begin Level:= 3; SpiderImage [1].Visible:= True; SpiderImage [2].Visible:= True; SpiderImage [3].Visible:= True; SpiderImage [4].Visible:= False; SpiderImage [5].Visible:= False; end; procedure TGameForm.NewMazeOptionClick(Sender: TObject); var Row, Col, Spider, SaveResult: integer; begin if edit then begin SaveResult:=MessageDlg('You Maze is not saved, Do you want to save it know? ',mtWarning,mbYesNoCancel,0); if SaveResult=mrYes then SaveMazeOptionClick(SaveMazeOption); end; for Row:= 1 to NUMROWS do for Col:= 1 to NUMCOLS do begin MazeMap[Row,Col] := GRASS; end; for Spider:= 1 to 5 do begin SpiderRow [Spider]:= 1; SpiderCol [Spider]:= Spider; end; AntRow:= 1; AntCol:= 7; HillRow:= 1; HillCol:= 6; Level:= 1; ShowMaze(Editing); NewMazeOption.Enabled:=False; GameForm.Caption:= 'Food Raid'; Edit:= False; end; procedure TGameForm.EdittingOptionClick(Sender: TObject); var Row, Col,Sp,Temp,SaveResult: Integer; begin if edit then begin SaveResult:=MessageDlg('You Maze is not saved, Do you want to save it know? ',mtWarning,mbYesNoCancel,0); if SaveResult=mrYes then SaveMazeOptionClick(SaveMazeOption); end; try if OpenMazeAsDialog.Execute then begin {if valid file entered} CurMazeFile:= OpenMazeAsDialog.Filename; AssignFile (FileBuffer,CurMazeFile); Reset(FileBuffer); Read(FileBuffer,Level,AntRow,AntCol,HillRow,HillCol); For Sp:= 1 to 5 do begin Read(FileBuffer,Temp); SpiderRow[Sp]:=Temp; Read(FileBuffer,Temp); SpiderCol[Sp]:=Temp; if Sp <= Level then SpiderImage [Sp].Visible:= True else SpiderImage [Sp].Visible:= False; end; For Row:= 1 to NumRows do For Col:= 1 to NumCols do begin Read(FileBuffer,Temp); MazeMap[Row,Col]:= Temp; end; ShowMaze(Editing); GameForm.Caption:= 'Edit-'+ CurMazeFile; end; except on EAccessViolation do begin MessageDlg('Invalid file format:'+CurMazeFile,mtWarning,mbOKCancel,0); NewMazeOptionClick(NewMazeOption); CloseFile(FileBuffer); end; on E: EInOutError do begin if (E.ErrorCode= 100)and(E.ErrorCode= 106)then begin MessageDlg('Invalid File Format:' +CurMazeFile,mtWarning,mbOKCancel,0); NewMazeOptionClick(NewMazeOption); end; CloseFile(FileBuffer); end; else begin MessageDlg('Fatal Error',mtWarning,mbOKCancel,0); CloseFile(FileBuffer); end; end; end; procedure TGameForm.OpenMazeForRunning1Click(Sender: TObject); var Row, Col,Sp,Temp,SaveResult: Integer; begin {code that errors might occur in} if edit then begin SaveResult:=MessageDlg('You Maze is not saved, Do you want to save it know? ',mtWarning,mbYesNoCancel,0); if SaveResult=mrYes then SaveMazeOptionClick(SaveMazeOption); end; try if OpenMazeAsDialog.Execute then begin {if valid file entered} CurMazeFile:= OpenMazeAsDialog.Filename; AssignFile (FileBuffer,CurMazeFile); Reset(FileBuffer); Read(FileBuffer,Level,AntRow,AntCol,HillRow,HillCol); For Sp:= 1 to 5 do begin Read(FileBuffer,Temp); SpiderRow[Sp]:=Temp; Read(FileBuffer,Temp); SpiderCol[Sp]:=Temp; if Sp <= Level then SpiderImage [Sp].Visible:= True else SpiderImage [Sp].Visible:= False; end; For Row:= 1 to NumRows do For Col:= 1 to NumCols do begin Read(FileBuffer,Temp); MazeMap[Row,Col]:= Temp; end; ShowMaze(Running); GameForm.Caption:= 'Run-'+ CurMazeFile; CloseFile(FileBuffer); end; except on EIntError do MessageDlg('Mathematical Error',mtWarning,mbOKCancel,0); on EAccessViolation do begin MessageDlg('Invalid file format:'+CurMazeFile,mtWarning,mbOKCancel,0); NewMazeOptionClick(NewMazeOption); CloseFile(FileBuffer); end; on E: EInOutError do begin if (E.ErrorCode= 100)and(E.ErrorCode= 106)then begin MessageDlg('Invalid File Format:' +CurMazeFile,mtWarning,mbOKCancel,0); NewMazeOptionClick(NewMazeOption); end; CloseFile(FileBuffer); end; else MessageDlg('Fatal Error',mtWarning,mbOKCancel,0); end; NewMazeOption.Enabled:=True; RunMenu.Enabled:=True; end; procedure TGameForm.OpenGame1Click(Sender: TObject); begin NewMazeOption.Enabled:=True; RunMenu.Enabled:=True; Continue.Enabled:=True; Quit.Enabled:=True; FileMenu.Enabled:=False; LevelMenu.Enabled:=False; StartMaze.Enabled:=False; Pause.Enabled:=False; end; procedure TGameForm.FourSpidersClick(Sender: TObject); begin Level:= 4; SpiderImage [1].Visible:= True; SpiderImage [2].Visible:= True; SpiderImage [3].Visible:= True; SpiderImage [4].Visible:= True; SpiderImage [5].Visible:= False; end; procedure TGameForm.FiveSpidersClick(Sender: TObject); begin Level:= 5; SpiderImage [1].Visible:= True; SpiderImage [2].Visible:= True; SpiderImage [3].Visible:= True; SpiderImage [4].Visible:= True; SpiderImage [5].Visible:= True; end; procedure TGameForm.StartMazeClick(Sender: TObject); begin Pause.Enabled:=True; Quit.Enabled:=True; FileMenu.Enabled:=False; LevelMenu.Enabled:=False; HighScoreMenu.Enabled:=False; InstructMenu.Enabled:=False; Continue.Enabled:=False; end; procedure TGameForm.ContinueClick(Sender: TObject); begin Pause.Enabled:=True; Quit.Enabled:=True; HighScoreMenu.Enabled:=False; InstructMenu.Enabled:=False; Continue.Enabled:=False; end; procedure TGameForm.PauseClick(Sender: TObject); begin HighScoreMenu.Enabled:=True; InstructMenu.Enabled:=True; Continue.Enabled:=True; Quit.Enabled:=True; Pause.Enabled:=False; end; procedure TGameForm.QuitClick(Sender: TObject); begin FileMenu.Enabled:=True; LevelMenu.Enabled:=True; HighScoreMenu.Enabled:=True; InstructMenu.Enabled:=True; StartMaze.Enabled:=True; NewMazeOption.Enabled:=False; RunMenu.Enabled:=False; Continue.Enabled:=False; Pause.Enabled:=False; Quit.Enabled:=False; end; procedure TGameForm.SaveMazeAsOptionClick(Sender: TObject); var Row,Col,Sp,Temp: Integer; begin {code that errors might occur in} if SaveMazeAsDialog.Execute then begin {if valid file entered} CurMazeFile:= SaveMazeAsDialog.Filename; try AssignFile (FileBuffer,CurMazeFile); Rewrite(FileBuffer); Write(FileBuffer,Level,AntRow,AntCol,HillRow,HillCol); For Sp:= 1 to 5 do begin Temp:= SpiderRow[Sp]; Write(FileBuffer,Temp); Temp:=SpiderCol[Sp]; Write(FileBuffer,Temp); end; For Row:= 1 to NumRows do For Col:= 1 to NumCols do begin Temp:= MazeMap[Row,Col]; Write(FileBuffer,Temp); end; GameForm.Caption:= 'Edit-'+ CurMazeFile; except on EIntError do MessageDlg('Mathematical Error',mtWarning,mbOKCancel,0); on EAccessViolation do begin MessageDlg('Invalid file format:'+CurMazeFile,mtWarning,mbOKCancel,0); NewMazeOptionClick(NewMazeOption); CloseFile(FileBuffer); end; on E: EInOutError do begin if (E.ErrorCode= 100)and(E.ErrorCode= 106)then begin MessageDlg('Invalid File Format:' +CurMazeFile,mtWarning,mbOKCancel,0); NewMazeOptionClick(NewMazeOption); end; CloseFile(FileBuffer); end; else MessageDlg('Fatal Error',mtWarning,mbOKCancel,0); end; end; end; procedure TGameForm.ViewInstructions1Click(Sender: TObject); begin InstructForm.Visible:= True; end; procedure TGameForm.SaveMazeOptionClick(Sender: TObject); var Row,Col,Sp,Temp: Integer; begin {code that errors might occur in} if CurMazeFile= '' then SaveMazeAsOptionClick(SaveMazeAsOption) else begin {if valid file entered} CurMazeFile:= SaveMazeAsDialog.Filename; try AssignFile (FileBuffer,CurMazeFile); Rewrite(FileBuffer); Write(FileBuffer,Level,AntRow,AntCol,HillRow,HillCol); For Sp:= 1 to 5 do begin Temp:= SpiderRow[Sp]; Write(FileBuffer,Temp); Temp:=SpiderCol[Sp]; Write(FileBuffer,Temp); end; For Row:= 1 to NumRows do For Col:= 1 to NumCols do begin Temp:= MazeMap[Row,Col]; Write(FileBuffer,Temp); end; GameForm.Caption:= 'Edit-'+ CurMazeFile; except on EIntError do MessageDlg('Mathematical Error',mtWarning,mbOKCancel,0); on EAccessViolation do begin MessageDlg('Invalid file format:'+CurMazeFile,mtWarning,mbOKCancel,0); NewMazeOptionClick(NewMazeOption); CloseFile(FileBuffer); end; on E: EInOutError do begin if (E.ErrorCode= 100)and(E.ErrorCode= 106)then begin MessageDlg('Invalid File Format:' +CurMazeFile,mtWarning,mbOKCancel,0); NewMazeOptionClick(NewMazeOption); end; {if error 100 or 106} CloseFile(FileBuffer); end; {if I/O error} else begin MessageDlg('Fatal Error',mtWarning,mbOKCancel,0); CloseFile(FileBuffer); end; end; end; end; procedure TGameForm.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if runmenu.enabled then case key of 38: begin dir:=up; end; 40: begin dir:=down; end; 37: begin dir:=left; end; 39: begin dir:=right; end; end; end; procedure TGameForm.MoveTimerTimer(Sender: TObject); begin if dir= up then begin if mazemap[antrow-1,antcol]=GRASS then begin antrow:=antrow-1; antimage.top:=(antrow-1)*boxheight; antimage.picture:=antupimage.picture; end; end; if dir=down then begin if mazemap[antrow+1,antcol]=GRASS then begin antrow:=antrow+1; antimage.top:=(antrow-1)*boxheight; antimage.picture:=antdownimage.picture; end; end; if dir= left then begin if mazemap[antrow,antcol-1]=GRASS then begin antcol:=antcol-1; antimage.left:=(antcol-1)*boxheight; antimage.picture:=antleftimage.picture; end; end; if dir= right then begin if mazemap[antrow,antcol+1]=GRASS then begin antcol:=antcol+1; antimage.left:=(antcol-1)*boxheight; antimage.picture:=antrightimage.picture; end; end; end; end.