Multiple conditions to win with drag and drop game [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.
Post Reply
Message
Author
User avatar
Kowai
Regular
Posts: 50
Joined: Thu Feb 25, 2016 6:33 am
Tumblr: kowaikowia
Deviantart: kowai-usagi
Contact:

Multiple conditions to win with drag and drop game [SOLVED]

#1 Post by Kowai »

Hi everyone!

So I'm a newbie in this community and a complete noob at Ren'py and especially Python, so I crossed a problem while trying to code a drag and drop game.

The screen looks like this (the 16 drops' area is grey for visibility purpose):
Image

The aim of the game is simple: One has to get the jewels on the left on the correct positions using the hints provided by the grid.

The code currently looks like this:

Code: Select all

init python:

    p1place1 = "false"
    p1place2 = "false"

    def bdragged(drags, drop):

        if not drop:
            return

        store.butt = drags[0].drag_name
        store.place = drop.drag_name

        if butt == "jewel" and place == "01":
            p1place1 = "true"
        else:
            p1place1 = "false"

        if butt == "jewel" and place == "03":
            p1place2 = "true"
        else:
            p1place2 = "false"

        if p1place1 == "true" and p1place2 == "true":
            return True
        else:
            pass


screen puzzle1:
    tag menu

    window:
        background "Puzzle/Puzzle_01.png" xpos -16 ypos 0.0 xanchor 0.0 yanchor 0.0

    draggroup:
        drag:
            drag_name "jewel"
            child "Button/Button_Jewell.png"
            droppable False
            dragged bdragged
            xpos 68
            ypos 100
            xanchor 0.5
            yanchor 0.5

        drag:
            drag_name "jewel"
            child "Button/Button_Jewell.png"
            droppable False
            dragged bdragged
            xpos 68
            ypos 160
            xanchor 0.5
            yanchor 0.5

        # Repeated until there are 6 drags

        drag:
            drag_name "01"
            child "Button/Empty2.png"
            draggable False
            xpos 348
            ypos 77
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "02"
            child "Button/Empty2.png"
            draggable False
            xpos 299
            ypos 163
            xanchor 0.0
            yanchor 0.0

        # Repeated until there are 16 drops
So, my problem is with the very first part of the code.

While this works and get me back to the story mode:

Code: Select all

    def bdragged(drags, drop):

        if not drop:
            return

        store.butt = drags[0].drag_name
        store.place = drop.drag_name

        if butt == "jewel" and place == "01":
            p1place1 = "true"
        else:
            p1place1 = "false"

        if p1place1 == "true":
            return True
        else:
            pass
As soon as I add another condition (like I did in the first code excerpt), nothing happens.

What am I doing wrong? Knowing I have to make ALL six conditions "true" TOGETHER for the game to be won.

Here is a link to a zip of the game with the assets: https://www.dropbox.com/s/ic1ofiplkzitw ... 2.zip?dl=0
Last edited by Kowai on Fri Mar 11, 2016 3:31 pm, edited 2 times in total.

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Multiple conditions to win with drag and drop game

#2 Post by gas »

Hi my BASIC friend!
Python manage IF statements his own way. So, you need to use the ELIF statement for more than a couple of conditions. IF... ELIF...ELIF...ELSE.
ELSE must be the last one.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
Kowai
Regular
Posts: 50
Joined: Thu Feb 25, 2016 6:33 am
Tumblr: kowaikowia
Deviantart: kowai-usagi
Contact:

Re: Multiple conditions to win with drag and drop game

#3 Post by Kowai »

The problem with elif, as I understand it, is that it's going to check if any of the statements is true and execute a command. But what I want is to have all the conditions true, not just one. I can't wrap my mind around how this elif can help me in this case.

User avatar
D_first
Newbie
Posts: 1
Joined: Fri Mar 11, 2016 11:33 am
Contact:

Re: Multiple conditions to win with drag and drop game

#4 Post by D_first »

puzzle1

Code: Select all

#### Puzzle game1
init python:

    class Jewel():
        def __init__(self, posX, posY):
            self.posX = posX
            self.posY = posY
            self.place = None

    def bdragged(drags, drop):

        if not drop:
            return
        jewels[drags[0].drag_name].place = drop.drag_name

        return check()

    def check():
        places = {x.place for x in jewels.values()}
        if {'01', '03'}.issubset(places):
            return True

        return

screen puzzle1:
    # This ensures that any other menu screen is replaced.
    tag menu

#################### LAYOUT #####################

    window:
        background "Puzzle/Puzzle_01.png" xpos -16 ypos 0.0 xanchor 0.0 yanchor 0.0

    hbox:
        style_group "puzzle"

        image "Box/Box_Jewel.png" xpos 16 ypos 10 xanchor 0.0 yanchor 0.0

        vbox:
            xpos 700
            xanchor 1.0
            ypos 10

            vbox:
                xanchor 0.0
                # xpos 1.0
                image "Box/Box_Hint.png"

            vbox:
                xalign 0.9
                yalign 257
                yanchor 1.0
                spacing 5

                textbutton _("Hint")
                textbutton _("Back") action Return()

#################### DRAGGABLE #####################

    draggroup:
        for key, value in jewels.items():
            drag:
                drag_name key
                child "Button/Button_Jewell.png"
                droppable False
                dragged bdragged
                xpos value.posX
                ypos value.posY
                xanchor 0.5
                yanchor 0.5

        drag:
            drag_name "01"
            child "Button/Empty2.png"
            draggable False
            xpos 348
            ypos 77
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "02"
            child "Button/Empty2.png"
            draggable False
            xpos 299
            ypos 163
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "03"
            child "Button/Empty2.png"
            draggable False
            xpos 348
            ypos 163
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "04"
            child "Button/Empty2.png"
            draggable False
            xpos 397
            ypos 163
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "05"
            child "Button/Empty2.png"
            draggable False
            xpos 250
            ypos 249
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "06"
            child "Button/Empty2.png"
            draggable False
            xpos 299
            ypos 249
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "07"
            child "Button/Empty2.png"
            draggable False
            xpos 348
            ypos 249
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "08"
            child "Button/Empty2.png"
            draggable False
            xpos 397
            ypos 249
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "09"
            child "Button/Empty2.png"
            draggable False
            xpos 446
            ypos 249
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "10"
            child "Button/Empty2.png"
            draggable False
            xpos 201
            ypos 335
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "11"
            child "Button/Empty2.png"
            draggable False
            xpos 250
            ypos 335
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "12"
            child "Button/Empty2.png"
            draggable False
            xpos 299
            ypos 335
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "13"
            child "Button/Empty2.png"
            draggable False
            xpos 348
            ypos 335
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "14"
            child "Button/Empty2.png"
            draggable False
            xpos 397
            ypos 335
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "15"
            child "Button/Empty2.png"
            draggable False
            xpos 446
            ypos 335
            xanchor 0.0
            yanchor 0.0

        drag:
            drag_name "16"
            child "Button/Empty2.png"
            draggable False
            xpos 495
            ypos 335
            xanchor 0.0
            yanchor 0.0

#################### MORE LAYOUT #####################

init -2:
    style puzzle_button:
        size_group "puzzle"

    style puzzle_button_text:
        xpos 0.9
        xanchor 1.0
        size 24
        insensitive_color "#000"

    style puzzle_button is button:
        background "Button/Button_Menu1.png"
        hover_background "Button/Button_Menu2.png"
        insensitive_background "Button/Button_Menu3.png"
        xminimum 211
        yminimum 53
script

Code: Select all

define e = Character('Character')


# The game starts here.
label start:
    python:
        jewels = {}
        for i, y in enumerate(range(100, 401, 60), 1):
            jewels['jewel'+str(i)] = Jewel(68, y)

menu:
    e "Do you want to play a game?"

    "Yes!":
        call screen puzzle1

        e "That was a nice game!"

    "No!":
        e "Okay, my bad..."

label after_menu:

    return

User avatar
Kowai
Regular
Posts: 50
Joined: Thu Feb 25, 2016 6:33 am
Tumblr: kowaikowia
Deviantart: kowai-usagi
Contact:

Re: Multiple conditions to win with drag and drop game

#5 Post by Kowai »

Thank you very much D_first! This works like a charm!

Post Reply

Who is online

Users browsing this forum: Sugar_and_rice