[Solved] Is it possible to use a FOR loop to display tiles in screen language?

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
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

[Solved] Is it possible to use a FOR loop to display tiles in screen language?

#1 Post by Chekhov »

Let's say I wanted to create a chessboard inside the screen language, and for whatever reason, I wanted to display each tile separately rather than a pre-created background that shows a chessboard.

Code: Select all

screen Gameboard():
    fixed:
        xpos 300 ypos 400
        
        python:
        
            for y in xrange(8):
                for x in xrange(8):
                    fixed:
                        xysize(50,100)
                        fixed:
                            xoffset (x*100)
                            yoffset (50)
                            add "blacktile.png" xalign 0.0 yalign 0.0
                        fixed:
                            xoffset (50+x*100)
                            yoffset (50)
                            add "whitetile.png" xalign 0.0 yalign 0.0
Now the mistake is obvious: I'm using screen language inside python (fixed, xysize, etcetera). However, I'm failing to figure out a way to use some kind of for or while loop to create such a board.

I thought about predefining things in python and storing locations in an array or something, but that would still require me to run through that array with some kind of loop to use it in renpy screen language. Any ideas?
Last edited by Chekhov on Fri Sep 13, 2019 4:51 am, edited 1 time in total.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Is it possible to use a FOR loop to display tiles in screen language?

#2 Post by hell_oh_world »

Chekhov wrote: Fri Sep 13, 2019 3:35 am Let's say I wanted to create a chessboard inside the screen language, and for whatever reason, I wanted to display each tile separately rather than a pre-created background that shows a chessboard.

Code: Select all

screen Gameboard():
    fixed:
        xpos 300 ypos 400
        
        python:
        
            for y in xrange(8):
                for x in xrange(8):
                    fixed:
                        xysize(50,100)
                        fixed:
                            xoffset (x*100)
                            yoffset (50)
                            add "blacktile.png" xalign 0.0 yalign 0.0
                        fixed:
                            xoffset (50+x*100)
                            yoffset (50)
                            add "whitetile.png" xalign 0.0 yalign 0.0
Now the mistake is obvious: I'm using screen language inside python (fixed, xysize, etcetera). However, I'm failing to figure out a way to use some kind of for or while loop to create such a board.

I thought about predefining things in python and storing locations in an array or something, but that would still require me to run through that array with some kind of loop to use it in renpy screen language. Any ideas?
You can use grids for that https://www.renpy.org/doc/html/screens.html#grid

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: Is it possible to use a FOR loop to display tiles in screen language?

#3 Post by Chekhov »

I can't use grids for it as I'm doing some other manipulation that precludes it (for example, it's more isometric rather than perfectly aligned squares).

I just gave a code example to show the mismatch of trying to use screen language inside a python block; it's a common problem I run into and I think I misunderstand something fundamental about the interplay between renpy and python.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: Is it possible to use a FOR loop to display tiles in screen language?

#4 Post by hell_oh_world »

Chekhov wrote: Fri Sep 13, 2019 3:59 am I can't use grids for it as I'm doing some other manipulation that precludes it (for example, it's more isometric rather than perfectly aligned squares).

I just gave a code example to show the mismatch of trying to use screen language inside a python block; it's a common problem I run into and I think I misunderstand something fundamental about the interplay between renpy and python.
Well, then you can go with the traditional way of using windows or frames or other containers and loop those in the screen.

Code: Select all

screen sample_screen:
	for y in range(8):
                for x in range(8):
                    fixed:
                        xysize(50,100)
                        fixed:
                            xoffset (x*100)
                            yoffset (50)
                            add "blacktile.png" xalign 0.0 yalign 0.0
                        fixed:
                            xoffset (50+x*100)
                            yoffset (50)
                            add "whitetile.png" xalign 0.0 yalign 0.0

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: Is it possible to use a FOR loop to display tiles in screen language?

#5 Post by Remix »

I would advise calculating both the x and y for each image individually and just have all of those as separate items in a single fixed.

A basic example that just does a checkerboard of 50x50 squares...

Code: Select all

screen Gameboard():

    fixed:
        xpos 200 ypos 100
        
        for y in xrange(8):

            for x in xrange(8):

                add Solid('#000' if (x+y) % 2 else '#FFF', xysize=(50,50)):
                    align (0, 0)
                    xoffset x * 50
                    yoffset y * 50
If you needed to stagger the odd rows you could adjust the xoffset maybe xoffset (x * 50) + (y % 2) * 25

If you really wanted pairs of images together you'd want to position child fixed widgets using the x and y values then just add the images to those using static positions relevant to those fixed widgets.
Frameworks & Scriptlets:

User avatar
Chekhov
Regular
Posts: 113
Joined: Tue Jun 26, 2018 9:19 am
Projects: Pluton
Contact:

Re: Is it possible to use a FOR loop to display tiles in screen language?

#6 Post by Chekhov »

Remix wrote: Fri Sep 13, 2019 4:33 am I would advise calculating both the x and y for each image individually and just have all of those as separate items in a single fixed.

A basic example that just does a checkerboard of 50x50 squares...

Code: Select all

screen Gameboard():

    fixed:
        xpos 200 ypos 100
        
        for y in xrange(8):

            for x in xrange(8):

                add Solid('#000' if (x+y) % 2 else '#FFF', xysize=(50,50)):
                    align (0, 0)
                    xoffset x * 50
                    yoffset y * 50
If you needed to stagger the odd rows you could adjust the xoffset maybe xoffset (x * 50) + (y % 2) * 25

If you really wanted pairs of images together you'd want to position child fixed widgets using the x and y values then just add the images to those using static positions relevant to those fixed widgets.


Yeah the pair idea was pretty stupid. What you made is rather elegant, thanks.



hell_oh_world wrote: Fri Sep 13, 2019 4:03 am
Chekhov wrote: Fri Sep 13, 2019 3:59 am I can't use grids for it as I'm doing some other manipulation that precludes it (for example, it's more isometric rather than perfectly aligned squares).

I just gave a code example to show the mismatch of trying to use screen language inside a python block; it's a common problem I run into and I think I misunderstand something fundamental about the interplay between renpy and python.
Well, then you can go with the traditional way of using windows or frames or other containers and loop those in the screen.

Code: Select all

screen sample_screen:
	for y in range(8):
                for x in range(8):
                    fixed:
                        xysize(50,100)
                        fixed:
                            xoffset (x*100)
                            yoffset (50)
                            add "blacktile.png" xalign 0.0 yalign 0.0
                        fixed:
                            xoffset (50+x*100)
                            yoffset (50)
                            add "whitetile.png" xalign 0.0 yalign 0.0
Lmao, thank you so much. Why the hell didn't I just use a renpy loop? I'm laughing at my own mistake here.

Anyways, there were some mistakes in my code, so just on the off-chance anyone searches and finds this page, this is how to create a chessboard out of tiles. It can probably be improved. (I first missed the above post by remix, which is even better).

Code: Select all

screen Gameboard():
    $ whitetile = True #is the next tile a white one?
    for y in range(8):
        for x in range(8):
            fixed:
                xpos 300 ypos 400
                
                fixed:
                    xysize(50,50)
                    xoffset (x*50)
                    yoffset (y*50)
                    if whitetile == True:
                        add "white-tile.png" xalign 0.0 yalign 0.0
                        $ whitetile = False
                    else:
                        add "black-tile.png" xalign 0.0 yalign 0.0
                        $ whitetile = True

        #make sure that the next line starts with the opposite color
        if whitetile == True:
            $ whitetile = False
        else:
            $ whitetile = True

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: [Solved] Is it possible to use a FOR loop to display tiles in screen language?

#7 Post by Kia »

one thing to mention, you don't have to position each block, utilizing the hbox and vbox can do the job

Code: Select all

hbox:
    for i in range(8):
        vbox:
            for ii in range(8):
                fixed:
                    xysize 100,100
there are few standards you might want to consider, from what I have seen, many boards start from the bottom up and 1A is often black.

Post Reply

Who is online

Users browsing this forum: 3N16M4