Page 1 of 1

Key Input?

Posted: Sat Mar 06, 2010 8:56 am
by Colo
Hi @all!

As you see, I'm a beginner with Ren'Py... But I think that I am quiet good at understanding it ^^

Anyway, my question ist really simpel: Is there a command that asks for a keyboard input?

I want to advise the reader to quickly input keys from his or her keyboard.

Example:
The reader has to press A, otherwise the else oder fail condition will appear...

Idea of Code:

Code: Select all

show picture_of_bottom_A
     press.button(A)
     time 1.0

         "Good job! You pressed A!"
     else
         "You are far too slow!"
Thanks for help!

And I am sorry for my bad english skills, I am not native.

Re: Key Input?

Posted: Sat Mar 06, 2010 9:57 am
by backansi

Code: Select all

label start:
    $ ui.timer(2, ui.jumps('slow'))
    $ ui.keymap(w = ui.jumps('next'))
    $ ui.interact()
 
label slow:
    'you\'re too slow'
    return
    
label next:
    'you are fast enough'
    return
    
:)
reference of ui.keymap[link]

Re: Key Input?

Posted: Sat Mar 06, 2010 12:45 pm
by Colo
Thanks!
I got that.

Now I was of cause trying to play around with it.
What I got was this to be wrong:

Code: Select all

label start:
    $ ui.timer(2, ui.jumps('slow'))
    $ ui.keymap(w = ui.jumps('next'))
    $ ui.keymap(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, x, y, z, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 =ui.jumps('wrong'))
    $ ui.interact()

label slow:
    'you\'re too slow'
    return
   
label next:
    'you are fast enough'
    return
    
label wrong:
    'Wrong Key!!'
4 reference:

Code: Select all

SyntaxError: keyword can't be an expression (2)
Now, I have to state every key on its own?
Like:

Code: Select all

$ ui.keymap(a =ui.jumps('wrong'))
$ ui.keymap(b =ui.jumps('wrong'))

etc...
This would be far too extensive...

I also tried this:

Code: Select all

init:
    $ p = renpy.random.randint (1, 3)
        if p == 1
                Test = "a"
        if p == 2
                Test = "b"
        if p == 3
                Test = "c"

label start:
    $ ui.timer(2, ui.jumps('slow'))
    $ ui.keymap('Test' = ui.jumps('next'))
    $ ui.interact()

label slow:
    'you\'re too slow'
    return
   
label next:
    'you are fast enough'
    return
But got this error:

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


On line 2 of F:\RenPy\Rumprobieren/game/script.rpy: one-line python statement does not expect a block. Please check the indentation of the line after this one.
$ p = renpy.random.randint (1, 3)
                   ^

Ren'Py Version: Ren'Py 6.10.2e
That should be enough questions for now ^^

I am sorry, this is problably really n00b-like...

Re: Key Input?

Posted: Sun Mar 07, 2010 8:45 am
by JQuartz
Colo wrote:Now, I have to state every key on its own?
If you use for you wouldn't need to state every key, like so:

Code: Select all

init:
    $ all_alphabets=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    $ all_numbers=['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
    $ all_keys=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','1', '2', '3', '4', '5', '6', '7', '8', '9', '0']

label start:
    $ correct_key=renpy.random.choice(all_keys)
    
label keyboard:
    python:
        ui.text(correct_key) #just for testing otherwise you won't know what the correct key is
        ui.timer(2, ui.jumps('slow'))
    
    
    
        for key in all_alphabets:
            if key==correct_key:
                exec("ui.keymap("+key+" = ui.jumps('next'))")
            else:
                exec("ui.keymap("+key+" = ui.jumps('wrong'))")
                
        for key in all_numbers:
            if key==correct_key:
                exec("ui.keymap(K_"+key+" = ui.jumps('next'))")
            else:
                exec("ui.keymap(K_"+key+" = ui.jumps('wrong'))")
    $ ui.interact()

label slow:
    'you\'re too slow'
    return
   
label next:
    'you are fast enough'
    return
   
label wrong:
    'Wrong Key!!'

Re: Key Input?

Posted: Sun Mar 07, 2010 9:42 am
by Colo
That is very sweet! Thx you very much, works perfect!

I have got a last question on this:

Code: Select all

init:
    $ all_alphabets=['a', 'b', 'c']
    $ all_keys=['a', 'b', 'c']
    
    image n a = "strasse01 (1).jpg"
    image n b = "girl01 (2).png"
    image n c = "girl01 (1).png"

label start:
   $ correct_key=renpy.random.choice(all_keys)
   
label keyboard:
    python:
        ui.text(correct_key) #just for testing otherwise you won't know what the correct key is
        ui.timer(2, ui.jumps('slow'))
   
   
        for key in all_alphabets:
            if key==correct_key:
                exec("ui.keymap("+key+" = ui.jumps('next'))")
            else:
                exec("ui.keymap("+key+" = ui.jumps('wrong'))")               
    
    if correct_key=="a":
        show n a
    if correct_key=="b":
        show n b
    if correct_key=="c":
        show n c
    $ ui.interact()

label slow:
    'you\'re too slow'
    jump start
   
label next:
    'you are fast enough'
    jump start
   
label wrong:
    'Wrong Key!!'
    jump start
Is there an easyer way to perform this script?

Otherwise I'm going to be very happy ;)

Re: Key Input?

Posted: Sun Mar 07, 2010 1:00 pm
by chronoluminaire
You can replace this kind of thing:

Code: Select all

    if correct_key=="a":
        show n a
    if correct_key=="b":
        show n b
    if correct_key=="c":
        show n c
with this:

Code: Select all

    $ renpy.show(["n", correct_key])

Re: Key Input?

Posted: Sun Mar 07, 2010 1:35 pm
by Colo
nice, thanks!

Ok, now its done, perfect!

Re: Key Input?

Posted: Mon Mar 08, 2010 4:57 pm
by usul
How do you take account the space bar in the above code? If I just add ' ' in the all keys list I get this error:

SyntaxError: invalid syntax (<string>, line 1)

Re: Key Input?

Posted: Mon Mar 08, 2010 5:42 pm
by Asphodel
I think you want 'K_SPACE'.

Re: Key Input?

Posted: Tue Mar 09, 2010 6:59 am
by Colo
There is this link (klick), where all keys are listed.