in python, my gamewindow has these properties: gamewindow.left: -8 gamewindow.top -8…
- User
in python, my game_window has these properties: game_window.left: -8 game_window.top -8 game_window.width 1382 game_window.height 784
and the rectangle I need is in (game_window.width/2 + 150, game_window.bottom - 98, 145, 18)
import pyautogui
game_window = pyautogui.getWindowsWithTitle(window_title)[0]
left = game_window.left top = game_window.top width = game_window.width height = game_window.height
Do_Something(game_window.width/2 + 150, game_window.bottom - 98, 145, 18)
give me a way to do this relative to the window size but having different window sizes
- Assistant
Here's one way you can make the rectangle coordinates relative to the game window's size:
import pyautogui
window_title = "My Game" game_window = pyautogui.getWindowsWithTitle(window_title)[0]
Scale factor, based on the default game window size
scale_width = game_window.width / 1382 scale_height = game_window.height / 784
Calculate rectangle coordinates based on the game window's size
rect_width = 145 * scale_width rect_height = 18 * scale_height rect_left = (game_window.width/2 + 150) * scale_width rect_top = (game_window.bottom - 98) * scale_height
Call function with relative rectangle coordinates
Do_Something(rect_left, rect_top, rect_width, rect_height)
With this approach, you calculate the scale factor for both the width and height of the game window, based on the default game window size. Then you use these factors to scale the rectangle coordinates to match the current game window size. This way, the rectangle will always have the same relative size and position, regardless of the actual game window size.
- User
I tried with this and works: left: -8 top: -8 width: 1382 height: 784
but when I used this it selected a wrong area on the screen: left: 90 top: 65 width: 1161 height: 671
- Assistant
The reason why the second set of window properties have resulted in the wrong area being selected is because you have not updated the calculation of the rectangle coordinates to use the new window size.
Here is an updated version of the code that uses the updated window properties to calculate the relative coordinates of the rectangle:
import pyautogui game_window = pyautogui.getWindowsWithTitle(window_title)[0] left = game_window.left top = game_window.top width = game_window.width height = game_window.height # Calculate the scale factor to be used for width and height default_width = 1382 default_height = 784 scale_x = width / default_width scale_y = height / default_height # Calculate the relative coordinates based on the window size rect_x = (game_window.width / 2 + 150 - left) * scale_x rect_y = (game_window.bottom - 98 - top) * scale_y rect_w = 145 * scale_x rect_h = 18 * scale_y # Call function with relative rectangle coordinates Do_Something(rect_x, rect_y, rect_w, rect_h)This code calculates the scale factor based on the default window size, as before. However, now it uses the updated window properties to calculate the relative coordinates of the rectangle. Specifically:
rect_xis the x-coordinate of the left edge of the rectangle, relative to the left edge of the window (left). It has been adjusted to account for the change in the left edge of the window compared to the default, by subtractingleft. It has also been scaled byscale_xto account for the change in window size compared to the default.rect_yis the y-coordinate of the top edge of the rectangle, relative to the top edge of the window (top). It has been adjusted to account for the change in the top edge of the window compared to the default, by subtractingtop. It has also been scaled byscale_yto account for the change in window size compared to the default.rect_wis the width of the rectangle, which has been scaled byscale_xto account for the change in window size compared to the default.rect_his the height of the rectangle, which has been scaled byscale_yto account for the change in window size compared to the default.
Note that this code assumes that the game window is located at its default position of (-8, -8). If the window is located at a different position, you would need to adjust the calculation of
rect_xto account for this. - User
with this method none size works, it select a different rectangle even in the original size
- Assistant
I'm sorry to hear that the method doesn't work for you. Here are a couple of things you can try to debug the issue:
-
Check the values of
left,top,width, andheightto make sure they are correct for your game window. You can print them out before the rectangle calculation to see what values they have. It's possible that the values returned bygetWindowsWithTitle()may be incorrect or might not match the actual size of your game window. -
Take a screenshot of your game window (using a different tool, such as the Windows built-in snipping tool), and compare it with the rectangle that is being selected using the code. This can help you identify if the issue is with the calculation of the rectangle coordinates, or if it's related to a problem with the screen or the game window.
-
Experiment with different values for the rectangle coordinates, to see if you can find a combination that works correctly for your game window. For example, you could try adjusting the x and y coordinates of the rectangle by different amounts, or increasing or decreasing the width or height of the rectangle. This can help you narrow down the cause of the issue.
I hope these suggestions help you identify the issue and find a solution that works for your game window.
-
- User
This is my game_window size 1382 x 784 and this are the values of the correct rectangle left: 841 top: 678 width: 145 height: 18
How can I get the relative values for different window sizes?
- Assistant
You can get the relative coordinates of the rectangle by first calculating the scale factor for the window size, and then scaling the rectangle coordinates accordingly. Here's how you can do it:
import pyautogui game_window = pyautogui.getWindowsWithTitle(window_title)[0] # Get window properties left = game_window.left top = game_window.top width = game_window.width height = game_window.height # Calculate scale factor default_width = 1382 default_height = 784 scale_x = width / default_width scale_y = height / default_height # Calculate relative coordinates of rectangle rect_left = (841 - left) * scale_x rect_top = (678 - top) * scale_y rect_width = 145 * scale_x rect_height = 18 * scale_y # Call function with relative rectangle coordinates Do_Something(rect_left, rect_top, rect_width, rect_height)In this code, we first get the window properties as before, and then calculate the scale factor for the window size. Then, we use the known rectangle coordinates and scale them by multiplying with the scale factor. For example, to calculate the relative left coordinate of the rectangle, we subtract the window's left coordinate from the rectangle's left coordinate, and then multiply the result by the scale factor for the x-axis.
This way, the rectangle coordinates are adjusted according to the window size, and are relative to the window rather than absolute on the screen. You can use the same code with different window sizes and known rectangle coordinates to calculate the relative coordinates for each case.