curssses

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

commit 2feda4ec00d89e38d43823d303405f55173b5746
Author: amin <dev@aminmesbah.com>
Date:   Thu,  1 Dec 2016 00:06:10 +0000

Initial commit. Move '@' around.

FossilOrigin-Name: d57e3828b39e43251ff461fd0d15919776d8f4b917ae9d769bbbfb584a9cd330
Diffstat:
A.gitignore | 1+
AMakefile | 9+++++++++
Acurssses.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +curssses diff --git a/Makefile b/Makefile @@ -0,0 +1,9 @@ +EXECUTABLE = curssses + +build: + gcc -Wall -Wextra -lcurses curssses.c -o $(EXECUTABLE) + +run: + ./$(EXECUTABLE) + +test: build run diff --git a/curssses.c b/curssses.c @@ -0,0 +1,75 @@ +#include <curses.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> + +#define DISP_W 60 +#define DISP_H 24 + +#define HERO_CHAR '@' + +int main(void) +{ + setlocale(LC_ALL, ""); + initscr(); + cbreak(); + noecho(); + nonl(); + 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(); + + 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, '.'); + } + } + switch (input) + { + case KEY_LEFT: + { + mvaddch(hero_y, --hero_x, HERO_CHAR); + } break; + case KEY_RIGHT: + { + mvaddch(hero_y, ++hero_x, HERO_CHAR); + } break; + case KEY_UP: + { + mvaddch(--hero_y, hero_x, HERO_CHAR); + } break; + case KEY_DOWN: + { + mvaddch(++hero_y, hero_x, HERO_CHAR); + } break; + } + printw(" (%d, %d)", hero_x, hero_y); + refresh(); + } + + clear(); + endwin(); + exit(0); + + return 0; +}