program pong;
uses crt;

var size,vv,g,score1,score2,zz,dir,time,p1,p2,bx,by,skill:integer;
    x:char;
    startdir,invis,center:boolean;

procedure clear_variables;
begin

if center = true then begin
p1 := 36;
p2 := 36;
end;
bx := 40;
by := 9;
if startdir = true then begin
randomize;
dir := random(4);
dir := dir + 1;
end
else dir := 1;
time := 2500;
zz := 0;

end;


procedure border;
begin
textcolor(3);
gotoxy (15,3);
write (#201);
for g := 0 to 48 do write (#205);
write (#187);

gotoxy (15,24);
write (#200);
for g := 0 to 48 do write (#205);
write (#188);

for g := 4 to 23 do begin
gotoxy (15,g);
write (#186);
end;

for g := 4 to 23 do begin
gotoxy (65,g);
write (#186);
end;

gotoxy (36,3);
write (#202);
gotoxy (45,3);
write (#202);
gotoxy (36,2);
write (#210);
textcolor (11);
write ('  PONG  ');
textcolor (3);
write (#210);

textcolor (red);
gotoxy (23,2);
write ('You : 0');
gotoxy (50,2);
write ('Computer : 0');

gotoxy (30,25);
textcolor (9);
write ('Press ''O'' for options');
end;


procedure ready;
begin
gotoxy (36,13);
textcolor(red);
write ('Get ready!');
gotoxy (80,25);
delay (450);
gotoxy (36,13);
write ('  Go!!!   ');
gotoxy (80,25);
delay (300);
gotoxy (36,13);
write ('          ');
end;


procedure options;
begin
repeat
x := ' ';
clrscr;
gotoxy (35,2);
textcolor(11);
write ('OPTIONS');
textcolor (blue);
gotoxy (5,4);
write ('[D]ifficulty Setting');
gotoxy (5,6);
write ('[R]andomize Ball Direction at Startup');
gotoxy (5,8);
write ('[C]enter Paddles at Startup');
gotoxy (5,10);
write ('[I]nvisible Ball');
gotoxy (5,14);
textcolor(green);
write ('[B]ack to the Game!!');
gotoxy (10,18);
textcolor (red);
write ('Please type in a letter : ');
x := readkey;

x := upcase(x);
case x of
'D' : begin

repeat
clrscr;
gotoxy (13,10);
textcolor(blue);
write ('        What skill level do you want to play at?'); gotoxy (13,11);
write ('       Press 1 for easy, 2 for medium, 3 for hard');
gotoxy (80,25);
x := readkey;

case x of
'1' : skill := 1;
'2' : skill := 2;
'3' : skill := 3;
end;
until (x = '1') or (x = '2') or (x = '3');

end;
'R' : begin
repeat
clrscr;
textcolor(blue);
gotoxy (1,10);
writeln ('     Should the direction the ball is going be randomized at the begnning');
writeln ('     of the game?');
writeln ('     Press ''y'' for yes, press ''n'' for no');
gotoxy (80,25);
x := readkey;
x := upcase(x);
case x of
'Y' : startdir := true;
'N' : startdir := false;
end;
until (x = 'Y') or (x = 'N');
end;

'C' : begin
repeat
clrscr;
textcolor (blue);
gotoxy (1,10);
writeln ('     Should the paddles be centered at the start of each game?');
writeln ('     Press ''y'' for yes, press ''n'' for no');
gotoxy (80,25);
x := readkey;

x := upcase(x);
case x of
'Y' : center := true;
'N' : center := false;
end;
until (x = 'Y') or (x = 'N');
end;

'I' : begin
repeat
clrscr;
gotoxy (5,10);
textcolor(blue);
write ('Do you want ''invisible ball'' to be turned on?');
gotoxy (5,11);
write ('Try it, it''s kind of fun');
gotoxy (5,12);
write ('(Press 1 for on, press 2 for off)');
gotoxy (80,25);
x := readkey;

case x of
'1' : invis := true;
'2' : invis := false;
end;
until (x = '1') or (x = '2');

end;

'B' : begin
clrscr;
clear_variables;
score1 := 0;
score2 := 0;
end
else
options;
end;
until (x = 'B');

border;
ready;
end;


procedure body;
begin

repeat

repeat
textcolor (green);
gotoxy (p2,6);
for g := 1 to 8 do write (#223);

textcolor (blue);
gotoxy (p1,22);
for g := 1 to 8 do write (#223);

textcolor (red);
gotoxy (bx,by);
if invis = false then write (#9)
else
write (' ');

gotoxy (80,25);
{delay (round(time / 50));  }

zz := zz + 1;

if zz = 300 then begin
{if time > 1000 then time := time - 1;}
gotoxy (bx,by);
write (' ');
gotoxy (p2,6);
write ('        ');


case dir of
1 : begin
bx := bx + 1;
by := by - 1;
if bx >= 64 then begin
dir := 2;
bx := 64;
end;
if by = 6 then begin
if (bx >= p2) and (bx <= p2 + 7) then dir := 3;
end;
end;
2 : begin
bx := bx - 1;
by := by - 1;
if bx <= 16 then begin
dir := 1;
bx := 16;
end;
if by = 6 then begin
if (bx >= p2) and (bx <= p2 + 7) then dir := 4;
end;
end;
3 : begin
bx := bx + 1;
by := by + 1;
if bx >= 64 then begin
dir := 4;
bx := 64;
end;
if by = 22 then begin
if (bx > p1) and (bx <= p1 + 3) then dir := 2;
if (bx >= p1 + 4) and (bx < p1 + 7) then dir := 1;
if bx = p1 then dir := 5;
if bx = p1 + 7 then dir := 6;
end;
end;
4 : begin
bx := bx - 1;
by := by + 1;
if bx <= 16 then begin
bx := 16;
dir := 3;
end;
if by = 22 then begin
if (bx > p1) and (bx <= p1 + 3) then dir := 2;
if (bx >= p1 + 4) and (bx < p1 + 7) then dir := 1;
if bx = p1 then dir := 5;
if bx = p1 + 7 then dir := 6;
end;
end;
5 : begin
bx := bx - 2;
by := by - 1;
if bx <= 16 then begin
dir := 6;
bx := 16;
end;
if by = 6 then begin
if (bx >= p2) and (bx <= p2 + 7) then dir := 7;
end;
end;
6 : begin
bx := bx + 2;
by := by - 1;
if bx >= 64 then begin
dir := 5;
bx := 64;
end;
if by = 6 then begin
if (bx >= p2) and (bx <= p2 + 7) then dir := 8;
end;
end;
7 : begin
bx := bx - 2;
by := by + 1;
if bx <= 16 then begin
dir := 8;
bx := 16;
end;
if by = 22 then begin
if (bx > p1) and (bx <= p1 + 3) then dir := 2;
if (bx >= p1 + 4) and (bx < p1 + 7) then dir := 1;
if bx = p1 then dir := 5;
if bx = p1 + 7 then dir := 6;
end;
end;
8 : begin
bx := bx + 2;
by := by + 1;
if bx >= 64 then begin
dir := 7;
bx := 64;
end;
if by = 22 then begin
if (bx > p1) and (bx <= p1 + 3) then dir := 2;
if (bx >= p1 + 4) and (bx < p1 + 7) then dir := 1;
if bx = p1 then dir := 5;
if bx = p1 + 7 then dir := 6;
end;
end;
9 : begin
bx := bx + 3;
by := by + 1;
if bx >= 64 then begin
dir := 10;
bx := 64;
end;
if by = 22 then begin
if (bx > p1) and (bx <= p1 + 3) then dir := 2;
if (bx >= p1 + 4) and (bx < p1 + 7) then dir := 1;
if bx = p1 then dir := 5;
if bx = p1 + 7 then dir := 6;
end;
end;
10 : begin
bx := bx - 3;
by := by + 1;
if bx <= 16 then begin
dir := 9;
bx := 16;
end;
if by = 22 then begin
if (bx > p1) and (bx <= p1 + 3) then dir := 2;
if (bx >= p1 + 4) and (bx < p1 + 7) then dir := 1;
if bx = p1 then dir := 5;
if bx = p1 + 7 then dir := 6;
end;
end;
11 : begin
bx := bx + 3;
by := by - 1;
if bx >= 64 then begin
dir := 12;
bx := 64;
end;
if by = 6 then begin
if (bx >= p2) and (bx <= p2 + 7) then dir := 9;
end;
end;
12 : begin
bx := bx - 3;
by := by - 1;
if bx <= 16 then begin
dir := 11;
bx := 16;
end;
if by = 6 then begin
if (bx >= p2) and (bx <= p2 + 7) then dir := 10;
end;
end;
end;
for g := 1 to skill do begin
if bx > p2 + 4 then p2 := p2 + 1;
if bx < p2 + 4 then p2 := p2 - 1;
end;
if p2 < 16 then p2 := 16;
if p2 > 57 then p2 := 57;

if (by = 23) and ((bx >= p1) and (bx <= p1 + 3)) then begin
by := 22;
dir := 12;
end;

if (by = 23) and ((bx >= p1 + 4) and (bx <= p1 + 7)) then begin
by := 22;
dir := 11;
end;

zz := 0;

if by = 5 then begin
textcolor(red);
gotoxy (29,2);
score1 := score1 + 1;
write (score1);
gotoxy (21,13);
write ('You scored! Press any key to continue');
gotoxy (80,25);
x := readkey;
gotoxy (21,13);
write ('                                     ');
gotoxy (p1,22);
write ('        ');
clear_variables;
end;

if by = 23 then begin
textcolor(red);
gotoxy (61,2);
score2 := score2 + 1;
write (score2);
gotoxy (18,13);
write ('Computer scored!! Press any key to continue');
gotoxy (80,25);
x := readkey;
gotoxy (18,13);
write ('                                           ');
gotoxy (p1,22);
write ('        ');
clear_variables;
end;

end;


until keypressed;
x := readkey;

gotoxy (p1,22);
write ('        ');

case x of
#77 : p1 := p1 + 1;
#75 : p1 := p1 - 1;
end;

if p1 > 57 then p1 := 57;
if p1 < 16 then p1 := 16;

if (x = 'o') or (x = 'O') then options;

until (x = 'q') or (x = 'Q') or (x = #27);
end;


begin
clrscr;
center := true;
startdir := true;
clear_variables;
score1 := 0;
score2 := 0;
skill := 2;
invis := false;
clrscr;
border;
ready;
body;
clrscr;
textcolor (7);
end.