Ren'Py Ren'Py - Mouse Position / Cursor

Xavster

Well-Known Member
Game Developer
Mar 27, 2018
1,243
7,572
After the completion of Callisto, I am into development on the next game Stellar Crossroads. As part of the early development work, I am attempting to create a little exploration / mining mini game and need some assistance with the Ren'Py interface. The specific items where I require assistance relates to mouse position and mouse cursor.

The screen for the mini game is to have a portion (square about 75% of the full screen height on the left side of the screen simulating a x,y exploration grid. As part of this exploration game I want the player to click somewhere in this square to indicate where they would like to drill an exploration hole. When they click, the x,y coordinates are stored as two variables such that I can calculate a grade at that particular position.

Hence I require assistance with the following:
- Having a certain portion of the screen clickable, corresponding to the exploration grid. (say 800 x 800 pixel square on 1080p screen).
- Having the cursor change from the default to say crosshairs, when the mouse is within this zone.
- On clicking, returning the x,y coordinates within this exploration location to a X coordinate variable and a Y coordinate variable.

There will be a bunch of other code in the background, just need some assistance with the Ren'Py interface aspects.
 

Xavster

Well-Known Member
Game Developer
Mar 27, 2018
1,243
7,572
Have managed to work it out over the course of the morning. It returns coordinates in the 0 to 100 range, when you click inside the area defined by the imagemap. Happy to hear recommendations of potential improvements.
Python:
define mousex = 0
define mousey = 0

init python:
    def getMousePosition():
        import pygame
        u, i = renpy.get_physical_size()
        x, y = pygame.mouse.get_pos()
        store.mousex = ((x*1920/u) - 100)/8
        store.mousey = ((y*1080/i) - 180)/8

screen exploration:
    imagemap:
        auto "explore_back_%s.jpg"
        hotspot (100, 180, 800, 800) action getMousePosition tooltip "probe location" mouse "cross"

    grid 1 1:
        xalign 0.5
        yalign 0.05
        text "Mining Exploration" size 60

    vbox:
        $ tooltip = GetTooltip()
        xpos 0.6
        ypos 0.4
        text "Tooltip - [tooltip]"
        text "X Position = [mousex]"
        text "Y Position = [mousey]"

    grid 1 1:
        xpos 0.9
        ypos 0.85
        imagebutton auto "b_back_%s.webp" action Jump("ending")
In the above I made an adjustment to the position location to account for screen resolutions other than 1080p. Also to have the custom mouse it is necessary to define it in the options.rpy file as follows.
Python:
define config.mouse = { "cross" : [ ("cross.png", 16, 16)] }
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,369
15,285
Python:
    def getMousePosition():
        import pygame
        u, i = renpy.get_physical_size()
        x, y = pygame.mouse.get_pos()
        store.mousex = ((x*1920/u) - 100)/8
        store.mousey = ((y*1080/i) - 180)/8
It would be better that you directly use renpy.get_mouse_pos() here. It will guaranty that the conversion is correct and that you get the real position of the mouse according to the image ; what is what you need to works with.