Game PingPong Pascal

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,619
744
21
Phú Yên
Trường THPT Lương Văn Chánh
[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
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.

Hey.....Thi xong rồi nên mình viết vài dòng Code Game chia sẻ mấy bạn cùng chơi....Mình dùng thuật toán Random nên nó đi không như ý muốn lắm.....;)
Mã:
 Program Ping_Pong;
 uses crt;
 const
      trenduoi = 0;
      trai = 1;
      phai = 2;
      dodai = 9;

 var x,y,bx,by,dx,dy,mx,my,diem,mdiem : integer;
     k: Char;
     T, Time: integer;

 Procedure In_Vot(a,b: integer);
 var i:integer;
 begin
      for i := 0 to dodai do
      begin
           TextColor(Brown);
           gotoxy(a+i,b);
           write(#219);
      end;
 end;

 Procedure Clear(vt,x,y: integer);
 var i: integer;
 begin
      case vt of
      trenduoi:
      for i := 0 to 9 do
      begin
           gotoxy(x+i,y);
           write(#32);
      end;
      trai: begin gotoxy(x,y);write(#32);end;
      phai: begin gotoxy(x+dodai,y);write(#32);end;
      end;
 end;

 Procedure Tao_Ban;
 var i:integer;
 begin
      for i := 15 to 65 do
      begin
            TextColor(White);
            gotoxy(i,13);write(#240);
            TextColor(Green);
            gotoxy(i,1); write(#196);
            gotoxy(i,25);write(#196);
      end;
      for i := 2 to 24 do
      begin
            TextColor(Green);
            gotoxy(14,i);write(#179);
            gotoxy(66,i);write(#179);
      end;
 end;

 Function Gioi_Han(x,y,y1,y2,dodai : integer): Boolean;
 begin
      if (x <= 15) or (x >= 65-dodai)
      or (y >= y2) or (y <= y1) then Gioi_Han := False
      else Gioi_Han := True;
 end;

 Procedure Di_Chuyen(k: Char);
 begin
      case k of
      #75: begin Clear(Phai,x,y); x := x-1;if not Gioi_Han(x,y,20,24,dodai) then x :=x+1; end;
      #77: begin Clear(Trai,x,y); x := x+1;if not Gioi_Han(x,y,20,24,dodai) then x :=x-1; end;
      #72: begin Clear(Trenduoi,x,y); y := y-1;if not Gioi_Han(x,y,20,24,dodai) then y :=y+1; end;
      #80: begin Clear(Trenduoi,x,y); y := y+1;if not Gioi_Han(x,y,20,24,dodai) then y :=y-1; end;
      end;
      In_Vot(x,y);
 end;

 Procedure Bong_Bay;
 begin
    if T = Time then
    begin
      TextColor(White);
      gotoxy(bx,by);
      if by <> 13 then
      write(#32)
      else write(#240);
      bx := bx + dx;
      by := by + dy;
      if not Gioi_Han(bx,by,2,24,0) then
      begin
            TextColor(Red);
            gotoxy(bx,by);
            write('o');
            gotoxy(68,7);write('Press Enter!');
            repeat
            until readkey = #13;
            gotoxy(bx,by);write(#32);
            gotoxy(68,7);write('            ');

            if by < 13 then begin bx := x + dodai div 2;by := y-1;
            diem := diem + 1; end
            else if by > 13 then
            begin bx := mx + dodai div 2;by := my+1;mdiem := mdiem + 1; end
            else
            begin
                 if dy < 0 then
                 begin bx := mx + dodai div 2;by := my+1;mdiem := mdiem + 1; end
                 else begin bx := x + dodai div 2;by := y-1; diem := diem + 1; end
            end;
            TextColor(Blue);
            gotoxy(70,5);write(mdiem,' : ',diem);
      end;
      TextColor(Red);
      gotoxy(bx,by);
      write('o');
      T := 0;
    end;
 end;

 Procedure AI;
 begin
    if T mod (Time div 2 - 1) = 0 then
    begin
      if (mx > bx - dodai div 2) then
      begin
           Clear(Phai,mx,my); mx := mx-1;if not Gioi_Han(mx,my,2,12,dodai) then mx :=mx+1;
      end
      else
      if (mx < bx - dodai div 2) then
      begin
           Clear(Trai,mx,my); mx := mx+1;if not Gioi_Han(mx,my,2,12,dodai) then mx :=mx-1;
      end;
      In_Vot(mx,my);
    end;
 end;

 Function Va_Cham: Integer;
 begin
      Va_Cham := 0;
      if ((bx >= x) and (bx <= x + dodai) and (by = y-1)) then Va_Cham := 1;
      if ((bx >= mx) and (bx <= mx + dodai) and (by = my+1)) then Va_Cham := 2;
 end;

 Procedure Chuyen_Huong;
 var dau: integer;
 begin
       dau := random(2);
       dx := Random(2);
       repeat
       dy := Random(2);
       until dy <> 0;
       if dau = 0 then dx := -dx;
       if Va_Cham = 1 then dy := -dy
       else if Va_Cham = 2 then dy := dy;
 end;

 Function EndGame: Boolean;
 begin
      EndGame := False;
      if (k = #27) or (diem = 10) or (mdiem = 10) then EndGame := True;
 end;

 Procedure OutGame;
 begin
      if k <> #27 then
      begin
      TextColor(Red);
      gotoxy(35,13);
      if diem = 10 then
      Write('YOU WIN!')
      else Write('GAME OVER!');
      readln;
      end;
 end;

 Procedure Chon_Che_Do;
 var cd: integer;
 begin
      clrscr;
      cd := 1;
      TextColor(Black);
      Gotoxy(25,8);Write('CHE DO:');
      TextColor(Red);
      Gotoxy(25,10);Write('1, De');
      Gotoxy(25,11);Write('2, Thuong');
      Gotoxy(25,12);Write('3, Kho');
      TextColor(Yellow);
      repeat
      Gotoxy(25,14);
      clreol;
      Write('Chon Che Do: ');
      Readln(cd);
      until (cd >= 1) and (cd <= 3);
      case cd of
      1: Time := 150;
      2: Time := 100;
      3: Time := 50;
      end;
      clrscr;
 end;

 Procedure Khoi_Tao;
 begin
      Randomize;
      x := 40;
      y := 23;
      bx := 45;
      by := 22;
      dx := 0;
      dy := -1;
      mx := 30;
      my := 3;
      diem := 0;
      mdiem := 0;
      TextColor(Blue);
      gotoxy(70,5);write(mdiem,' : ',diem);
      T := 0;
      Tao_Ban;
      In_Vot(30,3);
      In_Vot(40,23);
 end;

 Procedure Main;
 begin
      TextBackGround(Cyan);
      clrscr;
      Chon_Che_Do;
      Khoi_Tao;
      repeat
      k := #1;
      if Keypressed then k := Readkey;
      Di_Chuyen(k);
      Bong_Bay;
      AI;
      T := T + 1;
      if Va_Cham <> 0 then Chuyen_Huong;
      if T = Time then
      sound(Random(10)*10+250);
      until EndGame;
      OutGame;
      NoSound;
 end;

 BEGIN
      Main;
 END.
 
Last edited:

ASDFGHJKQWERGHJKSDNMSXCGVHBZS

Học sinh mới
Thành viên
15 Tháng chín 2020
3
1
6
15
Thừa Thiên Huế
trung học cơ sở Hà thế hạnh
Program Ping_Pong;
uses crt;
const
trenduoi = 0;
trai = 1;
phai = 2;
dodai = 9;

var x,y,bx,by,dx,dy,mx,my,diem,mdiem : integer;
k: Char;
T, Time: integer;

Procedure In_Vot(a,b: integer);
var i:integer;
begin
for i := 0 to dodai do
begin
TextColor(Brown);
gotoxy(a+i,b);
write(#219);
end;
end;
Procedure StartGame;
var i: integer;
Begin
TextColor(Cyan);
for i := 1 to 20 do
begin
Gotoxy(23+Random(8)*5,6+Random(5));Write(#15);
end;
TextColor(Brown);
Gotoxy(35,5);Write('P I N G');
TextColor(Green);
Gotoxy(35,7);Write('P O N G');
TextColor(Blue);
Gotoxy(28,9);Write('Create by: NGUYEN NGOC AN');
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;




Procedure Tao_Ban;
var i:integer;
begin
for i := 15 to 65 do
begin
TextColor(White);
gotoxy(i,13);write(#240);
TextColor(Green);
gotoxy(i,1); write(#196);
gotoxy(i,25);write(#196);
end;
for i := 2 to 24 do
begin
TextColor(Green);
gotoxy(14,i);write(#179);
gotoxy(66,i);write(#179);
end;
end;

Function Gioi_Han(x,y,y1,y2,dodai : integer): Boolean;
begin
if (x <= 15) or (x >= 65-dodai)
or (y >= y2) or (y <= y1) then Gioi_Han := False
else Gioi_Han := True;
end;

Procedure Di_Chuyen(k: Char);
begin
case k of
#75: begin Clear(Phai,x,y); x := x-1;if not Gioi_Han(x,y,20,24,dodai) then x :=x+1; end;
#77: begin Clear(Trai,x,y); x := x+1;if not Gioi_Han(x,y,20,24,dodai) then x :=x-1; end;
#72: begin Clear(Trenduoi,x,y); y := y-1;if not Gioi_Han(x,y,20,24,dodai) then y :=y+1; end;
#80: begin Clear(Trenduoi,x,y); y := y+1;if not Gioi_Han(x,y,20,24,dodai) then y :=y-1; end;
end;
In_Vot(x,y);
end;

Procedure Bong_Bay;
begin
if T = Time then
begin
TextColor(White);
gotoxy(bx,by);
if by <> 13 then
write(#32)
else write(#240);
bx := bx + dx;
by := by + dy;
if not Gioi_Han(bx,by,2,24,0) then
begin
TextColor(Red);
gotoxy(bx,by);
write('o');
gotoxy(68,7);write('Press Enter!');
repeat
until readkey = #13;
gotoxy(bx,by);write(#32);
gotoxy(68,7);write(' ');

if by < 13 then begin bx := x + dodai div 2;by := y-1;
diem := diem + 1; end
else if by > 13 then
begin bx := mx + dodai div 2;by := my+1;mdiem := mdiem + 1; end
else
begin
if dy < 0 then
begin bx := mx + dodai div 2;by := my+1;mdiem := mdiem + 1; end
else begin bx := x + dodai div 2;by := y-1; diem := diem + 1; end
end;
TextColor(Blue);
gotoxy(70,5);write(mdiem,' : ',diem);
end;
TextColor(Red);
gotoxy(bx,by);
write('o');
T := 0;
end;
end;

Procedure AI;
begin
if T mod (Time div 2 - 1) = 0 then
begin
if (mx > bx - dodai div 2) then
begin
Clear(Phai,mx,my); mx := mx-1;if not Gioi_Han(mx,my,2,12,dodai) then mx :=mx+1;
end
else
if (mx < bx - dodai div 2) then
begin
Clear(Trai,mx,my); mx := mx+1;if not Gioi_Han(mx,my,2,12,dodai) then mx :=mx-1;
end;
In_Vot(mx,my);
end;
end;

Function Va_Cham: Integer;
begin
Va_Cham := 0;
if ((bx >= x) and (bx <= x + dodai) and (by = y-1)) then Va_Cham := 1;
if ((bx >= mx) and (bx <= mx + dodai) and (by = my+1)) then Va_Cham := 2;
end;

Procedure Chuyen_Huong;
var dau: integer;
begin
dau := random(2);
dx := Random(2);
repeat
dy := Random(2);
until dy <> 0;
if dau = 0 then dx := -dx;
if Va_Cham = 1 then dy := -dy
else if Va_Cham = 2 then dy := dy;
end;

Function EndGame: Boolean;
begin
EndGame := False;
if (k = #27) or (diem = 10) or (mdiem = 10) then EndGame := True;
end;

Procedure OutGame;
begin
if k <> #27 then
begin
TextColor(Red);
gotoxy(35,13);
if diem = 10 then
Write('YOU WIN!')
else Write('GAME OVER!');
readln;
end;
end;

Procedure Chon_Che_Do;
var cd: integer;
begin
clrscr;
cd := 1;
TextColor(Black);
Gotoxy(25,8);Write('CHE DO:');
TextColor(Red);
Gotoxy(25,10);Write('1, De');
Gotoxy(25,11);Write('2, Thuong');
Gotoxy(25,12);Write('3, Kho');
TextColor(Yellow);
repeat
Gotoxy(25,14);
clreol;
Write('Chon Che Do: ');
Readln(cd);
until (cd >= 1) and (cd <= 3);
case cd of
1: Time := 150;
2: Time := 100;
3: Time := 50;
end;
clrscr;
end;

Procedure Khoi_Tao;
begin
Randomize;
x := 40;
y := 23;
bx := 45;
by := 22;
dx := 0;
dy := -1;
mx := 30;
my := 3;
diem := 0;
mdiem := 0;
TextColor(Blue);
gotoxy(70,5);write(mdiem,' : ',diem);
T := 0;
Tao_Ban;
In_Vot(30,3);
In_Vot(40,23);
end;

Procedure Main;
begin
TextBackGround(Cyan);
clrscr;
StartGame;
Chon_Che_Do;
Khoi_Tao;
repeat
k := #1;
if Keypressed then k := Readkey;
Di_Chuyen(k);
Bong_Bay;
AI;
T := T + 1;
if Va_Cham <> 0 then Chuyen_Huong;
if T = Time then
sound(Random(10)*10+250);
until EndGame;
OutGame;
NoSound;
end;

BEGIN
Main;
END.
 
Top Bottom