Page 1 of 1

[Solved] How to change images dynamically based on mouse position

Posted: Sun Jul 11, 2021 11:46 pm
by mirrabella
Hello! I am hoping someone can help me out understand dynamic displayables and conditional images logic better. I have spent multiple weeks in the cookbook and documentation, but it is possible and probable that I missed something!

What I am trying to do is show 2 different images based on where the mouse is positioned or what the score is (I posted my full code below). I have the mouse position function working and displaying the mouse current position in real time. I have a global score variable that increments based on the mouse position differently, and I want to have an image change either based on a score value or if the x/y are in a certain place.

I have not been able to make the images show over the background in any way. I have tried doing it with a screen or a label with no luck, and I think the switchCondition will be the cleanest option but it just wont change.

When I run the code below I see imgone.png showing up right away, and when I get to label end I see imagetwo.png, but I am unable to see image two when I move my mouse and see the score change to be over 10.

Any ideas how to approach this or what I am doing wrong? Thank you!

Code: Select all

define nar = Character("Narrator")
default x = 0
default y = 0
default score = 0

image scoreimage = ConditionSwitch(
        "score <= 10", "imgone.png",
        "score > 10", "imgtwo.png",
        "True", "imgthree.png")



init python:

    import math

    ## Get cursor position
    def dd_cursor_position(st, at):
        x, y = renpy.get_mouse_pos()

        if (x<=750 or (x>=750 and x<=1170 and y<=450) or x>=1170):
            globals()['score'] += 1
            renpy.hide("scoreimage")
            renpy.show("scoreimage")
            return Text("{size=-5}%d - %d: %d"%(x, y, score)), .1
        elif (x>=750 and x<=1070 and y>=450):
            globals()['score'] -= 1
            renpy.hide("scoreimage")
            renpy.show("scoreimage")
            return Text("{size=-5}%d - %d: %d"%(x, y, score)), .1

        return Text("{size=-5}%d - %d"%(x, y)), .1
       
## screen for the cursor position 
screen Mouse_Position():

    zorder 10**10
    add DynamicDisplayable(dd_cursor_position) xpos 10 yalign 0.99


label start:

    #test background to put images on top
    scene testing bkgd

    show screen Mouse_Position
 
    "'Space' when done"

    jump end


label end:
    nar "Your score is [score]"
    # This ends the game.

    return

Re: How to change images dynamically based on mouse position

Posted: Mon Jul 12, 2021 2:50 am
by Ocelot
This code shouold work:

Code: Select all

default score = 0

image scoreimage = ConditionSwitch(
        "score <= 10", Solid('#f00'),
        "score > 10", Solid('#0f0'),
        "True", Solid('#00f'))


init python:
    def dd_cursor_position(st, at):
        x, y = renpy.get_mouse_pos()

        if (x<=750 or (x>=750 and x<=1170 and y<=450) or x>=1170):
            store.score += 1
            return Text("{size=-5}%d - %d: %d"%(x, y, score)), .1
        elif (x>=750 and x<=1070 and y>=450):
            store.score -= 1
            return Text("{size=-5}%d - %d: %d"%(x, y, score)), .1

        return Text("{size=-5}%d - %d"%(x, y)), .1


screen Mouse_Position():
    zorder 10**10
    add DynamicDisplayable(dd_cursor_position) xpos 10 yalign 0.99
    timer 0.5 action Function(renpy.restart_interaction)


label start:
    scene testing bkgd
    show screen Mouse_Position
    show scoreimage
    "'Space' when done"
label end:
    "Your score is [score]"
    return
ConditionSwitch usually updates once per interaction, so it would not update until you advance the game in some way. If you want immediate chance, you will need to force interaction to occure. I did this by adding a timer which restarts interaction periodically. This is not the best way to do it, because it triggers a lot of unnessesary restarts, better way would be setting flag from within displayable when redraw is nessesary and checking it in timer, restarting interaction only when needed.

Re: How to change images dynamically based on mouse position

Posted: Wed Jul 21, 2021 9:00 pm
by mirrabella
Thank you @Ocelot for the reply and I am sorry for the delay on my end! I have been looking at what you sent and it still does not work for me. I see the red color on start, and once I tip over the 10 score I see the green, but it never goes back to red if the score goes below 10. I have to pres the "space" key for the color to change, but that also ends the game. Do you have any post where I can read more about "redraw". I have been having hard time wrapping my head around that.
Thanks!

Re: How to change images dynamically based on mouse position

Posted: Thu Jul 22, 2021 2:42 am
by Ocelot
Ok, I missed one thing:
Change timer 0.5 action Function(renpy.restart_interaction) to
timer 0.5 action Function(renpy.restart_interaction) repeat True

Re: How to change images dynamically based on mouse position

Posted: Sun Jul 25, 2021 3:08 pm
by mirrabella
That did it! THANK YOU so much!