Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

moving a letter around the screen

fredlegmanfredlegman Posts: 3Member
i've been trying to expand on my simple letter scrolling programand add the ability to control the movement of the letter. Unfortunately the nly thing that seems to come up is a small cross of letters and then nothing more. I am really getting confused again. Thanks.

[code]
program mrpac;

uses crt;

var
position : integer;
pac : string;
timer : integer ;
t : integer;
blank : string;
c : char;
posx: integer;
posy: integer;

begin
for posx := 1 to 40 do
begin
pac := ('c');
gotoxy(posx,0);
writeln(pac:posx);
delay(50);
gotoxy(posx,1);

write(blank:posx);
end;

delay(100);

writeln('helo welcome to pacman':0);
delay(300);
clrscr;
write('hit esc to quit');

while c <> #27 do


begin
c := readkey;
posx := 20;
posy := 20;
case c of
#75 : posx := posx-1;
#72 : posy := posy + 1;
#77 : posx := posx+1;
#80 : posy := posy-1;
end;

gotoxy(posx,posy);
write(pac);
if (posx > 40) or (posx < 1) then break;
if (posy > 40) or (posy < 1) then break;
end
end.
[/code]
Matt

Comments

  • zibadianzibadian Posts: 6,349Member
    : i've been trying to expand on my simple letter scrolling programand add the ability to control the movement of the letter. Unfortunately the nly thing that seems to come up is a small cross of letters and then nothing more. I am really getting confused again. Thanks.
    :
    : [code]
    : program mrpac;
    :
    : uses crt;
    :
    : var
    : position : integer;
    : pac : string;
    : timer : integer ;
    : t : integer;
    : blank : string;
    : c : char;
    : posx: integer;
    : posy: integer;
    :
    : begin
    : for posx := 1 to 40 do
    : begin
    : pac := ('c');
    : gotoxy(posx,0);
    : writeln(pac:posx);
    : delay(50);
    : gotoxy(posx,1);
    :
    : write(blank:posx);
    : end;
    :
    : delay(100);
    :
    : writeln('helo welcome to pacman':0);
    : delay(300);
    : clrscr;
    : write('hit esc to quit');
    :
    : while c <> #27 do
    :
    :
    : begin
    : c := readkey;
    : posx := 20;
    : posy := 20;
    : case c of
    : #75 : posx := posx-1;
    : #72 : posy := posy + 1;
    : #77 : posx := posx+1;
    : #80 : posy := posy-1;
    : end;
    :
    : gotoxy(posx,posy);
    : write(pac);
    : if (posx > 40) or (posx < 1) then break;
    : if (posy > 40) or (posy < 1) then break;
    : end
    : end.
    : [/code]
    : Matt
    :
    :
    Just before you change the position, you need to write a space to delete the previous position.
  • Phat NatPhat Nat Posts: 757Member
    : i've been trying to expand on my simple letter scrolling programand add the ability to control the movement of the letter. Unfortunately the nly thing that seems to come up is a small cross of letters and then nothing more. I am really getting confused again. Thanks.
    :
    [code]
    : program mrpac;
    :
    : uses crt;
    :
    : var
    : position : integer;
    : pac : string;
    : timer : integer ;
    : t : integer;
    : blank : string;
    : c : char;
    : posx: integer;
    : posy: integer;
    :
    : begin
    : for posx := 1 to 40 do
    : begin
    : pac := ('c');
    : gotoxy(posx,0);
    : writeln(pac:posx);
    : delay(50);
    : gotoxy(posx,1);
    :
    : write(blank:posx);
    : end;
    :
    : delay(100);
    :
    : writeln('helo welcome to pacman':0);
    : delay(300);
    : clrscr;
    : write('hit esc to quit');
    :
    : while c <> #27 do
    :
    :
    : begin
    : c := readkey;
    : posx := 20;
    : posy := 20;
    : case c of
    : #75 : posx := posx-1;
    : #72 : posy := posy + 1;
    : #77 : posx := posx+1;
    : #80 : posy := posy-1;
    : end;
    :
    : gotoxy(posx,posy);
    : write(pac);
    : if (posx > 40) or (posx < 1) then break;
    : if (posy > 40) or (posy < 1) then break;
    : end
    : end.
    [/code]
    : Matt

    Hey there. A few problems here. First off, your program is only displaying a cross because of the [green]green[/green] lines here:
    [code]
    [red]while c <> #27 do
    begin[/red]
    c := readkey;
    [green]posx := 20;
    posy := 20;[/green]
    [blue]gotoxy(posx,posy);
    write(' ');[/blue]
    case c of
    #75 : posx := posx-1;
    #72 : posy := posy + 1;
    #77 : posx := posx+1;
    #80 : posy := posy-1;
    end;

    gotoxy(posx,posy);
    write(pac);
    if (posx > 40) or (posx < 1) then break;
    if (posy > 40) or (posy < 1) then break;
    [red]end[/red]
    [/code]

    Above, you are setting the X & Y values to 20 on every pass. So when they puch up, it subtracts 1 from the number (20-1 = 19) and then writes [italic]pac[/italic] at PosX,PosY (20,19). Your program then loops again and sets the numbers back to (20,20). Move the [green]green[/green] lines above your loop and that should fix the cross problem.
    Also, the [blue]blue[/blue] lines added above will erase the character before drawing the new one. (Draws a space BEFORE moving the PosX,PosY variables)
    Lastly, you should change the [red]Red[/red] loop to a [b]Repeat...Until[/b] loop, although this one isn't quite as important. The reason for this is that when the program first starts, older versions of TP didn't always initialize the variables to zero, so it is *possible* that c could be equal to #27 apon starting and skip over your loop (although very unlikely)

    Phat Nat

Sign In or Register to comment.