Basic Customized Menu With Keyboard Shortcuts

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
Peter Kirby
Newbie
Posts: 1
Joined: Sun Dec 14, 2014 3:26 am
Contact:

Basic Customized Menu With Keyboard Shortcuts

#1 Post by Peter Kirby » Sun Dec 14, 2014 3:43 am

Hey everybody! I'm new to Ren'py, Python, making visual novels, and to this forum, so please forgive me if I'm in the wrong place for this... or if something about my code or explanation is wrong.

I'm making a visual novel that has a mecha combat minigame. If the user isn't skipping the minigame entirely, they'll spend a lot of time there. So I wanted to have keyboard shortcuts for making the "moves" in the minigame. This led to the title snippet... Basic Customized Menu With Keyboard Shortcuts.

Code: Select all

# Demonstration
label start:

    "Take your best shot."

    call screen attack_choice
    
    "You picked choice number %(_return)d. Excellent decision!"

    return

screen attack_choice:

    window:
        style "menu_window"
        xalign 0.5
        yalign 0.5

        vbox:
            style "menu"
            spacing 2

            button:
                action Return(1)
                style "menu_choice_button" xminimum int(config.screen_width * 0.2)
                text "1. Shields" style "menu_choice" text_align 0.0 xalign 0.0
            
            button:
                action Return(2)
                style "menu_choice_button" xminimum int(config.screen_width * 0.2)
                text "2. Laser" style "menu_choice" text_align 0.0 xalign 0.0
                
            button:
                action Return(3)
                style "menu_choice_button" xminimum int(config.screen_width * 0.2)
                text "3. Dodge" style "menu_choice" text_align 0.0 xalign 0.0
                
            button:
                action Return(4)
                style "menu_choice_button" xminimum int(config.screen_width * 0.2)
                text "4. Punch" style "menu_choice" text_align 0.0 xalign 0.0
                
            button:
                action Return(5)
                style "menu_choice_button" xminimum int(config.screen_width * 0.2)
                text "5. Rockets" style "menu_choice" text_align 0.0 xalign 0.0
                    
    # Keyboard shortcuts
    key "K_1" action Return(1)
    key "K_2" action Return(2)
    key "K_3" action Return(3)
    key "K_4" action Return(4)
    key "K_5" action Return(5)
The above code that I wrote is now placed in the public domain. Use without any restrictions.

There isn't very much to this snippet. It just makes a specific kind of screen, instead of relying on the general purpose "choice" screen that gets called normally. It's not even very clever, so you'll have to adjust it depending on the number of options you have.

The "text_align 0.0 xalign 0.0" makes it left-aligned so that the numbered options don't look bad. The "0.2" makes it 20% of the screen width minimum, which you can adjust depending on how verbose your options are.

The choice selected ends up in the _return variable. Alternatively you could jump conditionally to 5 different labels, using Jump('shields') instead of Return(1) for example.

Maybe this saves you time doing something similar. :)

Post Reply

Who is online

Users browsing this forum: Bing [Bot]