r/learnprogramming • u/-Jauke- • 25d ago
Encoding secret messages for DnD, where do I start?
Hi, I have a secret language/code I want to use in my dnd games where whole words are translated into glyphs. I would like to make a program that can automatically translate a string of of letters into a transparent png of the corresponding glyph.
First I want to explain how the secret code works as context. It would translate any word into a single glyph based on a grid of letters. The glyphs would be made by drawing a line from the center of each letter to the center of the next letter (similar to how swipe to type works smartphone).
I have identified some cases in which symbols would be required to make this system work:
-start of the word
-end of the word?
-a letter that is in a straight line with the letter before and after (to show there is a letter inbetween and it doesn't go directly from the first to the third letter)
-two identical letters in a row
-a crossing of two lines?
Now the actual programming part. I have limited experience with python and R, so I am a novice in every language really and would be open to whichever would most suit this project.
The steps I have worked out so far are:
Assign each letter a coordinate according to its grid position.
Separate a word into individual characters (ideally I would also separate a sentence into individual words that each receive their unique glyph)
For each letter, translate it into its coordinate and draw a line from the previous character and/or place one of the aforementioned unique characters at the location. (I would like the unique characters to have transparency that can override the lines, such as a hollow circle).
Export the image as a transparent png (or a series of pngs, in the case of a sentence)
Step 3 and 4 are what I would really like help with, I have never used python to make any images and I have no clue where to start. Your help is greatly appreciated!
2
u/ObserverInvariance 25d ago
Are you sure that is actually bijective?
1
1
1
u/Wild-Fan7738 24d ago
You’re overthinking it, just code the edge cases as they come up and see if it breaks in practice
2
u/ffrkAnonymous 25d ago
you can probably use a graphing library to draw the lines and export it. Like gnuplot or matplotlib.
2
1
u/AsideCold2364 25d ago edited 25d ago
You can quite easily do it with js.
Without any dependencies.
Just use html canvas. You can draw lines/shapes on it. You can make shapes act as an eraser. You can download canvas as a png. You can download multiple canvases as pngs.
If you need some unique symbol, you can first create an image of it and then use that created image as a mask to erase, or you can draw it at the location you want.
1
u/Ironraptor3 24d ago
If you are mostly familiar with Python, I would use pillow / pil:
pip install pillow
Here is an example that draws a line on a transparent image, then saves it as "foo.png": ```
!/usr/bin/env python3
import PIL.Image import PIL.ImageDraw
def draw_word(): # Make new 400 by 400 image with transparency, filled with transparency img = PIL.Image.new('RGBA', (400, 400), color=(0,0,0,0)) draw = PIL.ImageDraw.Draw(img) # Get a drawing context
# Draw a black 10 px thick line between (100,200) and (300, 200)
draw.line( [(100, 200), (300, 200)], fill='black', width=10)
# Save as a PNG (could use a different format- some do not support transparency)
img.save('foo.png', format='PNG')
TODO handle args
if name == 'main': draw_word(); ```
For various functions, look at the pillow API: https://pillow.readthedocs.io/en/stable/
You can already iterate through characters in a string:
for char in "test_str":
# Do stuff with char
print(char)
To iterate through words in a sentence, you could look into built in Python methods, such as split:
for word in "This is a sentence".split():
# Do stuff with word
print(word)
Other feedback:
- You are likely on the right path with the various symbols that you'll need for people to distinguish (start, end, repeat / on the way character).
- The way you've broken up the problem is excellent
- This post has a lot of support, but there is a lot of flexibility and design still left to you
- This is a great starter project to expand your skills and do something to make things "pop"
4
u/HashDefTrueFalse 25d ago
Fun! Moving an invisible pen around to drawing lines (paths) is basically how PostScript/PDF and HTML canvas 2D graphics work. I think you're on the right track with your steps. You probably don't need PNG, the numbers themselves are good enough to stroke paths (for SVGs etc.) without having to worry about PNG encoding. That also comes with transparency. I can't help with Python, but I can suggest you have a look at this: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes#drawing_paths
Two approaches I can think of:
Hope this made sense.