Program emuluje hru ping pong

Delphi & Pascal (česká wiki)
Přejít na: navigace, hledání
Kategória: Programy v Pascale

Program: Pingpong.pas
Súbor exe: Pingpong.exe
Súbor ubuntu: Pingpong

Program emuluje hru ping pong.
{ PINGPONG.PAS              Copyright (c) TrSek alias Zdeno Sekerak }
{ Program emuluje hru ping pong.                                    }
{                                                                   }
{ Datum:23.2.2011                              http://www.trsek.com }
 
program pingpong;
uses crt, dos;
 
var
  zotrvacnost1: integer;
  zotrvacnost2: integer;
  loptickax, loptickay: integer;
  stroj: boolean;
  kroku: longint;
  score1, score2: integer;
  klav: char;
  rychlost: integer;
 
procedure uvod;
begin
  clrscr;
  writeln('Program Ping-pong');
  writeln('-----------------');
  writeln(' n - nova hra');
  writeln(' o - options');
  writeln(' p - pause');
  writeln('ESC - koniec');
  writeln;
end;
 
 
procedure zaver;
begin
   clrscr;
   writeln('Konec');
   writeln('Nahral jsi ', kroku);
end;
 
 
procedure options;
var klav: char;
begin
  repeat
   clrscr;
   writeln('Options');
   writeln('--------');
   writeln('a. Hrac 1 human (sipky nahoru, dolu)');
   write  ('b. Hrac 2 ');
   if(stroj)then writeln('automat')
            else writeln('human (w-nahoru, s-dolu)');
   writeln('c. zniz rychlost na ', rychlost-1);
   writeln('d. zvys rychlost na ', rychlost-1);
   writeln('----------------');
   writeln('Stiskni a,b,c,d,esc pro zmenu');
 
    klav:=readkey;
    if(klav='b')then stroj:=not(stroj);
    if(klav='B')then stroj:=not(stroj);
    if(klav='c')then rychlost:=rychlost-1;
    if(klav='C')then rychlost:=rychlost-1;
    if(klav='d')then rychlost:=rychlost+1;
    if(klav='D')then rychlost:=rychlost+1;
  until(klav=#27);
end;
 
 
procedure zobraz_palku(hrac:integer; y:integer);
begin
  if not((y<1)or(y>20))then begin
 
      if(hrac=1)then begin
         gotoxy( 2,y  ); writeln(' ');
         gotoxy( 2,y+1); writeln('[');
         gotoxy( 2,y+2); writeln('|');
         gotoxy( 2,y+3); writeln('[');
         gotoxy( 2,y+4); writeln(' ');
      end else begin
         gotoxy(79,y  ); writeln(' ');
         gotoxy(79,y+1); writeln('[');
         gotoxy(79,y+2); writeln('|');
         gotoxy(79,y+3); writeln('[');
         gotoxy(79,y+4); writeln(' ');
      end;
 
  end;
end;
 
 
procedure pohyb_lopticky(var x:integer; var y:integer);
begin
   gotoxy(1,1); 
   write('Ping pong od Hudecky 2011,');
   write('                             ');
   write('[kroku=',kroku:3);
   write(', x=',x:2,' y=',y:2,']');
   writeln;
 
   gotoxy(1,25);
   write('Hrac1 =', score1:3);
   write('                                                          ');
   write('Hrac2 =', score2:3);
 
   gotoxy(x,y);
   writeln(' ');
 
   x:=x+loptickax;
   y:=y+loptickay;   
 
   { odraz od horni, dolni hrany }
   if((y<=2) or (y>=23))then begin
      loptickay:=(-1)*loptickay;
   end;
 
   { odraz od hrace1 }
   if(x<=2)then begin
      loptickax:=(-1)*loptickax;
      x:=3;
   end;
 
   { odraz od hrace2 }
   if(x>=79)then begin
      loptickax:=(-1)*loptickax;
      x:=78;
   end;
 
   gotoxy(x,y);
   writeln('O');
end;
 
 
function pohyb_palku(y:integer; smer:integer; var zotrvacnost:integer): integer;
begin
  y:=y+smer;
  if(abs(zotrvacnost)>3)then
     y:=y+2*smer;
  if(abs(zotrvacnost)>6)then
     y:=y+4*smer;
 
  { zmenil smer }
  if ((zotrvacnost>0) and (smer<0)) then
      zotrvacnost:=0;
  if ((zotrvacnost<0) and (smer>0)) then
      zotrvacnost:=0;
 
  if ((zotrvacnost>0) and (smer>0)) then
       zotrvacnost:=zotrvacnost+1;
  if ((zotrvacnost<0) and (smer<0)) then
       zotrvacnost:=zotrvacnost-1;
 
  {}
  pohyb_palku:=y;
end;
 
 
procedure hra;
var stuj: boolean;
    y1, y2: integer;
    x, y: integer;
    klav: char;
    restart: boolean;
    kroky: integer;
    i: integer;
	old_rychlost: integer;
 
begin
   stuj:=false;
   score1:=0;
   score2:=0;
   restart:=true;
   old_rychlost:=rychlost;
   randomize;
 
   repeat
     if restart then begin
       x:=40;
       y:=10;
       y1:=10;
       y2:=10;
       loptickax:=random(2)+1;
       loptickay:=random(2)+1;
       restart:=false;
       clrscr;
       zobraz_palku(1,y1);
       zobraz_palku(2,y2);
       pohyb_lopticky(x,y);
     end;
 
     klav:=#0;
     for i:=1 to rychlost do begin
        if keypressed then
		begin
    	   klav:=readkey;
           if(klav=#0)then
          	  klav:=readkey;
        end;
 
        delay(1);
     end;
 
     { co stisknul }
     case(klav) of
       'P': { jede nahoru hrac 1 }
            begin
            y1 := pohyb_palku(y1, 1, zotrvacnost1);
            if stroj then
               y2 := pohyb_palku(y2, 1, zotrvacnost2);
            end;
       'H': { jede dolu hrac 1 }
            begin
            y1 := pohyb_palku(y1, -1, zotrvacnost1);
            if stroj then
               y2 := pohyb_palku(y2, -1, zotrvacnost2);
            end;
       's': { jede nahoru hrac2 nebo stroj }
            begin
            if not(stroj)then
               y2 := pohyb_palku(y2, 1, zotrvacnost2);
            end;
       'w': { jede dolu hrac2 nebo stroj }
            begin
            if not(stroj)then
               y2 := pohyb_palku(y2, -1, zotrvacnost2);
            end;
       'p': begin
	         if stuj then stuj:=false
                     else stuj:=true;
            end;
     end;
 
    zobraz_palku(1,y1);
    zobraz_palku(2,y2);
    pohyb_lopticky(x,y);
 
    { nekomu to spadlo }
    if(( (y<=y1) or (y>=(y1+4))) and (x=3))then begin
        score2:=score2+1;
        if(score2>=11)then begin
           clrscr;
           writeln('[ --- Hrac cislo 1 prohral --- ]');
           repeat until keypressed;
        end;
        restart:=true;
    end;
 
    if(( (y<=y2) or (y>=(y2+4))) and (x=78))then begin
        score1:=score1+1;
        if(score1>=11)then begin
           clrscr;
           writeln('[ --- Hrac cislo 2 prohral --- ]');
           repeat until keypressed;
        end;
        restart:=true;
    end;
   gotoxy(1,1);
 
   { kroky }
   kroku:=kroku+1;
   if(kroku=300)then rychlost:=rychlost div 2;
   if(kroku=600)then rychlost:=rychlost div 2;
 
   until klav=#27;
   rychlost:=old_rychlost;
end;
 
 
begin
   stroj:=true;
   rychlost:=100;
 
   repeat
     uvod;
 
     klav:=readkey;
     case klav of
       'o': options;
       'n': hra;
     end;
   until (klav=#27); 
 
   zaver;
end.