Skip to content

Instantly share code, notes, and snippets.

@dangle
Created July 31, 2020 14:26
Show Gist options
  • Save dangle/4ff3898ae1d2513f83bb180c7a70f779 to your computer and use it in GitHub Desktop.
Save dangle/4ff3898ae1d2513f83bb180c7a70f779 to your computer and use it in GitHub Desktop.
#include <curses.h>
#include <signal.h>
#define RED_RED 9
#define WHITE_BLACK 10
#define WHITE_WHITE 11
void done();
void rszf();
void drwc();
void drwf();
const char* TITLE = "Etch-a-Sketch";
const char* MOVE = "Use the Arrow Keys to Move.";
const char* KEYS = "Press 's' to Shake. Press 'q' to Quit.";
int x, y, mx, my, t, r, b, l;
void done()
{
endwin();
exit (0);
}
void rszf()
{
endwin();
curs_set(0);
refresh();
drwf();
drwc();
}
void drwc()
{
if (x > r) x = r;
if (x < l) x = l;
if (y > b) y = b;
if (y < t) y = t;
color_set(WHITE_WHITE, NULL);
mvaddch(y, x, '.');
refresh();
}
void drwf()
{
mx = getmaxx(stdscr);
my = getmaxy(stdscr);
t = 1;
r = mx - 2;
b = my - 2;
l = 1;
clear();
color_set(RED_RED, NULL);
box(stdscr, 0, 0);
if (r - l >= strlen(TITLE) && b - t > 2)
{
color_set(RED_RED, NULL);
mvhline(t + 1, l, 0, r);
attron(A_BOLD);
color_set(WHITE_BLACK, NULL);
mvaddstr(t, (r - l - strlen(TITLE)) / 2, TITLE);
attroff(A_BOLD);
t += 2;
}
if (r - l > 14 && b - t > 8)
{
color_set(RED_RED, NULL);
mvhline(b - 6, l, 0, r);
color_set(WHITE_WHITE, NULL);
mvaddstr(b - 4, l + 2, "....");
mvaddstr(b - 3, l + 1, "......");
mvaddstr(b - 2, l + 1, "......");
mvaddstr(b - 1, l + 2, "....");
mvaddstr(b - 4, r - 6, "....");
mvaddstr(b - 3, r - 7, "......");
mvaddstr(b - 2, r - 7, "......");
mvaddstr(b - 1, r - 6, "....");
if (r - l > strlen(MOVE) + 16 && r - l > strlen(KEYS) + 16)
{
color_set(WHITE_BLACK, NULL);
mvaddstr(b - 3, (r - l - strlen(MOVE) + 2) / 2, MOVE);
mvaddstr(b - 2, (r - l - strlen(KEYS) + 2) / 2, KEYS);
}
b -= 7;
}
touchwin(stdscr);
refresh();
}
int main()
{
signal(SIGINT, done);
signal(SIGWINCH, rszf);
initscr();
keypad(stdscr, TRUE);
nonl();
cbreak();
noecho();
curs_set(0);
if (has_colors())
{
start_color();
init_pair(RED_RED, COLOR_RED, COLOR_RED);
init_pair(WHITE_BLACK, COLOR_WHITE, COLOR_BLACK);
init_pair(WHITE_WHITE, COLOR_WHITE, COLOR_WHITE);
}
drwf();
x = l;
y = t;
drwc();
for (;;)
{
switch (getch())
{
case KEY_UP: y--; break;
case KEY_RIGHT: x++; break;
case KEY_DOWN: y++; break;
case KEY_LEFT: x--; break;
case 's': drwf(); break;
case 'q': done(); break;
}
drwc();
}
return 1;
}
@dangle
Copy link
Author

dangle commented Jan 15, 2021

This is oldest piece of code that I've written that I still have. I wrote it when I was 13!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment