Displaying multiple images through a loop

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
useless19
Newbie
Posts: 7
Joined: Fri Dec 28, 2018 9:33 am
Contact:

Displaying multiple images through a loop

#1 Post by useless19 »

I'm trying to display a hand of (randomly picked) cards. What I've found so far either can't find the image location, or thinks the cards are the same and only displays one at a time.

For example:

Code: Select all

$ space = 1.0 / len(player_hand)
$ i = 0
while i < len(player_hand):
    transform current_card_placement:
        xalign space * (i+1)
        yalign 0.5
    $ card_placement = space * (i+1)
    $ card_dir = "images/" + player_hand[i] + ".png"
    show expression card_dir at current_card_placement as i
    $ i += 1
which displays the cards using the right images and spacing, but fails to display more than 1 card at once. If I remove the 'as i' it does the same.

And:

Code: Select all

python:
     space = 1.0 / len(player_hand)
     for i in range(len(player_hand)):
         card_placement = space * (i+1)
         card_dir = "images/" + player_hand[i] + ".png"
         renpy.show(card_dir, at_list=[Position(xpos=card_placement, ypos=0.5)])
which appears to give me multiple images at once, but can't find the image location and uses the default renpy silhouette, so I'm unsure if it would actually display all the cards at once. Removing the '+ ".png"' doesn't help.

Is there a way to do this in renpy? Otherwise, is there a way to do this in python?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Displaying multiple images through a loop

#2 Post by Remix »

Firstly, I've not tested any of this, so it is mostly just pointers...

I'd suggest defining the transform during init and passing in parameters during the loop:

Code: Select all

transform current_card_placement( card_num, hand_size ):
    xalign int(card_num * hand_size)
    yalign 0.5

label start:
    $ i = 1
    while i <= len(player_hand):
        $ card_dir = "images/" + player_hand[i] + ".png"
        show expression card_dir at current_card_placement( i, len(player_hand) )
        $ i += 1
Note: I removed the aliasing as aliasing to i does not alias to 1,2,3 etc, it aliases to a displayable referenced as "i"

I'd strongly suggest putting this part of your code as a screen though and maybe using a grid or hbox to separate the elements.

Code: Select all

screen show_hand( player_hand = ['king', 'queen' ] ):
    hbox:
        for card in player_hand:
            add "images/[card].png"
Frameworks & Scriptlets:

useless19
Newbie
Posts: 7
Joined: Fri Dec 28, 2018 9:33 am
Contact:

Re: Displaying multiple images through a loop

#3 Post by useless19 »

Thanks! I'll look into that and see how it goes.

useless19
Newbie
Posts: 7
Joined: Fri Dec 28, 2018 9:33 am
Contact:

Re: Displaying multiple images through a loop

#4 Post by useless19 »

Using a screen seems to be the way I need to go. I knew there had to be a simpler way, thanks!

Post Reply

Who is online

Users browsing this forum: babayi0827, Semrush [Bot]