Problems understanding Puzzles

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
chaos marauder
Newbie
Posts: 5
Joined: Thu Dec 20, 2018 8:38 pm
Contact:

Problems understanding Puzzles

#1 Post by chaos marauder »

Hello! I'm someone who has taken programming as a hobby and I'm still struggling with some concepts that might be basic here. Basing myself on this thread viewtopic.php?f=8&t=12504 I decided to make my own puzzle. PROBLEM is that there are certain things I can't understand. For example:

Code: Select all

screen jigsaw:

    draggroup:

        drag:
            drag_name "00"
            child "darkness.jpeg"
            draggable False
            xpos coorlistx[0] ypos coorlisty[0]

        drag:
            drag_name "01"
            child "darkness.jpeg"
            draggable False
            xpos coorlistx[0] ypos coorlisty[1]

        drag:
            drag_name "02"
            child "darkness.jpeg"
            draggable False
            xpos coorlistx[0] ypos coorlisty[2]

        drag:
            drag_name "10"
            child "darkness.jpeg"
            draggable False
            xpos coorlistx[1] ypos coorlisty[0]

        drag:
            drag_name "11"
            child "darkness.jpeg"
            draggable False
            xpos coorlistx[1] ypos coorlisty[1]

        drag:
            drag_name "12"
            child "darkness.jpeg"
            draggable False
            xpos coorlistx[1] ypos coorlisty[2]

        drag:
            drag_name "20"
            child "darkness.jpeg"
            draggable False
            xpos coorlistx[2] ypos coorlisty[0]

        drag:
            drag_name "21"
            child "darkness.png"
            draggable False
            xpos coorlistx[2] ypos coorlisty[1]

        drag:
            drag_name "22"
            child "darkness.png"
            draggable False
            xpos coorlistx[2] ypos coorlisty[2]

        drag:
            drag_name "30"
            child "empty space.png"
            draggable False
            xpos coorlistx[3] ypos coorlisty[0]

        drag:
            drag_name "31"
            child "empty space.png"
            draggable False
            xpos coorlistx[3] ypos coorlisty[1]


        drag:
            drag_name "00 piece"
            child im.Crop("puzzle1.jpg", 0,0, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[0][0] ypos piecelist[0][1]

        drag:
            drag_name "01 piece"
            child im.Crop("puzzle1.jpg", 200,0, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[1][0] ypos piecelist[1][1]

        drag:
            drag_name "02 piece"
            child im.Crop("puzzle1.jpg", 400,0, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[2][0] ypos piecelist[2][1]

        drag:
            drag_name "03 piece"
            child im.Crop("puzzle1.jpg", 600,0, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[3][0] ypos piecelist[3][1]

        drag:
            drag_name "04 piece"
            child im.Crop("puzzle1.jpg", 800,0, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[4][0] ypos piecelist[4][1]

        drag:
            drag_name "05 piece"
            child im.Crop("puzzle1.jpg", 0,250, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[5][0] ypos piecelist[5][1]

        drag:
            drag_name "06 piece"
            child im.Crop("puzzle1.jpg", 200,250, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[6][0] ypos piecelist[6][1]

        drag:
            drag_name "07 piece"
            child im.Crop("puzzle1.jpg", 400,250, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[7][0] ypos piecelist[7][1]

        drag:
            drag_name "08 piece"
            child im.Crop("puzzle1.jpg", 600,250, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[8][0] ypos piecelist[8][1]

        drag:
            drag_name "09 piece"
            child im.Crop("puzzle1.jpg", 800,250, 200,250)
            droppable False
            dragged piece_dragged
            xpos piecelist[9][0] ypos piecelist[9][1]

Code: Select all

label puzzle:
    call screen jigsaw
    if ([coorlistx[movedplace[0]], coorlisty[movedplace[1]]] in piecelist):
        python:
            t1 = piecelist[movedpiece]
            t2 = piecelist.index([coorlistx[movedplace[0]], coorlisty[movedplace[1]]])
            piecelist[movedpiece] = [coorlistx[movedplace[0]],coorlisty[movedplace[1]]]
            piecelist[t2] = t1
    else:
        $ piecelist[movedpiece] = [coorlistx[movedplace[0]],coorlisty[movedplace[1]]]
    if piecelist == [[coorlistx[0],coorlisty[0]],
                        [coorlistx[1],coorlisty[0]],
                        [coorlistx[2],coorlisty[0]],
                        [coorlistx[3],coorlisty[0]],
                        [coorlistx[0],coorlisty[1]],
                        [coorlistx[1],coorlisty[1]],
                        [coorlistx[2],coorlisty[1]],
                        [coorlistx[3],coorlisty[1]],
                        [coorlistx[0],coorlisty[2]],
                        [coorlistx[1],coorlisty[2]]]:
        jump win
    jump puzzle

Code: Select all

label puzzle1:
    scene black
    image whole = "puzzle1.jpg"
    python:
        coorlistx = [10, 130, 250, 370]
        coorlisty = [10, 217, 424]
        piecelist = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
        for i in range(12):
            x = renpy.random.randint(0, 59) + 621
            y = renpy.random.randint(0, 480)
            piecelist[i] = [x,y]
        movedpiece = 0
        movedplace = [0, 0]
    jump puzzle

I tried to modify the original to fit 10 pieces, cutting them into 200x250 pieces out of a 1000x500 picture (I probably screwed with the x and y coordinates, hard.) but mostly I want to understand about:
>The use of this and how exactly it clicks with each thing "xpos piecelist[3][0] ypos piecelist[3][1]"
> The reason why the "blank spaces" were named 00, 01, 02, 10, 11 was there something I missed?
And the reason why this error appears based on the code above:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/dekupuzzles.rpy", line 184, in script
    python:
  File "game/dekupuzzles.rpy", line 191, in <module>
    piecelist[i] = [x,y]
IndexError: list assignment index out of range

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/dekupuzzles.rpy", line 184, in script
    python:
  File "C:\MMHP\renpy-7.1.1-sdk\renpy\ast.py", line 882, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\MMHP\renpy-7.1.1-sdk\renpy\python.py", line 1913, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/dekupuzzles.rpy", line 191, in <module>
    piecelist[i] = [x,y]
  File "C:\MMHP\renpy-7.1.1-sdk\renpy\python.py", line 713, in do_mutation
    return method(self, *args, **kwargs)
IndexError: list assignment index out of range


User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
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-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Problems understanding Puzzles

#2 Post by Per K Grok »

chaos marauder wrote: Tue Jan 22, 2019 4:54 am
-----

Code: Select all

label puzzle1:
  ------ 
        piecelist = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
        for i in range(12):
            x = renpy.random.randint(0, 59) + 621
            y = renpy.random.randint(0, 480)
            piecelist[i] = [x,y]
            x = renpy.random.randint(0, 59) + 621
            y = renpy.random.randint(0, 480)
            piecelist[i] = [x,y]
 ------
---
And the reason why this error appears based on the code above:

Code: Select all

I'm sorry, but an uncaught exception occurred.

---
IndexError: list assignment index out of range



I can at least give you an answer to the error part

You have 10 posts in the list piecelist, but when you assign x and y values to each post you do so for 12 posts. So you try to assign values to posts in the list, that does not exist. That's where you get 'index out of range'.
Change to
for i in range(10):
and that should take care of that problem.

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

Re: Problems understanding Puzzles

#3 Post by Alex »


Post Reply

Who is online

Users browsing this forum: Sugar_and_rice