Jigsaw Puzzle Help [Solved!]

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.
Message
Author
Gushi
Newbie
Posts: 24
Joined: Sun Apr 01, 2018 3:25 pm
Projects: Project 4th Wall
itch: https://umberbridge.
Contact:

Jigsaw Puzzle Help [Solved!]

#1 Post by Gushi »

Okay so I wanted to put a puzzle like this one into my game (The second one not the first):
https://lemmasoft.renai.us/forums/view ... 1#p267460

"j" is supposed to be the y and "I" is supposed to be the x, I'd suggest you look at the code yourself if you need further understanding of how the code works. if anybody is willing to help me out it would be greatly appreciated.

I've managed to get the game working up until it calls the screen, when I run into this traceback error

Code: Select all

Full traceback:
  File "game/event rom start.rpy", line 641, in script
    python:
  File "/Users/Gushi/Downloads/renpy-6.99.14.1-sdk/renpy/ast.py", line 848, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Users/Gushi/Downloads/renpy-6.99.14.1-sdk/renpy/python.py", line 1812, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/event rom start.rpy", line 647, in <module>
    piecelist[i,j] = [renpy.random.randint(0, 600)+600, renpy.random.randint(0, 480)]
NameError: name 'j' is not defined
Last edited by Gushi on Tue Apr 03, 2018 12:17 pm, edited 1 time in total.

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Jigsaw Puzzle Help

#2 Post by Milkymalk »

Well I can only guess that "NameError: name 'j' is not defined" means that you never told the program what j is supposed to be. Without the screen code, it's all guesswork.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Gushi
Newbie
Posts: 24
Joined: Sun Apr 01, 2018 3:25 pm
Projects: Project 4th Wall
itch: https://umberbridge.
Contact:

Re: Jigsaw Puzzle Help

#3 Post by Gushi »

How would I do that exactly? I've tried

Code: Select all

 default my_x = "j"
but that didn't do anything, heres the code that I'm trying to use:

Code: Select all

init python:
    
    def piece_dragged(drags, drop):
        
        if not drop:
            return
        
        p_x = drags[0].drag_name[0]
        p_y = drags[0].drag_name[1]
        t_x = drop.drag_name[0]
        t_y = drop.drag_name[1]
        
        if p_x == t_x and p_y == t_y:
            renpy.music.play("Pickup_Coin.ogg", channel="sound")
            my_x = int(p_x)*100+25
            my_y = int(p_y)*100+25
            drags[0].snap(my_x,my_y)
            drags[0].draggable = False
            placedlist[int(p_x),int(p_y)] = True
            for i in range(6):
                for j in range(4):
                    if placedlist[i,j] == False:
                        return
            return True
        return
        
screen jigsaw:
    
    draggroup:
        
        for i in range(6):
            for j in range(4):
                $ name = "%s%s"%(i,j)
                $ my_x = i*100+50
                $ my_y = j*100+50
                drag:
                    drag_name name
                    child "blank_space.png"
                    draggable False
                    xpos my_x ypos my_y
            
            
        for i in range(6):
            for j in range(4):
                $ name = "%s%s piece"%(i,j)
                drag:
                    drag_name name
                    child imagelist[i,j]
                    droppable False
                    dragged piece_dragged
                    xpos piecelist[i,j][0] ypos piecelist[i,j][1] 
this code goes before the label start (which I did), and heres the second bit

Code: Select all

label puzzle:
    call screen jigsaw
    jump win

label start:
    scene black
    image whole = "ShizukaClassroom_6X4.jpg"
    python:
        mainimage = im.Composite((650, 450),(25, 25), "ShizukaClassroom_6X4.jpg")
        piecelist = dict()
        imagelist = dict()
        placedlist = dict()
        for i in range(6):
            for j in range(4):
                piecelist[i,j] = [renpy.random.randint(0, 600)+600, renpy.random.randint(0, 480)]
                tempimage = im.AlphaMask(mainimage,"puzzle_pieces/%s_%s.png"%(j+1,i+1))
                imagelist[i,j] = im.Crop(tempimage, i*100,j*100, 150, 150)
                placedlist[i,j] = False
    jump puzzle 
my layout looks a little different since the puzzle only appears at another point in my game, rather than the start. This is just the code that the example game came with

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Jigsaw Puzzle Help

#4 Post by Milkymalk »

I think the problem is in the double indexing in piecelist[i,j]. If you want an index to be comprised of two values, you need to present them as a tuple. Try:

piecelist[(i,j)]

and the same with the other lists.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Gushi
Newbie
Posts: 24
Joined: Sun Apr 01, 2018 3:25 pm
Projects: Project 4th Wall
itch: https://umberbridge.
Contact:

Re: Jigsaw Puzzle Help

#5 Post by Gushi »

It's still coming up with the same error message, j is not defined.

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: Jigsaw Puzzle Help

#6 Post by Lord Hisu »

Milkymalk wrote: Mon Apr 02, 2018 2:00 pm I think the problem is in the double indexing in piecelist[i,j]. If you want an index to be comprised of two values, you need to present them as a tuple.
This is even possible in Python? I never heard of it.

I think...

Code: Select all

piecelist[i][j]
...is what you want. Remember that i and j only exist inside for loops.
But I think this is not the only problem. I can't test it right now, though.

Gushi
Newbie
Posts: 24
Joined: Sun Apr 01, 2018 3:25 pm
Projects: Project 4th Wall
itch: https://umberbridge.
Contact:

Re: Jigsaw Puzzle Help

#7 Post by Gushi »

I tested it and this came up

Code: Select all

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

Full traceback:
  File "game/event rom start.rpy", line 641, in script
    python:
  File "/Users/Gushi/Downloads/renpy-6.99.14.1-sdk/renpy/ast.py", line 848, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Users/Gushi/Downloads/renpy-6.99.14.1-sdk/renpy/python.py", line 1812, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/event rom start.rpy", line 647, in <module>
    piecelist[i][j] = [renpy.random.randint(0, 600)+600, renpy.random.randint(0, 480)]
KeyError: 0 
also, I have no idea. I'm somewhat of a beginner so this code is probably more advanced. Also the version of renpy the code was sent with is outdated I think. So I'm unsure if its even usable or not..

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Jigsaw Puzzle Help

#8 Post by Milkymalk »

Lord Hisu wrote: Mon Apr 02, 2018 6:14 pm
Milkymalk wrote: Mon Apr 02, 2018 2:00 pm I think the problem is in the double indexing in piecelist[i,j]. If you want an index to be comprised of two values, you need to present them as a tuple.
This is even possible in Python? I never heard of it.
It is possible because it is a dictionary. A dictionary maps "anything" to "anything". So you could map a string to a list, a number to another dictionary or even a tuple to a displayable.

Code: Select all

piecelist[i][j]
would mean "take element number i from piecelist and then take element number j from whatever is element number i". This is, for example, if you have a list with lists inside.
piecelist is, contrary to that, a dictionary of all jigsaw pieces, which are identified by their x and y coordinates in the finished picture. So they have to be identified by two values at once, hence a tuple.

Why the error persists is not clear to me. I will look at the code real closely.

Question: Is the error in the exact same line? If not, did you change all [i,j] to [(i,j)]?
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Gushi
Newbie
Posts: 24
Joined: Sun Apr 01, 2018 3:25 pm
Projects: Project 4th Wall
itch: https://umberbridge.
Contact:

Re: Jigsaw Puzzle Help

#9 Post by Gushi »

Yes, its in the same line.

philat
Eileen-Class Veteran
Posts: 1911
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Jigsaw Puzzle Help

#10 Post by philat »

Nobody can help you if you don't actually post what you did. The example project itself runs fine, so the example code isn't the problem.

Gushi
Newbie
Posts: 24
Joined: Sun Apr 01, 2018 3:25 pm
Projects: Project 4th Wall
itch: https://umberbridge.
Contact:

Re: Jigsaw Puzzle Help

#11 Post by Gushi »

I put this bit of code in my game, before the label "start"

Code: Select all

init python:
    
    def piece_dragged(drags, drop):
        
        if not drop:
            return
        
        p_x = drags[0].drag_name[0]
        p_y = drags[0].drag_name[1]
        t_x = drop.drag_name[0]
        t_y = drop.drag_name[1]
        
        if p_x == t_x and p_y == t_y:
            renpy.music.play("Pickup_Coin.ogg", channel="sound")
            my_x = int(p_x)*100+25
            my_y = int(p_y)*100+25
            drags[0].snap(my_x,my_y)
            drags[0].draggable = False
            placedlist[int(p_x),int(p_y)] = True
            for i in range(6):
                for j in range(4):
                    if placedlist[i,j] == False:
                        return
            return True
        return
        
screen jigsaw:
    
    draggroup:
        
        for i in range(6):
            for j in range(4):
                $ name = "%s%s"%(i,j)
                $ my_x = i*100+50
                $ my_y = j*100+50
                drag:
                    drag_name name
                    child "blank_space.png"
                    draggable False
                    xpos my_x ypos my_y
            
            
        for i in range(6):
            for j in range(4):
                $ name = "%s%s piece"%(i,j)
                drag:
                    drag_name name
                    child imagelist[i,j]
                    droppable False
                    dragged piece_dragged
                    xpos piecelist[i,j][0] ypos piecelist[i,j][1]
then, later down in another script file I put in this bit of code.

Code: Select all

label minigame:
    image whole = "Rom_puzzle_6x4"
    python:
        mainimage = im.Composite((650, 450),(25,25),"Rom_puzzle_6x4")
        piecelist = dict()
        imagelist = dict()
        placedlist = dict()
        for i in range(4):
            piecelist[i][j] = [renpy.random.randint(0, 600)+600, renpy.random.randint(0, 480)]
            tempimage = im.AlphaMask(mainimage,"puzzle_pieces/%s_%s.png"%(j+1,i+1))
            imagelist[i][j] = im.Crop(tempimage, i*100,j*100, 150, 150)
            placedlist[i][j] = False
    jump puzzle 
the only thing that I changed was the full image name.

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Jigsaw Puzzle Help

#12 Post by Milkymalk »

I just tried it out, for dictionaries [a,b] as index is treated the same as [(a,b)]. I'm surprised that Python (at least in version 3.x) corrects this by itself, actually.

There's the error: You didn't make a for-loop with j, only a loop with i.

Code: Select all

label minigame:
    image whole = "Rom_puzzle_6x4"
    python:
        mainimage = im.Composite((650, 450),(25,25),"Rom_puzzle_6x4")
        piecelist = dict()
        imagelist = dict()
        placedlist = dict()
        for i in range(6):
            for j in range(4):  #  <------------------ THIS
                piecelist[i,j] = [renpy.random.randint(0, 600)+600, renpy.random.randint(0, 480)]
                tempimage = im.AlphaMask(mainimage,"puzzle_pieces/%s_%s.png"%(j+1,i+1))
                imagelist[i,j] = im.Crop(tempimage, i*100,j*100, 150, 150)
                placedlist[i,j] = False
    jump puzzle 
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
Lord Hisu
Regular
Posts: 58
Joined: Sun Feb 11, 2018 2:31 am
Contact:

Re: Jigsaw Puzzle Help

#13 Post by Lord Hisu »

Milkymalk wrote: Mon Apr 02, 2018 7:44 pm It is possible because it is a dictionary. A dictionary maps "anything" to "anything". So you could map a string to a list, a number to another dictionary or even a tuple to a displayable.
I just realized that I've been programming in Python for almost 6 years and never really used anything besides integers and strings to index dictionaries. :lol:

Gushi
Newbie
Posts: 24
Joined: Sun Apr 01, 2018 3:25 pm
Projects: Project 4th Wall
itch: https://umberbridge.
Contact:

Re: Jigsaw Puzzle Help

#14 Post by Gushi »

Okay that problem seems to be fixed, now its saying it cannot find the puzzle image, I tried specifying where the image was but this came up

Code: Select all

I'm sorry, but an uncaught exception occurred.

While loading <'Crop' <'AlphaMask' <'Composite' (650, 450) (25, 25) u'game/Rom_puzzle_6x4'> u'puzzle_pieces/1_1.png'> 0 0 150 150>:
  File "game/event rom start.rpy", line 636, in script
    call screen jigsaw
  File "renpy/common/000statements.rpy", line 485, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
IOError: Couldn't find file 'game/Rom_puzzle_6x4'.

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Jigsaw Puzzle Help

#15 Post by Milkymalk »

When you make your composite, the game expects a filename, not a Ren'Py displayable/image. The way you did it, it's looking for a file called 'Rom_puzzle_6x4' in the game's main folder.
Lord Hisu wrote: Mon Apr 02, 2018 9:31 pmI just realized that I've been programming in Python for almost 6 years and never really used anything besides integers and strings to index dictionaries. :lol:
I don't think I ever used anything but integers and strings for that either, but I was aware that it's possible to use anything you want as the index. But I don't know if mapping a displayable or a function to something in a dictionary has any practical applications :D
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Post Reply

Who is online

Users browsing this forum: No registered users