XIN CHÀO CÁC BẠN MÌNH VỪA CODE XONG GAME NÀY CÒN NHIỀU LỔI SAI MỌI NGƯỜI GIÚP MÌNH HOÀNG THIỆN NÓ NHÉ XIN CẢM ƠN!!!!!1
MAIN .PAS
program TANK_GAME; uses Classes, TOOL_GAMEs, crt; VAR Game : TGame; begin Game := TGame.Create; while true do begin Game.Update; Game.ClearnCreen; Game.Render; Game.Delays; end; end.
GAME_TOOLS.PAS
unit TOOL_GAMES; interface uses SysUtils, Crt; const MAXCOLUM = 75; MAXCELL = 25; IMAGETANK1 = #179; IMAGETANK2 = #2; IMAGETANK3 = #197; KEY_UP = #72; KEY_DOWN = #80; KEY_LEFT = #75; KEY_RIGHT = #77; KEY_PASPACE = #32; KEY_ESC = #27; type TPoint = record colum, cell : byte; end; TDirection = (NODIRECTION, UP, DOWN, RIGHT,LEFT); TFaction = (NOFACTION, PLAYER, ENNEMY); TCommand = (NOCOMMAND,TANKUP,TANKDOWN,TANKLEFT,TANKRIGHT,TANKFIRE); { TAmmo } TAmmo = Object private A_Point : TPoint; A_Direction : TDirection; A_Death : Boolean; public procedure AmmoCreate(Point: TPoint; Direction : Tdirection); procedure AmmoMove; function AmmoGetPoint : TPoint; function AmmoGetDirectiion : TDirection; end; { TTank } TTank = object private T_Point : TPoint; T_Direction : TDirection; T_Faction : TFaction; T_Fire : Boolean; protected public procedure TankCreate(Point : TPoint; FacTion : TFaction); procedure TankSetPoint(Point : TPoint); procedure TankSetCommand(Command : TCommand); procedure TankMove; function TankGetPoint : TPoint; function TankGetDirection : TDirection; function TankGetFire : Boolean; end; { TCar } TCar = Object Private C_Point : TPoint; C_Death : Boolean; public procedure CarCreate(Point : TPoint); Procedure CarRandonCreat; function CarCheckAmmoHit(AmmoPoint : TPoint) : Boolean; end; { TGame } TGame = Class private Tank : TTank; Ammo : TAmmo; Car : TCar; protected Procedure Init; procedure DrawImage(Colum, Cell : Byte; Image : Char); Procedure DrawTank(Point : TPoint; Direction : TDirection); procedure DrawCar(Point : TPoint); function UpdateCommand : TCommand; public constructor Create; Procedure Update; procedure ClearnCreen; procedure Render; procedure Delays; destructor destroy; end; implementation { TCar } procedure TCar.CarCreate(Point: TPoint); begin C_Point := Point; C_Death := False; end; procedure TCar.CarRandonCreat; VAR cell, colum : Byte; begin cell := random(MAXCELL); COLUM := random(MAXCOLUM); if cell <2 then cell :=2 else if cell > MAXCELL Then cell:=MAXCELL-2 else if colum < 2 Then colum:=2 else if colum > MAXCOLUM Then colum:=MAXCOLUM -2; C_Point.cell:=cell; C_Point.colum:=colum; C_Death := False; end; function TCar.CarCheckAmmoHit(AmmoPoint: TPoint): Boolean; begin Result:= False; if (Ammopoint.cell >= C_Point.cell-1) And (AmmoPoint.cell<= C_Point.cell+1) AND (Ammopoint.colum >= C_Point.colum-1) And (AmmoPoint.colum<= C_Point.colum+1)then begin Result:= True; end; end; { TAmmo } procedure TAmmo.AmmoCreate(Point: TPoint; Direction: Tdirection); begin A_Point := Point; A_Direction := Direction; A_Death := False; end; procedure TAmmo.AmmoMove; begin if A_Point.cell <1 then A_Death := True else if A_Point.cell > MAXCELL Then A_Death := True else if A_Point.colum < 1 Then A_Death := True else if A_Point.colum > MAXCOLUM Then A_Death := True else case A_Direction of UP:Dec(A_Point.cell,2); DOWN:Inc(A_Point.cell,2); LEFT:Dec(A_Point.colum,2); RIGHT:Inc(A_Point.colum,2); end; end; function TAmmo.AmmoGetPoint: TPoint; begin Result := A_Point; end; function TAmmo.AmmoGetDirectiion: TDirection; begin Result := A_Direction; end; { TGame } procedure TGame.Init; var Point : TPoint; begin Randomize; Point.colum:=10; Point.cell:=10; Tank.TankCreate(Point,PLAYER); Point.cell:=14; Point.colum:=14; Car.CarCreate(Point); DrawTank(Tank.T_Point,Tank.T_Direction); DrawCar(Car.C_Point); end; procedure TGame.DrawImage(Colum, Cell: Byte; Image: Char); begin Gotoxy(Colum, Cell); Write(Image); Gotoxy(1,1); end; procedure TGame.DrawTank(Point: TPoint; Direction: TDirection); begin Case Direction of UP: begin DrawImage(Point.colum, Point.cell, IMAGETANK2); DrawImage(Point.colum-1, Point.cell, IMAGETANK2); DrawImage(Point.colum+1, Point.cell, IMAGETANK2); DrawImage(Point.colum-1, Point.cell-1, IMAGETANK2); DrawImage(Point.colum, Point.cell-1, IMAGETANK1); DrawImage(Point.colum+1, Point.cell-1, IMAGETANK2); DrawImage(Point.colum-1, Point.cell+1, IMAGETANK2); DrawImage(Point.colum+1, Point.cell+1, IMAGETANK2); end; DOWN: begin DrawImage(Point.colum, Point.cell, IMAGETANK2); DrawImage(Point.colum-1, Point.cell, IMAGETANK2); DrawImage(Point.colum+1, Point.cell, IMAGETANK2); DrawImage(Point.colum-1, Point.cell+1, IMAGETANK2); DrawImage(Point.colum, Point.cell+1, IMAGETANK1); DrawImage(Point.colum+1, Point.cell+1, IMAGETANK2); DrawImage(Point.colum-1, Point.cell-1, IMAGETANK2); DrawImage(Point.colum+1, Point.cell-1, IMAGETANK2); end; LEFT: begin DrawImage(Point.colum, Point.cell, IMAGETANK2); DrawImage(Point.colum, Point.cell-1, IMAGETANK2); DrawImage(Point.colum, Point.cell+1, IMAGETANK2); DrawImage(Point.colum-1, Point.cell-1, IMAGETANK2); DrawImage(Point.colum-1, Point.cell, IMAGETANK3); DrawImage(Point.colum-1, Point.cell+1, IMAGETANK2); DrawImage(Point.colum+1, Point.cell-1, IMAGETANK2); DrawImage(Point.colum+1, Point.cell+1, IMAGETANK2); end; RIGHT: begin DrawImage(Point.colum, Point.cell, IMAGETANK2); DrawImage(Point.colum, Point.cell-1, IMAGETANK2); DrawImage(Point.colum, Point.cell+1, IMAGETANK2); DrawImage(Point.colum+1, Point.cell-1, IMAGETANK2); DrawImage(Point.colum+1, Point.cell, IMAGETANK3); DrawImage(Point.colum+1, Point.cell+1, IMAGETANK2); DrawImage(Point.colum-1, Point.cell-1, IMAGETANK2); DrawImage(Point.colum-1, Point.cell+1, IMAGETANK2); end; end; end; procedure TGame.DrawCar(Point: TPoint); begin DrawImage(Point.colum, Point.cell,#2); DrawImage(Point.colum-1, Point.cell,#2); DrawImage(Point.colum+1, Point.cell,#2); DrawImage(Point.colum-1, Point.cell-1,#2); DrawImage(Point.colum+1, Point.cell-1,#2); DrawImage(Point.colum-1, Point.cell+1,#2); DrawImage(Point.colum+1, Point.cell+1,#2); end; function TGame.UpdateCommand: TCommand; var Key: Char; begin Result := NOCOMMAND; If Keypressed Then begin Key := ReadKey; Case Key of KEY_UP: Result := TANKUP; KEY_DOWN: Result := TANKDOWN; KEY_LEFT:Result := TANKLEFT; KEY_RIGHT: Result := TANKRIGHT; KEY_PASPACE: Result := TANKFIRE; KEY_ESC : Halt; end; end; end; constructor TGame.Create; begin Init; end; procedure TGame.Update; begin Tank.TankSetCommand(UpdateCommand); IF (Tank.TankGetFire = True) And (Ammo.A_Death = True) Then begin Ammo.AmmoCreate(Tank.TankGetPoint,Tank.TankGetDirection); end; IF Car.C_Death = True Then Car.CarRandonCreat; IF Car.CarCheckAmmoHit(Ammo.A_Point) = True Then Car.C_Death:=True; Ammo.AmmoMove; Tank.TankMove; end; procedure TGame.ClearnCreen; begin clrscr; end; procedure TGame.Render; begin DrawTank(Tank.T_Point,Tank.T_Direction); DrawCar(Car.C_Point); DrawImage(Ammo.A_Point.colum, Ammo.A_Point.cell,#3); end; procedure TGame.Delays; begin Delay(80); end; destructor TGame.destroy; begin end; { TTank } procedure TTank.TankCreate(Point: TPoint; FacTion: TFaction); begin T_Point := Point; T_Direction := NODIRECTION; T_Faction := Faction; end; procedure TTank.TankSetPoint(Point: TPoint); begin T_Point := Point; end; procedure TTank.TankSetCommand(Command : TCommand); begin T_Fire := False; Case Command of TANKUP: T_Direction := UP; TANKDOWN:T_Direction := DOWN; TANKLEFT:T_Direction :=LEFT; TANKRIGHT:T_Direction :=RIGHT; TANKFIRE: T_Fire := True; end; end; procedure TTank.TankMove; begin if T_Point.cell < 1 Then T_Point.cell:=1 else if T_Point.cell > MAXCELL Then T_Point.cell:= MAXCELL else if T_Point.colum < 1 Then T_Point.colum:= 1 else if T_Point.colum > MAXCOLUM Then T_Point.colum:= MAXCOLUM else Case T_Direction of UP:Dec(T_Point.cell); DOWN:Inc(T_Point.cell); LEFT:Dec(T_Point.colum); RIGHT:Inc(T_Point.colum); end; end; function TTank.TankGetPoint: TPoint; begin Result := T_Point; end; function TTank.TankGetDirection: TDirection; begin Result := T_Direction; end; function TTank.TankGetFire: Boolean; begin Result := t_Fire; end; end.
XIN CHỈ GIÁO!!!!