Eyes following the mouse cursor in the main menu.
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.
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.
Eyes following the mouse cursor in the main menu.
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!
- 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.
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.
Re: Eyes following the mouse cursor in the main menu.
I will try to do that using this information, thanks!m_from_space wrote: ↑Fri Dec 29, 2023 9:44 am1. 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.
- Sirifys-Al
- Newbie
- Posts: 9
- Joined: Sat Apr 20, 2024 3:55 pm
- Contact:
Re: Eyes following the mouse cursor in the main menu.
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.Diruker wrote: ↑Fri Dec 29, 2023 10:18 amI will try to do that using this information, thanks!m_from_space wrote: ↑Fri Dec 29, 2023 9:44 am1. 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.
Re: Eyes following the mouse cursor in the main menu.
Ai code
You have to draw the eyes in a specific way
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
Last edited by Andredron on Mon May 06, 2024 9:44 am, edited 1 time in total.
Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18
Sawa - a game of the Drow Nation
Honest Critique
Poses in visual novels, or how to hold a character properly in the frame
Help save articles to the webarchive. [/color]
Sawa - a game of the Drow Nation
Honest Critique
Poses in visual novels, or how to hold a character properly in the frame
Help save articles to the webarchive. [/color]
- 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.
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!Andredron wrote: ↑Sun May 05, 2024 4:35 pmYou have to draw the eyes in a specific wayCode: 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
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.
- 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.
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.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.
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!?"
Re: Eyes following the mouse cursor in the main menu.
Yet another sample:
https://www.renpy.org/doc/html/displaya ... splayables
https://www.renpy.org/doc/html/displaya ... isplayable
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
- Sirifys-Al
- Newbie
- Posts: 9
- Joined: Sat Apr 20, 2024 3:55 pm
- Contact:
Re: Eyes following the mouse cursor in the main menu.
m_from_space wrote: ↑Mon May 06, 2024 5:21 amHere 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.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.
eyeball.pngCode: 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!?"
Thank you very much! Sorry, I didn't see the notifications earlier : (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
Who is online
Users browsing this forum: Google [Bot]