Skip to content

Instantly share code, notes, and snippets.

@jarobins
Created April 12, 2016 10:50
Show Gist options
  • Select an option

  • Save jarobins/c72fee8bd5527f392f5e84309269d113 to your computer and use it in GitHub Desktop.

Select an option

Save jarobins/c72fee8bd5527f392f5e84309269d113 to your computer and use it in GitHub Desktop.

Revisions

  1. jarobins created this gist Apr 12, 2016.
    104 changes: 104 additions & 0 deletions morse_test.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    """
    Pygame base template for opening a window
    """

    import pygame
    from datetime import datetime

    # Define some colors
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    RED = (255, 0, 0)

    pygame.init()
    pygame.font.init()

    my_font = pygame.font.SysFont("Courier New", 14)

    # Set the width and height of the screen [width, height]
    size = (700, 500)
    screen = pygame.display.set_mode(size)

    pygame.display.set_caption("Morse Code")

    # Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()
    y_pos, x_pos = 0, 0

    # -------- Main Program Loop -----------
    textsurface = my_font.render('Start', False, (0,0,0))
    master_time = datetime.now()
    letter = ''

    screen.fill(WHITE)

    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',

    '-----': '0', '.----': '1', '..---': '2',
    '...--': '3', '....-': '4', '.....': '5',
    '-....': '6', '--...': '7', '---..': '8',
    '----.': '9',
    '':'_'
    }

    while not done:
    # --- Main event loop
    if (datetime.now() - master_time).total_seconds() > 2:
    textsurface = my_font.render(CODE[letter], False, (0,0,0))
    letter = ''
    master_time = datetime.now()
    x_pos += 10

    if x_pos > 500:
    x_pos = 0
    y_pos += 14

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    done = True

    # --- Game logic should go here
    if event.type == pygame.KEYDOWN:
    cur_time = datetime.now()
    if event.type == pygame.KEYUP:
    master_time = datetime.now()
    past_time = datetime.now() - cur_time
    if past_time.total_seconds() > .1:
    letter += '-'
    #textsurface = my_font.render('-', False, (0,0,0))
    else:
    letter += '.'
    #textsurface = my_font.render('.', False, (0,0,0))
    # --- Screen-clearing code goes here

    # Here, we clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.

    # If you want a background image, replace this clear with blit'ing the
    # background image.
    # screen.fill(WHITE)

    # --- Drawing code should go here
    screen.blit(textsurface,(x_pos, y_pos))

    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

    # --- Limit to 60 frames per second
    clock.tick(60)

    # Close the window and quit.
    pygame.quit()