2
\$\begingroup\$

Hello everyone I was told to post this here instead of StackOverFlow.

I am using PyAutoGUI currently as this was the best tools I could manage to find to use a piece of software that has no native Python function. I am using the mouse and keyboard control to design maps for Dungeons and Dragons using the software Dungeon Painter Studio. It is working as intended and I have figured out a way to actually use my script to create maps on it however, since PyAutoGUI is based on mouse location and pixels it has been quite a manual process.

EDIT:

Here is a picture of the software with nothing in it just opened up: enter image description here

Here is a picture after the code has been ran: enter image description here

My reason for creating this script is so that I can eventually randomize it to create a random generated map for my D&D campaign that is somewhat original compared to just taking a map off the internet.

I am inputting a starting location for the mouse and have it click and moved based on relative location to that starting position. Here is a piece of what it currently looks like:

#Switch to dps, maximize window, select the tile hotkey

pyautogui.keyDown('altleft'); pyautogui.press('tab'); pyautogui.keyUp('altleft')
fw = pyautogui.getActiveWindow()
fw.maximize()
pyautogui.keyDown('enter')

#Select the background tile

pyautogui.click('dirt_k.png')

#Create background

pyautogui.moveTo(9,189) 
pyautogui.mouseDown(9,189)
pyautogui.moveTo(748,808)
pyautogui.mouseUp(252,436)

#Select the dugeon tile

pyautogui.click('dirt_d.png')

#Create dungeon floor

pyautogui.moveTo(329,807)
pyautogui.mouseDown(329,807)
pyautogui.moveRel(100,-75)
pyautogui.mouseUp()

pyautogui.moveRel(-25,0)
pyautogui.mouseDown()
pyautogui.moveRel(-50,-50)
pyautogui.mouseUp()

pyautogui.moveRel(-100,0)
pyautogui.mouseDown()
pyautogui.moveRel(250,-125)
pyautogui.mouseUp()

pyautogui.moveRel(0,100)
pyautogui.mouseDown()
pyautogui.moveRel(50,25)
pyautogui.mouseUp()

pyautogui.moveRel(0,100)
pyautogui.mouseDown()
pyautogui.moveRel(100,-125)
pyautogui.mouseUp()

pyautogui.moveRel(0,0)
pyautogui.mouseDown()
pyautogui.moveRel(-25,-50)
pyautogui.mouseUp()

pyautogui.moveRel(-75,0)
pyautogui.mouseDown()
pyautogui.moveRel(175,-100)
pyautogui.mouseUp()

pyautogui.mouseDown()
pyautogui.moveRel(-25,-50)
pyautogui.mouseUp()

pyautogui.moveRel(25,0)
pyautogui.mouseDown()
pyautogui.moveRel(-225,-125)
pyautogui.mouseUp()

It basically continues on like that for hundreds of lines to create a sample map. I was just seeing if anyone else is familiar enough with PyAutoGUI (or any other tools) that would help automated this process a bit more.

I have the amount of pixels per square which is roughly 25px so it isn't too hard to calculate. Moving right is +25 left is -25 down is +25 up is -25. So using those measurements I have been able to calculate relative location where the mouse starts and move it around from there.

Any advice or help is greatly appreciated, thank you!

\$\endgroup\$
2
  • \$\begingroup\$ We are missing some information here. Can you include a screenshot of the program you're scripting, and explain why you're scripting it (instead of doing it once by hand?) \$\endgroup\$ Aug 9, 2021 at 21:08
  • \$\begingroup\$ Just added an edit with some screenshots so you can see it. The reason I am running this and want to optimize it is so that eventually I can randomly generate these maps to add to my D&D game. \$\endgroup\$ Aug 9, 2021 at 21:37

1 Answer 1

1
\$\begingroup\$

My reason for creating this script is so that I can eventually randomize it to create a random generated map for my D&D campaign that is somewhat original compared to just taking a map off the internet.

Given that you want to eventually make a random map, your current approach won't work. You're just using hardcoded values, and there is no way to "randomize" it without fundamentally re-writing it.

  • The first change I would recommend is separate the data out. This makes the program shorter, clearer, and easier to change to add something like randomness
#Create dungeon floor
pyautogui.moveTo(329,807)
pyautogui.mouseDown(329,807)
pyautogui.moveRel(100,-75)
pyautogui.mouseUp()

lines = [
    ((-25, 0), (-50, 50)),
    ((-100,0), (250,-125))
    ((0,100), (50,25)),
    ((0,100), (100,-125)),
    ((0,0), (-25,-50)),
    ((-75,0), (175,-100)),
    ((0,0), (-25,-50)),
    ((25,0), (-225,-125)),
]

for down, up in lines:
    pyautogui.moveRel(*down)
    pyautogui.mouseDown()
    pyautogui.moveRel(*up)
    pyautogui.mouseUp()
  • Work in absolute coordinates, not relative ones. It will make things easier for random dungeons.
  • Add comments. Does a particular mouse movement draw a room? Does it draw a line bordering a room? I have literally no idea. In this case, move movement is so opaque it will be useful for you as well, not just other readers.
  • As a warning, the whole idea of scripting a dungeon generation program may not be worth it. What happens if you want a map that's bigger than your screen resolution? If you just output your own image directly, it will be easier than scripting in some ways.
\$\endgroup\$
1
  • \$\begingroup\$ Yes this is perfect and what I was looking for something along these lines (pun intended)! In a all seriousness though I do recognize the limitations of working with a limited amount of screen space right now and was heading in the direction you just explained. Adding the lines like that should really help. As for the limited map spacing, I was planning on just creating multiple that could possibly connected if I wrote in some kind of connection point to be in that would carry over into the next map. This is a great start for me though and I really do appreciate the time you took to answer \$\endgroup\$ Aug 9, 2021 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.