[SOLVED] Pick one image random from a set of images

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
Benny
Newbie
Posts: 11
Joined: Sun Jul 30, 2017 11:10 am
Contact:

[SOLVED] Pick one image random from a set of images

#1 Post by Benny » Mon Jan 08, 2018 10:23 am

Hi,

I need to randomize the background image from a set of images, this is the code I actually use:

Code: Select all

init -1 python:
    numbers = [1, 2, 3, 4]
    def disco_background(trans, st, at):
        num = renpy.random.choice(numbers)
        renpy.show(("bg disco%d" % num))

init:
    image bg disco:
        function disco_background
        pause 0.50
        repeat

    image bg disco1 = "background/disco/disco1.jpg"
    image bg disco2 = "background/disco/disco2.jpg"
    image bg disco3 = "background/disco/disco3.jpg"
    image bg disco4 = "background/disco/disco4.jpg"
The result is a fixed background with only one image.

How can I randomize the images every 0.50 seconds?

Thanks
Matteo
Last edited by Benny on Tue Jan 09, 2018 5:01 am, edited 1 time in total.

User avatar
Ocelot
Miko-Class Veteran
Posts: 615
Joined: Tue Aug 23, 2016 10:35 am
Skype: miinipaa
Contact:

Re: Pick one image random from a set of images

#2 Post by Ocelot » Mon Jan 08, 2018 10:53 am

Code: Select all

image bg disco:
    choice:
        "bg disco1"
    choice:
        "bg disco2"
    choice:
        "bg disco3"
    choice:
        "bg disco4"
    pause 0.5
    repeat
< < insert Rick Cook quote here > >

User avatar
Benny
Newbie
Posts: 11
Joined: Sun Jul 30, 2017 11:10 am
Contact:

Re: Pick one image random from a set of images

#3 Post by Benny » Mon Jan 08, 2018 11:21 am

Thanks, Ocelot for the quick repy :)
Is also possibile to randomize the pause timing?

Thanks

User avatar
Benny
Newbie
Posts: 11
Joined: Sun Jul 30, 2017 11:10 am
Contact:

Re: Pick one image random from a set of images

#4 Post by Benny » Tue Jan 09, 2018 5:00 am

Quick update,
this is the code I use to randomize the pause (I hope it helps someone :) )

Code: Select all

init python:
    # Generate random floating number between min and max
    def randomFloatPause(min, max):
        return renpy.random.uniform(min, max)
    
    # Generate random integer number between min and max  
    def randomIntPause(min, max):
    	return renpy.random.randint(min,max)

init:
    image bg disco:
        choice:
            "bg disco1"
    	choice:
            "bg disco2"
    	choice:
            "bg disco3"
    	choice:
            "bg disco4"
        pause (randomFloatPause(0.5, 2))
        # pause (randomIntPause(1, 2))
        repeat

Post Reply

Who is online

Users browsing this forum: ComputerArt.Club