Simple Games using ncurses
Menu
//"Cave Escape" //[email protected] #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); start_color(); bkgd(COLOR_PAIR(1)); init_pair(1, COLOR_CYAN, COLOR_BLACK); bool quit = false; bool shield=false; int room=0; int c=7; int d=4; int rotate=0; int bird_direct=1;//moving obstacles direction...varies according to room... int bird_y=3+rand()%5; int bird_x=18+rand()%10; int player_direct = 2;//player int player_y=4;//player row int p_x=6;//player column int life=75; int food_x = 15+rand() % 10; int food_y = 5+rand() % 4 ; int clock_food=0; int clock_shield=0; int magic=0; int ch; while (!quit) { ch = getch(); switch (ch) { case 'a': player_direct = 1; break; case 'd': player_direct = 2; break; case 'w': player_direct = 3; break; case 'z': player_direct = 4; break; case 'g': rotate++; break; case 'q': quit = true; break; case 'p': timeout(555555); break; case 'o': timeout(175); break; } if (rotate>1) rotate=0; if (player_direct==1 && rotate==1) {player_direct=3; rotate=0;} if (player_direct==3 && rotate==1) {player_direct=2; rotate=0;} if (player_direct==2 && rotate==1) {player_direct=4; rotate=0;} if (player_direct==4 && rotate==1) {player_direct=1; rotate=0;} //Player direction: up, down, left, right. switch (player_direct){ case 1: p_x--; break; case 2: p_x++; break; case 3: player_y--; break; case 4: player_y++; break; } // directions of moving obstacles switch (bird_direct){ case 1: bird_x--; break; case 2: bird_x++; break; case 3: bird_y--; break; case 4: bird_y++; break; //extra: 45 degree...down+left case 5: bird_x--; bird_y++; break; //extra: 45 degree...down+right case 6: bird_x++; bird_y++; break; } bool collision_2=false; if (bird_x<0 && p_x<=12) { bird_x=4+rand()%12+1; bird_y= player_y; } if (bird_x<0 && p_x>=13) { bird_x=17+rand()%11+1; bird_y = player_y; } if (bird_x>29 && p_x<=12) { bird_x=4+rand()%12+1; bird_y= player_y; } if (bird_x<0 && p_x>=13) { bird_y=17+rand()%11+1; bird_y = player_y; } if (bird_y<0){bird_y=8+rand()%2; bird_x=p_x;} if (bird_y>10){bird_y=rand()%2; bird_x=p_x;} //clock_food makes the food disappear and re-appear again at random location (and time) on screen. clock_food++; if (clock_food>=26+rand()%10) { clock_food=0; food_x = rand() % 29 + 1; food_y = 1+rand() % 10 + 1; life=life-10; } if (p_x == food_x && player_y== food_y){ food_x = rand() % 29 + 1; food_y = 1+rand() % 10 + 1; life=life+15; collision_2=true; magic++; } //player needs 3 pieces of food to activate shield if (magic>=3){ shield=true;} //when player's life runs out, it's game over:-( if (life<=0) quit=true; erase(); if (life>=50) mvaddch(player_y, p_x,'O'); //changing into smaller body indicates low energy... if (life<50) mvaddch(player_y,p_x,'o'); //Player runs when '.' is pressed:-) bool fast=false; if (ch=='.' ) fast=true; if (fast){ switch (player_direct){ case 1: p_x--; break; case 2: p_x++; break; case 3: player_y--; break; case 4: player_y++; break; } } //general condition for moving for all levels. this will help avoid errors... if (bird_x<0){ bird_x=20+rand()%9; bird_y=player_y;} if (bird_x>29){ bird_x=rand()%9; bird_y=player_y;} if (bird_y<0){ bird_y=10; bird_x=p_x;} if (bird_y>10){ bird_y=0; bird_x=p_x;} bool collision=false; if (bird_y==player_y && bird_x==p_x && shield==false) collision=true; //player knows when hit by obstacles if (collision){ mvprintw(player_y-1,p_x, "x"); mvprintw(player_y+1,p_x, "x"); mvprintw(player_y,p_x-1, "x"); mvprintw(player_y,p_x+1, "x"); life=life-10; } //start screen if (room==0 ){ p_x=30; player_y=0; mvprintw(1,8,"Cave Escape"); mvprintw(4,9,"Start"); mvprintw(5,9,"Exit"); mvprintw(8,3," (Press Space to choose)"); mvprintw(9,3," ( Return key to select)"); mvprintw(d,c,">"); } if (room==0 && ch==' ') { d++; } if (room==0 && d>5 ) d=4; if (room==0 && d==4 && ch==10) { room=1; p_x=4; player_y=4; } if (room==0 && d==5 && ch==10) quit=true; if (magic>=2) shield=true; if (shield){ mvprintw(player_y-1,p_x, "="); mvprintw(player_y+1,p_x, "="); mvprintw(player_y,p_x-1, "("); mvprintw(player_y,p_x+1, ")"); clock_shield++; } if (clock_shield>=20){ clock_shield=0; shield=false; magic=0; } //if (magic==1) collision_2=true; if (collision_2){ mvprintw(player_y-1,p_x, "o"); mvprintw(player_y+1,p_x, "o"); mvprintw(player_y,p_x-1, "o"); mvprintw(player_y,p_x+1, "o"); } if (room==1){ bird_direct=4; mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"#"); mvprintw(0,0,"OxxxxxOOxxxOxxxoxooooOxxxOxxxO"); mvprintw(11,0,"xxxxxxOxxxOxxxxxOxxOxxOooxxxxx"); } //room 2 if (room==2){ bird_direct=2; mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"ox)"); mvprintw(0,0,"oooxxxOOxxoooOOoxxxoOOxxxOoxxx"); mvprintw(11,10,"OxxxxxxxoxxOxxOxxxxO"); } //room 3 if (room==3){ mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"#"); mvprintw(1,11,"xOxxxOoxxxxxOOoxxxx"); mvprintw(11,0,"xxOoxxOOOxxxxxOOxxxxOOxxxxOOxxx"); } //room 4 design if (room==4){ bird_direct=1; mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"oxo"); mvprintw(0,0,"OxxxxxOOO OxxxxxOOOOxxxxxOOOO"); mvprintw(11,0,"xxxxxxOxxxOxxxxxOxOxxOoooxxxxx"); } if (room==5){ bird_direct=5; mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"@"); for (int a=0; a<12; a++){ mvprintw(a,8,"0");} for (int b=0; b<12; b++){ mvprintw(b,11,"0");} } if (room==6){ bird_direct=1; mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"]"); mvprintw(2,8,"xxxOOxxxOxxxxxO"); mvprintw(8,11," OoxxOxxxxxxOxxxOoo"); for (int a_6=2; a_6<12; a_6++){ mvprintw(a_6,8,"0");} for (int b_6=8; b_6<12; b_6++){ mvprintw(b_6,11,"0");} for (int c_6=0; c_6<9; c_6++){ mvprintw(c_6,29,"0");} for (int d_6=0; d_6<3; d_6++){ mvprintw(d_6,22,"0");} } if (room==7){ bird_direct=4; mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"#"); for (int a_7=0; a_7<12; a_7++) mvprintw(a_7,22,"0"); for (int b_7=0; b_7<12; b_7++)mvprintw(b_7,29,"0"); } if (room==8){ mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"@"); for (int a_8=0; a_8<12; a_8++) mvprintw(a_8,22,"@"); for (int b_8=0; b_8<12; b_8++)mvprintw(b_8,29,"@"); for (int c_8=23; c_8<29; c_8++)mvprintw(5,c_8,":"); for (int c_8=23; c_8<29; c_8++)mvprintw(6,c_8,":"); } if (room==9){ mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"#"); for (int a_9=11; a_9>3; a_9--) mvprintw(a_9,22,"@"); for (int b_9=22; b_9<30; b_9++)mvprintw(4,b_9,"@"); for (int c_9=11; c_9>7; c_9--) mvprintw(c_9,29,"@"); } if (room==10){ mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"="); for (int a_10=0; a_10<30; a_10++) mvprintw(4,a_10,"@"); for (int b_10=0; b_10<30; b_10++) mvprintw(8,b_10,"@"); for (int c_10=18; c_10<24; c_10++)mvprintw(5,c_10,":"); for (int c_10=18; c_10<24; c_10++)mvprintw(6,c_10,":"); for (int c_10=18; c_10<24; c_10++)mvprintw(6,c_10,":"); for (int c_10=18; c_10<24; c_10++)mvprintw(7,c_10,":"); } if (room==11){ mvprintw(food_y, food_x, "e"); mvprintw(bird_y,bird_x,"@"); for (int a_11=0; a_11<30; a_11++) mvprintw(4,a_11,"/"); for (int b_11=0; b_11<30; b_11++) mvprintw(8,b_11,"/"); } //conditions in room 1 if (room==1 && p_x<-1){ p_x=30; room=2;} if (room==1 && p_x>29) {p_x=-1; room=4;} if (room==1 && player_y>10) player_direct=3; if (room==1 && player_y<2) player_direct=4; //conditions in room 2 if (room==2 && p_x<0) player_direct=2; if (room==2 && p_x>30) {p_x=-1; room=1;} if (room==2 && player_y<1) player_direct=4; if (room==2 && player_y>10 && p_x>=11) player_direct=3; if (room==2 && player_y>10 && p_x<=10 ){player_y=0; room=3;} //conditions in room 3 if (room==3 && player_y<0 && p_x<=10 ) {player_y=10; room=2;} if (room==3 && p_x>=11 ) player_direct=4; if (room==3 && p_x<0) player_direct=2; if (room==3 && p_x>29) player_direct=1; if (room==3 && player_y>10) player_direct=3; //conditions in room 4 if (room==4 && p_x>29 ) player_direct=1; if (room==4 && p_x<-1) {p_x=29; room=1;} if (room==4 && player_y<1 && p_x<=8 || room==4 && player_y<1 && p_x>=11) player_direct=4; if (room==4 && player_y<0 && p_x>=9 && p_x<=10 ){ player_y=10; room =5;} if (player_y>10 && room==4) player_direct=3; //conditions for room 5 if (room==5 && p_x<9) player_direct=2; if (room==5 && p_x>10) player_direct=1; if (room==5 && player_y>10 && p_x>=9 && p_x<=10) {player_y=0; room=4;} if (room==5 && player_y<0) {player_y=10; room=6;} //conditions for room 6 if (room==6 && p_x<9) player_direct=2; if (room==6 && player_y<3 && p_x<23 ) player_direct=4; if (room==6 && p_x>10 && player_y>7) player_direct=1; if (room==6 && p_x>29) player_direct=1; if (room==6 && p_x>=11 && player_y>7) player_direct=3; if (room==6 && p_x>=9 && p_x<=10 && player_y>10){player_y=0; room=5;} if (room==6 && player_y<0) {player_y=10; room=7; bird_y=1+rand()%2; bird_x=23+rand()%5; bird_direct=4;} //condition for room 7 if (room==7 && p_x<23) player_direct=2; if (room==7 && p_x>29) player_direct=1; if (room==7 && player_y>11){player_y=0; room=6;} if (room==7 && player_y<0){ player_y=10; bird_direct=1; bird_y=18+rand()%10;bird_y=4+rand()%3; room=8; } //conditions at room 8 if (room==8 && p_x<23) player_direct=2; if (room==8 && p_x>29) player_direct=1; if (room==8 && player_y>11){player_y=0; room=7;} if (room==8 && player_y<0){player_y=10; room=9;} //conditions at room 9 if (room==9 && p_x<23) player_direct=2; if (room==9 && p_x>28 && player_y>=8) player_direct=1; if (room==9 && p_x>29 && player_y<8){ p_x=0; room=10;} if (room==9 && player_y<5) player_direct=4; if (room==9 && player_y>10){ player_y=0; room=8;} //conditions at room 10 if (room==10 && p_x<0){ p_x=29; room=9;} if (room==10 && p_x>29){ p_x=0; room=11;} if (room==10 && player_y<5) player_direct=4; if (room==10 && player_y>7) player_direct=3; //conditions at room 11 if (room==11 && p_x<0){ p_x=29; room=10;} if (room==11 && player_y<5) player_direct=4; if (room==11 && player_y>7) player_direct=3; }// <-- !quit closing bracket timeout(-1); erase(); if (room>0) mvprintw(5, 9, "GAME OVER"); if (room==0) mvprintw(7,5," (Press back to exit)"); refresh(); getch(); endwin(); goto start; }//Closing bracket of main function
0 Comments
//"Key Quest"
#include <string> #include <ncurses.h> #include <stdlib.h> #include <time.h> using namespace std; int main() { home: bool quit=false; bool collide1=false; bool collide2=false; bool door_open=false; int clock1=0; int stage=1; int life=3; int energy=50; int x=2; int y=3; int key1_y=2+rand()%10; int key1_x=2+rand()%28; int rock_dir=2; int rock_y=1; int rock_x=6+rand()%4; int rock2_dir=4; int rock2_y=3+rand()%5; int rock2_x=17; int door_y=6; int door_x=23; int player_dir=0; int ch;//getkey srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(220); //initiate color function start_color(); //create color pairs as desired bkgd(COLOR_PAIR(1)); init_pair(1,COLOR_CYAN, COLOR_BLACK); init_pair(2,COLOR_GREEN, COLOR_BLACK); init_pair(3,COLOR_RED, COLOR_BLACK); init_pair(4,COLOR_WHITE, COLOR_RED); //the open door init_pair(5,COLOR_BLUE, COLOR_WHITE); init_pair(6,COLOR_BLACK, COLOR_WHITE); init_pair(7,COLOR_WHITE, COLOR_BLACK); while (!quit) { ch = getch(); switch (ch) { case 'a': player_dir = 1; break; case 'd': player_dir = 2;break; case 'w':player_dir= 3;break; case 'z':player_dir= 4;break; case 'p': timeout(555555); break; case 'u': timeout(225); break; case 'q': quit=true; } erase(); clock1++;//timer for whatever... //player continues to move in the direction when certain keys are pressed once. switch (player_dir) { case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } switch (rock_dir) { case 1: rock_x--; break; case 2: rock_x++; break; case 3: rock_y--; break; case 4: rock_y++; break; } switch (rock2_dir) { case 1: rock2_x--; break; case 2: rock2_x++; break; case 3: rock2_y--; break; case 4: rock2_y++; break; } //?maybe cut and paste the conditional code below into stage scope. ex: if (stage==1){...} if (x<0) player_dir=2; if (x>29) player_dir=1; if (y<1) player_dir=4; if (y>12) player_dir=3; //player collides with wall or ghost..And either losses energy or back to starting point attrset(COLOR_PAIR(7)); if (collide1){ mvprintw(y-1,x,"x"); mvprintw(y+1,x,"x"); mvprintw(y,x-1,"x"); mvprintw(y,x+1,"x"); } //player gets key if (collide2) { mvprintw(y-1,x,"o"); mvprintw(y+1,x,"o"); mvprintw(y,x-1,"o"); mvprintw(y,x+1,"o");} attrset(COLOR_PAIR(7)); mvprintw(y,x,"0"); attrset(COLOR_PAIR(3)); mvprintw(key1_y,key1_x,"t"); attrset(COLOR_PAIR(4)); mvprintw(door_y, door_x,"t"); if (stage==1) { attrset(COLOR_PAIR(2)); mvprintw(0, 0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); mvprintw(13, 0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); mvprintw(11, 16, "@@@@@@ @@@@@@@") ; attrset(COLOR_PAIR(1)); for (int wall_1=1; wall_1<12; wall_1++){ mvprintw(wall_1,5,"|");} for (int wall_2=2; wall_2<13; wall_2++){ mvprintw(wall_2,10,"|"); } for (int wall_3=1; wall_3<11; wall_3++){ mvprintw(wall_3,16,"|"); } attrset(COLOR_PAIR(1)); mvprintw(rock_y, rock_x,"#"); attrset(COLOR_PAIR(1)); mvprintw(rock2_y, rock2_x,"[xxxxxxxxxx]"); if (y==key1_y && x==key1_x) { key1_y=0; key1_x=30; collide1=true; door_open=true; } else collide1=false; if (rock_x<7) rock_dir=2; if (rock_x>14) rock_dir=1; if (rock2_y<5) rock2_dir=4; if (rock2_y>9) rock2_dir=3; if (door_open) { attrset(COLOR_PAIR(5)); mvprintw(door_y, door_x, "_"); key1_x=31; key1_y=0; } if (x==5 && y<=11 or x==10 && y>=2 or x==16 && y<=11 || x>=rock2_x && x<=rock2_x+11 && y==rock2_y ){ key1_y=2+rand()%8; key1_x=6+rand()%12; x=2; y=3; player_dir=0; life--; collide1=true; } else collide1=0; if (x==door_x && y==door_y && door_open==true){ erase(); attrset(COLOR_PAIR(7)); mvprintw(6, 5, "Stage Completed."); mvprintw(8, 5, "You are a great pilot!!"); quit=1; } }//closing stage 1 if (life<=0) {quit=true; attrset(COLOR_PAIR(7)); mvprintw(6,10, "GAME OVER"); } }//while !quit closing brace timeout(-1); refresh(); getch(); endwin(); goto home; } #include <ncurses.h> #include <stdlib.h> #include <time.h> using namespace std; int main() { home: int clock_1=0; bool quit=false; int x=14; int y=6; int player_dir=0; int ch;//getkey srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(175); //initiate color function start_color(); bkgd(COLOR_PAIR(1)); init_pair(1,COLOR_CYAN, COLOR_BLACK); while (!quit) { ch = getch(); switch (ch) { case 'a': player_dir = 1; break; case 'd': player_dir = 2;break; case 'w':player_dir= 3;break; case 'z':player_dir= 4;break; case 'p': timeout(555555); break; case 'u': timeout(225); break; case 'q': quit=true; } erase(); clock_1++;//timer for whatever... //player continues to move in the direction when certain keys are pressed once. switch (player_dir) { case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } //player y,x mvprintw(y,x,"0"); }//while !quit closing brace timeout(-1); refresh(); getch(); endwin(); goto home; } |
Archives
March 2016
Categories |