Skip to content

Instantly share code, notes, and snippets.

@PROPHESSOR
Created August 4, 2018 22:05
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 PROPHESSOR/0b10645fe28faa2d76991cc443727114 to your computer and use it in GitHub Desktop.
Save PROPHESSOR/0b10645fe28faa2d76991cc443727114 to your computer and use it in GitHub Desktop.
/**
* autopaint.c
* Copyright (c) PROPHESSOR 05.08.2018
* $ sudo apt install libncurses-dev
* $ gcc -g autopaint.c -o autopaint -lncurses && ./autopaint
*/
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#define SYMBOL '#'
void usleep(int);
short rnd(short min, short max) { return rand() % max + min; }
int main() {
srand(time(NULL));
short SCW;
short SCH;
initscr();
getmaxyx(stdscr, SCH, SCW);
keypad(stdscr, true);
nodelay(stdscr, true);
noecho();
curs_set(0);
bool isColorful = has_colors();
if(isColorful) {
if(start_color() == OK) {
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_YELLOW, COLOR_BLACK);
init_pair(3, COLOR_GREEN, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_pair(5, COLOR_BLUE, COLOR_BLACK);
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
} else {
printw("Can't initialize color mode!\n");
isColorful = false;
}
} else printw("Your terminal seems doesn't support colors!\n");
printw("Hello world! Press BACKSPACE key to exit.\n");
short x = SCW / 2;
short y = SCH / 2;
move(y, x);
while(1) {
if(getch() == KEY_BACKSPACE) break;
usleep(10 * 1000);
short move = rnd(0, 4);
switch(move) {
case 0: if(y > 0) y--; break;
case 1: if(x > SCW / 2) x--; break;
case 2: if(y < SCH - 1) y++; break;
case 3: if(x < SCW - 1) x++; break;
}
if(isColorful) attrset(COLOR_PAIR(rnd(1, 7)));
move(y, x);
addch(SYMBOL);
move(y, SCW - x);
addch(SYMBOL);
refresh();
}
endwin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment