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!
Self moving "X"
Started by Masonic, Apr 17 2010 04:38 AM
7 replies to this topic
#1
Posted 17 April 2010 - 04:38 AM
|
|
|
#2
Posted 17 April 2010 - 05:51 AM
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?
#3
Posted 17 April 2010 - 05:57 AM
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 (' ');
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
Posted 17 April 2010 - 06:28 AM
Is this in Pascal or Delphi? Your code looks like Pascal.
#5
Posted 17 April 2010 - 06:31 AM
yes, it's pascal
#6
Posted 17 April 2010 - 06:38 AM
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.
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.
#7
Posted 17 April 2010 - 09:36 AM
yeah, i know what i need, but i don't know how to write that... :(
#8
Posted 19 April 2010 - 01:09 AM
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:
Unfortunately, I don't have TP anymore.
In any case, for the rest of the program, consider:
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)
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.
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.


Sign In
Create Account


Back to top









