Displaying a random sprite

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
CainVoorhees
Regular
Posts: 45
Joined: Sun Feb 03, 2013 2:34 am
Soundcloud: peterjiangvo
Contact:

Displaying a random sprite

#1 Post by CainVoorhees »

I'd like to have my software randomly select a sprite and display it on screen. On top of that, I'd like for it to repeat the process every time I click on it. My initial theory is to have the game randomly pick a number between 1 and 100 (yes, that many sprites) and display the sprite associated with that number.

I got as far as having the game select a random number, but I'm having trouble implementing it without coding elif number = 1, elif number = 2, etc. in each clickable item that will feature this function. With how flexible ren'py is, I'd like to know if there's a much simpler code I could use without having to go into the tedious work that I may have to fall back on.

User avatar
Alex
Lemma-Class Veteran
Posts: 3097
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Displaying a random sprite

#2 Post by Alex »

You can use either

Code: Select all

show expression my_var
where you can set "my_var" to a file name or a displayable, or python equivalent of show statement

Code: Select all

$ renpy.show(my_var)
http://www.renpy.org/doc/html/displayin ... -statement
http://www.renpy.org/doc/html/statement ... ing-images

><320amai
Newbie
Posts: 2
Joined: Tue Jan 24, 2012 7:04 pm
Contact:

Re: Displaying a random sprite

#3 Post by ><320amai »

The choice statement selects randomly an option for display:

http://www.renpy.org/doc/html/atl.html#choice-statement

And if you want to know which image is displayed, you can try showing, but it might not work with ATL:

http://www.renpy.org/wiki/renpy/doc/ref ... py.showing

User avatar
CainVoorhees
Regular
Posts: 45
Joined: Sun Feb 03, 2013 2:34 am
Soundcloud: peterjiangvo
Contact:

Re: Displaying a random sprite

#4 Post by CainVoorhees »

><320amai wrote:The choice statement selects randomly an option for display:

http://www.renpy.org/doc/html/atl.html#choice-statement
I feel like this is what I'm looking for, but I don't understand how I might install it. Although I looked at the tutorial and compared the script, I think I need further explanation as to how one works with ATL in python.

The game is set up on a image map with hotspots. If you click on the "char01" hotspot, a random sprite is supposed to appear in place of the hotspot. I have it set up to when you click on the hotspot, the game selects a random number. Only problem is, the engine doesn't recognize numbers as names.

Code: Select all

label game:
    scene bg starswirl
    $ result = renpy.imagemap("images/whowouldwin-mockup.png", "images/cdef.jpg", [
        #character
        (120, 127, 360, 477, "char01"),
        (600, 127, 840, 477, "char02"),
        ])
    if result == "char01":
        $ char01_roll = renpy.random.randint(1, 8)
        show expression char01_roll at left with dissolve
        jump game
    elif result == "char02":
        $ char02_roll = renpy.random.randint(1, 8)
        show expression char02_roll at right with dissolve
        jump game
I defined the sprites as:

Code: Select all

image c1 = "images/c001.jpg"
image c2 = "images/c002.jpg"
image c3 = "images/c003.jpg"
image c4 = "images/c004,jpg"
image c5 = "images/c005.jpg"
image c6 = "images/c006.jpg"
image c7 = "images/c007.jpg"
image c8 = "images/c008.jpg"
image c9 = "images/c009.jpg"
image c10 = "images/c010.jpg"
Previously, the sprites did not have the "c" prefix. I'm looking at installing 100 sprites...

I also eventually want to set it up so that the program can record which sprites were selected and be able to display those later, but it's one of those "if I have the time" goals.

><320amai
Newbie
Posts: 2
Joined: Tue Jan 24, 2012 7:04 pm
Contact:

Re: Displaying a random sprite

#5 Post by ><320amai »

CainVoorhees wrote:I feel like this is what I'm looking for, but I don't understand how I might install it. Although I looked at the tutorial and compared the script, I think I need further explanation as to how one works with ATL in python.
One can use the choice statement in the init block:

Code: Select all

init:
    $ num = 2
    image somePicture 0 = "c1.png"
    image somePicture 1 = "c2.png"
    image somePicture anotherName:
        "somePicture 1"
    image somePicture rand:
        choice:
            "somePicture 0"
        choice:
            "somePicture anotherName"
init python:
    def showRandom():
        randomNumber = renpy.random.randint(0, num-1)
        renpy.show("somePicture " + str(randomNumber))
        return
label start:
    show somePicture rand
    "..."
    hide somePicture
    $ showRandom()
    "..."
    hide somePicture
CainVoorhees wrote:Only problem is, the engine doesn't recognize numbers as names.
In the code above I changed integer into string and used the function mentioned by Alex. Perhaps you could use in your code:

Code: Select all

        $ char01_roll = "c" + str(renpy.random.randint(1, 8))
        show expression char01_roll at left with dissolve

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]