Simple Games using ncurses
Menu
#include <string> #include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int main() { start: srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(175); bool quit = false; int ch; int life=30; int points=0; int player_direct=0; int x=12; int y=6; int food_x=2+rand()%20; int food_y=2+rand()%9; int rock_direct=0; int rock_x=rand()%20; int rock_y=rand()%2; int bird_direct=0; int bird_x=16+rand()%9; int bird_y=1+rand()%10; int clock_1=0; int clock_2=0; //initiate color function start_color(); bkgd(COLOR_PAIR(1)); init_pair(1, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_RED, COLOR_BLACK); init_pair(3, COLOR_MAGENTA, COLOR_BLACK); init_pair(4, COLOR_WHITE, COLOR_BLACK); init_pair(5, COLOR_WHITE, COLOR_BLACK); init_pair(6, COLOR_RED, COLOR_WHITE); while (!quit) { ch = getch(); switch (ch) { case 'q': player_direct = 1; break; case 'w': player_direct = 1; break; case 'a': player_direct = 1; break; case 's': player_direct = 1; break; case 'z': player_direct = 1; break; case 'x': player_direct = 1; break; case 'o': player_direct = 2; break; case 'k': player_direct = 2; break; case 'l': player_direct = 2; break; case 'n': player_direct = 2; break; case 'm': player_direct = 2; break; case 'r': player_direct = 3; break; case 't': player_direct = 3; break; case 'y': player_direct = 3; break; case 'u': player_direct = 3; break; case 'c': player_direct = 4; break; case 'v': player_direct = 4; break; case 'b': player_direct = 4; break; case ' ': player_direct = 4; break; case '1': quit = true; break; case '0': timeout(555555); break; case 'p': timeout(175); break; } clock_1++; clock_2++; if (clock_1>10+rand()%5) rock_direct=4; if (clock_1>20+rand()%5) bird_direct=1; if (clock_2>=25+rand()%15){ food_x=rand()%31; food_y=1+rand()%11; clock_2=rand()%3; life=life-2; } //this erase function keep the moving characters from leaving a trace on the screen erase(); bool collision=false; bool collision_2=false; //player gets hit in certain conditions if (x>=rock_x && x<=rock_x+10 && rock_y==y || x==bird_x && y==bird_y){ life=life-3; collision=true; } if (x==food_x && y==food_y){ collision_2=true; food_x=rand()%31; food_y=1+rand()%11; life=life+5; points=points+50; } if (collision) { bkgd(COLOR_PAIR(6)); attrset(COLOR_PAIR(4)); mvprintw(1,0,":-("); mvprintw(y-1,x,"x"); mvprintw(y+1,x,"x"); mvprintw(y,x-1,"x"); mvprintw(y,x+1,"x"); } else bkgd(COLOR_PAIR(1)); if (collision_2) { attrset(COLOR_PAIR(4)); mvprintw(1,0,":-)"); mvprintw(y-1,x,"o"); mvprintw(y+1,x,"o"); mvprintw(y,x-1,"o"); mvprintw(y,x+1,"o"); } //keys or buttons pressed switch (player_direct){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } switch (rock_direct){ case 1: rock_x--; break; case 2: rock_x++; break; case 3: rock_y--; break; case 4: rock_y++; break; } switch (bird_direct){ case 1: bird_x--; break; case 2: bird_x++; break; case 3: bird_y--; break; case 4: bird_y++; break; } if (rock_x<0) rock_x=29; if (rock_x>29) rock_x=0; if (rock_y>14){ rock_y=rand()%2; rock_x=rand()%22; } //this code makes bird more likely to hit player if (bird_x<0 && x<=14){ bird_x=4+rand()%10; bird_y=y;} if (bird_x<0 && x>=15){ bird_x=19+rand()%10; bird_y=y;} if (x<0) x=32; if (x>32) x=0; if (y<0) player_direct=4; if (y>12) player_direct=3; //game over if (life<=0) quit=true; //player attrset(COLOR_PAIR(2)); mvprintw(food_y, food_x, "@"); attrset(COLOR_PAIR(3)); mvprintw(y,x,"O"); attrset(COLOR_PAIR(1)); mvprintw(bird_y,bird_x,"]"); attrset(COLOR_PAIR(5)); mvprintw(rock_y,rock_x,"oxoxoxoxoxo"); attrset(COLOR_PAIR(4)); mvprintw(0,1,"life:"); mvprintw(0,6,"%i", life); mvprintw(0,17,"SCORE:"); mvprintw(0,23,"%i", points); }//while (!quit) bracket. Do not delete this bracket //The area below here is for when quit=true, which stops the program. timeout(-1); erase(); mvprintw(5, 9, "GAME OVER"); mvprintw(7,9,"SCORE: $"); mvprintw(7,16,"%i", points); refresh(); getch(); endwin(); goto start; // restart game }//Closing bracket of main function
2 Comments
//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; */ } } int ch;
ch=getch(); switch(ch) { case 'p': mvprintw(7,12,"[ PAUSED ]"); refresh(); while (1) { key_press = getch(); if (key_press == 'p') { break; } } break; } |
Archives
March 2016
Categories |