- 14 Tháng năm 2017
- 3,974
- 7,623
- 744
- 22
- Phú Yên
- Trường THPT Lương Văn Chánh
Chắc suất Đại học top - Giữ chỗ ngay!! ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.
Nhìn đơn giản vậy mà mình viết phải mấy ngày, dùng đến cả Object mới được
Đây là Code Game
Còn đây là hình Demo
Sử dụng các phím mũi tên để di chuyển và phím cách để bắn. Game này giống game Tank trên máy điện tử hồi xưa
Đây là Code Game
Mã:
Program Tank_Game;
uses crt,dos,Graph;
const
_Up = #72;
_Down = #80;
_Left = #75;
_Right = #77;
_Space = #32;
_NX = 25;
_NY = 25;
_NTank = 5;
_Nlives = 5;
_Bgr = LightGreen;
_None = 0;
_Player = _NTank + 1;
_Block = 15;
type TTank = object
X,Y,value,time: integer;
Dir, LDir: char;
Enable: Boolean;
Function CanMove(Dr: char): Boolean;
Procedure SetTank(Cx,Cy,vl,t: integer; dr,Ldr: char;en: Boolean);
Procedure ClearTank;
Procedure DrawTank(Cx,Cy: integer);
Procedure TargetAI;
end;
type TBullet = object
X,Y,Tank: integer;
Dir: char;
Enable: Boolean;
Procedure SetBullet(Cx,Cy,tak: integer;dr: char;en: boolean);
Procedure Test;
Procedure Move;
end;
var
Scr: array[1.._NX,1.._NY] of integer;
TrTank: array[1.._NTank] of TTank;
Bullet: array[1.._Player] of TBullet;
PTank: TTank;
Score,Lives: integer;
procedure setcusor(bot,top: byte);
var regs : registers;
begin
regs.ah :=1;
regs.ch:=bot;
regs.cl:=top;
intr($10,regs);
end;
Procedure TBullet.SetBullet(Cx,Cy,Tak: integer;Dr: char;En: Boolean);
Begin
Tank := Tak;
Dir := Dr;
Enable := en;
case Dir of
_Up:
begin
X := Cx + 1;
Y := Cy - 1;
end;
_Down:
begin
X := Cx + 1;
Y := Cy + 3;
end;
_Left:
begin
X := Cx - 1;
Y := Cy + 1;
end;
_Right:
begin
X := Cx + 3;
Y := Cy + 1;
end;
end;
End;
Procedure TBullet.Test;
var tak: integer;
Begin
tak := Scr[X,Y];
if (Tak = _Block) then
Enable := False
else if (Tak = _Player) then
begin
inc(Lives,-1);
Enable := false;
Sound(1000);
Delay(75);NoSound;
end
else
if (Tak <> Tank) then
begin
TrTank[Tak].Enable := False;
TrTank[Tak].Value := 0;
TrTank[Tak].DrawTank(TrTank[Tak].X,TrTank[Tak].Y);
Enable := False;
if Tank = _Player then inc(Score);
Sound(1000);
Delay(75);NoSound;
end;
End;
Procedure TBullet.Move;
Begin
if Enable then
begin
if (Scr[X,Y] = _None) then
begin
Textcolor(_Bgr);
Gotoxy(X,Y);Write('Û');
case dir of
_Up: if Y > 1 then inc(Y,-1);
_Down: if Y < _NY then inc(Y);
_Left: if X > 1 then inc(X,-1);
_Right: if X < _NX then inc(X);
end;
TextColor(Tank);
Gotoxy(X,Y);Write('Û');
end
else Test;
end;
PTank.DrawTank(PTank.X,PTank.Y);
End;
Function TTank.CanMove(Dr: char): Boolean;
Begin
CanMove := False;
case Dr of
_Up:
if (Scr[X+1,Y-1] <> _BLock)
and (Scr[X,Y] <> _Block)
and (Scr[X+2,Y] <> _Block)
then CanMove := True;
_Down:
if (Scr[X+1,Y+3] <> _Block)
and (Scr[X,Y+2] <> _Block)
and (Scr[X+2,Y+2] <> _Block)
then CanMove := True;
_Left:
if (Scr[X-1,Y+1] <> _Block)
and (Scr[X,Y] <> _Block)
and (Scr[X,Y+2] <> _Block)
then CanMove := True;
_Right:
if (Scr[X+3,Y+1] <> _Block)
and (Scr[X+2,Y] <> _Block)
and (Scr[X+2,Y+2] <> _Block)
then CanMove := True;
end;
End;
Procedure TTank.SetTank(Cx,Cy,vl,t: integer; dr,ldr: char;en: Boolean);
Begin
X := Cx;
Y := Cy;
value := vl;
time := t;
dir := dr;
ldir := ldr;
Enable := en;
End;
Procedure TTank.ClearTank;
Begin
case Ldir of
_Up:
begin
Scr[X+1,Y] := _None;
Scr[X,Y+1] := _None;
Scr[X+1,Y+1] := _None;
Scr[X+2,Y+1] := _None;
Scr[X,Y+2] := _None;
Scr[X+2,Y+2] := _None;
end;
_Down:
begin
Scr[X,Y] := _None;
Scr[X+2,Y] := _None;
Scr[X,Y+1] := _None;
Scr[X+1,Y+1] := _None;
Scr[X+2,Y+1] := _None;
Scr[X+1,Y+2] := _None;
end;
_Left:
begin
Scr[X+1,Y] := _None;
Scr[X+2,Y] := _None;
Scr[X,Y+1] := _None;
Scr[X+1,Y+1] := _None;
Scr[X+1,Y+2] := _None;
Scr[X+2,Y+2] := _None;
end;
_Right:
begin
Scr[X,Y] := _None;
Scr[X+1,Y] := _None;
Scr[X+1,Y+1] := _None;
Scr[X+2,Y+1] := _None;
Scr[X,Y+2] := _None;
Scr[X+1,Y+2] := _None;
end;
end;
End;
Procedure TTank.DrawTank(Cx,Cy: integer);
Begin
ClearTank;
X := Cx;
Y := Cy;
case dir of
_Up:
begin
Scr[X+1,Y] := value;
Scr[X,Y+1] := value;
Scr[X+1,Y+1] := value;
Scr[X+2,Y+1] := value;
Scr[X,Y+2] := value;
Scr[X+2,Y+2] := value;
end;
_Down:
begin
Scr[X,Y] := value;
Scr[X+2,Y] := value;
Scr[X,Y+1] := value;
Scr[X+1,Y+1] := value;
Scr[X+2,Y+1] := value;
Scr[X+1,Y+2] := value;
end;
_Left:
begin
Scr[X+1,Y] := value;
Scr[X+2,Y] := value;
Scr[X,Y+1] := value;
Scr[X+1,Y+1] := value;
Scr[X+1,Y+2] := value;
Scr[X+2,Y+2] := value;
end;
_Right:
begin
Scr[X,Y] := value;
Scr[X+1,Y] := value;
Scr[X+1,Y+1] := value;
Scr[X+2,Y+1] := value;
Scr[X,Y+2] := value;
Scr[X+1,Y+2] := value;
end;
end;
End;
Procedure TTank.TargetAI;
var i: integer;
Begin
case dir of
_Up:
begin
for i := Y - 1 downto 1 do
if Scr[X+1,i] <> _None then break;
if Scr[X+1,i] = _Player then
Bullet[Value].SetBullet(X+1,Y-1,value,dir,True);
end;
_Down:
begin
for i := Y + 3 to _NY do
if Scr[X+1,i] <> _None then break;
if Scr[X+1,i] = _Player then
Bullet[Value].SetBullet(X+1,Y+3,value,dir,True);
end;
_Left:
begin
for i := X - 1 downto 1 do
if Scr[i,Y+1] <> _None then break;
if Scr[i,Y+1] = _Player then
Bullet[Value].SetBullet(X-1,Y+1,value,dir,True);
end;
_Right:
begin
for i := X + 3 to _NX do
if Scr[i,Y+1] <> _None then break;
if Scr[i,Y+1] = _Player then
Bullet[Value].SetBullet(X+3,Y+1,value,dir,True);
end;
end;
End;
Procedure CreateScreen;
var x,y: integer;
Begin
for x := 1 to _NX do
begin
Scr[x,1] := _Block;
Scr[x,_NY] := _Block;
end;
for y := 1 to _NY do
begin
Scr[1,Y] := _Block;
Scr[_Nx,y] := _Block;
end;
End;
Procedure DrawScreen;
var x,y,l: integer;
Begin
for x := 1 to _NX do
for y := 1 to _NY do
begin
if Scr[X,Y] <> _None then
Textcolor(Scr[X,Y])
else
TextColor(_Bgr);
Gotoxy(X,Y);Write('Û');
end;
TextColor(Yellow);
Gotoxy(_NX +5,2);Write('T A N K');
TextColor(Cyan);
Gotoxy(_NX + 2,5);Write('Lives: ');
for l := 1 to _NLives do
begin
TextColor(Red);
Gotoxy(_NX + 8+l,5);
if l <= Lives then
Write(#3)
else Write(' ');
end;
TextColor(Cyan);
Gotoxy(_NX + 2,7);Write('Score: ',Score,' ');
End;
Procedure CreateTarget;
var i,X,Y: integer;
Begin
for i := 1 to _NTank do
begin
X := 2 + Random(_NX - 5);
Y := 2 + Random(_NY - 5);
TrTank[i].SetTank(X,Y,i,0,_Right,_Right,True);
trTank[i].DrawTank(TrTank[i].X,TrTank[i].Y);
end;
End;
Procedure ChangeTarget;
var i,dir,x,y: integer;
Begin
for i := 1 to _NTank do
if TrTank[i].Enable then
begin
dir := 1 + Random(7);
case dir of
1:
if TrTank[i].CanMove(_Up) then
Begin
TrTank[i].LDir := TrTank[i].Dir;
TrTank[i].Dir := _Up;
TrTank[i].DrawTank(TrTank[i].X,TrTank[i].Y-1);
End;
2:
if TrTank[i].CanMove(_Down) then
Begin
TrTank[i].LDir := TrTank[i].Dir;
TrTank[i].Dir := _Down;
TrTank[i].DrawTank(TrTank[i].X,TrTank[i].Y+1);
End;
3:
if TrTank[i].CanMove(_Left) then
Begin
TrTank[i].LDir := TrTank[i].Dir;
TrTank[i].Dir := _Left;
TrTank[i].DrawTank(TrTank[i].X-1,TrTank[i].Y);
End;
4:
if TrTank[i].CanMove(_Right) then
Begin
TrTank[i].LDir := TrTank[i].Dir;
TrTank[i].Dir := _Right;
TrTank[i].DrawTank(TrTank[i].X+1,TrTank[i].Y);
End;
5,6,7: Bullet[TrTank[i].Value].SetBullet(TrTank[i].X,
TrTank[i].Y,i,TrTank[i].Dir,True);
end;
end
else
begin
inc(TrTank[i].Time);
if TrTank[i].time = 3 then
begin
X := 2 + Random(_NX - 5);
Y := 2 + Random(_NY - 5);
TrTank[i].SetTank(X,Y,i,0,_Right,_Right,True);
end;
end;
End;
Procedure MoveTarget;
var i: integer;
Begin
for i := 1 to _NTank do
if TrTank[i].Enable then
begin
case TrTank[i].Dir of
_Up:
if TrTank[i].CanMove(_Up) then
Begin
TrTank[i].LDir := TrTank[i].Dir;
TrTank[i].Dir := _Up;
TrTank[i].DrawTank(TrTank[i].X,TrTank[i].Y-1);
End;
_Down:
if TrTank[i].CanMove(_Down) then
Begin
TrTank[i].LDir := TrTank[i].Dir;
TrTank[i].Dir := _Down;
TrTank[i].DrawTank(TrTank[i].X,TrTank[i].Y+1);
End;
_Left:
if TrTank[i].CanMove(_Left) then
Begin
TrTank[i].LDir := TrTank[i].Dir;
TrTank[i].Dir := _Left;
TrTank[i].DrawTank(TrTank[i].X-1,TrTank[i].Y);
End;
_Right:
if TrTank[i].CanMove(_Right) then
Begin
TrTank[i].LDir := TrTank[i].Dir;
TrTank[i].Dir := _Right;
TrTank[i].DrawTank(TrTank[i].X+1,TrTank[i].Y);
End;
end;
TrTank[i].TargetAI ;
end;
End;
Procedure MoveBullet;
var i: integer;
begin
for i := 1 to _Player do
begin
Bullet[i].Move;
end;
end;
Procedure MoveTank;
var k: char;
t: integer;
Begin
CreateTarget;
MoveTarget;
DrawScreen;
t := 0;
repeat
k := #0;
if Keypressed then begin k := readkey;end;
case k of
_Up:
if Ptank.CanMove(_Up) then
begin
PTank.LDir := PTank.Dir;
PTank.Dir := k;
PTank.DrawTank(PTank.X,PTank.Y - 1);
Sound(500);
end;
_Down:
if Ptank.CanMove(_Down) then
begin
PTank.LDir := PTank.Dir;
PTank.Dir := k;
PTank.DrawTank(PTank.X,PTank.Y + 1);
Sound(500);
end;
_Left:
if Ptank.CanMove(_Left) then
begin
PTank.LDir := PTank.Dir;
PTank.Dir := k;
PTank.DrawTank(PTank.X-1,PTank.Y);
Sound(500);
end;
_Right:
if Ptank.CanMove(_Right) then
begin
PTank.LDir := PTank.Dir;
PTank.Dir := k;
PTank.DrawTank(PTank.X+1,PTank.Y);
Sound(500);
end;
_Space: Bullet[_Player].SetBullet(PTank.X,PTank.Y,_Player,PTank.Dir,True);
end;
inc(t);
if t mod 500 = 0 then
MoveBullet;
if t mod 1000 = 0 then
begin
if t mod 5000 = 0 then
begin
MoveTarget;
end;
DrawScreen;
NoSound;
end;
if t mod 15000 = 0 then
begin
ChangeTarget;
t := 0;
end;
until (k = #27) or (Lives <= 0);
Gotoxy(_NX div 2 - 7,_NY div 2);
TextBackGround(_Bgr);
TextColor(Red);Write('G A M E O V E R');
if (Lives <= 0) then readln;
End;
Procedure StartGame;
var i: integer;
Begin
SetCusor(32,0);
TextColor(Cyan);
for i := 1 to 20 do
begin
Gotoxy(23+Random(8)*5,6+Random(5));Write(#15);
end;
TextColor(Brown);
Gotoxy(30,5);Write('T A N K');
TextColor(Green);
Gotoxy(35,7);Write('B A T T L E');
TextColor(Blue);
Gotoxy(28,9);Write('Create by: TRA NGUYEN HUU NGHIA');
for i := 17 to 69 do
begin
TextColor(Red);
Gotoxy(i,11);Write('Ä');
Gotoxy(i,15);Write('Ä');
Gotoxy(i,3); Write('Ä');
end;
for i := 4 to 14 do
begin
Gotoxy(17,i);Write('³');
Gotoxy(69,i);Write('³');
end;
Gotoxy(17,3);Write( 'Ú');
Gotoxy(17,15);Write('À');
Gotoxy(69,3);Write( '¿');
Gotoxy(69,15);Write('Ù');
Gotoxy(17,11);Write('Ã');
Gotoxy(69,11);Write('´');
for i := 20 to 63 do
begin
Gotoxy(i+1,12);Write(' ');
Gotoxy(i+1,13);Write(' ');
TextColor(5);
Gotoxy(i,12);Write('Ü');
TextColor(Cyan);
Gotoxy(i+2,12);Write('ßÛÜ');
Gotoxy(i+2,13);Write('ßß');
TextColor(Brown);
Gotoxy(38,14);Write('Loading...',(i - 20)*100 div 43,'%');
Delay(200);
end;
TextColor(Blue+Blink);
Gotoxy(30,16);Write('Press Any Key To Start');
repeat until keypressed;
clrscr;
End;
BEGIN
TextBackGround(Black);
clrscr;
StartGame;
TextMode(Co40);
SetCusor(32,0);
Randomize;
Score := 0;
Lives := 5;
CreateScreen;
PTank.SetTank(12,12,_Player,0,_Up,_Up,True);
PTank.DrawTank(PTank.X,PTank.Y);
MoveTank;
END.
Còn đây là hình Demo
Sử dụng các phím mũi tên để di chuyển và phím cách để bắn. Game này giống game Tank trên máy điện tử hồi xưa