The ball (asterisk) wont move =/

{*** Made by Code4fun ***}
Program DamnBall;

Uses crt;

const
  ANP = 79;
  ALP = 24;
  MIN=1;
  ALT=3;
  ANC=4;
  Time=100;

Var
  PosX,PosY,Dx,Dy:integer;

Procedure DP(pX,pY:integer); {this draws the ball (asterisk)}
BEGIN
  Gotoxy (pX,pY); write('*');
  Delay (kk);
  Gotoxy (pX,pY); write(' ');
END;

Procedure VP(pX,pY,dirx,diry:integer); {this makes the ball move}
BEGIN
  px:=px+dirx;
  py:=py+diry;
  DP(pX,pY);
END;



Begin {From Here is the code that makes the ball bounce with the walls/top/floor
       and call the procedures to draw and make it move}
  ClrScr;
  cursoroff;
  PosX := MIN;
  PosY := MIN;
  Dx:=1;
  Dy:=1;

    Begin
      IF (PosX <> ANP) AND (PosY <> ALP) AND (PosX <> MIN) AND (PosY <> MIN) THEN
          VP(PosX,PosY,Dx,Dy)
      ELSE IF (PosX = ANP) AND (PosY <> ALP) AND (PosY <> MIN) THEN
        BEGIN
          Dx:= Dx*(-1);
          VP(PosX,PosY,Dx,Dy);
        END
      ELSE IF (PosX <> ANP) AND (PosY = ALP) AND (PosX <> MIN) THEN
        BEGIN
          Dy:= Dy*(-1);
          VP(PosX,PosY,Dx,Dy);
        END
      ELSE IF (PosX = ANP) AND (PosY = ALP) THEN
        BEGIN
          Dx:= Dx*(-1);
          Dy:= Dy*(-1);
          VP(PosX,PosY,Dx,Dy);
        END
      ELSE IF (PosX = MIN) AND (PosY <> ALP) AND (PosY <> MIN) THEN
        BEGIN
          Dx:= Dx*(-1);
          VP(PosX,PosY,Dx,Dy);

        END
      ELSE IF (PosX <> MIN) AND (PosY = MIN) AND (PosX <> MIN) THEN
        BEGIN
          Dy:= Dy*(-1);
          VP(PosX,PosY,Dx,Dy);

        END
      ELSE IF (PosX = MIN) AND (PosY = MIN) THEN
        BEGIN
          Dy:= Dy*(-1);
          Dx:= Dx*(-1);
          VP(PosX,PosY,Dx,Dy);
        END;

    END
END.

Thats why debugging was invented.

Step through your code, see why its not doing what you thought..

First thing that comes to mind is

Delay (kk);

kk doesnt seem to be defined, so I truely doubt that your code compiles.

oh lol i forgot to add that here but it is in the real program and still doesn't move KK its a constant... I was going to chage it for TIME but i forgot... bcus KK says nothing =/. I only post when it compiled and i dont know what is wrong x.x

It compiled... there was no bug there just the program didn't do what it should x.x, anyway i fixed it now somehow O.o . Thanks for trying =P

Thats why you DEBUG it, that doesnt mean the thing didnt compile, it means you step through the code looking at the values as it goes.. Its a very simple and easy way to find out why something like that doesnt do as you expected.

I've had a similar problem in Turbo Pascal that I haven't been able to solve either.

In my case, what it seems to come down to is a bug in the TP procedure GotoXY, where it doesn't actually move the cursor where the coordinates say to move it. I have no idea why - both values are well within limits.

If I were you, I'd try making a slight modification to one of your procedures, as follows:

Instead of

{*** Made by Code4fun ***}
Procedure DP(pX,pY:integer); {this draws the ball (asterisk)}
BEGIN
  Gotoxy (pX,pY); write('*');
  Delay (kk);
  Gotoxy (pX,pY); write(' ');
END;

I'd try:

{*** Made by Code4fun; modified by Beerslayer ***}
Procedure DP(pX,pY:integer); {this draws the ball (asterisk)}
Var CurX, CurY: integer;
BEGIN
  CurX := WhereX; CurY := WhereY; {get current XY values before call to GotoXY}
{set breakpoint and inspect values of CurX and CurY here}
  Gotoxy (pX,pY); 
  CurX := WhereX; CurY := WhereY; {and then get them again after call to GotoXY}
{inspect CurX and CurY again here - should have changed to match pX,pY}
  write('*');
  Delay (kk);
  Gotoxy (pX,pY); write(' ');
  CurX := WhereX; CurY := WhereY;
END;

Then, in your IDE, I'd set a breakpoint at the first assignment after BEGIN, and set watches on CurX and CurY. Once it hits the breakpoint, step through the code from that point. After the first assignments but before the first GotoXY, the values of CurX and CurY should be wherever the cursor was last on the screen. After the first GotoXY (but before the write), those values should have changed to match pX and pY.

In my case, those values are not changing as a result of calling the GotoXY function. I'm not sure why, but I cannot find anything I'm doing wrong. If anyone knows what I might be doing wrong, or knows of any workaround to what appears to be a bug, please let me know.

i got it working in other way... got my arkanoid finished =P... Making the blocks and the make them disapear was more toruble than this one though x.x

This article has been dead for over six months. Start a new discussion instead.