curssses

Snake game for the linux terminal.
git clone git://git.amin.space/curssses.git
Log | Files | Refs | LICENSE

commit def33a23cd09f8e2e2da44e8c20869fcff626289
parent 2feda4ec00d89e38d43823d303405f55173b5746
Author: amin <dev@aminmesbah.com>
Date:   Thu,  1 Dec 2016 05:13:30 +0000

Add snake-like drifting movement; screen wrapping.

FossilOrigin-Name: eb409c917ac5923ed89144ad2b6fade24ca2c3543bdc115f2917cc81e358ee51
Diffstat:
Mcurssses.c | 103++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 76 insertions(+), 27 deletions(-)

diff --git a/curssses.c b/curssses.c @@ -2,11 +2,26 @@ #include <locale.h> #include <stdio.h> #include <stdlib.h> +#include <unistd.h> -#define DISP_W 60 -#define DISP_H 24 +#define SECOND 1000000 +#define FPS 20 -#define HERO_CHAR '@' +typedef enum +{ + LEFT, + RIGHT, + UP, + DOWN, +} direction; + +typedef struct +{ + char symbol; + int x; + int y; + direction d; +} snake; int main(void) { @@ -15,56 +30,90 @@ int main(void) cbreak(); noecho(); nonl(); + nodelay(stdscr, TRUE); intrflush(stdscr, FALSE); keypad(stdscr, TRUE); curs_set(0); - int hero_x = DISP_W / 2; - int hero_y = DISP_H / 2; - for (int y = 0; y < DISP_H; ++y) - { - for (int x = 0; x < DISP_W; ++x) - { - mvaddch(y, x, '.'); - } - } - mvaddch(hero_y, hero_x, HERO_CHAR); - printw(" (%d, %d)", hero_x, hero_y); - refresh(); + snake s; + s.symbol = 'o'; + s.x = COLS / 2; + s.y = LINES / 2; int input = 0; while (1) { + input = getch(); + erase(); - for (int y = 0; y < DISP_H; ++y) - { - for (int x = 0; x < DISP_W; ++x) - { - mvaddch(y, x, '.'); - } - } + + printw("Position: (%d, %d) Total: [%d, %d]", s.x, s.y, COLS, LINES); switch (input) { case KEY_LEFT: + case 'h': { - mvaddch(hero_y, --hero_x, HERO_CHAR); + s.d = LEFT; } break; case KEY_RIGHT: + case 'l': { - mvaddch(hero_y, ++hero_x, HERO_CHAR); + s.d = RIGHT; } break; case KEY_UP: + case 'k': { - mvaddch(--hero_y, hero_x, HERO_CHAR); + s.d = UP; } break; case KEY_DOWN: + case 'j': { - mvaddch(++hero_y, hero_x, HERO_CHAR); + s.d = DOWN; } break; } - printw(" (%d, %d)", hero_x, hero_y); + + switch (s.d) + { + case LEFT: + { + --s.x; + } break; + case RIGHT: + { + ++s.x; + } break; + case UP: + { + --s.y; + } break; + case DOWN: + { + ++s.y; + } break; + } + + if (s.y == LINES) + { + s.y = 0; + } + else if (s.y < 0) + { + s.y = LINES - 1; + } + if (s.x == COLS) + { + s.x = 0; + } + else if (s.x < 0) + { + s.x = COLS - 1; + } + + mvaddch(s.y, s.x, s.symbol); + refresh(); + usleep(SECOND / FPS); } clear();