2
votes

I'm a beginner in Pascal and I'm working on a small WIngraph game. At some point on the game, the character (which is a block) has to lay down (the block gets half of its original height). I want this to happen while holding the arrow-down key but the way I implemented it is not actually working. Another problem I have is I don't know how to read keys simultaneously (that would be needed when, for example, running to the right and jumping).

That's how I tried to write it:

procedure joystick;
begin
  key:=readkey;
  case key of 
  #0:begin  
  key:=readkey;  
    case key of  
    #80:with block do  
       begin  
        y1:=y2-100; //make it get half of its height  
        repeat  
         moveblock; //these are the drawing routines.   
         moveball;  //they are in another procedure, which is the 'main loop'   
         collisioncheck;  
         draw;      //i expected the code to run inside here with the block's  
         alternateball; //height changed, and as soon as the arrow key gets released  
         updateGraph(updateNow);  //it should go back to the 'main loop'  
         killball;  
         delay(10);  
        until keypressed = false; //<--thats what i think is not working  
        y1:=y2-200; //this would make the block get normal again  
       end;  
     end;  
   end;      
 end;  

I expected the code to run fine while the key was pressed and as soon as its released, the block should get its normal height and then the program would run based on its main loop, but outside of this procedure.

Everything is working, except that key-holding thing.

2
Please post your actual code. We can't tell what you're doing wrong without it. (Think of it like this - tell your auto mechanic, "My car makes a funny sound. It's a red car, and it looks like the one parked over there. What's wrong with my car?". Would you expect an answer? I hope not.) We can't read your code (or your mind) from here; we only have what you provide us to work with here. Please edit your question and post your actual code; format it when posting by indenting each line four (or more) spaces as needed so that it's readable, and preview below where you're typing it. Thanks. - Ken White
here's the code: pastebin.com/AqkD90kL - Thiago
Almost there. :-) First, your code needs to go here, not at pastebin. If for some reason pastebin is down or the link disappears, your question becomes meaningless. Second, you posted way too much code; you need to post just the part about handling the keys here. StackOverflow is not going to read all that - reduce it to the part that's giving you trouble, and post it here with your question. We'd like to help you, but there are lots of questions here, and you need to help us by asking good questions. Thanks. :-) - Ken White
I changed it again. Sorry for the inconvenience, please make me know if I did something wrong again. ;) - Thiago
You'll need a fuller control over the keyboard than what KeyPressed and ReadKey allow you to have to achieve what you want. This tutorial (Word document) on how to make a keyboard handler in Pascal might be a good starting point. I suspect there must be many ready-made units around, with source code too, but I've only managed to come across this site in Russian (with some parts in English) where you can download working examples with sources (not all of them are what you need!). - Andriy M

2 Answers

4
votes

It does not work because after each keypressed() you should have a readkey(). The function keypressed() returns true until you call readkey() again.

Demo:

uses crt;
var c:char;
    i:longint;
begin
while c<>#27 do
  begin
  while not keypressed() do
    begin
    clrscr;
    writeln('not pressing anything');
    delay(500);
    end;
  i:=0;
  while keypressed() do
    begin
    clrscr;
    c:=readkey();
    if(c=#0) then
      c:=readkey();
    inc(i);
    writeln(c,' ',i);
    delay(300);
    end;
  end
end.
4
votes

If you use freepascal/Lazarus:

  • don't use unit crt together with wingraph, but use wincrt. Wingraph hooks into win32 GUI events, while (win32) crt hooks into works via console API calls. Wincrt hooks into GUI (message pump) events.
  • preferably don't use *crt at all, but better use unit keyboard
  • Have a look in the Free Pascal examples, it contains several little games (a tetris and a samegame implementation) that optionally can also work with wingraph and unit keyboard. There is even some unit for highscores and a simple lineediting procedure on top of keyboard and wingraph.

Next time, please provide more precise details about the development platform (and version) that you use.