I have a game window with the following information: gamewindow =…
- User
I have a game window with the following information: game_window = pyautogui.getWindowsWithTitle(window_title)[0]
game_window.left -8, game_window.top -8, game_window.width 1382, game_window.height 784
How do I programatically get a rectangle which is in the positions: x: 234 y: 726 size: 39x39
- Assistant
To get a rectangle with positions x=234, y=726 and size 39x39, you can use the following code:
coderect = (game_window.left + 234, game_window.top + 726, 39, 39)This code adds the x and y values to the position of the game window to get the absolute position of the rectangle on the screen. The size of the rectangle is specified by the tuple (39, 39). The resulting
rectvariable contains a tuple with four values: the x and y position of the top-left corner of the rectangle, followed by its width and height. - User
it works but the window size can change and I need to be able to select it even if the window size changes, how can I do that?
- Assistant
If the window size can change, you can calculate the position of the rectangle relative to the size of the game window. You can do this by calculating the percentage position of the rectangle with respect to the size of the game window, then multiplying those percentages by the current size of the game window.
Here's an example:
code