Articles

Pascal KeyPressed and ReadKey

November 25, 2010 at 07:59:00
Specs: Windows 7

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;


See More: Pascal KeyPressed and ReadKey

Report •


#1
November 26, 2010 at 08:31:09

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.


Report •

#2
November 26, 2010 at 08:43:03

Beware the code I posted may fail under Windows 7 since you seem to use Turbo Pascal 7 for DOS that enables as high resolution VGA (640x480) forcing a full screen mode under NT systems.

Win 7 and Vista AFAIK doesn't allow DOS full screen so I had to run this code in a DOS environment inside MS Virtual PC. It runs fine at full screen under Windows XP SP3 instead..


Report •

#3
November 26, 2010 at 11:06:56

Thanks a lot! Though the graph didn't work because windows 7, I'm going to continue doing without it and I'll take your advice I'll stop using label in pascal. THANKS

Report •

Related Solutions

#4
November 26, 2010 at 12:15:08

I took you advice and I was trying to get fluid movement of a square this time without graph. But movement is just not right it gets often mixed up and I can't get the trails of the square to go away with clrscr;

program project;
uses CRT;
var
key: char;
x1, y1, x2, y2: integer;


procedure coordinates;
begin
x1 := 10;
y1 := 10;
x2 := 11;
y2 := 11;
end;

procedure move_up;
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
inc(y1); inc(y2);
key := ReadKey;
if (key = 's') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(y1); dec(y2);
key := ReadKey;
until key <> 's';
end else
if (key = 'd') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
inc(x1); inc(x2);
key := ReadKey;
until key <> 'd';
end else
if (key = 'a') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(x1); dec(x2);
key := ReadKey;
until key <> 'a';
end;
until key <> 'w';
end;

procedure move_down;
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(y1); dec(y2);
key := ReadKey;
if (key = 'w') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(y1); dec(y2);
key := ReadKey;
until key <> 'w';
end else
if (key = 'd') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
inc(x1); inc(x2);
key := ReadKey;
until key <> 'd';
end else
if (key = 'a') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(x1); dec(x2);
key := ReadKey;
until key <> 'a';
end;
until key <> 's';
end;

procedure move_right;
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
inc(x1); inc(x2);
key := ReadKey;
if (key = 'w') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(y1); dec(y2);
key := ReadKey;
until key <> 'w';
end else
if (key = 's') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
inc(x1); inc(x2);
key := ReadKey;
until key <> 's';
end else
if (key = 'a') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(x1); dec(x2);
key := ReadKey;
until key <> 'a';
end;
until key <> 'd';
end;

procedure move_left;
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(x1); dec(x2);
key := ReadKey;
if (key = 'w') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(y1); dec(y2);
key := ReadKey;
until key <> 'w';
end else
if (key = 's') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
inc(x1); inc(x2);
key := ReadKey;
until key <> 's';
end else
if (key = 'd') then
begin
repeat
Window (x1, y1, x2, y2); TextBackground(yellow); ClrScr;
dec(x1); dec(x2);
key := ReadKey;
until key <> 'd';
end;
until key <> 'a';
end;


begin
coordinates;
key := ReadKey;
if (key = 'w') then
begin
move_up;
end else
if (key = 's') then
begin
move_down;
end else
if (key = 'd') then
begin
move_right;
end else
if (key = 'a') then
begin
move_left;
end;

ReadLn; ReadLn;
end.


Report •

#5
November 26, 2010 at 12:47:38

Well, I'll take a look at your code and maybe I can post the fixed version, but...

If you need to operate Turbo Pascal 7 for DOS under Win 7 for your assignments I suggest you spend some time to set up a virtual machine and install a DOS environment. Programming is widely an experimental experience and theory doesn't suffice.

MS Virtual PC 2007 is free from Microsoft (install under Home Premium too, just ignore the warning) and to get a DOS system you can install FreeDOS, the Open Source DOS clone, absolutely free. Turbo Pascal 7 runs perfectly in that environment and you can share files between Win 7 and DOS easily. The setup is quite easy, more simple to do than to explain.


Report •


Ask Question