Help on using the renpy.get_mouse_pos()

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
HazeE
Regular
Posts: 26
Joined: Sun Feb 19, 2017 4:52 am
Contact:

Help on using the renpy.get_mouse_pos()

#1 Post by HazeE »

Hey!! So I stumbled upon a function called "renpy.get_mouse_pos()" and base from description, it returns a tuple containing the x and y coordinates of where the mouse was clicked at. The documentation only has this description and so I have a few questions about it:

1. When the player clicks the mouse, does renpy.get_mouse_pos() get called automatically?
2. Does renpy.get_mouse_pos() update the value of the tuple everytime the player clicks the mouse?
3. What happens if the player decides to click and drag the mouse?
4. Is it possible to split the tuple into 2 different variables?

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Help on using the renpy.get_mouse_pos()

#2 Post by saguaro »

If you watch renpy.get_mouse_pos() in the console (shift+O), it updates whenever the mouse button is released. So if I click and drag, it gives me the ending position. You can assign the coordinates to separate variables like

x, y = renpy.get_mouse_pos()

or

x = renpy.get_mouse_pos()[0]
y = renpy.get_mouse_pos()[1]

HazeE
Regular
Posts: 26
Joined: Sun Feb 19, 2017 4:52 am
Contact:

Re: Help on using the renpy.get_mouse_pos()

#3 Post by HazeE »

Oh, thank you for answering my questions :D

I'm trying to make a game where the player has to find a certain object in the background. The background is just one picture so I would know if the player chose the correct object in the background by checking its mouse position when clicked. Right now the game is working but it's not going the way I want it to... Maybe you could check if there's something wrong in my code?

show bgBeach with fade
$ oldx = 0 #I would like to check if the player chooses the right object everytime he clicks the mouse.
$ oldy = 0 #So I was thinking of using variables to check whether if they clicked a new area or not.
$ newx = 0
$ newy = 0
$ sx = 163 #Starting and ending x coordinate of the correct object in the background.
$ ex = 187 #
$ sy = 431 # Starting and ending y coordinate of the correct object in the background.
$ ey = 569 #

$ newx = renpy.get_mouse_pos()[0] #getting the x when clicked.
$ newy = renpy.get_mouse_pos()[1] #Getting the y when clicked.
if oldx != newx and oldy != newy: #If the player clicked on a different area than the previous.
$ oldx = newx #Updates the variables.
$ oldy = newy #
if (newx >= sx and newy >= sy) and (newx <= ex and newy <= ey): #If the area clicked is within the boundary of the object.
"You did it!" #If the correct object is clicked.
else:
"Try again." #If the wrong object is clicked.

Actually I want it to say "Try again" then let the player click another area. So it could be in a loop until the right object is found.

The output I'm seeing right now is that when I start my program, it automatically says "Try again" and when I try to click any area, the game closes itself. Any suggestions?

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Help on using the renpy.get_mouse_pos()

#4 Post by Divona »

Wouldn't use Imagemap be easier?

If you insist in manually check mouse position, you have to do it in Screen Language, as "label" aren't loop and mouse position need to be constantly checking every frame.
Last edited by Divona on Sat May 06, 2017 1:03 am, edited 1 time in total.
Completed:
Image

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Help on using the renpy.get_mouse_pos()

#5 Post by trooper6 »

Either an imagemap or an image with image buttons.
This is an old thread, but it could serve as a starting place: viewtopic.php?f=51&t=20541
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

HazeE
Regular
Posts: 26
Joined: Sun Feb 19, 2017 4:52 am
Contact:

Re: Help on using the renpy.get_mouse_pos()

#6 Post by HazeE »

Thanks for the suggestions Divona and trooper6!! :D forgive me but I forgot to mention that I also want to count each and every time the player clicks the wrong area, that is why I thought of using renpy.get_mouse_pos(). Also all the objects are compressed so it's just one whole image with the background.
Hmm... for image buttons, do you think we can code something like "if not clicked image button"?
But again, thank you for your suggestions~! :D

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Help on using the renpy.get_mouse_pos()

#7 Post by Divona »

Here is the code:

Code: Select all

default fail_count = 3

init python:
    def CheckRect(x, y, w, h):
        mouse_x, mouse_y = renpy.get_mouse_pos()

        if mouse_x >= x and mouse_x <= (x + w) and mouse_y >= y and mouse_y <= (y + h):
            return True
        else:
            return False

screen search_object():
    default object_found = False
    modal True

    # For debug purpose: show rect area on screen.
    add "#FF0000" alpha 0.5 pos (163, 187) size (320, 320)

    timer 0.1 repeat True action SetScreenVariable("object_found", CheckRect(163, 187, 320, 320))

    key "mouseup_1" action If(
        object_found,
        true = [ Hide("search_object"), Jump("success") ],
        false = [ SetVariable("fail_count", fail_count - 1), Jump("fail") ]
    )

label start:

    scene bg room

    show eileen happy
    "Let's find hidden object!"

    hide eileen

    $ fail_count = 3
    show screen search_object
    "Can you find it?"

label fail:

    if fail_count > 1:
        "That's not it. Try again. You have [fail_count] chances left."
    elif fail_count == 1:
        "Come on. This is your last chance!"
    else:
        hide screen search_object

        show eileen sad
        "That's it. Game over."

        return

label success:

    show eileen happy
    "You did it!"

    return
Completed:
Image

HazeE
Regular
Posts: 26
Joined: Sun Feb 19, 2017 4:52 am
Contact:

Re: Help on using the renpy.get_mouse_pos()

#8 Post by HazeE »

Divona!!! Thank you so much for posting your code, it helps a lot especially I'm still not used to Renpy yet with all the screens and such TwT XD
Though is it okay to ask for more of your help?? ^^;

I added a timer in the search_object screen. I saw the code from the wiki and it works fine too :D How do you incorporate the timer with the clicks of the player? Like for every wrong click, 3 seconds is taken away from the timer. I also wanted to change the color of the timer with green: full time or less, yellow: half or less, red: 10 seconds left or less. I'm not totally sure how to put if statements in screens yet.
And lastly, is it possible to put what eileen says in something like a tab? The player could have a header saying "Eileen" then when clicked, it would slide upwards showing what she is saying. And when the tab is clicked again, it would slide down again to avoid getting in the way of the background.

I know I'm asking much... I hope you or anyone can respond ^^
Attachments
halp.JPG

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Help on using the renpy.get_mouse_pos()

#9 Post by Divona »

Is there a reason you use "time" instead of just decrease the variable? Here is a simple version without the need to import Python library:

Code: Select all

screen search_object(timer_range, timer_jump):
    default object_found = False
    default bar_colour = "#00FF00"
    default countdown_timer = timer_range
    modal True

    # For every one second, decrease countdown timer by one second. Once reach zero, do the jump.
    timer 1.0 repeat True action If(
        countdown_timer > 0,
        true = SetScreenVariable("countdown_timer", countdown_timer - 1),
        false = [ Hide("countdown"), Jump(timer_jump) ]
    )

    # Change colour of the bar according to the percentage of the time left.
    python:
        if countdown_timer > (timer_range * 0.5):
            bar_colour = "#00FF00"
        elif countdown_timer <= (timer_range * 0.5) and countdown_timer > (timer_range * 0.2):
            bar_colour = "#FFE400"
        else:
            bar_colour = "#FF0000"

    # Display countdown timer bar on screen.
    bar at notify_appear:
        xalign 0.5
        yalign 0.9
        xmaximum 300
        value countdown_timer
        range timer_range
        left_bar Solid(bar_colour)

    # Check for mouse position and see if it hover over the rect area (object).
    timer 0.1 repeat True action SetScreenVariable("object_found", CheckRect(163, 187, 320, 320))

    # When mouse left button is released, do it things.
    key "mouseup_1" action If(
        object_found,
        true = [ Hide("search_object"), Jump("success") ],
        false = [
            SetScreenVariable("countdown_timer", countdown_timer - 3),        # remove 3 seconds out from countdown timer.
            SetVariable("fail_count", fail_count - 1),
            Jump("fail")
        ]
    )
For the tab thing, you could make another part of the screen using Showif Statement, or another screen to do that. It would be easier than messing with say dialogue box (screen say) itself.
Completed:
Image

HazeE
Regular
Posts: 26
Joined: Sun Feb 19, 2017 4:52 am
Contact:

Re: Help on using the renpy.get_mouse_pos()

#10 Post by HazeE »

The reason I used time instead of a variable is because when I first tried running it, somehow the timer would last longer than the given time. But trying it again, it runs the exact given time now, weird haha. Thanks for your help Divona!! :D I'll look into the ShowIf statement for the tab ^^

Though I would like to take the dialogue box out when the game starts or when the player clicks the wrong area. Instead of jumping it to fail, I would want it to play a song that's about 1 second. It's like a beep indicating that it is wrong. Is it possible to let that sound play then resume to the original song?
When I tried removing the jump to fail, the game closes by itself once I start it, any suggestions??

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Help on using the renpy.get_mouse_pos()

#11 Post by Divona »

Use "call screen" instead of "show screen" to have screen show over the dialogue box.

Use renpy.play() to play sound effect.
Completed:
Image

HazeE
Regular
Posts: 26
Joined: Sun Feb 19, 2017 4:52 am
Contact:

Re: Help on using the renpy.get_mouse_pos()

#12 Post by HazeE »

The dialogue box is gone, thanks Divona!! :D
And I have a few more questions about the code earlier and about the ifShow if it's alright with you ^^;

I noticed that the timer doesn't decrease as smoothly as before. Like right now, it doesn't have that motion of decreasing, the timer is disappearing by chunks at a time. Is it possible to make it go smoothly again?
For the ifShow statement, I want to use renpy.notify() to bring out the hint for 5 seconds, afterwards it disappears but I think I'm doing it wrong? I'm not sure where or how is it supposed to be placed XD
And lastly, is this how you play the renpy.play() for sound effects? Did I place it right?

Thanks for your reply btw Divona :D TwT
Attachments
q.JPG

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Help on using the renpy.get_mouse_pos()

#13 Post by Divona »

The countdown timer bar wasn't designed to do smooth animation from the start. To have it animated smoothly, it need a redesign. Perhaps something like make the bar range 100 and decrease it down by "timer_range" divided by 100.

On notify, best to show it as another screen rather than using renpy.notify, as it will have issue with current screen.

Code: Select all

default countdown_value = 100
default countdown_timer = float(timer_range) / countdown_value

timer countdown_timer repeat True action If(
    countdown_value > 0,
    true = SetScreenVariable("coundown_value", countdown_value - 1),
    false = [ Hide("countdown"), Jump(timer_jump) ]
)

python:
    if countdown_timer > (100 * 0.5):
        bar_colour = "#00FF00"
    elif countdown_timer <= (100 * 0.5) and countdown_timer > (100 * 0.2):
        bar_colour = "#FFE400"
    else:
        bar_colour = "#FF0000"

    # Show notify hint.
    if hint == 1:
        hint = 0
        Show("notify", message="Hey")()

bar at notify_appear:
    xalign 0.5
    yalign 0.9
    xmaximum 300
    value countdown_value
    range 100
    left_bar Solid(bar_colour)
Completed:
Image

HazeE
Regular
Posts: 26
Joined: Sun Feb 19, 2017 4:52 am
Contact:

Re: Help on using the renpy.get_mouse_pos()

#14 Post by HazeE »

I got an error when I tried your new code ^^; When the game started, the timer wasn't moving and it was color red even though it was still full. And when I clicked an area, an exception popped out. The exception said something that the delay should be grater than 0.
I'm not sure what I should change XD

And as usual, thanks for your help Divona!! :D
Attachments
error.JPG

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Help on using the renpy.get_mouse_pos()

#15 Post by Divona »

Ah, I thought you have some understanding of the concept already, so didn't write a whole code. There still some changes need to be done with the time reduce of the click. You need to change from decrease the time in "countdown_timer" to "countdown_value". We switch things around.

It was my bad forgot to change the variable use to check when to change bar colour. It needs to change from countdown_timer to coundown_value.

Let me dump a whole thing here. Next time please copy and paste the code here instead of a screenshot. I have to retype everything every time just to edit little things.

Code: Select all

screen search_object(timer_range, timer_jump):
    default object_found = False
    default bar_color = #00FF00
    default countdown_range = 100
    default countdown_value = countdown_range

    # Calculate the time it take to decrease countdown_value by one.
    default countdown_timer = float(timer_range) / countdown_value

    # Calculate the value it need to cut out from countdown_value when fail.
    default fail_cut = 3 / countdown_timer
    
    modal True

    # For debug purpose: show mouse position and rect area on screen.
    add "#FF0000" pos (163, 187) size (320, 320) alpha 0.5

    # For every countdown_timer value, decrease countdown value by one. Once reach zero, do the jump.
    timer countdown_timer repeat True action If(
        countdown_value > 0,
        true = SetScreenVariable("countdown_value", countdown_value - 1),
        false = [ Hide("countdown"), Jump(timer_jump) ]
    )

    # Change colour of the bar.
    python:
        if countdown_value > (countdown_range * 0.5):
            bar_colour = "#00FF00"
        elif countdown_value <= (countdown_range * 0.5) and countdown_value >= (countdown_range * 0.2):
            bar_colour = "#FFE400"
        else:
            bar_colour = "#FF0000"

    # Display countdown timer bar on screen.
    bar at notify_appear:
        xalign 0.5
        yalign 0.9
        xmaximum 300
        value countdown_value
        range countdown_range
        left_bar Solid(bar_colour)

    # Check for mouse position, and see if it over rect area.
    timer 0.1 repeat True action [
        SetScreenVariable("object_found", CheckRect(163, 187, 320, 320))
    ]

    # When mouse left button is released, do it things.
    key "mouseup_1" action If(
        object_found,
        true = [ Hide("search_object"), renpy.play(correct.mp3), Jump("success") ],
        false = [
            SetScreenVariable("countdown_value", countdown_value - fail_cut),
            SetVariable("fail_count", fail_count - 1),
            renpy.play(fail.mp3)
        ]
    )

    # Hint notification.
    vbox:
        textbutton "HINT" action Show("notify", message="Hello World")
Completed:
Image

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]