Results 1 to 4 of 4

Thread: Regarding a loop and drawing in console mode (etc.)

  1. #1

    Regarding a loop and drawing in console mode (etc.)

    Well, since I'm back after so long and have picked again up the process of learning and doing programming, it has been going pretty well, hah hah hah, and I was actually having some fun with it I thought "If I had only continued this little fun hobby after these 4, 5 years or so since I stopped that previous time, then by now I would've been a much better programmer." I still recalled things I learnt in the past and applied them creatively and I couldn't help but suddenly start with a clone of a DOS bowling game I played as a kid, just for the fun of it. I think that, after this is finished and it shouldn't be too long, I will move on to something more difficult and, of course, more pleasing to the eye. (I recall another member long ago telling me I should use more modern libraries and he is right, but first I want to finish this thing for the sake of completing a small project, quick practicing, getting used to seeing code again, trying to think like a programmer and solve programming questions, nostalgia, and fun) (I think (but don't know for sure) there was someone giving me an example how such a simple game could be made, but I could not find this on the forum if it were there.)

    Anyway, unfortunately I've encountered a problem and I'll phrase it as follows: how can I keep on drawing a moving ball, have it stop only if the player hits the button? When hit, the ball will move to the left. So far, for example temporarily:

    Code:
    { GAME LOOP }
    repeat
      Key:= readKey;
      case Key of
        '=':  moveBallUp(1, 1, 60);      { the parameters are a bunch of bytes for the variables X, Y (for a for loop and coordinates) and SpeedMs for use with a delay }
        '\':  moveBallDown(1, 1, 60);    { here too }
        #8 :  throwBall(1, 1);
      end;
    until Key = #27;
    Maybe drawBallUp and drawBallDown are better descriptions for these procedures. moveBallUp and moveBallDown draw the ball, going on one fixed X coordinate and where the Y coordinate changes position of course.

    While the program checks for user input, I want the ball to move. Then, upon hitting <Backspace>, the ball will stop its moving up or down and will go straight to the left right on the Y coordinate that the user chose.
    As you know, the problem is that now one must press = or \ to move the ball. While I might also use that anyway to make it more difficult, I really want to clone that old game and have the ball move up and down without the user having to specify it. Maybe something like:

    Code:
    repeat
      moveBallUp(1, 1, 60);
      moveBallDown(1, 1, 60):
      Key:= readKey;
    until (Key = #27) or (Key = #8);
    
    ...
    ...
    ...but of course the program waits for user input and doesn't continue to move the ball up and down.

    I'm thinking at the moment and maybe something in the sense of the following might do:
    Code:
    repeat
      moveBallUp(1, 1, 60);
      moveBallDown(1, 1, 60);
    until keyPressed;
    
    case Key of
      #27:  goto ENDING;
      #8 :  throwBall(1, 1);
    end;
    
    ...
    ...
    ?
    The goto ENDING is just there for ease of quickly jumping to the "ENDING" label which is just before "end." of the main program. Hmmm, well, at least at the moment, to me, this loop seems much better. Then somehow get the user input while the key was pressed. throwBall would have to start exactly from the Y coordinate when the user pressed <Backspace>.

    I know it's a simple thing. Even if so, I really want to finish this thing as I have spent some time on it, drawing a cheap start screen, writing a few simple algorithms to print characters of character arrays or strings at certain speed forwards and backwards and using it in this little program, etc., where I finally creatively applied basic programming knowledge. As one who knows another subject well, I can certainly appreciate creativity, so I can certainly appreciate computer programming creativity. I am eager to also apply knowledge in regard to programming creatively to produce programs and this is one of the experiences. When this little game is done, I will play it and have a laugh. I will also send it to certain others so we can have a laugh together and talk of the old DOS days Then, if possible time-wise and whatever-wise, move on to the next little programming project to increase experience, probably finally having a go at Pong or a version of Snake either in console mode or using something like SDL, or maybe to simply go and learn some more about classes and other things first. Please advise

  2. #2
    you might get some ideas from. I didn't test any of this:
    Code:
    velocityX:=0;
    velocityY:=0;
    repeat
      StartTime:=GetTickCount;
      Key:=SomeGetKeyFunction(); // Some function that doesn't stop waiting for press
      // return 0 or something if nothing was pressed.
    
      case Key of
        VK_LEFT: velocityX:=-1;
        VK_RIGHT: velocityX:=1;
      end;
    
      ballX:=ballX+velocityX;
      ballY:=ballY+velocityY;
      DrawScene;
    
      // If drawing and moving would take different amount of time each frame
      // then you may need to control the delay length.
      // So that it runs same speed on all computers.
      DelayTime:=50-(GetTickCount-StartTime);
      if DelayTime>0 then Delay(DelayTime);
    until Key = #27;
    This is just a quick and dirty delay solution, but it should work decently enough for some simple projects.

  3. #3
    My reply seems to be eaten up by forum software, so I'll repeat:

    Try
    Code:
    if keypressed then Key:= readKey;
    instead of just
    Code:
     Key:= readKey;
    That should fix the issue!

  4. #4
    Thanks, guys. I'm moving forward again.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •