computing
  • 0

Pascal KeyPressed And ReadKey

  • 0

Hey, I’ve been messing with pascal lately and I’m stuck at one point, thought that you guys could help me.

So the thing, that I want to do is, while a person is pressing a desired key the circle would move, when he stops doing that, it will stop. I don’t know why, but I can’t get it right, could you guys look at my little project ant tell me what’s wrong? Thanks.

program programa;
uses graph, crt;
label pradžia;
var
x, y, z: integer;
key: char;

procedure grafika;
var gt, gb: integer;
begin
gt := Detect;
InitGraph (gt, gb, ‘C:\TP\BGI\’)
end;

begin
grafika;
x := 980;
y := 500;
z := 30;
pradžia:
SetColor (yellow);
Circle (x, y, z);
SetFillStyle (SolidFill, yellow);
FloodFill (x, y, yellow);
key := ReadKey;
if (key = #97) then
begin
inc(x);
GoTo pradžia;
end;

readln; readln;

Share

1 Answer

  1. You made two horrible mistakes in your code.

    First NEVER code labels in Pascal since the language was developed to enable structured programming theory also known as “GOTOless Programming”.

    Second your circle’s center is out of graphic window since the origin is at the left upper corner (0,0) and y is downward.

    Here the working code ;

    Program Programa;
    
    uses Graph, CRT;
    
    var
      x, y, radius: integer;
      key: char;
      gt, gb: integer;
    
    Begin
    
    {Initialize Graphic Mode}
    
    gt:= Detect;
    InitGraph (gt, gb, 'C:\TP\BGI');
    if GraphResult <> grOk then begin
      WriteLn;
      WriteLn ('  Error initializing Graphic Mode');
      WriteLn;
      Halt (1)
    end;
    
    x:= 100;
    y:= 200;
    radius:= 30;
    
    {Draw and move the circle pressing the <a> key}
    
    repeat
      ClearViewPort;
      SetColor (yellow);
    
      Circle (x, y, radius);
    
      SetFillStyle (SolidFill, yellow);
      FloodFill (x, y, yellow);
    
      inc(x);
      key:= ReadKey
    until key <> 'a';
    
    CloseGraph {Return to TEXT Mode}
    End.
    

    • 0