Simple Games using ncurses
Menu
// Cave Escape, 2016 //Brow: Wilson //[email protected] #include <ncurses.h> #include <stdlib.h> #include <time.h> bool message=false; int timer=0; struct Food { int col, row , clock; }; Food bread={ 20, 4, 0 }; int area = 0; enum Movement { NONE, LEFT, RIGHT, UP, DOWN }; enum Color { BK = 0, RD = 1, GN = 2, YW = 3, BE = 4, MA = 5, CN = 6, WE = 7 }; struct Trap { }; struct Energy { int amount, x , y; }; Energy drink={ 1, 40, -5 }; struct Barrier { bool locked; int x, y; }; Barrier door= { 1 }; struct Player { int dir, energy, col, row; }; Player player={ NONE, 100, 5, 8, }; struct Obstacle { }; struct Enemy { int life, dir, row, col; }; Enemy creature = { 10, 1, 6, 22 }; void StartCurses() { srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(175); } void game_over() { player.energy=0; mvprintw(6, 9, "GAME OVER "); getch(); timeout(-1); refresh(); endwin(); } int main() { StartCurses(); start_color(); bkgd(COLOR_PAIR(1)); init_pair(1, BK, BK); init_pair(2, WE, BK); init_pair(3, CN, BK); init_pair(4, BE, BK); init_pair(5, GN, BK); init_pair(6, WE, BE); init_pair(7, WE, RD); init_pair(8, RD, BK); int getkey; while (player.energy > 0) { getkey = getch(); switch (getkey) { case KEY_LEFT: case 'q': case 'w': case 'a': case 's': case 'd': player.dir = LEFT; player.energy--; break; case KEY_RIGHT: case 'i': case 'o': case 'p': case 'j': case 'k': case 'l': player.dir = RIGHT; player.energy--; break; case KEY_UP: case 'r': case 't': case 'y': case 'u': player.dir = UP; player.energy--; break; case KEY_DOWN: case 'c': case 'v': case 'b': case ' ': player.dir = DOWN; player.energy--; break; case 10: case ',': case '.': mvprintw(7, 12, "PAUSED"); refresh(); while (1) { getkey = getch(); if (getkey == 10) { break; } } break; } switch (player.dir) { case 1: player.col--; break; case 2: player.col++; break; case 3: player.row--; break; case 4: player.row++; break; } switch (creature.dir) { case 1: creature.col--; break; case 2: creature.col++; break; case 3: creature.row--; break; case 4: creature.row++; break; case 5: creature.col--; creature.row++; break; case 6: creature.col++; creature.row++; break; } bool collision_energy = false; if (creature.col < 0) { creature.col = player.col+3+rand()%2; creature.row = player.row; creature.life--; } if (creature.col > 30) { creature.col = player.col-3-rand()%2; creature.row = player.row; creature.life--; } if (creature.row < 0) { creature.row = 8 + rand() % 2; creature.col = player.col; } if (creature.row > 17) { creature.row = 1+rand() % 3; creature.col = player.col; creature.life--; } bread.clock++; if (bread.clock >= 26 + rand() % 10) { bread.clock = 0; bread.col = rand() % 29 + 1; bread.row = 1 + rand() % 10 + 1; player.energy-=2; } if (player.col == bread.col && player.row == bread.row) { bread.col = rand() % 29 + 1; bread.row = 1 + rand() % 10 + 1; player.energy+=10; collision_energy = true; } if (player.energy <= 0) game_over(); erase(); attron(COLOR_PAIR(3)); attron(A_BOLD); player.energy > 50 ? mvprintw(player.row, player.col, "O") : mvprintw(player.row, player.col, "o"); bool collision_damage = false; if (area == 0) { player.col = 30; player.row = 0; attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(0,0," OoxoooOoxxxoOOoooxOoxxxOxxoooooooxxO"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(5, 9, "Cave Escape"); mvprintw(9, 5, "(Press [SPACE] Key To Start)"); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(15,0," OoxoOOoxxxoOoOoooxOoxxxOxxoOOOooxOxO"); if (getkey == ' ') { area = 1; player.dir=NONE; player.col = 4; player.row = 8; }; }; if (area == 1) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16,20,"Area 1"); mvprintw(16, 0, "E:%i", player.energy); creature.dir = 4; attron(COLOR_PAIR(3)); mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); mvprintw(creature.row, creature.col, "#"); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(0, 0, "OxxxxxOOxxxOxxxoxoxxxxoooOxxxOxxxO"); mvprintw(15, 0, "xxxxxxOxxxOxOxooxxxxOxxOxxOooxxxxx"); if (player.col < 0) { player.col = 34; area = 2; creature.life=20; door.locked=true; } if (player.col > 34) { player.col = 0; area = 4; door.locked=true; creature.life=20; } if (player.row > 14) player.dir = UP; if (player.row < 2) player.dir = DOWN; if (player.col==creature.col && player.row==creature.row ) { collision_damage = true; player.energy-=3; }; if (creature.row>14){ creature.row=player.row-2-rand()%5; creature.col=player.col; }; if (creature.life<1) creature.dir=NONE; } // area 2 if (area == 2) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16, 0, "Life:%i", player.energy); mvprintw(16, 20, "Area 2"); creature.dir = 2; mvprintw(bread. row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "ox)"); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(0, 0, "oooxxxOOxxoooOoxxoOoxxxoOOxxxOoxxx"); mvprintw(15, 10, "OxxxxxooOxxxoxxOxxOxxxxO"); if (door.locked==true ) mvprintw(15,1,"#########"); for (int r=0; r<16; r++) mvprintw(r,0,"O"); for (int r=15; r<18; r++) mvprintw(r,10,"@"); if (player.col < 0) player.dir = 2; if (player.col > 34) { player.col = 0; area = 1; } if (player.row < 1) player.dir = DOWN; if (player.row > 14 && player.col >= 11) player.dir = UP; if (player.row > 14 && player.col <= 10 && door.locked==false ) { player.row = 0; area = 3; creature.dir=LEFT; door.locked=true; creature.life=20; } if (player.row > 14 && player.col <= 10 && door.locked==true ) player.dir=UP; if (player.col >=creature.col && player.col<=creature.col+2 && player.row==creature.row) { collision_damage=true; player.energy-=10; }; if (creature.life<15) door.locked=false; mvprintw(15,13,"%i", creature.life); } if (area == 3) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16, 0, "Life:%i", player.energy); mvprintw(16, 20, "Area 3"); mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "]xo"); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(1, 11, "xOxxxOoxxxxxOOoxxxx"); mvprintw(15, 0, "xxOoxxOOOxxxxxOOxxxxOOxxxxOOxxx"); if (door.locked==true ) mvprintw(1,1,"##########"); for (int r=0; r<16; r++){ mvprintw(r,0,"@"); mvprintw(r,30,"@"); }; if (player.row < 1 && player.col <= 10 && door.locked==false ) { player.row = 10; area = 2; creature.life=20; door.locked=true; } if (player.row < 2 && player.col <= 10 && door.locked==true ) player.dir=DOWN; if (player.row < 1 && player.col >= 11) player.dir = DOWN; if (player.col < 0) player.dir = RIGHT; if (player.col > 29) player.dir = LEFT; if (player.row > 14) player.dir = UP; if (player.col >=creature.col && player.col<=creature.col+2 && player.row==creature.row){ collision_damage=true; player.energy-=10; }; if (creature.life<1) { creature.dir=NONE; drink.x=15; drink.y=14; attron(COLOR_PAIR(7)); if (drink.amount>0){ mvprintw(drink.y,drink.x,"E");}; door.locked=false; }; if (player.col==drink.x && player.row==drink.y && drink.amount>0) { drink.amount--; player.energy+=200; drink.x=40; drink.y=0; collision_energy=true; }; } if (area == 4) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16, 0, "Life:%i", player.energy); mvprintw(16, 20, "Area 4"); creature.dir = 1; mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "(xo["); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(0, 0, "OxxxxxOOO OxxxxxOOxxOOxxxxxOOOxxxO"); mvprintw(15, 0, "xxxxxxOxxxOxxxxxooxxOxOxxOoooxxxxx"); if (door.locked) mvprintw(0,9,"##"); for (int r=0; r<16; r++) mvprintw(r,29,"@"); if (player.col > 30) player.dir = LEFT; if (player.col < 0) { player.col = 34; area = 1; } if (player.row < 1 && player.col <= 8 || player.row < 1 && player.col >= 11) player.dir = DOWN; if (player.row < 0 && player.col >= 9 && player.col <= 10 && door.locked == false) { player.row = 10; area = 5; } if (player.row < 0 && player.col >= 9 && player.col <= 10 && door.locked == true ) player.dir=DOWN; if (player.row > 14) player.dir = UP; if (player.col >=creature.col && player.col<=creature.col+3 && player.row==creature.row){ collision_damage=true; player.energy-=5; } if (creature.life<15) door.locked=0; } if (area == 5) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16, 0, "Life:%i", player.energy); mvprintw(16, 20, "Area 5"); creature.dir = 5; mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "(xxoxoxx)"); for (int r = 0; r < 16; r++) { attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(r, 8, "O"); mvprintw(r, 11, "O"); }; if (player.col < 9) player.dir = RIGHT; if (player.col > 10) player.dir= LEFT; if (player.row > 10 && player.col >= 9 && player.col <= 10) { player.row = 0; area = 4; } if (player.row < 0) { player.row = 10; area = 6; } if (player.col>=creature.col && player.col<=creature.col+8 && player.row==creature.row) { player.energy-=10; collision_damage=true; }; } if (area == 6) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16, 0, "Life:%i", player.energy); mvprintw(16, 20, "Area 6"); creature.dir = 1; mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, ")xo"); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(2, 8, "xxxOOxxxOxxxxxO"); mvprintw(8, 11, " OoxxOxxxxxxOxxxOoo"); for (int r = 2; r < 16; r++) { mvprintw(r, 8, "0"); } for (int s = 8; s < 16; s++) { mvprintw(s, 11, "0"); } for (int t= 0; t < 9; t++) { mvprintw(t, 29, "0"); } for (int u = 0; u < 3; u++) { mvprintw(u, 22, "0"); }; if (player.col < 9) player.dir = RIGHT; if (player.row < 3 && player.col < 23) player.dir= DOWN; if (player.col > 10 && player.row > 7) player.dir = LEFT; if (player.col > 29) player.dir = LEFT; if (player.col >= 11 && player.row > 7) player.dir = UP; if (player.col >= 9 && player.col <= 10 && player.row > 10) { player.row = 0; area = 5; } if (player.row < 0) { player.row = 10; area = 7; creature.row = 1 + rand() % 2; creature.col = 23 + rand() % 5; creature.dir = 4; } if (player.col >=creature.col && player.col<=creature.col+2 && player.row==creature.row){ collision_damage=true; player.energy-=5; }; }; if (area == 7) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16,0,"Life:%i", player.energy); mvprintw(16, 20, "Area 7"); creature.dir = 4; mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "#"); attron(COLOR_PAIR(1)); attron(A_BOLD); for (int r = 0; r< 16; r++) { mvprintw(r, 22, "0"); mvprintw(r, 29, "0"); }; if (player.col < 23) player.dir = RIGHT; if (player.col > 28) player.dir=LEFT; if (player.row > 11) { player.row = 0; area = 6; } if (player.row < 0) { player.row = 10; creature.dir=LEFT; creature.row = 18 + rand() % 10; creature.row = 4 + rand() % 3; area = 8; } } if (area == 8) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16,0,"Life:%i", player.energy); mvprintw(16, 20, "Area 8"); mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "(xo["); attron(COLOR_PAIR(1)); attron(A_BOLD); for (int r = 0; r < 16; r++) { mvprintw(r, 22, "O"); mvprintw(r, 29, "O"); }; if (player.col < 23) player.dir = RIGHT; if (player.col > 28) player.dir = LEFT; if (player.row > 11) { player.row = 0; area = 7; } if (player.row < 0) { player.row = 10; area = 9; } if (player.col >=creature.col && player.col<=creature.col+3 && player.row==creature.row){ collision_damage=true; player.energy-=10; }; } if (area == 9) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16,0,"Life:%i", player.energy); mvprintw(16, 20, "Area 9"); mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "]xo"); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(6,0, "@@@@@@@@@@@@@@@xxxxoxxxxxxxxxxxooxxxx"); mvprintw(12,0,"@@@@@@@@@@@xxxxxxxxxxxO OxxoOoox"); for (int r=7; r<12; r++) mvprintw(r,0,"@@@"); if (player.col<4) player.dir=RIGHT; if (player.col > 35) { player.col = 0; area = 10; } if (player.row <7) player.dir = DOWN; if (player.row > 11 && player.col<=22 || player.row > 11 && player.col>=29) player.dir=UP; if (player.row > 12 && player.col>=23 && player.col<=30) { player.row=0; area=8; }; if (player.col >=creature.col && player.col<=creature.col+2 && player.row==creature.row) collision_damage=true; if (player.col >=creature.col && player.col<=creature.col+2 && player.row==creature.row){ collision_damage=true; player.energy-=10; }; } if (area == 10) { attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(16,0,"Life:%i", player.energy); mvprintw(16, 20, "Area 10"); mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "]ox("); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(6,0, "#####################################"); mvprintw(10,0, "#####################################"); if ( player.col < 0) { player.col = 30; area = 9; } if (player.col>37) { player.col=0; creature.col=20; creature.row=8; creature.dir=DOWN; area=11; }; if (player.row < 7) player.dir = DOWN; if (player.row > 9) player.dir=UP; if (player.col >=creature.col && player.col<=creature.col+3 && player.row==creature.row){ collision_damage=true; player.energy-=10; }; }; if (area==11){ attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(creature.row, creature.col, "[xxxxx]"); mvprintw(bread.row, bread.col, "e"); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(6,0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); mvprintw(10,0, "@@@@@@@@@@@@@@@@@@@@"); for (int r=7; r<13; r++) mvprintw(r, 27, "x"); for (int t=11; t<17; t++) mvprintw(t, 19, "x"); mvprintw(12,28, "xxxxxxxxxx"); mvprintw(16,19, "xxxxxxxxxxxxxxxxxxx"); mvprintw(16,0,"%i", player.col); if (player.col<0){ player.col=37; area=10; creature.dir=LEFT; }; if (player.col>37){ player.col=0; area=12; }; if (area==11 && player.row < 7 && player.col<=28 || area==11 && player.row <13 && player.col>=29) player.dir=DOWN; if (area==11 && player.row > 9 && player.col<=19 || area==11 && player.row > 15 && player.col>=20) player.dir=UP; if (creature.row<7) creature.dir=DOWN; if (creature.row>15) creature.dir=UP; if (player.col>=creature.col && player.col<=creature.col+6 && player.row==creature.row){ collision_damage=true; player.energy-=20; }; }; if (area==12){ attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(12,0, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); mvprintw(16,0, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); if (player.col<0){ player.col=37; area=11; creature.col=20; creature.row=8; creature.dir=DOWN; }; if (player.col>37){ player.col=0; area=13; }; if (player.row<13) player.dir=DOWN; if (player.row>15) player.dir=UP; }; if (area==13){ attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(12,0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); mvprintw(16,0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); if (player.col<0){ player.col=37; area=12; }; if (player.col>37){ player.col=0; area=14; }; if (player.row<13) player.dir=DOWN; if (player.row>15) player.dir=UP; }; if (area==14){ attron(COLOR_PAIR(1)); //attron(A_BOLD); for (int r=12; r<17; r++) mvprintw(r, 20,"@@@@@@@@@@@@@@@@@"); attron(COLOR_PAIR(6)); mvprintw(15,20,"_"); attron(COLOR_PAIR(2)); attron(A_BOLD); mvprintw(12,0, "@@@@@@@@@@@@@@@@@@@@"); mvprintw(16,0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); if (player.col==20 && player.row==15){ player.col=4; player.row=15; creature.dir=NONE; creature.col=40; creature.row=0; area=15; }; if (player.col<0){ player.col=37; area=13; }; if (player.row<13) player.dir=DOWN; if (player.row>15) player.dir=UP; }; if (area==15){ timer++; player.dir=NONE; if (timer>=5 && timer<=50){ attron(COLOR_PAIR(2)); mvprintw(3,3,"You escaped from the cave!!"); mvprintw(5,3,"You escaped from the cave!!!"); mvprintw(7,3,"You are a great traveler."); }; if (timer>=51){ attron(COLOR_PAIR(2)); mvprintw(8,3,"Now go West and return home!"); }; if (timer>=55) attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(1,0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); mvprintw(16,0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); if ( timer>60) game_over(); }; if (collision_damage) { attron(COLOR_PAIR(1)); mvprintw(player.row - 1, player.col, "x"); mvprintw(player.row + 1, player.col, "x"); mvprintw(player.row, player.col - 1, "x"); mvprintw(player.row, player.col + 1, "x"); } if (collision_energy) { attron(COLOR_PAIR(1)); mvprintw(player.row - 1, player.col, "o"); mvprintw(player.row + 1, player.col, "o"); mvprintw(player.row, player.col - 1, "o"); mvprintw(player.row, player.col + 1, "o"); } } game_over(); }
0 Comments
Leave a Reply. |
Archives
March 2016
Categories |