Simple Games using ncurses
Menu
//Snake_8.3
#include <ncurses.h> #include <list> #include <stdlib.h> #include <time.h> class Snake { private: int x, y; public: Snake(int a, int b) { x = a; y = b; } int getX() { return x; } int getY() { return y; } }; enum Nav { LEFT = 1, RIGHT = 2, UP = 3, DOWN = 4, LEFT_UP = 5, RIGHT_UP = 6, LEFT_DOWN = 7, RIGHT_DOWN = 8 }; enum Colors { BK = 0, RD = 1, GN = 2, YW = 3, BE = 4, MA = 5, CN = 6, WE = 7 }; enum Border_limit { MIN_X = -1, MAX_X = 32, MIN_Y = 1, MAX_Y = 15 }; class Player { public: int getkey, dir, lives, energy, points, clock; }; Player p = { -1, 2, 3, 20, 0, 0 }; class Machine { public: int dir, x, y; }; Machine spaceship = { 1, 100, 3 }; class Food { public: int x, y, clock; }; Food apple = { 20, 5, 0 }; void add_color() { start_color(); bkgd(COLOR_PAIR(1)); init_pair(1, WE, BK); init_pair(2, GN, BK); init_pair(3, MA, BK); init_pair(4, RD, BK); init_pair(5, BK, CN); init_pair(6, BE, WE); } void start_both_clocks() { apple.clock++; p.clock++; } void start_cursing() { srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(200); } void draw() { attrset(COLOR_PAIR(1)); for (int r = 1; r < 16; r++) { mvprintw(r, 0, "B"); mvprintw(r, 32, "B"); }; mvprintw(1, 0, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); mvprintw(15, 0, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"); attrset(COLOR_PAIR(1)); mvprintw(0, 0, "Lives:%i", p.lives); mvprintw(0, 20, "Energy:%i", p.energy); mvprintw(16, 0, "Score:%i", p.points); attrset(COLOR_PAIR(4)); mvprintw(apple.y, apple.x, "@"); mvprintw(16, 15, "Spaceship GPS: %i", spaceship.x); } void draw_plane() { attrset(COLOR_PAIR(1)); mvprintw(spaceship.y, spaceship.x, "[==]"); } void player_nav() { p.getkey = getch(); switch (p.getkey) { case KEY_LEFT: case 'w': case 'a': case 's': case 'd': p.dir = LEFT; break; case KEY_RIGHT: case 'i': case 'o': case 'j': case 'k': case 'l': p.dir = RIGHT; break; case KEY_UP: case 'r': case 't': case 'y': case 'u': p.dir = UP; break; case KEY_DOWN: case 'c': case 'v': case 'b': case ' ': p.dir = DOWN; break; case 'q': p.dir=LEFT_UP; break; case 'p': p.dir=RIGHT_UP; break; case ',': p.dir=LEFT_DOWN; break; case '.': p.dir=RIGHT_DOWN; break; case 10: attrset(COLOR_PAIR(1)); mvprintw(5, 12, " PAUSED "); refresh(); while (1) { p.getkey = getch(); if (p.getkey == 10) { break; } } break; } } void game_end() { erase(); timeout(-1); attrset(COLOR_PAIR(1)); mvprintw(6, 12, "GAME OVER"); mvprintw(8, 12, "SCORE: %i", p.points); if (p.points<2000) mvprintw(10, 12, "Need more practice."); if (p.points>=2000 && p.points<=4000) mvprintw(10, 12, "Good Game!"); if (p.points>4000 && p.points<=8000) mvprintw(10, 12, "You did fantastic!!!"); if (p.points>8000) mvprintw(10, 12, "You are a Snake Master!!"); refresh(); getch(); endwin(); } int main() { start_cursing(); 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) { player_nav(); start_both_clocks(); Snake logic = snakes.front(); int x = logic.getX(); int y = logic.getY(); switch (p.dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; case LEFT_UP: x--; y--; break; case RIGHT_UP: x++; y--; break; case LEFT_DOWN: x--; y++; break; case RIGHT_DOWN: x++; y++; break; } attron(A_BOLD); snakes.push_front(Snake(x, y)); if (apple.clock >= 25 + rand() % 10 + 1) { apple.x = 1 + rand() % 29; apple.y = 2 + rand() % 11; p.energy -= 5; apple.clock = 0; } if (x == apple.x && y == apple.y) { apple.x = rand() % 29; apple.y = 2 + rand() % 11; p.energy += 2 + rand() % 5; p.points += 50 + rand() % 100; apple.clock = 0; } else snakes.pop_back(); erase(); if (spaceship.y >= MAX_Y) { spaceship.x = 1 + rand() % 25; spaceship.y = rand() % 1 + 2; } draw(); if (p.clock >= 2 && p.clock <= 8) { attrset(COLOR_PAIR(1)); mvprintw(5, 12, "STAGE 1 "); } if (p.clock >= 10 && p.clock <= 18) { attrset(COLOR_PAIR(1)); mvprintw(2, 9, "Eat Food!"); mvprintw(apple.y, apple.x - 2, ">"); mvprintw(apple.y, apple.x + 2, "<"); }; if (spaceship.x<=30) { spaceship.dir = DOWN; draw_plane(); } if (spaceship.dir == DOWN) spaceship.y++; if (spaceship.dir == LEFT) spaceship.x--; if (x >= spaceship.x && x <= spaceship.x + 3 && y == spaceship.y) { attrset(COLOR_PAIR(1)); for (int r = 1; r < 12; r++) { mvprintw(r, x, ":"); } p.energy -= 1 + rand() % 2; } attrset(A_BOLD); for (it = snakes.begin(); it != snakes.end(); it++) { attron(A_BOLD); attrset(COLOR_PAIR(2)); mvprintw((*it).getY(), (*it).getX(), "O"); if ((*it).getY() == y && (*it).getX() == x && it != snakes.begin()) p.energy-=10; if (x < MIN_X){ p.dir=RIGHT; p.energy-=5; }; if (x > MAX_X){ p.dir=LEFT; p.energy-=5; }; if (y <= MIN_Y){ p.dir=DOWN; p.energy-=5; }; if (y >= MAX_Y){ p.dir=UP; p.energy-=5; }; } if (p.energy<1){ p.energy=30; p.lives--; }; if (p.energy>60){ p.energy=30; p.lives++; }; if (p.lives <= 0) game_end(); /* Auto_pilot mode: if (x<apple.x) p.dir=RIGHT; if (x>apple.x) p.dir=LEFT; if (y<apple.y) p.dir=DOWN; if (y>apple.y) p.dir=UP; */ } }
0 Comments
Leave a Reply. |
Archives
March 2016
Categories |