Header Ads

Header ADS

Practice Morse Code

 For practicing morse code there is no good source. So why not build our own. Here I am with brand new code.

Though there need some development. I will see them letter.

The code:

import pygame, time

text = input("Enter text to translate: ")

# Initialize pygame and set the screen size
pygame.init()
screen = pygame.display.set_mode((400, 300))

# Define a dictionary of letters and their corresponding Morse code
morse_code = {
    'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
    'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
    'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
    'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
    'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
    'Z': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
    '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
    '0': '-----', ' ': '/'
}

# Get the text to translate from the user
#text = "ahsan"#input("Enter text to translate: ")

# Convert the text to uppercase
text = text.upper()

# Create a font object to display the text on the screen
font = pygame.font.Font(None, 36)

# Loop through each character in the text
for char in text:
    # Get the corresponding Morse code for the character
    morse = morse_code.get(char)
    # Loop through each symbol in the Morse code
    for symbol in morse:
        if symbol == '.':
            # Display the letter on the screen
            text_surface = font.render(char, True, (255, 255, 255))
            screen.blit(text_surface, (200, 150))
            # Flash the circle white for a short duration
            pygame.draw.circle(screen, (255, 255, 255), (200, 150), 50)
            pygame.display.update()
            time.sleep(0.2)
        elif symbol == '-':
            # Display the letter on the screen
            text_surface = font.render(char, True, (100, 255, 2))
            screen.blit(text_surface, (200, 150))
            # Flash the circle white for a longer duration
            pygame.draw.circle(screen, (255, 255, 255), (200, 150), 50)
            pygame.display.update()
            time.sleep(0.2)
            # Turn the circle off
            pygame.draw.circle(screen, (0, 0, 0), (200, 150), 50)
            pygame.display.update()
            time.sleep(0.2)
           
    # Clear the screen for the next letter
    screen.fill((0, 0, 0))
    pygame.display.update()
    time.sleep(0.5)

# Exit pygame
pygame.quit()



Result:




explain:  Read the code properly.


Great news !!!!!!!


pygame window was not working properly. after any click it get stuck and no more response and also you need to install pygame before running the code. I tried many time to improve the code. but sadly with pygame it is not possible. So I tried tkinter which is built in pkg. So you don't need to install any pkg to run this code and also instead of using terminal now i am using GUI interface to take input. You have many more freedom now. You can change duration, flashlight color, bg color, text color and a lot. I am thinking from now i will take input in GUI so it will be more user friendly.


The code:

import tkinter as tk
import time

# Define a dictionary of letters and their corresponding Morse code
morse_code = {
    'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
    'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
    'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
    'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
    'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
    'Z': '--..', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
    '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
    '0': '-----', ' ': '/'
}

#color adjustment
light = 'white'
bgc = 'black' #background color

#time dureation adjustment
dit_len = 0.5
dah_len = 1
rbl = 1 #rest between letter

# Define the function to flash a circle
def flash_circle(color, duration):
    canvas.itemconfig(circle, fill=color)
    root.update()
    time.sleep(duration)
    #canvas.itemconfig(circle, fill='white')
    canvas.itemconfig(circle, fill='black')
    root.update()
    time.sleep(duration/2)

# Define the function to translate the text to Morse code and flash the circle
def translate_and_flash():
    # Get the text to translate from the user
    text = entry.get().upper()





    # Loop through each character in the text
    for char in text:
        # Get the corresponding Morse code for the character
        morse = morse_code.get(char)
        # Display the letter on the screen
        canvas.itemconfig(letter, text=char)
        root.update()
        # Loop through each symbol in the Morse code
        for symbol in morse:
            if symbol == '.':
                # Flash the circle white for a short duration
                flash_circle(light, dit_len)
            elif symbol == '-':
                # Flash the circle white for a longer duration
                flash_circle(light, dah_len)
        # Clear the screen for the next letter
        canvas.itemconfig(letter, text='')
        root.update()
        time.sleep(rbl)

# Create the Tkinter window and canvas
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=300, bg = bgc)
canvas.pack()

# Create the circle and letter objects
#circle = canvas.create_oval(175, 125, 225, 175, fill='white')
circle = canvas.create_oval(300, 125, 225, 175, fill='black')
letter = canvas.create_text(200, 150, text='', fill='green', font=('Arial', 36))

# Create the entry and button objects
entry = tk.Entry(root, width=30, font=('Arial', 18))
entry.pack(pady=10)
button = tk.Button(root, text='Translate and Flash', command=translate_and_flash)
button.pack(pady=10)

root.mainloop()


Output:






No comments

Powered by Blogger.