Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
moving a letter around the screen Posted by fredlegman on 10 Apr 2003 at 2:28 PM
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.

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.

Matt

Report
Re: moving a letter around the screen Posted by zibadian on 10 Apr 2003 at 11:35 PM
: 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.
:
:
: 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.
: 

: Matt
:
:
Just before you change the position, you need to write a space to delete the previous position.
Report
Re: moving a letter around the screen Posted by Phat Nat on 11 Apr 2003 at 12:08 AM
: 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.
:
: 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.

: Matt

Hey there. A few problems here. First off, your program is only displaying a cross because of the green lines here:
   while c <> #27 do
   begin
        c := readkey;
        posx := 20;
        posy := 20;
        gotoxy(posx,posy);
        write(' ');
        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


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 pac at PosX,PosY (20,19). Your program then loops again and sets the numbers back to (20,20). Move the green lines above your loop and that should fix the cross problem.
Also, the 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 loop to a Repeat...Until 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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.