Eyes following the mouse cursor in the main menu.

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
Diruker
Newbie
Posts: 2
Joined: Mon Dec 25, 2023 11:44 am
Contact:

Eyes following the mouse cursor in the main menu.

#1 Post by Diruker »

Okay I've been thinking, my game is kinda a psychological horror game and I wanted to remake the main menu because its a little.. bland. I have came up with an idea to make it a big image of the main antagonist with eyes following the cursor after an important thing has happened. The problem is.. I don't know how to make the eyes follow the mouse, and I can't find anything that useful on the internet, so I'm asking for help all of you. Any help is appreciated! :D

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1009
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Eyes following the mouse cursor in the main menu.

#2 Post by m_from_space »

Diruker wrote: Thu Dec 28, 2023 9:44 pm Any help is appreciated! :D
1. Create a screen that is also shown within the main_menu screen you will find in <screens.rpy> (or put all your code into the main menu)
2. Within that screen call renpy.get_mouse_pos() using a repeating timer of let's say 0.1 secs, which gets you the mouse coordinates
3. Calculate the vector from the position of the eyes to the mouse pos (it's actually just the difference of the x and y coordinates I think)
4. Calculate the angle of that vector and save the angle in a persistent variable
5. Use that persistent variable to define the rotation of the eyes

I hope that makes sense.

Diruker
Newbie
Posts: 2
Joined: Mon Dec 25, 2023 11:44 am
Contact:

Re: Eyes following the mouse cursor in the main menu.

#3 Post by Diruker »

m_from_space wrote: Fri Dec 29, 2023 9:44 am
Diruker wrote: Thu Dec 28, 2023 9:44 pm Any help is appreciated! :D
1. Create a screen that is also shown within the main_menu screen you will find in <screens.rpy> (or put all your code into the main menu)
2. Within that screen call renpy.get_mouse_pos() using a repeating timer of let's say 0.1 secs, which gets you the mouse coordinates
3. Calculate the vector from the position of the eyes to the mouse pos (it's actually just the difference of the x and y coordinates I think)
4. Calculate the angle of that vector and save the angle in a persistent variable
5. Use that persistent variable to define the rotation of the eyes

I hope that makes sense.
I will try to do that using this information, thanks!

User avatar
Sirifys-Al
Newbie
Posts: 9
Joined: Sat Apr 20, 2024 3:55 pm
Contact:

Re: Eyes following the mouse cursor in the main menu.

#4 Post by Sirifys-Al »

Diruker wrote: Fri Dec 29, 2023 10:18 am
m_from_space wrote: Fri Dec 29, 2023 9:44 am
Diruker wrote: Thu Dec 28, 2023 9:44 pm Any help is appreciated! :D
1. Create a screen that is also shown within the main_menu screen you will find in <screens.rpy> (or put all your code into the main menu)
2. Within that screen call renpy.get_mouse_pos() using a repeating timer of let's say 0.1 secs, which gets you the mouse coordinates
3. Calculate the vector from the position of the eyes to the mouse pos (it's actually just the difference of the x and y coordinates I think)
4. Calculate the angle of that vector and save the angle in a persistent variable
5. Use that persistent variable to define the rotation of the eyes

I hope that makes sense.
I will try to do that using this information, thanks!
Hello, did it work? If so, could you please share the code? I need the same thing, but I'm not good enough in coding for this.

User avatar
Andredron
Miko-Class Veteran
Posts: 728
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Eyes following the mouse cursor in the main menu.

#5 Post by Andredron »

Ai code

Code: Select all

.
init python:
    eye_angle = 0

screen Eyes():
    add "eyes.png" # Replace "eyes.png" with the name of your eye sprite
    
    timer 0.1 repeat True action Jump("update_eye_angle")

label update_eye_angle:
    python:
        mouse_x, mouse_y = renpy.get_mouse_pos()
        eye_x, eye_y = # Eye center coordinates (you can replace them with your own coordinates)
        dx = mouse_x - eye_x
        dy = mouse_y - eye_y
        if dx != 0:
            eye_angle = atan(dy / dx)
            if dx < 0:
                eye_angle += pi
        else:
            eye_angle = pi / 2 if dy > 0 else -pi / 2

    $ eye_angle = degrees(eye_angle)
    $ eye_angle = clamp(eye_angle, -30, 30) # Limit eye angle

    with open("eye_angle.txt", "w") as f:
        f.write(str(eye_angle))

    transform eye_transform:
        rotate eye_angle

    imagebutton:
        idle "eyes.png" # Replace "eyes.png" with the name of your eye sprite
        hovered "eyes_hovered.png" # Replace "eyes_hovered.png" with the name of your eye sprite on mouseover
        action Jump("update_eye_angle")
        at eye_transform # Apply the eye angle transformation

label start:
    scene black
    call screen Eyes

You have to draw the eyes in a specific way
Last edited by Andredron on Mon May 06, 2024 9:44 am, edited 1 time in total.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1009
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Eyes following the mouse cursor in the main menu.

#6 Post by m_from_space »

Andredron wrote: Sun May 05, 2024 4:35 pm

Code: Select all

.
init python:
    eye_angle = 0

screen Eyes():
    add "eyes.png" # Replace "eyes.png" with the name of your eye sprite
    
    timer 0.1 repeat True action Jump("update_eye_angle")

label update_eye_angle:
    python:
        mouse_x, mouse_y = renpy.get_mouse_pos()
        eye_x, eye_y = # Eye center coordinates (you can replace them with your own coordinates)
        dx = mouse_x - eye_x
        dy = mouse_y - eye_y
        if dx != 0:
            eye_angle = atan(dy / dx)
            if dx < 0:
                eye_angle += pi
        else:
            eye_angle = pi / 2 if dy > 0 else -pi / 2

    $ eye_angle = degrees(eye_angle)
    $ eye_angle = clamp(eye_angle, -30, 30) # Limit eye angle

    with open("eye_angle.txt", "w") as f:
        f.write(str(eye_angle))

    transform eye_transform:
        rotate eye_angle

    imagebutton:
        idle "eyes.png" # Replace "eyes.png" with the name of your eye sprite
        hovered "eyes_hovered.png" # Replace "eyes_hovered.png" with the name of your eye sprite on mouseover
        action Jump("update_eye_angle")
        at eye_transform # Apply the eye angle transformation

label start:
    scene black
    call screen Eyes

You have to draw the eyes in a specific way
Is this some kind of LLM/ChatGPT code? Because I think it lacks reasoning and some things don't make sense or are just plain wrong. Please mark AI generated code if so! Also, please do not pollute the net with AI generated code to begin with!

So first of all, there is no need to write anything to an external file, second of all, calling a label to do some calculations is weird. And declaring a variable inside an python init block back practice.

You also cannot just use screen keywords inside a label.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1009
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Eyes following the mouse cursor in the main menu.

#7 Post by m_from_space »

Sirifys-Al wrote: Sun May 05, 2024 9:29 am Hello, did it work? If so, could you please share the code? I need the same thing, but I'm not good enough in coding for this.
Here is a very simple way of doing it, just put the image into your images folder for that. You can of course add another eyeball, figuring that out should be a little learning challenge. ;)

Code: Select all

init python:
    import math
    def update_eyes():
        # get the mouse position
        mx, my = renpy.get_mouse_pos()
        # calculate vector from eye position to the mouse
        vx = mx - eye_x
        vy = my - eye_y
        # calculate angle of the vector
        store.eye_angle = (math.atan2(vy, vx) * 180.0 / math.pi) + eye_angle_offset
        return

# a variable for saving the angle
default eye_angle = 0.0

# an offset to the angle (to adapt the image rotation)
define eye_angle_offset = 130.0

# the eyeball's position on the screen
define eye_x = 200
define eye_y = 100

screen eyeballs():
    add "eyeball" at transform:
        subpixel True
        anchor (0.5, 0.5)
        pos (eye_x, eye_y)
        rotate eye_angle
    timer 0.1 action Function(update_eyes) repeat True

# The game starts here.
label start:

    show screen eyeballs

    e "Why are you looking at me!?"
eyeball.png
eyeball.png (34.78 KiB) Viewed 289 times

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

Re: Eyes following the mouse cursor in the main menu.

#8 Post by Alex »

Yet another sample:

Code: Select all

init python:
    import math
    def update_eyes_1(st, at):
        # get the mouse position
        mx, my = renpy.get_mouse_pos()
        # calculate vector from eye position to the mouse
        vx = mx - eye_x_1
        vy = my - eye_y_1
        # calculate angle of the vector
        store.eye_angle_1 = (math.atan2(vy, vx) * 180.0 / math.pi) + eye_angle_offset
        # eyeball at transform
        rv = At("eyeball", eyes_tr(x_=eye_x_1, y_=eye_y_1, angle_=eye_angle_1))
        # update the result every 0.01 sec
        return rv, 0.01

    def update_eyes_2(st, at):
        # get the mouse position
        mx, my = renpy.get_mouse_pos()
        # calculate vector from eye position to the mouse
        vx = mx - eye_x_2
        vy = my - eye_y_2
        # calculate angle of the vector
        store.eye_angle_2 = (math.atan2(vy, vx) * 180.0 / math.pi) + eye_angle_offset
        rv = At("eyeball", eyes_tr(x_=eye_x_2, y_=eye_y_2, angle_=eye_angle_2))
        return rv, 0.01

# transform to rotate eyeball
transform eyes_tr(x_, y_, angle_):
    subpixel True
    anchor (0.5, 0.5)
    pos (x_, y_)
    rotate angle_

screen eyes_test_scr():
    add DynamicDisplayable(update_eyes_1)
    add DynamicDisplayable(update_eyes_2)

image eyeball:
    "images/eyeball.png"

# a variables for saving the angle
default eye_angle_1 = 0.0
default eye_angle_2 = 0.0

# an offset to the angle (to adapt the image rotation)
define eye_angle_offset = 130.0

# the eyeball's position on the screen
define eye_x_1 = 600
define eye_y_1 = 400
define eye_x_2 = 800
define eye_y_2 = 400


label start:
    "..."
    show screen eyes_test_scr
    "Have fun...))"

https://www.renpy.org/doc/html/displaya ... splayables
https://www.renpy.org/doc/html/displaya ... isplayable

User avatar
Sirifys-Al
Newbie
Posts: 9
Joined: Sat Apr 20, 2024 3:55 pm
Contact:

Re: Eyes following the mouse cursor in the main menu.

#9 Post by Sirifys-Al »

m_from_space wrote: Mon May 06, 2024 5:21 am
Sirifys-Al wrote: Sun May 05, 2024 9:29 am Hello, did it work? If so, could you please share the code? I need the same thing, but I'm not good enough in coding for this.
Here is a very simple way of doing it, just put the image into your images folder for that. You can of course add another eyeball, figuring that out should be a little learning challenge. ;)

Code: Select all

init python:
    import math
    def update_eyes():
        # get the mouse position
        mx, my = renpy.get_mouse_pos()
        # calculate vector from eye position to the mouse
        vx = mx - eye_x
        vy = my - eye_y
        # calculate angle of the vector
        store.eye_angle = (math.atan2(vy, vx) * 180.0 / math.pi) + eye_angle_offset
        return

# a variable for saving the angle
default eye_angle = 0.0

# an offset to the angle (to adapt the image rotation)
define eye_angle_offset = 130.0

# the eyeball's position on the screen
define eye_x = 200
define eye_y = 100

screen eyeballs():
    add "eyeball" at transform:
        subpixel True
        anchor (0.5, 0.5)
        pos (eye_x, eye_y)
        rotate eye_angle
    timer 0.1 action Function(update_eyes) repeat True

# The game starts here.
label start:

    show screen eyeballs

    e "Why are you looking at me!?"
eyeball.png

Alex wrote: Tue May 07, 2024 3:18 pm Yet another sample:

Code: Select all

init python:
    import math
    def update_eyes_1(st, at):
        # get the mouse position
        mx, my = renpy.get_mouse_pos()
        # calculate vector from eye position to the mouse
        vx = mx - eye_x_1
        vy = my - eye_y_1
        # calculate angle of the vector
        store.eye_angle_1 = (math.atan2(vy, vx) * 180.0 / math.pi) + eye_angle_offset
        # eyeball at transform
        rv = At("eyeball", eyes_tr(x_=eye_x_1, y_=eye_y_1, angle_=eye_angle_1))
        # update the result every 0.01 sec
        return rv, 0.01

    def update_eyes_2(st, at):
        # get the mouse position
        mx, my = renpy.get_mouse_pos()
        # calculate vector from eye position to the mouse
        vx = mx - eye_x_2
        vy = my - eye_y_2
        # calculate angle of the vector
        store.eye_angle_2 = (math.atan2(vy, vx) * 180.0 / math.pi) + eye_angle_offset
        rv = At("eyeball", eyes_tr(x_=eye_x_2, y_=eye_y_2, angle_=eye_angle_2))
        return rv, 0.01

# transform to rotate eyeball
transform eyes_tr(x_, y_, angle_):
    subpixel True
    anchor (0.5, 0.5)
    pos (x_, y_)
    rotate angle_

screen eyes_test_scr():
    add DynamicDisplayable(update_eyes_1)
    add DynamicDisplayable(update_eyes_2)

image eyeball:
    "images/eyeball.png"

# a variables for saving the angle
default eye_angle_1 = 0.0
default eye_angle_2 = 0.0

# an offset to the angle (to adapt the image rotation)
define eye_angle_offset = 130.0

# the eyeball's position on the screen
define eye_x_1 = 600
define eye_y_1 = 400
define eye_x_2 = 800
define eye_y_2 = 400


label start:
    "..."
    show screen eyes_test_scr
    "Have fun...))"

https://www.renpy.org/doc/html/displaya ... splayables
https://www.renpy.org/doc/html/displaya ... isplayable
Thank you very much! Sorry, I didn't see the notifications earlier : (

Post Reply

Who is online

Users browsing this forum: Google [Bot]