Search This Blog

Saturday, April 11, 2020

Random Alien Writing

I've been thinking about this for some time and finally found some time to write the program.  The program is at the bottom of the post and written with Python 3.8.  Basically, it creates a canvas filled with strange looking writing.  I was inspired by some of the examples of "alien" writing used in various sci-fi movies and TV shows.  I thought to myself, should be easy to create a program that can create examples of this kind of writing.  I re-used the basic framework I had developed for the program to simulate cellular automata. (I have re-used the framework for some other bits I've made for myself, not (yet) posted here and it is becoming one of my favorites.)  My first attempts, frankly, looked like white noise on a screen.  Over a couple of days I refined the parameters to get it closer to what the brain will recognize as having some semantic content and organization and the current version is pretty good.  I've used it to generate the background I've added to the blog.  I hope you like it!  I'm not going to go into a detailed explanation of the program elements, but if you have questions about it, post a comment.

The Program




from tkinter import *
from random import randint

class App:
    """This class is derived, I think, from a TK class and is designed to
    work with the TK environment.
    Requires imports from Tkinter, math, and random"""
    def __init__(self, master):
        """This class is the whole of the application """
        # Necessary Frames
        frame = Frame(master)
        control_frame = Frame(frame)
        control_frame.grid(row = 0, column = 0, sticky = N)
        canvas_frame = Frame(frame)
        canvas_frame.grid(row =0, column = 1)

        #Application variables
        self.char_pts = [(1,1),(7,1),(14,1),(1,7),(7,7),(14,7),(1,14),(7,14)
                         ,(14,14)]
        self.chars = []
        self.text = []
        self.tmpstr = '\nFor another,\nHit Randomize'
        
        #Control FrameWidgets
        self.lab = Label(control_frame, text=self.tmpstr)
        self.lab.pack(side = TOP)
        self.b1 = Button(control_frame, text='Randomize')
        self.b1.config(command=self.randomize)
        self.b1.pack()
        
        # The Canvas
        self.canvas = Canvas(canvas_frame, width = 800, height = 800)
        self.canvas.config(bg='white')
        self.canvas.pack()
        frame.pack()

        #Menu
        menubar = Menu(root)
        menu_1 = Menu(menubar, tearoff=0)
        menu_1.add_command(label='Quit',command=root.destroy)
        menubar.add_cascade(label='File', menu=menu_1)
        master.config(menu=menubar)
        self.randomize()

    def randomize(self):
        self.canvas.delete(ALL)
        # create characters
        self.chars = []
        for j in range(10):
            new_char = []
            b = randint(0,8)
            for i in range(randint(2,7)):
                a = b
                b = (a + randint(1,8))%9
                new_char.append((a,b,randint(1,2)))
            self.chars.append(new_char)
        # create text, in 2-7 letter long words.
        tl = 0
        self.text = []
        while tl <3000:
            wl = randint(2,7)
            for k in range(wl):
                self.text.append(randint(1,len(self.chars)-1))
            tl = tl + wl + 1
            self.text.append(0)
        # now write the text using the characters
        i = 0
        j = 0
        for char in self.text:
            if char != 0:
                for seg in self.chars[char]:
                    start = seg[0]
                    end = seg[1]
                    wt = seg[2]
                    x1 = i*15+self.char_pts[start][0]
                    y1 = j*17+self.char_pts[start][1]
                    x2 = i*15+self.char_pts[end][0]
                    y2 = j*17+self.char_pts[end][1]
                    if x1==x2 or y1==y2:
                        self.canvas.create_line(x1,y1,x2,y2,width=wt)
                    else:
                        self.canvas.create_arc(x1,y1,x2,y1,width=wt)
            if i == 50:
                j = j + 1
                i =0
            else:
                i= i + 1
        self.canvas.update()

if __name__ == '__main__':
    root = Tk()
    root.wm_title('Alien Text')
    app = App(root)
    root.mainloop()
     


No comments:

Post a Comment