Simple Games using ncurses
Menu
/* "Snake_5.3" Game [email protected] */ #include <ncurses.h> #include <list> #include <stdlib.h> #include <time.h> class Snake { private: int p_x, p_y; public: Snake(int play_col, int play_row) { p_x = play_col; p_y = play_row; } int locate_col() { return p_x; } int locate_row() { return p_y; } }; void ncurses_activate() { srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(1000 / 4); } struct Timers { int clock_1, clock_2; }; Timers T = { 0, 0 }; int clock_1 = 0; int clock_2 = 0; struct Player { int energy, points, dir, miss, hit, crash, getkey, food_x, food_y; }; Player p = { 50, 0, 2, 0, 0, 0, 0, 14, 4 }; struct Machine { int x, y, dir; }; Machine jet = { 14, 1, 0 }; void timers_on() { T.clock_1++; T.clock_2++; } enum Border { B_LEFT = 0, B_RIGHT = 34, B_TOP = 0, B_BOTTOM = 14 }; enum Colors { BK = 0, RD = 1, GN = 2, YW = 3, BE = 4, MA = 5, CN = 6, WE = 7 }; void add_color() { /* COLOR_BLACK 0 COLOR_RED 1 COLOR_GREEN 2 COLOR_YELLOW 3 COLOR_BLUE 4 COLOR_MAGENTA 5 COLOR_CYAN 6 COLOR_WHITE 7 */ start_color(); bkgd(COLOR_PAIR(1)); init_pair(1, WE, BK); init_pair(2, BK, RD); init_pair(3, RD, BK); init_pair(4, CN, BK); init_pair(5, GN, BK); init_pair(6, WE, BE); } void display() { attrset(COLOR_PAIR(3)); attron(A_BOLD); mvprintw(p.food_y, p.food_x, "@"); attrset(COLOR_PAIR(4)); attron(A_BOLD); mvprintw(jet.y, jet.x, "[********]"); attrset(COLOR_PAIR(2)); mvprintw(0, 0, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); mvprintw(15, 0, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); for (int wall = 0; wall < 15; wall++) { mvprintw(wall, 0, "X"); mvprintw(wall, 35, "X"); } attrset(COLOR_PAIR(4)); attron(A_BOLD); mvprintw(16, 0, "Energy: %i", p.energy); attrset(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(16, 18, "Score: %i", p.points); } void game_over() { erase(); attrset(COLOR_PAIR(6)); for (int c = 2; c < 31; c++) { mvprintw(0, c, "@"); mvprintw(14, c, "@"); for (int r = 1; r < 15; r++) { mvprintw(r, 2, "@"); mvprintw(r, 30, "@"); } } attrset(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(2, 10, "GAME OVER"); mvprintw(4, 10, " Summary:"); mvprintw(6, 10, "Collisions: %i", p.crash); mvprintw(7, 10, "missed_food: %i", p.miss); mvprintw(8, 10, "Hits by jet: %i", p.hit); mvprintw(12, 10, "Score: %i", p.points); mvprintw(10, 4, "///////////////////////"); refresh(); getch(); timeout(-1); endwin(); } int main() { ncurses_activate(); add_color(); std::list < Snake > snakes; std::list < Snake >::iterator it; for (int i = 0; i < 3; i++) snakes.push_front(Snake(5 + i, 6)); while (1) { p.getkey = getch(); switch (p.getkey) { case 'a': case 's': case 'd': p.dir = 1; break; case 'j': case 'k': case 'l': p.dir = 2; break; case 'r': case 't': case 'y': case 'u': p.dir = 3; break; case 'c': case 'v': case 'b': case 'n': case ' ': p.dir = 4; break; case 10: attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(7, 12, "PAUSED"); refresh(); while (1) { p.getkey = getch(); if (p.getkey == 10) { break; } } break; } Snake logic = snakes.front(); int p_x = logic.locate_col(); int p_y = logic.locate_row(); enum Move { NONE = 0, LEFT = 1, RIGHT = 2, UP = 3, DOWN = 4 }; switch (p.dir) { case LEFT: p_x--; break; case RIGHT: p_x++; break; case UP: p_y--; break; case DOWN: p_y++; break; } switch (jet.dir) { case LEFT: jet.x--; break; case RIGHT: jet.x++; break; case UP: jet.y--; break; case DOWN: jet.y++; break; } erase(); timers_on(); snakes.push_front(Snake(p_x, p_y)); if (T.clock_1 >= 22 + rand() % 10 + 1) { p.food_x = rand() % 29 + 1; p.food_y = 1 + rand() % 10 + 1; p.energy -= 3 + rand() % 6; T.clock_1 = 0; p.miss++; } if (p_x == p.food_x && p_y == p.food_y) { p.food_x = rand() % 29 + 1; p.food_y = 1 + rand() % 10 + 1; p.energy += 5 + rand() % 15; p.points += 100 + rand() % 400; T.clock_1 = 0; } else snakes.pop_back(); if (p_x < B_LEFT) { p.dir = RIGHT; } if (p_x > B_RIGHT) { p.dir = LEFT; } if (p_y < B_LEFT) { p.dir = DOWN; } if (p_y > B_BOTTOM) { p.dir = UP; } display(); if (T.clock_2 >= 5 + rand() % 5) jet.dir = DOWN; if (jet.y > B_BOTTOM) { jet.y = 1 + rand() % 2; jet.x = 1 + rand() % 15; T.clock_2 = 0; jet.dir = 0; } if (p_x >= jet.x && p_x <= jet.x + 10 && p_y == jet.y) { for (int r = 0; r < 15; r++) { mvprintw(r, p_x, ":"); p.energy -= 2; } p.hit++; } for (it = snakes.begin(); it != snakes.end(); it++) { if (p.energy < 25) { attrset(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(2, 6, "WARNING: LOW ENERGY!!"); } attron(COLOR_PAIR(5)); attron(A_BOLD); mvprintw((*it).locate_row(), (*it).locate_col(), "O"); // Self destruction if ((*it).locate_row() == p_y && (*it).locate_col() == p_x && it != snakes.begin()) { attron(COLOR_PAIR(1)); mvprintw(0, 0, "Self Harm :-("); p.energy -= 10; p.crash++; } if (p.energy <= 0) game_over(); } } }
0 Comments
Leave a Reply. |
Archives
March 2016
Categories |