Skip to main content
added 2 characters in body
Source Link

Here is how it will run (bear with me when I spent too much time presenting the basics):

enter image description hereenter image description here

import pygame
pygame.font.init()

wn = pygame.display.set_mode((600, 600))
font = pygame.font.Font(None, 32)

class TextBox():
    def __init__(self, x, y, w, h, title='Text Box', color=(0, 0, 0), default=''):
        self.input_box = pygame.Rect(x, y, w, h)
        self.w = w
        self.h = h
        self.color_inactive = color
        self.color_active = pygame.Color('purple')
        self.color = self.color_inactive
        self.default = default
        self.text = ['']
        self.active = False
        self.title = title

    def draw(self):
        title = font.render(self.title, True, self.color)
        wn.blit(title, (self.input_box.x+5, self.input_box.y-self.h))
        txts = [font.render(''.join(t), True, self.color) for t in self.text]
        width = max(self.w, max(txts, key=lambda x:x.get_width()).get_width()+10)
        height = self.h * len(txts)
        self.input_box.w = width
        self.input_box.h = height
        if len(txts) == 1 and txts[0].get_width() == 1:
            wn.blit(font.render(self.default, True, self.color), (self.input_box.x+5, self.input_box.y+5))
        else:
            for i, txt in enumerate(txts):
                wn.blit(txt, (self.input_box.x+5, self.input_box.y+5+i*self.h))
        pygame.draw.rect(wn, self.color, self.input_box, 2)

    def check_status(self, pos):
        if self.input_box.collidepoint(pos):
            self.active = not self.active
        else:
            self.active = False
        self.color = self.color_active if self.active else self.color_inactive

    def type(self, event):
        if self.active:
            if event.key == pygame.K_RETURN:
                self.text.append('')
            elif event.key == pygame.K_BACKSPACE:
                if self.text[-1]:
                    self.text[-1] = self.text[-1][:-1]
                else:
                    if len(self.text) > 1:
                        self.text = self.text[:-1]
            else:
                self.text[-1] += event.unicode

def rot(alp, num):
    while num >= 26:
        num -= 26
    n = ord(alp) + num
    if alp in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ''abcdefghijklmnopqrstuvwxyz':
        if n > 122:
            n -= ord(26
        alp) += numchr(n)
    elif alp in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
 if n > 122 or 97 > if n > 90:
            n -= 26
        alp = chr(n)
    return alp

box = TextBox(110, 60, 140, 32, 'Input Text')
rot_num = TextBox(10, 60, 50, 32, 'Rot', default='0')
rot_box = TextBox(110, 300, 140, 32, 'Output Text')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            box.check_status(event.pos)
            rot_num.check_status(event.pos)
        if event.type == pygame.KEYDOWN:
            box.type(event)
            if (event.unicode.isdigit() or event.key == pygame.K_BACKSPACE) and len(rot_num.text[0]) < 6 or event.key == pygame.K_BACKSPACE:
                rot_num.type(event)
#            if event.key == pygame.K_RETURN:
#                print('\n'.join([''.join(row) for row in rot_box.text])+'\n--------------------')

    rt = int(rot_num.text[0]) if rot_num.text[0] else 0
    rot_box.text = [[rot(char, rt) for char in row] for row in box.text]
    wn.fill((255, 255, 200))
    box.draw()
    rot_num.draw()
    rot_box.draw()
    pygame.display.flip()

Here is how it will run (bear with me when I spent too much time presenting the basics)

enter image description here

import pygame
pygame.font.init()

wn = pygame.display.set_mode((600, 600))
font = pygame.font.Font(None, 32)

class TextBox():
    def __init__(self, x, y, w, h, title='Text Box', color=(0, 0, 0), default=''):
        self.input_box = pygame.Rect(x, y, w, h)
        self.w = w
        self.h = h
        self.color_inactive = color
        self.color_active = pygame.Color('purple')
        self.color = self.color_inactive
        self.default = default
        self.text = ['']
        self.active = False
        self.title = title

    def draw(self):
        title = font.render(self.title, True, self.color)
        wn.blit(title, (self.input_box.x+5, self.input_box.y-self.h))
        txts = [font.render(''.join(t), True, self.color) for t in self.text]
        width = max(self.w, max(txts, key=lambda x:x.get_width()).get_width()+10)
        height = self.h * len(txts)
        self.input_box.w = width
        self.input_box.h = height
        if len(txts) == 1 and txts[0].get_width() == 1:
            wn.blit(font.render(self.default, True, self.color), (self.input_box.x+5, self.input_box.y+5))
        else:
            for i, txt in enumerate(txts):
                wn.blit(txt, (self.input_box.x+5, self.input_box.y+5+i*self.h))
        pygame.draw.rect(wn, self.color, self.input_box, 2)

    def check_status(self, pos):
        if self.input_box.collidepoint(pos):
            self.active = not self.active
        else:
            self.active = False
        self.color = self.color_active if self.active else self.color_inactive

    def type(self, event):
        if self.active:
            if event.key == pygame.K_RETURN:
                self.text.append('')
            elif event.key == pygame.K_BACKSPACE:
                if self.text[-1]:
                    self.text[-1] = self.text[-1][:-1]
                else:
                    if len(self.text) > 1:
                        self.text = self.text[:-1]
            else:
                self.text[-1] += event.unicode

def rot(alp, num):
    while num >= 26:
        num -= 26
    if alp in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
        n = ord(alp) + num
        if n > 122 or 97 > n > 90:
            n -= 26
        alp = chr(n)
    return alp

box = TextBox(110, 60, 140, 32, 'Input Text')
rot_num = TextBox(10, 60, 50, 32, 'Rot', default='0')
rot_box = TextBox(110, 300, 140, 32, 'Output Text')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            box.check_status(event.pos)
            rot_num.check_status(event.pos)
        if event.type == pygame.KEYDOWN:
            box.type(event)
            if (event.unicode.isdigit() or event.key == pygame.K_BACKSPACE) and len(rot_num.text[0]) < 6 or event.key == pygame.K_BACKSPACE:
                rot_num.type(event)
#            if event.key == pygame.K_RETURN:
#                print('\n'.join([''.join(row) for row in rot_box.text])+'\n--------------------')

    rt = int(rot_num.text[0]) if rot_num.text[0] else 0
    rot_box.text = [[rot(char, rt) for char in row] for row in box.text]
    wn.fill((255, 255, 200))
    box.draw()
    rot_num.draw()
    rot_box.draw()
    pygame.display.flip()

Here is how it will run:

enter image description here

import pygame
pygame.font.init()

wn = pygame.display.set_mode((600, 600))
font = pygame.font.Font(None, 32)

class TextBox():
    def __init__(self, x, y, w, h, title='Text Box', color=(0, 0, 0), default=''):
        self.input_box = pygame.Rect(x, y, w, h)
        self.w = w
        self.h = h
        self.color_inactive = color
        self.color_active = pygame.Color('purple')
        self.color = self.color_inactive
        self.default = default
        self.text = ['']
        self.active = False
        self.title = title

    def draw(self):
        title = font.render(self.title, True, self.color)
        wn.blit(title, (self.input_box.x+5, self.input_box.y-self.h))
        txts = [font.render(''.join(t), True, self.color) for t in self.text]
        width = max(self.w, max(txts, key=lambda x:x.get_width()).get_width()+10)
        height = self.h * len(txts)
        self.input_box.w = width
        self.input_box.h = height
        if len(txts) == 1 and txts[0].get_width() == 1:
            wn.blit(font.render(self.default, True, self.color), (self.input_box.x+5, self.input_box.y+5))
        else:
            for i, txt in enumerate(txts):
                wn.blit(txt, (self.input_box.x+5, self.input_box.y+5+i*self.h))
        pygame.draw.rect(wn, self.color, self.input_box, 2)

    def check_status(self, pos):
        if self.input_box.collidepoint(pos):
            self.active = not self.active
        else:
            self.active = False
        self.color = self.color_active if self.active else self.color_inactive

    def type(self, event):
        if self.active:
            if event.key == pygame.K_RETURN:
                self.text.append('')
            elif event.key == pygame.K_BACKSPACE:
                if self.text[-1]:
                    self.text[-1] = self.text[-1][:-1]
                else:
                    if len(self.text) > 1:
                        self.text = self.text[:-1]
            else:
                self.text[-1] += event.unicode

def rot(alp, num):
    while num >= 26:
        num -= 26
    n = ord(alp) + num
    if alp in 'abcdefghijklmnopqrstuvwxyz':
        if n > 122:
            n -= 26
        alp = chr(n)
    elif alp in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        if n > 90:
            n -= 26
        alp = chr(n)
    return alp

box = TextBox(110, 60, 140, 32, 'Input Text')
rot_num = TextBox(10, 60, 50, 32, 'Rot', default='0')
rot_box = TextBox(110, 300, 140, 32, 'Output Text')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            box.check_status(event.pos)
            rot_num.check_status(event.pos)
        if event.type == pygame.KEYDOWN:
            box.type(event)
            if (event.unicode.isdigit() or event.key == pygame.K_BACKSPACE) and len(rot_num.text[0]) < 6 or event.key == pygame.K_BACKSPACE:
                rot_num.type(event)
#            if event.key == pygame.K_RETURN:
#                print('\n'.join([''.join(row) for row in rot_box.text])+'\n--------------------')

    rt = int(rot_num.text[0]) if rot_num.text[0] else 0
    rot_box.text = [[rot(char, rt) for char in row] for row in box.text]
    wn.fill((255, 255, 200))
    box.draw()
    rot_num.draw()
    rot_box.draw()
    pygame.display.flip()
Post Made Community Wiki
Source Link

Rot-anything real-time

With this little app, you can see your text being encoded with rot-13 (any other number) as you type.

Here is how it will run (bear with me when I spent too much time presenting the basics)

enter image description here

Here is the entire code:

import pygame
pygame.font.init()

wn = pygame.display.set_mode((600, 600))
font = pygame.font.Font(None, 32)

class TextBox():
    def __init__(self, x, y, w, h, title='Text Box', color=(0, 0, 0), default=''):
        self.input_box = pygame.Rect(x, y, w, h)
        self.w = w
        self.h = h
        self.color_inactive = color
        self.color_active = pygame.Color('purple')
        self.color = self.color_inactive
        self.default = default
        self.text = ['']
        self.active = False
        self.title = title

    def draw(self):
        title = font.render(self.title, True, self.color)
        wn.blit(title, (self.input_box.x+5, self.input_box.y-self.h))
        txts = [font.render(''.join(t), True, self.color) for t in self.text]
        width = max(self.w, max(txts, key=lambda x:x.get_width()).get_width()+10)
        height = self.h * len(txts)
        self.input_box.w = width
        self.input_box.h = height
        if len(txts) == 1 and txts[0].get_width() == 1:
            wn.blit(font.render(self.default, True, self.color), (self.input_box.x+5, self.input_box.y+5))
        else:
            for i, txt in enumerate(txts):
                wn.blit(txt, (self.input_box.x+5, self.input_box.y+5+i*self.h))
        pygame.draw.rect(wn, self.color, self.input_box, 2)

    def check_status(self, pos):
        if self.input_box.collidepoint(pos):
            self.active = not self.active
        else:
            self.active = False
        self.color = self.color_active if self.active else self.color_inactive

    def type(self, event):
        if self.active:
            if event.key == pygame.K_RETURN:
                self.text.append('')
            elif event.key == pygame.K_BACKSPACE:
                if self.text[-1]:
                    self.text[-1] = self.text[-1][:-1]
                else:
                    if len(self.text) > 1:
                        self.text = self.text[:-1]
            else:
                self.text[-1] += event.unicode

def rot(alp, num):
    while num >= 26:
        num -= 26
    if alp in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
        n = ord(alp) + num
        if n > 122 or 97 > n > 90:
            n -= 26
        alp = chr(n)
    return alp

box = TextBox(110, 60, 140, 32, 'Input Text')
rot_num = TextBox(10, 60, 50, 32, 'Rot', default='0')
rot_box = TextBox(110, 300, 140, 32, 'Output Text')

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            box.check_status(event.pos)
            rot_num.check_status(event.pos)
        if event.type == pygame.KEYDOWN:
            box.type(event)
            if (event.unicode.isdigit() or event.key == pygame.K_BACKSPACE) and len(rot_num.text[0]) < 6 or event.key == pygame.K_BACKSPACE:
                rot_num.type(event)
#            if event.key == pygame.K_RETURN:
#                print('\n'.join([''.join(row) for row in rot_box.text])+'\n--------------------')

    rt = int(rot_num.text[0]) if rot_num.text[0] else 0
    rot_box.text = [[rot(char, rt) for char in row] for row in box.text]
    wn.fill((255, 255, 200))
    box.draw()
    rot_num.draw()
    rot_box.draw()
    pygame.display.flip()

If you scroll down you will see

box = TextBox(110, 60, 140, 32, 'Input Text')
rot_num = TextBox(10, 60, 50, 32, 'Rot', default='0')
rot_box = TextBox(110, 300, 140, 32, 'Output Text')

There you can choose the position, size, title, color and default text of each text box.

If you want to be able to copy-paste the output text, simply remove the two #s from this part of the code:

#            if event.key == pygame.K_RETURN:
#                print('\n'.join([''.join(row) for row in rot_box.text])+'\n--------------------')

and you will be able to whenever you hit Enter.

I know there's rot13.com, but hey, this is customizable and can be used offline.