Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Timing Problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zeppelin




PostPosted: Sun Dec 02, 2007 4:05 pm   Post subject: Timing Problem

Ok I'm working on an RPG for my Grade 10 ISP and I want to make a Paper Marioish battle system. One of the things I need to do is a random button press as in the computer generates a random number and the player has to push the coresponding key within the time limit. Now I got the key pressing part to work but I can't get the time limit. I'm using a counter instead of Time.Elapsed. Here's what I have so far.

code:

var Action : int
var Timing : int

randint (Action,1,2)

Timing := 0

put Action

loop
var ch : string (1)
            getch (ch)
           
            Timing := Timing + 1
           
            if Action = 1 and ch = "a" and Timing  <= 2000 then
            put "Success"
            exit
           
            elsif Action = 2 and ch = "s" and Timing <= 2000 then
            put "Success"
            exit
           
            else
            put "Fail"
            exit
           
            end if       

end loop


Now can I get this to work?
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Sun Dec 02, 2007 4:40 pm   Post subject: Re: Timing Problem

getch stops the machine from continuing on to code below in a procedural fashion so you need to use Input.KeyDown (which i always recommend except for in non-live input.)

for example:

Turing:

% Booleans for Each Character On keyboard
var chars : array char of boolean
% Key To Hit In Time
var key : char := 'a'

put key

% Time Since Key Was Shown To Hit
var timeStart := Time.Elapsed

loop
    Input.KeyDown (chars) % Update Booleans For Keyboard
   
    % If more then 500 milliseconds has passed fail user,
    % if character 'a' has been pressed in time put sucess
    if ((Time.Elapsed - timeStart) > 500) then
        put "Fail"
        exit
    elsif chars ('a') then
        put "Success!"
        exit
    end if
end loop



also, you should not use randint (due to it modifying a variable, which is bad practice) instead you should use Rand.Int(lower,upper).
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: