Jump to content

Self moving "X"

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Masonic

Masonic

    Newbie

  • Members
  • PipPip
  • 10 posts
Hi, i need to make program in pascal that moves
"X" across the window and when reaches one of
the sides - changes direction (like that one of
windows screensavers). and also - when u press key (up, down, left, right) "X" starts moving to pressed direction and further again across window till again key presses. How can i do that?
please help!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Do you have the X displaying in the window yet? How are you drawing it? Do you know how to handle keyboard input? What code do you have so far?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Masonic

Masonic

    Newbie

  • Members
  • PipPip
  • 10 posts

Quote

program x_game;
uses crt;
var
x, y, curx, cury :byte;
ch :char;
a :integer;
begin
clrscr;
x:=80 div 2;
y:=25 div 2;
a:=0;
repeat
gotoxy(x,y);
write('X');
curx:=wherex;
cury:=wherey;
gotoxy(curx-1,cury-1);
write(' ');
gotoxy(curx+1,cury-1);
write (' ');
gotoxy(curx+1,cury+1);
write(' ');
gotoxy(curx-1,cury+1);
write (' ');

yes i have x .i have this far, but i don't know what to write further that'll make it move.
basically i know how to input something - i have made this: uz4.pas , there is input.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Is this in Pascal or Delphi? Your code looks like Pascal.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Masonic

Masonic

    Newbie

  • Members
  • PipPip
  • 10 posts
yes, it's pascal

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
My guess is you'll need to do one of two things:
1) build a keyboard handler to grab keyboard interrupts directly
2) Have a loop that checks for keyboard input and only move the X in response to a keystroke.

Note: your options will depend HEAVILY on what compiler you are using.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Masonic

Masonic

    Newbie

  • Members
  • PipPip
  • 10 posts
yeah, i know what i need, but i don't know how to write that... :(

#8
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I forget how to do this exactly, but I think your function is called KeyPressed.

So, to find out what the left, right, up, down keys are, run this:

 
var
 c:Char;
begin
 writeln('Press enter to exit');
 repeat
  c:=KeyPressed;
  writeln('Your key is: #',ord(c));
 until c=#13;
end.

Unfortunately, I don't have TP anymore.

In any case, for the rest of the program, consider:

 
uses crt;
 
var oldx,oldy:Integer;
 
procedure SetX(x,y:Integer);
begin
 GotoXY(oldx,oldy);
 write(' '); {erase old one}
 GotoXY(x,y); {maybe you'll want x-1,y-1.. because I believe the coord sys is 1 based..., it's up to you tho}
 Write('X'); {write the X at new pos}
 {store pos for erasing later}
 oldx:=x;
 oldy:=y; 
end;
 
const
 ku='W';
 kd='Z';
 kl='A';
 kr='S';
 kesc=#27; {this is the ESC key}
 
var c:char;
     x,y:integer;
begin
 oldx=1;
 oldy=1;  
 x:=40;
 y:=12;
 clrscr;
 
 repeat
  c:=KeyPressed; {this will NOT wait for input. Also, it uses keyboard deleay and repeat speeds ("command.com /c mode con:rate=32 delay=1" to set max speed) }
  case c of
   kl: begin dec(x); if x<1 then x:=1; end; {stops at edge}
   kr: begin inc(x); if x>80 then x:80; end;
   ku: begin dec(y); if y<1 then y:=1; end;
   kd: begin inc(y); if y>25 then y:=25; end;
  end;
  SetX(x,y);
 until c=kesc;
end.

This is off the top of my head and uses ASWZ for controlling the dir. It will not bounce or move by itself. To make it bounce and such, you have to combine it with a current x and y direction and remember that one. When it hits top or bottom edge, negate y, if hits left or right, negate x.
Init dirx and diry to 1 (or -1)

 
x:=x+dirx; if (x<1) or (x>80) then begin dirx:=-dirx; x:=x+dirx*2; end; {this will do a better bounce... when the "if" is true, x is}
                                                                 { 0 or 81, it was 1 or 80, so now it should be 2 or 79. Think about it.}

Do the same for y (80 would be 25, all x's are y's)

You could store the last pushed key dir in the appropriate dirx or diry. Be advised that of you overwrite the dirx AND diry with your key dir, it can't move diagonally, unless you add keys for that.

TP doesn't have functions that allow you to read which key is currently pressed, and that becomes especially hard when you try to get key combinations.

Good luck and such.