Tin học Game Xếp hình (Tetris) viết bằng 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,623
744
22
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.

Code thì rối khỏi nói luôn rồi. Thôi thì ae nào thích cứ copy về chơi vậy. Đây là một game quen thuộc nên cách chơi cũng khỏi cần phải hướng dẫn luôn nha.
Mã:
 Program Tetris;
 uses crt,dos;
 const SoHang = 32;
       SoCot = 22;
       Khoi = #219#219;
       Trong = '. ';
       Len = #72;
       Xuong = #80;
       Trai = #75;
       Phai = #77;
       Pause = 'p';
       TocDo = 200;
       ChieuDai = 3;
       MauTrong = White;

 var k: char;
     Time,i,Diem : integer;
     X,Y,MauXY,MauTiep: array[1..ChieuDai] of integer;
     MaTran: array[1..SoHang div 2,1..SoCot] of string[2];
     Mau: array[1..SoHang div 2,1..SoCot] of integer;

 procedure ConTro(duoi,tren: byte);
 var regs : registers;
 begin
 regs.ah :=1;
 regs.ch:=duoi;
 regs.cl:=tren;
 intr($10,regs);
 end;

 Procedure In_Khoi(x,y: integer; hinh: string;maus: integer);
 Begin
      TextColor(maus);
      Gotoxy(x,y);
      Write(hinh);
      MaTran[X div 2,Y] := hinh;
      Mau[X div 2,Y] := maus;
 End;

 Procedure Khoi_Tao;
 var k,i,j: integer;
 Begin
      for k := 1 to ChieuDai do
      begin
      X[k] := SoHang div 2;
      Y[k] := ChieuDai + 1 - k;
      MauXY[k] := 1 + Random(10);
      MauTiep[k] := 1 + Random(10);
      Gotoxy(70,5 + k);
      TextColor(MauTiep[k]);Write(Khoi);
      end;
      Time := 0;
      Diem := 0;
      ConTro(32,0);
      for i := 1 to SoHang div 2 do
          for j := 1 to SoCot do
          begin
               In_Khoi(i*2,j,Trong,MauTrong);
               Mau[i,j] := MauTrong;
          end;
      Gotoxy(63,6);Write('NEXT: ');
      Gotoxy(63,10);Write('SCORE: ',Diem);
 End;

 Procedure Tao_Khoi_Moi;
 var i: integer;
 Begin
      for i := 1 to ChieuDai do
      begin
      In_Khoi(X[i],Y[i],MaTran[X[i] div 2,Y[i]],Mau[X[i] div 2,Y[i]]);
      X[i] := SoHang div 2;
      Y[i] := ChieuDai  + 1 - i;
      MauXY[i] := MauTiep[i];
      MauTiep[i] := 1 + Random(10);
      Gotoxy(70,5 + i);
      TextColor(MauTiep[i]);Write(Khoi);
      In_Khoi(X[i],Y[i],Khoi,MauXY[i]);
      end;
 End;

 Procedure Di_Chuyen(k: char);
 var i,j,tam: integer;
 Begin
      case k of
      Len:
      begin
      tam := MauXY[ChieuDai];
      for i := ChieuDai downto 2 do
      MauXY[i] := MauXY[i-1];
      MauXY[1] := tam;
      end;
      Xuong: begin i := Y[1];
             while (i < SoCot) and (MaTran[X[1] div 2,i + 1] = Trong) do
             inc(i);
             for j := 1 to ChieuDai do begin
             In_Khoi(X[j],Y[j],Trong,MauTrong);
             Y[j] := i-j;
             end;
             Time := TocDo-1;
             end;
      Trai: if (X[1] > 2) and (MaTran[X[1] div 2 - 1,Y[1]] = Trong)
      then for j := 1 to ChieuDai do begin In_Khoi(X[j],y[j],Trong,MauTrong);
      X[j] := X[j] - 2;In_Khoi(X[j],Y[j],Khoi,MauXY[j]); end;
      Phai: if (X[1] < SoHang) and (MaTran[X[1] div 2 + 1,Y[1]] = Trong)
      then for j := 1 to ChieuDai do begin In_Khoi(X[j],Y[j],Trong,MauTrong);
      X[j] := X[j] + 2; In_Khoi(X[j],Y[j],Khoi,MauXY[j]);end;
      end;
 End;

 Procedure Ve_Lai;
 var i,j,k: integer;
 Begin
      for i := 1 to SoHang div 2 do
          for j := SoCot downto 2 do
          begin
               if MaTran[i,j] = Khoi then
               begin
                    In_Khoi(i*2,j,Khoi,Mau[i,j]);
               end
               else
               begin
                    for k := j downto 2 do
                    In_Khoi(i * 2,k,MaTran[i,k-1],Mau[i,k - 1]);
                    break;
               end;
          end;
 End;

 Procedure Cung_Mau;
 var i,j,count,dau,cuoi,vX,vY: integer;
 Max: array[1..ChieuDai] of integer;
     cung: array[1..ChieuDai] of Boolean;
 Begin

      for i := 1 to ChieuDai do
      begin
      cung[i] := False;
      Max[i] := 0;
      vX := X[i];
      vY := Y[i];
      begin
           count := 0;
           if vX div 2 > 2 then
           dau := vX div 2 - 2
           else dau := 1;
           if vX div 2 < SoHang div 2 - 2 then
           cuoi := vX div 2 + 2
           else cuoi := SoHang div 2;
           for j := vX div 2 downto dau do
           if Mau[j,vY] = Mau[vX div 2,vY] then inc(count)
           else begin dau := j + 1;break;end;
           for j := vX div 2 + 1 to cuoi do
           if Mau[j,vY] = Mau[vX div 2,vY] then inc(count)
           else begin cuoi := j - 1;break;end;
           if count >= 3 then
           begin
           for j := dau to cuoi do
           if j <> vX div 2 then In_Khoi(j * 2,vY,Trong,MauTrong);
           cung[i] := True;
           diem := diem + count;
           end;
      end;
      if count > max[i] then
      Max[i] := Count;

      begin
           count := 0;
           dau := vY - 2;
           cuoi := vY + 2;
           for j := vY downto vY - 2 do
           if Mau[vX div 2,j] = Mau[vX div 2,vY] then inc(count)
           else begin dau := j + 1;break;end;
           for j := vY + 1 to vY + 2 do
           if Mau[vX div 2,j] = Mau[vX div 2,vY] then inc(count)
           else begin cuoi := j - 1;break;end;
           if count >= 3 then
           begin
           for j := dau to cuoi do if j<> vY then In_Khoi(vX,j,Trong,MauTrong);
           Cung[i] := True;
           diem := diem + count;
           end;
      end;
      if Count > Max[i] then Max[i] := Count;

      begin
           if (vX div 2 > 2) and (vY > 2) then
           dau := -2
           else if (vX div 2 < vY) then dau := -vX div 2
           else dau := -vY;
           if (vX div 2 < SoHang div 2 - 2) and (vY < SoCot - 2) then
           cuoi := 2
           else if (vX div 2 > vY) then cuoi := (SoHang - vX) div 2
           else cuoi := SoCot - vY;
           count := 0;
           for j := 0 downto dau do
           begin
                if Mau[vX div 2 + j,vY + j] = Mau[vX div 2,vY] then inc(count)
                else begin dau := j + 1;break;end;
           end;
           for j := 1 to cuoi do
           begin
                if Mau[vX div 2 + j,vY + j] = Mau[vX div 2,vY] then inc(count)
                else begin cuoi := j - 1;break;end;
           end;
           if count >= 3 then
           begin
           for j := dau to cuoi do
           if j <> 0 then In_Khoi(vX + j * 2,vY + j,Trong,MauTrong);
           Cung[i] := True;
           diem := diem + count;
           end;
      end;
      if Count > Max[i] then Max[i] := Count;

      begin
           if (vX div 2 > 2) and (vY < SoCot - 2) then
           dau := -2
           else if (vX div 2 < SoCot - vY) then dau := -vX div 2
           else dau := -SoCot + vY;
           if (vX div 2 < SoHang div 2 - 2) and (vY > 2) then
           cuoi := 2
           else if (vX div 2 > SoCot - vY) then cuoi := (SoHang - vX) div 2
           else cuoi := SoCot - vY;
           count := 0;
           for j := 0 downto dau do
           begin
                if Mau[vX div 2 + j,vY - j] = Mau[vX div 2,vY] then inc(count)
                else begin dau := j + 1;break;end;
           end;
           for j := 1 to cuoi do
           begin
                if Mau[vX div 2 + j,vY - j] = Mau[vX div 2,vY] then inc(count)
                else begin cuoi := j - 1;break;end;
           end;
           if count >= 3 then
           begin
           for j := dau to cuoi do
           if j <> 0 then In_Khoi(vX + j * 2,vY - j,Trong,MauTrong);
           Cung[i] := True;
           diem := diem + count;
           end;
      end;
      if Count > Max[i] then Max[i] := Count;
      end;

      for i := 1 to ChieuDai do
      if cung[i] then
      begin
      In_Khoi(X[i],Y[i],Trong,MauTrong);
      for j := 1 to Max[i] do
      Ve_Lai;
      end;

 End;

 Procedure Roi;
 var j: integer;
 Begin
      if Time = TocDo then
      begin
      if (MaTran[X[1] div 2,Y[1] + 1] = Trong) and (Y[1] < SoCot) then
      begin
      In_Khoi(X[3],Y[3],Trong,MauTrong);
      for j := 1 to ChieuDai do
      begin
           Y[j] := Y[j] + 1;
           In_Khoi(X[j],Y[j],Khoi,MauXY[j]);
      end;
      end else
      begin
      Cung_Mau;
      Tao_Khoi_Moi;
      end;
      Time := 0;
      end;
      if k <> Pause then
      Inc(Time)
      else for j := 1 to ChieuDai do In_Khoi(X[j],Y[j],Khoi,MauXY[j]);
 End;

 Function EndGame: Boolean;
 var i,j,count: integer;
 Begin
      EndGame := False;
      for i := 1 to SoHang div 2 do
      begin
      count := 0;
      for j := 2 to 5 do
      if (MaTran[i,j] = Khoi) then
      inc(count);
      if count = 4 then begin EndGame := True;
      TextColor(Red);
      Gotoxy(12,12);Write('GAME OVER');
      break;end;
      end;
 End;

 BEGIN
      clrscr;
      Khoi_Tao;
      repeat
      if Keypressed then
      begin
           k := readkey;
           Di_Chuyen(k);
      end;
      Roi;
      TextColor(MauTrong);
      Gotoxy(63,10);Write('SCORE: ',Diem);
      Until (k = #27) or EndGame;
      readln;
 END.
 

Fairy Piece

Học sinh
Thành viên
22 Tháng ba 2018
162
88
46
22
Tiền Giang
THPT Chuyên Tiền Giang
anh ơi khi em nhấn mũi tên xuống thì nó bị lỗi exitcode 201 là sao ạ
 

ka1412

Học sinh chăm học
Thành viên
24 Tháng mười một 2017
874
730
121
Hà Nội
CNN | Life
Code thì rối khỏi nói luôn rồi. Thôi thì ae nào thích cứ copy về chơi vậy. Đây là một game quen thuộc nên cách chơi cũng khỏi cần phải hướng dẫn luôn nha.
Mã:
 Program Tetris;
 uses crt,dos;
 const SoHang = 32;
       SoCot = 22;
       Khoi = #219#219;
       Trong = '. ';
       Len = #72;
       Xuong = #80;
       Trai = #75;
       Phai = #77;
       Pause = 'p';
       TocDo = 200;
       ChieuDai = 3;
       MauTrong = White;

 var k: char;
     Time,i,Diem : integer;
     X,Y,MauXY,MauTiep: array[1..ChieuDai] of integer;
     MaTran: array[1..SoHang div 2,1..SoCot] of string[2];
     Mau: array[1..SoHang div 2,1..SoCot] of integer;

 procedure ConTro(duoi,tren: byte);
 var regs : registers;
 begin
 regs.ah :=1;
 regs.ch:=duoi;
 regs.cl:=tren;
 intr($10,regs);
 end;

 Procedure In_Khoi(x,y: integer; hinh: string;maus: integer);
 Begin
      TextColor(maus);
      Gotoxy(x,y);
      Write(hinh);
      MaTran[X div 2,Y] := hinh;
      Mau[X div 2,Y] := maus;
 End;

 Procedure Khoi_Tao;
 var k,i,j: integer;
 Begin
      for k := 1 to ChieuDai do
      begin
      X[k] := SoHang div 2;
      Y[k] := ChieuDai + 1 - k;
      MauXY[k] := 1 + Random(10);
      MauTiep[k] := 1 + Random(10);
      Gotoxy(70,5 + k);
      TextColor(MauTiep[k]);Write(Khoi);
      end;
      Time := 0;
      Diem := 0;
      ConTro(32,0);
      for i := 1 to SoHang div 2 do
          for j := 1 to SoCot do
          begin
               In_Khoi(i*2,j,Trong,MauTrong);
               Mau[i,j] := MauTrong;
          end;
      Gotoxy(63,6);Write('NEXT: ');
      Gotoxy(63,10);Write('SCORE: ',Diem);
 End;

 Procedure Tao_Khoi_Moi;
 var i: integer;
 Begin
      for i := 1 to ChieuDai do
      begin
      In_Khoi(X[i],Y[i],MaTran[X[i] div 2,Y[i]],Mau[X[i] div 2,Y[i]]);
      X[i] := SoHang div 2;
      Y[i] := ChieuDai  + 1 - i;
      MauXY[i] := MauTiep[i];
      MauTiep[i] := 1 + Random(10);
      Gotoxy(70,5 + i);
      TextColor(MauTiep[i]);Write(Khoi);
      In_Khoi(X[i],Y[i],Khoi,MauXY[i]);
      end;
 End;

 Procedure Di_Chuyen(k: char);
 var i,j,tam: integer;
 Begin
      case k of
      Len:
      begin
      tam := MauXY[ChieuDai];
      for i := ChieuDai downto 2 do
      MauXY[i] := MauXY[i-1];
      MauXY[1] := tam;
      end;
      Xuong: begin i := Y[1];
             while (i < SoCot) and (MaTran[X[1] div 2,i + 1] = Trong) do
             inc(i);
             for j := 1 to ChieuDai do begin
             In_Khoi(X[j],Y[j],Trong,MauTrong);
             Y[j] := i-j;
             end;
             Time := TocDo-1;
             end;
      Trai: if (X[1] > 2) and (MaTran[X[1] div 2 - 1,Y[1]] = Trong)
      then for j := 1 to ChieuDai do begin In_Khoi(X[j],y[j],Trong,MauTrong);
      X[j] := X[j] - 2;In_Khoi(X[j],Y[j],Khoi,MauXY[j]); end;
      Phai: if (X[1] < SoHang) and (MaTran[X[1] div 2 + 1,Y[1]] = Trong)
      then for j := 1 to ChieuDai do begin In_Khoi(X[j],Y[j],Trong,MauTrong);
      X[j] := X[j] + 2; In_Khoi(X[j],Y[j],Khoi,MauXY[j]);end;
      end;
 End;

 Procedure Ve_Lai;
 var i,j,k: integer;
 Begin
      for i := 1 to SoHang div 2 do
          for j := SoCot downto 2 do
          begin
               if MaTran[i,j] = Khoi then
               begin
                    In_Khoi(i*2,j,Khoi,Mau[i,j]);
               end
               else
               begin
                    for k := j downto 2 do
                    In_Khoi(i * 2,k,MaTran[i,k-1],Mau[i,k - 1]);
                    break;
               end;
          end;
 End;

 Procedure Cung_Mau;
 var i,j,count,dau,cuoi,vX,vY: integer;
 Max: array[1..ChieuDai] of integer;
     cung: array[1..ChieuDai] of Boolean;
 Begin

      for i := 1 to ChieuDai do
      begin
      cung[i] := False;
      Max[i] := 0;
      vX := X[i];
      vY := Y[i];
      begin
           count := 0;
           if vX div 2 > 2 then
           dau := vX div 2 - 2
           else dau := 1;
           if vX div 2 < SoHang div 2 - 2 then
           cuoi := vX div 2 + 2
           else cuoi := SoHang div 2;
           for j := vX div 2 downto dau do
           if Mau[j,vY] = Mau[vX div 2,vY] then inc(count)
           else begin dau := j + 1;break;end;
           for j := vX div 2 + 1 to cuoi do
           if Mau[j,vY] = Mau[vX div 2,vY] then inc(count)
           else begin cuoi := j - 1;break;end;
           if count >= 3 then
           begin
           for j := dau to cuoi do
           if j <> vX div 2 then In_Khoi(j * 2,vY,Trong,MauTrong);
           cung[i] := True;
           diem := diem + count;
           end;
      end;
      if count > max[i] then
      Max[i] := Count;

      begin
           count := 0;
           dau := vY - 2;
           cuoi := vY + 2;
           for j := vY downto vY - 2 do
           if Mau[vX div 2,j] = Mau[vX div 2,vY] then inc(count)
           else begin dau := j + 1;break;end;
           for j := vY + 1 to vY + 2 do
           if Mau[vX div 2,j] = Mau[vX div 2,vY] then inc(count)
           else begin cuoi := j - 1;break;end;
           if count >= 3 then
           begin
           for j := dau to cuoi do if j<> vY then In_Khoi(vX,j,Trong,MauTrong);
           Cung[i] := True;
           diem := diem + count;
           end;
      end;
      if Count > Max[i] then Max[i] := Count;

      begin
           if (vX div 2 > 2) and (vY > 2) then
           dau := -2
           else if (vX div 2 < vY) then dau := -vX div 2
           else dau := -vY;
           if (vX div 2 < SoHang div 2 - 2) and (vY < SoCot - 2) then
           cuoi := 2
           else if (vX div 2 > vY) then cuoi := (SoHang - vX) div 2
           else cuoi := SoCot - vY;
           count := 0;
           for j := 0 downto dau do
           begin
                if Mau[vX div 2 + j,vY + j] = Mau[vX div 2,vY] then inc(count)
                else begin dau := j + 1;break;end;
           end;
           for j := 1 to cuoi do
           begin
                if Mau[vX div 2 + j,vY + j] = Mau[vX div 2,vY] then inc(count)
                else begin cuoi := j - 1;break;end;
           end;
           if count >= 3 then
           begin
           for j := dau to cuoi do
           if j <> 0 then In_Khoi(vX + j * 2,vY + j,Trong,MauTrong);
           Cung[i] := True;
           diem := diem + count;
           end;
      end;
      if Count > Max[i] then Max[i] := Count;

      begin
           if (vX div 2 > 2) and (vY < SoCot - 2) then
           dau := -2
           else if (vX div 2 < SoCot - vY) then dau := -vX div 2
           else dau := -SoCot + vY;
           if (vX div 2 < SoHang div 2 - 2) and (vY > 2) then
           cuoi := 2
           else if (vX div 2 > SoCot - vY) then cuoi := (SoHang - vX) div 2
           else cuoi := SoCot - vY;
           count := 0;
           for j := 0 downto dau do
           begin
                if Mau[vX div 2 + j,vY - j] = Mau[vX div 2,vY] then inc(count)
                else begin dau := j + 1;break;end;
           end;
           for j := 1 to cuoi do
           begin
                if Mau[vX div 2 + j,vY - j] = Mau[vX div 2,vY] then inc(count)
                else begin cuoi := j - 1;break;end;
           end;
           if count >= 3 then
           begin
           for j := dau to cuoi do
           if j <> 0 then In_Khoi(vX + j * 2,vY - j,Trong,MauTrong);
           Cung[i] := True;
           diem := diem + count;
           end;
      end;
      if Count > Max[i] then Max[i] := Count;
      end;

      for i := 1 to ChieuDai do
      if cung[i] then
      begin
      In_Khoi(X[i],Y[i],Trong,MauTrong);
      for j := 1 to Max[i] do
      Ve_Lai;
      end;

 End;

 Procedure Roi;
 var j: integer;
 Begin
      if Time = TocDo then
      begin
      if (MaTran[X[1] div 2,Y[1] + 1] = Trong) and (Y[1] < SoCot) then
      begin
      In_Khoi(X[3],Y[3],Trong,MauTrong);
      for j := 1 to ChieuDai do
      begin
           Y[j] := Y[j] + 1;
           In_Khoi(X[j],Y[j],Khoi,MauXY[j]);
      end;
      end else
      begin
      Cung_Mau;
      Tao_Khoi_Moi;
      end;
      Time := 0;
      end;
      if k <> Pause then
      Inc(Time)
      else for j := 1 to ChieuDai do In_Khoi(X[j],Y[j],Khoi,MauXY[j]);
 End;

 Function EndGame: Boolean;
 var i,j,count: integer;
 Begin
      EndGame := False;
      for i := 1 to SoHang div 2 do
      begin
      count := 0;
      for j := 2 to 5 do
      if (MaTran[i,j] = Khoi) then
      inc(count);
      if count = 4 then begin EndGame := True;
      TextColor(Red);
      Gotoxy(12,12);Write('GAME OVER');
      break;end;
      end;
 End;

 BEGIN
      clrscr;
      Khoi_Tao;
      repeat
      if Keypressed then
      begin
           k := readkey;
           Di_Chuyen(k);
      end;
      Roi;
      TextColor(MauTrong);
      Gotoxy(63,10);Write('SCORE: ',Diem);
      Until (k = #27) or EndGame;
      readln;
 END.
Hay phết. Nhưng màu nhiều lúc nhìn ảo ảo kiểu gì ấy @trà nguyễn hữu nghĩa
 

Ocmaxcute

Học sinh chăm học
Thành viên
13 Tháng chín 2017
801
884
146
20
Nghệ An
Làm sao để cop vào pas vậy ạ, ngồi gõ từng chữ chắc chết mất
 

ka1412

Học sinh chăm học
Thành viên
24 Tháng mười một 2017
874
730
121
Hà Nội
CNN | Life
Bạn cop vào notepad rồi lưu đổi đuôi sang .pas rồi mở lên là chơi được thôi @Ocmaxcute
 
Top Bottom