Input/Speed problem...

Started by
1 comment, last by Camus 22 years, 9 months ago
I''m trying to write a tetris clone using DelphiX. I want my blocks (sprites) to move one square at a time when I press left or right on the keyboard EVEN IF THEY ARE HELD DOWN! How can I do this? Another thing...if the user holds down the down key I want the blocks to go down faster, but currently they are going way to fast. How can I control the speed of the blocks when the down key is held down? Thanx in advance. Regards, Bergsteinn Einarsson
Regards,Bergsteinn Einarsson
Advertisement
quote:
I want my blocks (sprites) to move one square at a time when I press left or right on the keyboard EVEN IF THEY ARE HELD DOWN!


Let me see if I understand you. You want the pieces to move only one square to the left or right each time the respective movement key is pressed? And if the key is held down no further movement should take place. If you want to move again, you need to release and re-press the key again? If this is what you want you''ll need to track the OnKeyUp/OnKeyPress/OnKeyDown events of your TDXDraw object for the respective keys and just make sure the sprite only moves according to your preferences.

quote:
Another thing...if the user holds down the down key I want the blocks to go down faster, but currently they are going way to fast. How can I control the speed of the blocks when the down key is held down?


To throttle the speed you either need to put a pause in so only a certain amount of keytstrokes are processed, or simply use a timer check for instance:

  procedure MoveDown();const  ROD := 100; {Rate of decent 100ms)  ExitTime: LongInt := 0; {Static Variable}var  EnterTime: LongInt;begin//Get Procedure Entry Time  EnterTime := LongInt(GetTickCount);//Check  if (ExitTime < EnterTime) then begin    Sprite.Y := Sprite.Y + 16;    ExitTime := LongInt(GetTickCount) + ROD;    end;end;      


At least that''s my take on it, but there could be a better way. I just wrote this code now, so if it doesn''t work let me know. It should "throttle" the code so that if MoveDown is constantly called the Sprite.Y will only change every 100 milliseconds. That value may need to be higher, but at least it''s a starting point.

Anyone have a better way? I have yet to delv into DXInput so I''m not sure if there are any methods of modifying the rate at which input devices are polled. I''m sure there is a way though.
Whoops... that was me. And just so you know those consts should be set like this:

  const  ROD = 100; {Rate of decent 100ms)  ExitTime: LongInt = 0; {Static Variable}  


not like this:

  const  ROD := 100; {Rate of decent 100ms)  ExitTime: LongInt := 0; {Static Variable}  


I always make that mistake. I''m so used to adding ":" to those equal signs... force of habit

This topic is closed to new replies.

Advertisement