Cycling images on menu

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
ricardo456
Newbie
Posts: 6
Joined: Tue Dec 11, 2018 11:24 pm
Contact:

Cycling images on menu

#1 Post by ricardo456 »

Hello im new at using renpy and i wanted to try something a bit more complex this time around.

I want to have the background image of the menu change every time you open the game, or cycle around like some vns that i played before.

I know the basics of the program and coding, however im not quite sure how to set it up.

I only need to cycle between like 5 images that i have already prepared.

Any help would be appreciated

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Cycling images on menu

#2 Post by Per K Grok »

ricardo456 wrote: Wed Dec 12, 2018 12:22 am Hello im new at using renpy and i wanted to try something a bit more complex this time around.

I want to have the background image of the menu change every time you open the game, or cycle around like some vns that i played before.

I know the basics of the program and coding, however im not quite sure how to set it up.

I only need to cycle between like 5 images that i have already prepared.

Any help would be appreciated
You can add images to the menu screen in the screen.rpy file.
Find "screen main-menu():". Below the line 'add gui.main_menu_background' you add your image ' add "yourImage1.png" '

To randomly show a different image each time you open the game you need to import a random module. You can do that in script.rpy

Code: Select all

init python:
    from random import randint   


You can then use this to randomly show a new image when opening the menu

Code: Select all

$ randPix = randint(1,3)
    if randPix==1:
        add "yourImage1.png":
    elif randPix==2:
        add "yourImage2.png":
    else:
        add "yourImage3.png":

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Cycling images on menu

#3 Post by Imperf3kt »

Note that the above code will only generate a different (possibly the same) image each time you start the game, it won't change the menu image any other time, including when quitting or finishing the game and returning to the main menu.

To rotate the image every time the menu is shown, I use the following in one of my projects. Its technically a click to continue kind of screen and replaces the general main menu of the project, but can be adapted to suit any screen.
This cycles through the images in order, but can be made to randomise the image by adding Per K Grok's suggestion of using randint

Code: Select all

init -1:
    default persistent.main_menu_sprite = 1

screen main_menu():

    tag menu

    style_prefix "main_menu"

    add gui.main_menu_background
    if persistent.main_menu_sprite == 1:
        add my_image_1.png
    if persistent.main_menu_sprite == 2:
        add my_image_2.png
    if persistent.main_menu_sprite == 3:
        add my_image_3.png
    if persistent.main_menu_sprite == 4:
        add my_image_4.png
    

    if persistent.main_menu_sprite == 1:
        imagebutton idle gui.main_menu_text hover gui.main_menu_text xpos 0 ypos 0 focus_mask True action ShowMenu("main_menu_2"), SetField(persistent, "main_menu_sprite", 2)
    elif persistent.main_menu_sprite == 2:
        imagebutton idle gui.main_menu_text hover gui.main_menu_text xpos 0 ypos 0 focus_mask True action ShowMenu("main_menu_2"), SetField(persistent, "main_menu_sprite", 3)
    elif persistent.main_menu_sprite == 3:
        imagebutton idle gui.main_menu_text hover gui.main_menu_text xpos 0 ypos 0 focus_mask True action ShowMenu("main_menu_2"), SetField(persistent, "main_menu_sprite", 4)
    elif persistent.main_menu_sprite == 4:
        imagebutton idle gui.main_menu_text hover gui.main_menu_text xpos 0 ypos 0 focus_mask True action ShowMenu("main_menu_2"), SetField(persistent, "main_menu_sprite", 1)
The first section deals with adding the image to the main menu. The second section (the imagebuttons) are used to close the screen and increment the counter by one.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

ricardo456
Newbie
Posts: 6
Joined: Tue Dec 11, 2018 11:24 pm
Contact:

Re: Cycling images on menu

#4 Post by ricardo456 »

I appreciate the quick responses, unfortunately im not too sure where everywhere goes.

So i initiate things on script, but i am kind of confused where each part of the code goes. Sorry for being a newbie. I think just them cycling in order is fine. I might actually prefer that since i want each image to show around the same number of times

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Cycling images on menu

#5 Post by Imperf3kt »

For both given examples, the init part can go almost anywhere.
Best place is just in the screens.rpy file at the same indentation (spacing) it's at, outside other screens.
The top works fine.

An easier way may be to create a new file, call it in inits.rpy or something, and place it in there.

The rest of the code goes anywhere below screen main_menu():, so that it is included as part of that screen.


To make it a bit simpler (because it's a bit specific to the game I pulled it from), I'll try to rewrite the code I posted so it'll work without the imagebuttons, just using the default screens.rpy
I may take a whole day though, as my time to browse forums is a bit limited at the moment.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

ricardo456
Newbie
Posts: 6
Joined: Tue Dec 11, 2018 11:24 pm
Contact:

Re: Cycling images on menu

#6 Post by ricardo456 »

Oh no thank you very much for your time, its great to see you are willing to put this much effort. I will try to make it work if i manage i will let you know so you don't have to spend that much time. It gives me a couple of errors mostly than my images are not defined.

Code: Select all

init -1:
    default persistent.main_menu_sprite = 1

screen main_menu():

    tag menu

    style_prefix "main_menu"

    add gui.main_menu_background
    if persistent.main_menu_sprite == 1:
        add Novella_Bg.jpg
    if persistent.main_menu_sprite == 2:
        add Lisa_Bg.jpg
    if persistent.main_menu_sprite == 3:
        add Fabiola_Bg.jpg
    if persistent.main_menu_sprite == 4:
        add Adria_Bg.jpg

    if persistent.main_menu_sprite == 1:
        imagebutton idle gui.main_menu_text hover gui.main_menu_text xpos 0 ypos 0 focus_mask True action ShowMenu("main_menu_2"), SetField(persistent, "main_menu_sprite", 2)
    elif persistent.main_menu_sprite == 2:
        imagebutton idle gui.main_menu_text hover gui.main_menu_text xpos 0 ypos 0 focus_mask True action ShowMenu("main_menu_2"), SetField(persistent, "main_menu_sprite", 3)
    elif persistent.main_menu_sprite == 3:
        imagebutton idle gui.main_menu_text hover gui.main_menu_text xpos 0 ypos 0 focus_mask True action ShowMenu("main_menu_2"), SetField(persistent, "main_menu_sprite", 4)
    elif persistent.main_menu_sprite == 4:
        imagebutton idle gui.main_menu_text hover gui.main_menu_text xpos 0 ypos 0 focus_mask True action ShowMenu("main_menu_2"), SetField(persistent, "main_menu_sprite", 1)

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Cycling images on menu

#7 Post by Imperf3kt »

That'd be the imagebutton bit and I forgot to add quotation.

Code: Select all

 
init -1:
    default persistent.main_menu_sprite = 1

screen main_menu():

    tag menu

    style_prefix "main_menu"

    add gui.main_menu_background
    if persistent.main_menu_sprite == 1:
        add "Novella_Bg.jpg" 
    if persistent.main_menu_sprite == 2:
        add "Lisa_Bg.jpg" 
    if persistent.main_menu_sprite == 3:
        add "Fabiola_Bg.jpg" 
    if persistent.main_menu_sprite == 4:
        add "Adria_Bg.jpg" 
Should fix up the undefined images, but they won't cycle until the imagebuttons bit is fixed up.

I was just planning on editing the textbutton 'start' to include a set of if checks and add
SetField(persistent, "main_menu_sprite", #)

Don't have a computer to do that right now, but will try throuout the day.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

ricardo456
Newbie
Posts: 6
Joined: Tue Dec 11, 2018 11:24 pm
Contact:

Re: Cycling images on menu

#8 Post by ricardo456 »

Ok you were right that does fix the image issue, unfortunately i could not make the second part work

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Cycling images on menu

#9 Post by Imperf3kt »

The solution by Per K Grok is pretty good, and a bit simpler. Have you tried it yet?

I'm going to be really busy today, but will still try to edit things for you.

I would have done it last night, but for some unknown reason my laptop keyboard broke and now I cannot login to my laptop.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

ricardo456
Newbie
Posts: 6
Joined: Tue Dec 11, 2018 11:24 pm
Contact:

Re: Cycling images on menu

#10 Post by ricardo456 »

I have tried it, but cant seem to make it work well, i guess i need to give a better read to the basics, also dont rush yourself, i can still write around without this feature. Its just something i wanted to add this time around. There is a lot of writing to be done, so take all the time you need

ricardo456
Newbie
Posts: 6
Joined: Tue Dec 11, 2018 11:24 pm
Contact:

Re: Cycling images on menu

#11 Post by ricardo456 »

managed to make Per K Grok thing work, however it does repeat a bit much, but i guess it works well enough

Post Reply

Who is online

Users browsing this forum: No registered users