Simple Games using ncurses
Menu
//"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; }
0 Comments
Leave a Reply. |
Archives
March 2016
Categories |