Skip to content

Instantly share code, notes, and snippets.

@raymondberg
Last active February 18, 2019 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raymondberg/0e5d8301917cd5757fb9081c0d10fcde to your computer and use it in GitHub Desktop.
Save raymondberg/0e5d8301917cd5757fb9081c0d10fcde to your computer and use it in GitHub Desktop.
Group Programming Problem to Use Curses to Draw a House
import sys,os
import curses
def draw_house(screen):
# Your job is to draw a house! Feel free to get creative!
pass
def draw_square(screen, color, offset_x=0, offset_y=0):
screen.addstr(0 + offset_x, 0 + offset_y, 'X', color)
screen.addstr(0 + offset_x, 1 + offset_y, 'X', color)
screen.addstr(0 + offset_x, 2 + offset_y, 'X', color)
screen.addstr(1 + offset_x, 0 + offset_y, 'X', color)
screen.addstr(1 + offset_x, 2 + offset_y, 'X', color)
screen.addstr(2 + offset_x, 0 + offset_y, 'X', color)
screen.addstr(2 + offset_x, 1 + offset_y, 'X', color)
screen.addstr(2 + offset_x, 2 + offset_y, 'X', color)
def draw(screen):
k = 0
# Clear and refresh the screen for a blank canvas
screen.clear()
screen.refresh()
# Start colors in curses
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
# Loop where k is the last character pressed
while (k != ord('q')):
draw_square(screen, curses.color_pair(1))
draw_square(screen, curses.color_pair(2), offset_x=3)
draw_square(screen, curses.color_pair(2), offset_y=3)
draw_square(screen, curses.color_pair(1), offset_x=3, offset_y=3)
draw_house(screen)
# Refresh the screen
screen.refresh()
# Wait for next input
k = screen.getch()
if __name__ == "__main__":
curses.wrapper(draw)
'''
Next steps: Did you know curses lets you figure out what keys are pressed? Got anything you can move around?
if k == curses.KEY_DOWN:
elif k == curses.KEY_UP:
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment