Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting a simple game to work

Status
Not open for further replies.

conna

Technical User
Mar 19, 2004
3
FI
Ok, this is what I'm trying to make: A game with 4 buttons in it, that highlight in a random order. The player has to press the according key (1,2,3,4) depending on which button is highlighted. The speed should gradually increase at which the button changes, so it becomes harder and harder over time

I dont quite know how to do this however, you can see what I have so far at


How do I make it so that the same button doesn't highlight twice in a row?

Could anyone tell me how to get it to work, or possibly make a working version of it?
 
Hi,

a simple loop should do this...

oldX5 := X5;
repeat
x5:=random(4)+1;
until X5 <> oldX5;

the repeat-loop will never leave until X5 is another Value than the OldX5...


You shold set X5 at the begin of your game so something lik -1... it is not function-relevant, but it looks better if the variable is preset.

Greetings
Leonardo


 
x5 is the random number that defines which button is lit. Now, I need the player to react to it, by pressing the number. Since x5 is a number between 1 and 4, I guess I can use the keys 1-4 for the game. If the player presses 1, and x5 is 1 (the first button is lit), the game will continue, if it isn't it will end. But how do I do it?
 
Oh, and I'd guess I have to use a percentage somehow to increase the speed (IE delay decreases). Do I have to take a new variable, which I'll use as delay, which is xx% shorter than the previous delay, if so, how?
 
Ok,

This Function waits for the given time for a key, and returns the char wich is pressed or #0 if an timeout ocours.

It is not verry exact, becourse the Systemcall "keypressed" takes a little time. Bigger value of the "TimeStep"-const will give a more acurad time, but less responsetime for an keypress. I think 100ms is a good choice.

Some words to the function-Cals:
function keypressed:boolean;
- the function returns imidetly
- returns true, when any key is pressed.

function readkey:char;
- wait until a key is pressed and
- returnes than the key

Code:
function WaitForKey(t:longint):char;
const TimeStep = 100;
var i : longint;
begin
  i := 0;
  (* Loop until somone press any key or the time reaced the Limit *)
  while (i < t) and not keypressed do 
    begin
      delay(TimeStep); (* wailt a little*)
      i := i + TimeStep; (* and count haw log we are waiting  *)
    end;

  if (i < t) then 
    begin
      (* no Timeout... so we got a keypressed *)
      WaitForKey := Readkey;
    end 
  else 
    begin
      (* Timeout: return yust zerro *)
      WaitForKey := #0; 
    end;
end;

And Yes, you need another Variable to incrase the speed...

Leonardo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top