Change renpy.input color mid-input for typing minigame

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
Krlika
Newbie
Posts: 3
Joined: Thu Mar 17, 2022 11:45 am
Contact:

Change renpy.input color mid-input for typing minigame

#1 Post by Krlika » Fri Oct 14, 2022 2:45 am

Heyo! I've got a simple typing minigame working where the user must type out a prompt from a randomly selected list. Below is a section of the most relevant code.

Code: Select all

label codingRound(end_label="codingGameWon", codeDisplay=None, default=""):
    python:
        if codeDisplay is None: #if display is empty (which it is by default)
            codeDisplay = codingLines.pop() #variable is assigned a random subject

        typed = renpy.input(codeDisplay) #variable is user input

    if typed != codeDisplay: #If user input does not match the subject
        call codingRound (end_label, codeDisplay=codeDisplay) from _call_codingRound_1 #restart round with the same subject
    else:
        $ codeRoundsCorrect += 1 #increment the number of rounds

        if codeRoundsCorrect >= codingRoundNumber: #if successfull rounds meets max rounds
            $ renpy.call(end_label) #end the game
        else:
            call codingRound (end_label) from _call_codingRound_2 #start another round

    return
This works just fine, but what i'd like to do (If possible) is have the input text change color to red if the user starts misspelling the subject. As example:

#1 the prompt is given and the user starts typing
Prompt: The sky is blue
Input: The s

#2 The user misspells, changing the input color
Prompt: The sky is blue
Input: The sku is bl

#3 User deletes past the error, reverting the color back to normal
Prompt: The sky is blue
Input: The sk

It's a small thing, and not strictly necessary if it would take a lot of effort to make it work. Mainly just curious if there might be a relatively simple method to accomplish this. I'm not really a programmer and i've been doing a lot of reverse engineering and backsolving from other people's code examples to get this far. My best guess would be some kind of loop that constantly compares the user input with the codeDisplay and triggers the color change accordingly, but i'm not sure how to go about implementing that.

User avatar
enaielei
Regular
Posts: 114
Joined: Fri Sep 17, 2021 2:09 am
Tumblr: enaielei
Deviantart: enaielei
Github: enaielei
Skype: enaielei
Soundcloud: enaielei
itch: enaielei
Discord: enaielei#7487
Contact:

Re: Change renpy.input color mid-input for typing minigame

#2 Post by enaielei » Fri Oct 14, 2022 8:38 am

You can try this.

Code: Select all

screen inp(reference):
    default correct = "#fff"
    default wrong = "#f00"
    default text = ""
    default value = ScreenVariableInputValue("text", returnable=True)

    $ color = correct if text == reference[:len(text)] else wrong

    vbox:
        hbox:
            text "Prompt:"
            text reference
        hbox:
            text "Input:"
            input:
                value value
                color color
Then if I understood your code correctly, you can use it like...

Code: Select all

label codingRound(end_label="codingGameWon", codeDisplay=None, default=""):
    python:
        if codeDisplay is None: #if display is empty (which it is by default)
            codeDisplay = codingLines.pop() #variable is assigned a random subject

    call screen inp(codeDisplay)
    typed = _return
    ...

Krlika
Newbie
Posts: 3
Joined: Thu Mar 17, 2022 11:45 am
Contact:

Re: Change renpy.input color mid-input for typing minigame

#3 Post by Krlika » Sun Oct 16, 2022 2:09 am

Thank you! That works perfectly. Just had to throw a dollar sign on the last line and it's good to go.

Code: Select all

...
$ typed = _return
...

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]