Key Input?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Colo
Newbie
Posts: 21
Joined: Sat Mar 06, 2010 8:42 am
Projects: Some Testgames ^^
Location: Germany
Contact:

Key Input?

#1 Post by Colo » Sat Mar 06, 2010 8:56 am

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.
Real stregth is to show weakness.

User avatar
backansi
Veteran
Posts: 224
Joined: Sun May 31, 2009 7:15 am
Location: Korea, Republic of
Contact:

Re: Key Input?

#2 Post by backansi » Sat Mar 06, 2010 9:57 am

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]

User avatar
Colo
Newbie
Posts: 21
Joined: Sat Mar 06, 2010 8:42 am
Projects: Some Testgames ^^
Location: Germany
Contact:

Re: Key Input?

#3 Post by Colo » Sat Mar 06, 2010 12:45 pm

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...
Real stregth is to show weakness.

User avatar
JQuartz
Eileen-Class Veteran
Posts: 1265
Joined: Fri Aug 31, 2007 7:02 am
Projects: 0 completed game. Still haven't made any meaningfully completed games...
Contact:

Re: Key Input?

#4 Post by JQuartz » Sun Mar 07, 2010 8:45 am

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!!'
I suspect somebody is stealing my internet identity so don't believe everything I tell you via messages. I don't post or send messages anymore so don't believe anything I tell you via messages or posts.

User avatar
Colo
Newbie
Posts: 21
Joined: Sat Mar 06, 2010 8:42 am
Projects: Some Testgames ^^
Location: Germany
Contact:

Re: Key Input?

#5 Post by Colo » Sun Mar 07, 2010 9:42 am

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 ;)
Real stregth is to show weakness.

User avatar
chronoluminaire
Eileen-Class Veteran
Posts: 1153
Joined: Mon Jul 07, 2003 4:57 pm
Completed: Elven Relations, Cloud Fairy, When I Rule The World
Tumblr: alextfish
Skype: alextfish
Location: Cambridge, UK
Contact:

Re: Key Input?

#6 Post by chronoluminaire » Sun Mar 07, 2010 1:00 pm

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])
I released 3 VNs, many moons ago: Elven Relations (IntRenAiMo 2007), When I Rule The World (NaNoRenO 2005), and Cloud Fairy (the Cute Light & Fluffy Project, 2009).
More recently I designed the board game Steam Works (published in 2015), available from a local gaming store near you!

User avatar
Colo
Newbie
Posts: 21
Joined: Sat Mar 06, 2010 8:42 am
Projects: Some Testgames ^^
Location: Germany
Contact:

Re: Key Input?

#7 Post by Colo » Sun Mar 07, 2010 1:35 pm

nice, thanks!

Ok, now its done, perfect!
Real stregth is to show weakness.

User avatar
usul
Veteran
Posts: 415
Joined: Mon Oct 29, 2007 12:35 pm
Projects: Teachings of the Buddha, System-Addict, Generation XxX
Location: Quebec
Contact:

Re: Key Input?

#8 Post by usul » Mon Mar 08, 2010 4:57 pm

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)
"The universe is non-simultaneously apprehended"
— Buckminster Fuller

User avatar
Asphodel
Regular
Posts: 28
Joined: Sat Jan 17, 2009 1:40 am
Contact:

Re: Key Input?

#9 Post by Asphodel » Mon Mar 08, 2010 5:42 pm

I think you want 'K_SPACE'.

User avatar
Colo
Newbie
Posts: 21
Joined: Sat Mar 06, 2010 8:42 am
Projects: Some Testgames ^^
Location: Germany
Contact:

Re: Key Input?

#10 Post by Colo » Tue Mar 09, 2010 6:59 am

There is this link (klick), where all keys are listed.
Real stregth is to show weakness.

Post Reply

Who is online

Users browsing this forum: Adrian_DVL, noeinan