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

Username:   Password: 
 RegisterRegister   
 Need help on character control using the Input.Down and delay
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kar




PostPosted: Fri Feb 24, 2012 5:49 pm   Post subject: Need help on character control using the Input.Down and delay

What is it you are trying to achieve?
trying to control a character using the arrow keys for my snake game


What is the problem you are having?
if a key is pressed during the "delay" the action does not take place

Describe what you have tried to solve this problem
no clue how to solve it


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


% Snake game
var x, y, r, t : int
var vDir, hDir : int
var score : int := 0
var chars : array char of boolean
var snake : int


View.Set ("graphics,offscreenonly")


x := 320
y := 200

r := Rand.Int (10, 630)
t := Rand.Int (10, 390)


procedure draw
    drawfilloval (x, y, 20, 20, yellow)
    delay (150)
    cls
end draw




vDir := 0
hDir := 0

snake := 0

loop

    Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) and vDir ~= -1 then
        vDir := 1
        hDir := 0
    end if

    if chars (KEY_DOWN_ARROW) and vDir ~= 1 then
        vDir := -1
        hDir := 0
    end if

    if chars (KEY_LEFT_ARROW) and hDir ~= 1 then
        vDir := 0
        hDir := -1
    end if

    if chars (KEY_RIGHT_ARROW) and hDir ~= -1 then
        vDir := 0
        hDir := 1
    end if

    x := x + (20 * hDir)
    y := y + (20 * vDir)



    if x >= 640 then
        x := 0
    elsif y >= 400 then
        y := 0
    elsif x <= 0 then
        x := 640
    elsif y <= 0 then
        y := 400
    end if


    drawfilloval (x, y, 20, 20, yellow)
    Time.Delay (150)


    drawfilloval (r, t, 10, 10, blue)

    if (r <= x + 12 and r >= x - 12) and (t <= y + 12 and t >= y - 12) then
        r := Rand.Int (10, 630)
        t := Rand.Int (10, 390)
    end if


    View.Update
    cls
end loop




cls






Please specify what version of Turing you are using
4.1
Sponsor
Sponsor
Sponsor
sponsor
evildaddy911




PostPosted: Fri Feb 24, 2012 8:04 pm   Post subject: Re: Need help on character control using the Input.Down and delay

The best way to fix it is to decrease the delay. Another way to fix it is to use processes; right before the procedure, put
Turing:
Process input
Loop
Input.KeyDown(chars)
end loop
end input
fork input

and take out the input.keydown in the main loop

Remember; thus may not wok, you may need to play around withit a little
Aange10




PostPosted: Fri Feb 24, 2012 8:12 pm   Post subject: RE:Need help on character control using the Input.Down and delay

Alternatively, you could loop Input.KeyDown in a loop that doesn't exit until the time you wanted to pass has passed.
Raknarg




PostPosted: Sat Feb 25, 2012 9:40 am   Post subject: Re: Need help on character control using the Input.Down and delay

evildaddy911 @ Fri Feb 24, 2012 8:04 pm wrote:
The best way to fix it is to decrease the delay. Another way to fix it is to use processes; right before the procedure, put
Turing:
Process input
Loop
Input.KeyDown(chars)
end loop
end input
fork input

and take out the input.keydown in the main loop

Remember; thus may not wok, you may need to play around withit a little


Don't feed him false information. Never use processes. There have been too many arguments about this as it is.
kar




PostPosted: Sun Feb 26, 2012 12:20 am   Post subject: RE:Need help on character control using the Input.Down and delay

if i higher delay than is there a way to slow the skake down
evildaddy911




PostPosted: Sun Feb 26, 2012 8:05 am   Post subject: RE:Need help on character control using the Input.Down and delay

Use real numbers for your snake coords and round() for anything that requires them to be insegers
Aange10




PostPosted: Sun Feb 26, 2012 10:13 am   Post subject: RE:Need help on character control using the Input.Down and delay

If you MUST have input during a delay, here is an idea for it.


Turing:

const DELAYTIME : int := 5000
var delayTimer : int := 0
var keyPress : array char of boolean

loop
    % Do your program
    % When it is time to do a delay
    loop
        % Store input
        Input.KeyDown (keyPress)
        % Exit when we've delayed long enough
        if Time.Elapsed () - delayTimer >= DELAYTIME then
            exit
        end if
    end loop
    % Reset the timer
    delayTimer := Time.Elapsed ()
end loop


However this is an example with Time.DelaySinceLast (x) not Delay (x). They are basically the same thing, except delay will stop the program for x milliseconds, where Time.DelaySinceLast will keep the program from going faster than x milliseconds.
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  [ 7 Posts ]
Jump to:   


Style:  
Search: