List containing positions

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
Skiegh
Regular
Posts: 36
Joined: Mon Dec 19, 2011 2:47 pm
Contact:

List containing positions

#1 Post by Skiegh » Fri Jul 05, 2019 12:38 am

Here I am again!

I have a psuedo board game type thing I am working on and I am wondering the best way to track any sort of figures going across the board. I am not sure the best method, but I thought of using a list to have all of the positions of the board, then apply an offset for each other player so they don't fall on the exact same spot on the board tiles. However, my attempt to use a list has not been successful -- it doesn't seem to want to be used a way to dump in xpos and ypos values as I had hoped. Tends to just get an error about "Image xpos 157 ypos 8 was not found" which is a weird error to me. Anyway, here I am...


Code: Select all

## This likely doesn't work, but this is what I had in mind...

default board_tiles = {
1:"xpos 157 ypos 8",
2:"xpos 207 ypos 8"
}
## and so on...

default p1_pos = 1  ## This number will change based on dice rolls and such, so I just add to it
default p1_figure = "board_tiles[p1_pos]" 
default p2_pos = 1
default p2_figure = "board_tiles[p2_pos]"

screen player_figures:
    add "Example_Figure_001.jpg":
        anchor (0.5, 0.5)
        p1_figure
      
    add "Example_Figure_002.jpg":
        anchor (0.5, 0.5)
        p2_figure
        xoffset (5)   
So something like this, I guess.

Image

User avatar
Per K Grok
Miko-Class Veteran
Posts: 728
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 & 2
itch: per-k-grok
Location: Sverige
Contact:

Re: List containing positions

#2 Post by Per K Grok » Fri Jul 05, 2019 3:40 am

Skiegh wrote:
Fri Jul 05, 2019 12:38 am
Here I am again!

I have a psuedo board game type thing I am working on and I am wondering the best way to track any sort of figures going across the board. I am not sure the best method, but I thought of using a list to have all of the positions of the board, then apply an offset for each other player so they don't fall on the exact same spot on the board tiles. However, my attempt to use a list has not been successful -- it doesn't seem to want to be used a way to dump in xpos and ypos values as I had hoped. Tends to just get an error about "Image xpos 157 ypos 8 was not found" which is a weird error to me. Anyway, here I am...

----
I would do this in ordinary script, not as a screen. Well there would be a screen to catch player input, but everything else would be in ordinary script.

you would need a background with the gameboard, and images for the pieces. I'm for now assuming we have to sides with a number of pieses, each piece loking the same except for different colors for the two sides.

image w1 = "images/whitepiece.png"
image w2 = "images/whitepiece.png"
----

image b1 = "images/blackpiece.png"
image b2 = "images/blackpiece.png"


You would also need a variable to hold the position for each piece. I would do that as a tuple wher the first value is the column and the second is the row of the gameboard.


default w1pos=(0,0)
default w2pos=(0,1)

---

default b1pos=(9,0)
default b2pos=(9,1)


I will now assume that the gameboard starts at 50, 50 from the gamescreen-topleft corner and that each square of the game board is 40x40.


For drawing the scene we could now do

Code: Select all

label game:
    scene bg gameboard

    show w1:
       xpos 50 + (w1pos[0] * 40)
       ypos 50 + (w1pos[1] * 40)

    show w2:
       xpos 50 + (w2pos[0] * 40)
       ypos 50 + (w2pos[1] * 40)

-----


Skiegh
Regular
Posts: 36
Joined: Mon Dec 19, 2011 2:47 pm
Contact:

Re: List containing positions

#3 Post by Skiegh » Fri Jul 05, 2019 11:34 pm

Thanks for the response. I am a little confused as to how this works exactly. Though I think a bit might be due to my own lack of clarity. So let me be a bit more specific...

Image

Let us take this colorful mess. Character portraits are on the outskirt edges, with colored borders related to their player color, with pieces position in the top left of the board relative to that. The top left white square is the starting position, which the center of the tile is xpos 175, ypos 25. Each tile is 50x50. Each player position, I would likely just offset so it outlays the way it looks in that picture, so they don't overlap.

Players move from left to right, then down and so on.

My goal with having a list of positions and using a screen was to automate the process. So I could possibly have a random dice roll, add it to a variable, then it would change the position of the player position.

I am likely not utilizing your method correctly, but I am not seeing exactly how it is faster than just manually inputting xpos and ypos changes. I admit I am having trouble even getting it to change to a new position, so clarification is necessary, because I am in-fact stupid, hahaha.

Edit: (and again to fix some of the code)


Okay, so I think I have it working, thanks to your layout of things, I got it working with my original method in some manner. This seems to track everything easily enough...

Code: Select all

default board_tiles_x = {
1:175,
2:225
}
default board_tiles_y = {
1:25,
2:25
}

default p1_pos = 1 

screen player_figures():
    add "CG/Board/Board_Piece_001.jpg":
        anchor (0.5, 0.5)
        xpos 0 + (board_tiles_x[p1_pos])
        ypos 0 + (board_tiles_y[p1_pos])
        xoffset -18
        yoffset -17
This seems to work in about every way I could hope so far. I am not sure to the downside of screens, but this seems like it could easily be translated into a show statement as well, which I will be trying in a moment. Then I could just lump in a label for movement, which would just do all these show statements each dice roll.

Still haven't worked out how to add in a random dice roll number to a variable yet, but uh... pending. That will come later.

Edit again:

Does seem to work just fine with a show statement, so I think I will do that. Thanks for the assistance. I am sure I will run into more issues as I go along. So far so good though.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 728
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 & 2
itch: per-k-grok
Location: Sverige
Contact:

Re: List containing positions

#4 Post by Per K Grok » Sat Jul 06, 2019 2:20 am

Skiegh wrote:
Fri Jul 05, 2019 11:34 pm

----
Edit again:

Does seem to work just fine with a show statement, so I think I will do that. Thanks for the assistance. I am sure I will run into more issues as I go along. So far so good though.
This is how I would sett up the script using only one piece.

Your edit came while I was testing the script, so maybe you don't have any need of it, but since I wrote it I thought I may just as well show it anyway.

Code: Select all

image w1 = "images/brickWhite.png"
default w1pos= [0,0]

default diceNomb=0

default dicestop=1

screen rolldice():                                           # screen to catch left mousebutton clicks
    if dicestop==1:
        key "mousedown_1" action Jump("dice")

####################################################################
label start:
    scene board
    show screen rolldice

label draw:
    $ dicestop=1                       #enables new dice throws
    show w1:
       xpos 150 + (w1pos[0] * 50)
       ypos w1pos[1] * 50

    if w1pos[0] == 9  and w1pos[1] == 11:
        $ renpy.notify("You have reached goal.")

    $ renpy.pause(hard=True)            # waiting for mb click

label dice:
    $ dicestop=0                             # stops new dice throws not necessary at this stage but handy i.e. if you add a dice animation 
    $ diceNomb= renpy.random.randint(1, 6)
    $ renpy.notify("You rolled a " + str(diceNomb) )

    $ w1pos[0] += diceNomb               # change position on row
    if w1pos[0]>9:                       # if passing end of line
        $ w1pos[0] = w1pos[0] - 10       # number of steps on new row
        $ w1pos[1] += 1                  # new row
        if w1pos[1]==4:                 # jumping the black ditch
            $ w1pos[1] = 8

    if w1pos[1]==12:            # stoping at end of rows
        $ w1pos[0] = 9
        $ w1pos[1] = 11


    jump draw
    
 

Skiegh
Regular
Posts: 36
Joined: Mon Dec 19, 2011 2:47 pm
Contact:

Re: List containing positions

#5 Post by Skiegh » Tue Jul 09, 2019 3:15 pm

Oh, thanks for the extra information. That will actually come in handy when I get around to including the dice roll variables into things.

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

Re: List containing positions

#6 Post by Alex » Tue Jul 09, 2019 6:51 pm

Skiegh wrote:
Fri Jul 05, 2019 12:38 am
...
If you don't plan to use fancy animation for moves, you could use a grid to layout all the tiles and to place player's token.
Make a description of the game field as a matrix
gf = [ [1,1,1],
[1,1,1],
[1,1,1] ]

Then you can set player's position in it like gf[y][x]

The advantage is that you could set the position of the whole grid and/or scale it without any additional calculations.

Code: Select all

# game field
default gf = [ [1,1,1],
    [1,1,1],
    [1,1,1],
    [1,1,1] ]

# player's positions
default x = 0
default y = 0

default x_win = 2
default y_win = 3

# game field position and transform
default gf_align = (0.5, 0.2)

transform my_tr():
    zoom 1.5
    rotate -15

# images
image tile_1:
    Solid("#888")
    size(50, 50)
    
image token:
    Solid("#333")
    size(50, 50)
    
# game screen
screen game_field():
    
    # if player at the winning position
    if x == x_win and y == y_win:
        timer 0.01 action Jump("win_label") repeat False
    
    # if not yet - let him/her move the token
    else:
        key "focus_left" action SetVariable("x", max(x-1, 0))
        key "focus_right" action SetVariable("x", min(x+1, 2))
        key "focus_up" action SetVariable("y", max(y-1, 0))
        key "focus_down" action SetVariable("y", min(y+1, 3))
    
        # disallow further progress in game
        key "dismiss" action NullAction()
    
    # game field
    grid 3 4 spacing 2:
        align gf_align
        at my_tr
        
        for row in gf:
            for tile in row:
                if tile == 1:
                    add "tile_1"
                else:
                    null
                    
    # player's token
    grid 3 4 spacing 2:
        align gf_align
        at my_tr
        
        for py in range(4):
            for px in range(3):
                if py == y and px == x:
                    add "token"
                else:
                    null


    
# The game starts here.

label start:
    "..."
    show screen game_field with dissolve
    "Use arrow keys to move."
    "?!"
    
label win_label:
    "Well done!"
    hide screen game_field with dissolve
    "... ..."
    return
https://www.renpy.org/doc/html/screens.html#grid
https://www.renpy.org/doc/html/screens.html#key
https://www.renpy.org/doc/html/keymap.html#keymap
https://www.renpy.org/doc/html/screens.html#timer
https://www.renpy.org/doc/html/displayables.html#Solid
https://www.renpy.org/doc/html/screen_actions.html#Jump
https://www.renpy.org/doc/html/screen_a ... etVariable
https://www.renpy.org/doc/html/screen_a ... NullAction
https://www.renpy.org/doc/html/atl.html#atl

Post Reply

Who is online

Users browsing this forum: Alex, Google [Bot]