Using Curses, Draw A Smiley Face

Project #1

Using Curses, draw a smiley face. Sample Curses code is shown below.

Sample Code

import curses
from curses import wrapper
import time


def main(stdscr):

    # ---- create color (id,forground,background)
    curses.init_pair(1,curses.COLOR_RED,curses.COLOR_BLACK)
    curses.init_pair(2,curses.COLOR_WHITE,curses.COLOR_BLACK)
    red_black = curses.color_pair(1)
    white_black = curses.color_pair(2)

    # ---- clear screen
    stdscr.clear()

    # ---- loop
    loop_count = 12 
    while True:

        # ---- refresh screen
        stdscr.refresh()

        # ---- add text (row,col,text,color)
        if loop_count % 2:
            stdscr.addstr(5,5,'hello world',red_black)
        else:
            stdscr.addstr(5,5,'hello world',white_black)

        # ---- decrement loop counter
        if loop_count > 0:
            loop_count -= 1
        else:
            break

        # ---- wait for 0.25 seconds then do it again
        time.sleep(0.25)

# ---- initialize curses

wrapper(main)

or

#!/usr/bin/python3
# ===================================================================
# screen row/column coordinates
#
#        (0,0)   COLUMN
#          + --------------->
#          |
#     ROW  |
#          |
#          |
#          V
#
# ===================================================================

import curses
from curses import wrapper
import time
import sys

# -------------------------------------------------------------------
# ---- main function
# -------------------------------------------------------------------

def main():

    # ---- setup curses

    try:
        stdscr = curses.initscr() # init screen (window)
        curses.noecho()           # prevents user input from being echoed
        curses.curs_set(0)        # make cursor invisible

        # ---- tty driver buffers typed characters until a newline or
        # ---- carriage return is typed. The cbreak routine disables line
        # ---- buffering and erase/kill character-processing. Interrupt
        # ---- and flow control characters are unaffected.
        curses.cbreak()

        # ---- create color (id,foreground,background)
        curses.start_color()
        curses.init_pair(1,curses.COLOR_RED,curses.COLOR_BLACK)
        curses.init_pair(2,curses.COLOR_WHITE,curses.COLOR_BLACK)
        red_black = curses.color_pair(1)
        white_black = curses.color_pair(2)

        # ---- clear screen
        stdscr.clear()

    except Exception as e:
        print(str(e))
        curses.nobreak()
        sys.exit()

    # ---- window (screen) size
    rows,cols = stdscr.getmaxyx()
    stdscr.addstr(0,0,f'screen rows={rows}, cols={cols}',white_black)

    loop_count = 12

    while True:

        # ---- refresh screen

        stdscr.refresh()

        # ---- add text (row,col,text,color)

        if loop_count % 2:
            stdscr.addstr(5,5,'hello world',red_black)
        else:
            stdscr.addstr(5,5,'hello world',white_black)

        # ---- decrement loop counter (test loop)

        if loop_count > 0:
            loop_count -= 1
        else:
            break

        # ---- wait for 0.25 seconds the do it again

        time.sleep(0.25)

    curses.endwin()

# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

main()