Simple Games using ncurses
Menu
//"Ghost
//W.Perez //[email protected] #include <string> #include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int main() { home: bool quit = false; int life=50; int points=0; int x=7; int y=4; int food_x=15+rand()%8; int food_y=3+rand()%5; int ghost_x=18; int ghost_y=2+rand()%3; int ghost_direct=0; int player_direct=0; int clock=0; int clock_1=0; int clock_2=0; int clock_3=0; int ch; 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_WHITE, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_RED, COLOR_BLACK); init_pair(4, COLOR_GREEN, COLOR_BLACK); init_pair(5, COLOR_CYAN, COLOR_BLACK); 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; //Pause game case '0': timeout(555555); break; //unpause (resume game) case 'p': timeout(200); break;} attron(A_BOLD); clock++; clock_1++; clock_2++; clock_3++; erase(); bool collision=false; bool collision_2=false; bool chased=false; bool star=false; if (clock_3>=10+rand()%5 && clock_3 <=20+rand()%10) star=1; if (clock_3>=21) clock_3=0-rand()%5; if (x==food_x && y==food_y) collision_2=true; if (x==ghost_x && y==ghost_y && star==false || y==1 && star==false || y==9 && star==false){ collision=true; life--; } if (clock_1>=10 ) { chased=true; } if (clock_1>=21){ clock_1=rand()%5; chased=false; ghost_direct=0;} attrset(COLOR_PAIR(1)); if (collision) { mvprintw(0,12,":-("); mvprintw(y-1,x,"x"); mvprintw(y+1,x,"x"); mvprintw(y,x-1,"x"); mvprintw(y,x+1,"x");} if (collision_2) { mvprintw(0,12,":-)"); mvprintw(y-1,x,"o"); mvprintw(y+1,x,"o"); mvprintw(y,x-1,"o"); mvprintw(y,x+1,"o");} if (x==food_x && y==food_y){ collision_2=true; life=life+10; food_x=1+rand()%28; food_y=2+rand()%7; points=points+1; } switch (player_direct){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } switch (ghost_direct){ case 1: ghost_x--; break; case 2: ghost_x++; break; case 3: ghost_y--; break; case 4: ghost_y++; break;} //this code makes bird more likely to hit player if (clock>=25) { food_x=1+rand()%25; food_y=2+rand()%7; clock=rand()%7; life=life-5; } if (x<0) x=25; if (x>25) x=0; if (y<2) player_direct=4; if (y>8) player_direct=3; if (chased==1){ if (ghost_x>x) ghost_direct=1; if (ghost_x<x ) ghost_direct=2; if (ghost_y>y ) ghost_direct=3; if (ghost_y<y ) ghost_direct=4; } if (ghost_x<1) ghost_x=25; if (ghost_x>25) ghost_x=0; if (ghost_y<2) ghost_y=9; if (ghost_y>9) ghost_y=2; //game over if (life<=0) quit=true; attrset(COLOR_PAIR(3)); mvprintw(1, 0, "=========================="); mvprintw(9,0,"=========================="); attrset(COLOR_PAIR(5)); if (life<50) mvprintw(y,x,"o"); if (life>=51) mvprintw(y,x,"0"); if (star== true && player_direct==1) mvprintw(y, x, "("); if (star== true && player_direct==2 ) mvprintw(y, x, ")"); attrset(COLOR_PAIR(2)); mvprintw(food_y,food_x,"@"); attrset(COLOR_PAIR(1)); if (chased==1) mvprintw(ghost_y,ghost_x,"&"); if (chased==0) mvprintw(ghost_y,ghost_x,"_"); attrset(COLOR_PAIR(1)); mvprintw(0,1,"life:"); mvprintw(0,6,"%i", life); mvprintw(0,21,"@:"); mvprintw(0,23,"%i", points); }//while !quit closing brace timeout(-1); attrset(COLOR_PAIR(5)); mvprintw(4, 8, "GAME OVER"); mvprintw(6,8,"SCORE: $"); mvprintw(6,15,"%i", points*25); mvprintw(8, 4, "[email protected]"); refresh(); getch(); endwin(); goto home; }
0 Comments
//"Ghost
//W.Perez //[email protected] #include <string> #include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int main() { home: bool quit = false; int life=75; int points=0; int x=7; int y=4; int food_x=15+rand()%8; int food_y=3+rand()%5; int ghost_x=18; int ghost_y=2+rand()%3; int ghost_direct=0; int player_direct=0; int clock=0; int clock_1=0; int clock_2=0; int clock_3=0; int ch; 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_WHITE, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_WHITE, COLOR_BLUE); init_pair(4, COLOR_GREEN, COLOR_BLACK); init_pair(5, COLOR_CYAN, COLOR_BLACK); 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; //Pause game case '0': timeout(555555); break; //unpause (resume game) case 'p': timeout(200); break;} attron(A_BOLD); clock++; clock_1++; clock_2++; clock_3++; erase(); bool collision=false; bool collision_2=false; bool chased=false; bool star=false; if (clock_3>=10+rand()%5 && clock_3 <=20+rand()%10) star=1; if (clock_3>=21) clock_3=0-rand()%5; if (x==food_x && y==food_y) collision_2=true; if (x==ghost_x && y==ghost_y && star==false || y==1 && star==false || y==9 && star==false){ collision=true; life--; } if (clock_1>=10 ) { chased=true; } if (clock_1>=21){ clock_1=rand()%5; chased=false; ghost_direct=0;} attrset(COLOR_PAIR(1)); if (collision) { mvprintw(0,12,":-("); mvprintw(y-1,x,"x"); mvprintw(y+1,x,"x"); mvprintw(y,x-1,"x"); mvprintw(y,x+1,"x");} if (collision_2) { mvprintw(0,12,":-)"); mvprintw(y-1,x,"o"); mvprintw(y+1,x,"o"); mvprintw(y,x-1,"o"); mvprintw(y,x+1,"o");} if (x==food_x && y==food_y){ collision_2=true; life=life+10; food_x=1+rand()%28; food_y=2+rand()%7; points=points+25; } switch (player_direct){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } switch (ghost_direct){ case 1: ghost_x--; break; case 2: ghost_x++; break; case 3: ghost_y--; break; case 4: ghost_y++; break;} //this code makes bird more likely to hit player if (clock>=25) { food_x=1+rand()%25; food_y=2+rand()%7; clock=rand()%7; life=life-5; } if (x<0) x=25; if (x>25) x=0; if (y<2) player_direct=4; if (y>8) player_direct=3; if (chased==1){ if (ghost_x>x) ghost_direct=1; if (ghost_x<x ) ghost_direct=2; if (ghost_y>y ) ghost_direct=3; if (ghost_y<y ) ghost_direct=4; } if (ghost_x<1) ghost_x=25; if (ghost_x>25) ghost_x=0; if (ghost_y<2) ghost_y=9; if (ghost_y>9) ghost_y=2; //game over if (life<=0) quit=true; attrset(COLOR_PAIR(3)); mvprintw(1, 0, "((((((((((((()))))))))))))"); mvprintw(9,0,"((((((((((((()))))))))))))"); attrset(COLOR_PAIR(5)); if (life<50) mvprintw(y,x,"o"); if (life>=51) mvprintw(y,x,"0"); if (star== true ) mvprintw(y, x, "="); attrset(COLOR_PAIR(2)); mvprintw(food_y,food_x,"$"); attrset(COLOR_PAIR(1)); if (chased==1) mvprintw(ghost_y,ghost_x,"&"); if (chased==0) mvprintw(ghost_y,ghost_x,"_"); attrset(COLOR_PAIR(1)); mvprintw(0,1,"life:"); mvprintw(0,6,"%i", life); mvprintw(0,22,"$"); mvprintw(0,23,"%i", points); }//while !quit closing brace timeout(-1); attrset(COLOR_PAIR(5)); mvprintw(4, 8, "GAME OVER"); mvprintw(6,8,"SCORE: $"); mvprintw(6,15,"%i", points); mvprintw(8, 4, "[email protected]"); refresh(); getch(); endwin(); goto home; } //"Ghost
//W.Perez //[email protected] #include <string> #include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int main() { home: bool quit = false; int life=75; int points=0; int x=12; int y=5; int food_x=15+rand()%8; int food_y=3+rand()%5; int ghost_x=10; int ghost_y=2+rand()%5; int ghost_direct=0; int player_direct=0; int clock=0; int clock_1=0; int clock_2=0; int clock_3=0; int ch; 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_WHITE, COLOR_BLACK); init_pair(2, COLOR_RED, COLOR_BLACK); init_pair(3, COLOR_WHITE, COLOR_BLUE); init_pair(4, COLOR_GREEN, COLOR_BLACK); init_pair(5, COLOR_CYAN, COLOR_BLACK); 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; //Pause game case '0': timeout(555555); break; //unpause (resume game) case 'p': timeout(200); break;} attron(A_BOLD); clock++; clock_1++; clock_2++; clock_3++; erase(); bool collision=false; bool collision_2=false; bool chased=false; bool star=false; if (clock_3>=10+rand()%5 && clock_3 <=20+rand()%10) star=1; if (clock_3>=21) clock_3=0-rand()%5; if (x==food_x && y==food_y) collision_2=true; if (x==ghost_x && y==ghost_y || y==1 || y==9 && star== false ){ collision=true; life--; } if (clock_1>=10 ) { chased=true; } if (clock_1>=21){ clock_1=rand()%5; chased=false; ghost_direct=0;} attrset(COLOR_PAIR(1)); if (collision) { mvprintw(0,12,":-("); mvprintw(y-1,x,"x"); mvprintw(y+1,x,"x"); mvprintw(y,x-1,"x"); mvprintw(y,x+1,"x");} if (collision_2) { mvprintw(0,12,":-)"); mvprintw(y-1,x,"o"); mvprintw(y+1,x,"o"); mvprintw(y,x-1,"o"); mvprintw(y,x+1,"o");} if (x==food_x && y==food_y){ collision_2=true; life=life+10; food_x=1+rand()%28; food_y=2+rand()%7; points=points+25; } switch (player_direct){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } switch (ghost_direct){ case 1: ghost_x--; break; case 2: ghost_x++; break; case 3: ghost_y--; break; case 4: ghost_y++; break;} //this code makes bird more likely to hit player if (clock>=25) { food_x=1+rand()%25; food_y=2+rand()%7; clock=rand()%7; life=life-15; } if (x<0) x=25; if (x>25) x=0; if (y<2) player_direct=4; if (y>8) player_direct=3; if (chased==1){ if (ghost_x>x) ghost_direct=1; if (ghost_x<x ) ghost_direct=2; if (ghost_y>y ) ghost_direct=3; if (ghost_y<y ) ghost_direct=4; } if (ghost_x<1) ghost_x=25; if (ghost_x>25) ghost_x=0; if (ghost_y<2) ghost_y=9; if (ghost_y>9) ghost_y=2; //game over if (life<=0) quit=true; attrset(COLOR_PAIR(3)); mvprintw(1, 0, "@@@@@@@@@@@@@@@@@@@@@@@@@@"); mvprintw(9,0,"@@@@@@@@@@@@@@@@@@@@@@@@@@"); attrset(COLOR_PAIR(5)); if (life<50) mvprintw(y,x,"o"); if (life>=51) mvprintw(y,x,"O"); if (star== true ) mvprintw(y, x, "="); attrset(COLOR_PAIR(2)); mvprintw(food_y,food_x,"e"); attrset(COLOR_PAIR(1)); if (chased==1) mvprintw(ghost_y,ghost_x,"&"); if (chased==0) mvprintw(ghost_y,ghost_x,"_"); attrset(COLOR_PAIR(1)); mvprintw(0,1,"life:"); mvprintw(0,6,"%i", life); mvprintw(0,22,"$"); mvprintw(0,23,"%i", points); }//while !quit closing brace timeout(-1); attrset(COLOR_PAIR(5)); mvprintw(4, 8, "GAME OVER"); mvprintw(6,8,"SCORE: $"); mvprintw(6,15,"%i", points); mvprintw(8, 4, "[email protected]"); refresh(); getch(); endwin(); goto home; } //"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 KEY_LEFT: player_direct = 1; break; case KEY_RIGHT: player_direct = 2; break; case KEY_UP: player_direct = 3; break; case KEY_DOWN: 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 #include <string>
#include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int main() { start: bool quit = false; int level=0; int c=6; int d=4; int max_points; int ch; int player_points=0; int ai_points=0; int ai_score=0; int player_score=0; int player_direct=0; int x=0; int y=6; int rock_direct=0; int rock_x=14; int rock_y=5; int ai_direct=0; int ai_x=29; int ai_y=6; int clock_1=0; int clock_2=0; srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(75); //initiate color function start_color(); bkgd(COLOR_PAIR(1)); //multiple color pairs to use init_pair(1, COLOR_BLACK, COLOR_WHITE); init_pair(2, COLOR_CYAN, COLOR_WHITE); init_pair(3, COLOR_CYAN, COLOR_BLUE); init_pair(4, COLOR_WHITE, COLOR_RED); init_pair(5, COLOR_BLACK, COLOR_BLACK); init_pair(6, COLOR_BLUE, COLOR_WHITE); init_pair(7, COLOR_RED, COLOR_WHITE); init_pair(8, COLOR_BLACK, COLOR_CYAN); init_pair(9, COLOR_WHITE, COLOR_BLACK); init_pair(10, COLOR_BLUE, COLOR_WHITE); while (!quit) { ch = getch(); switch (ch) { case KEY_UP: player_direct=1; break; case KEY_DOWN: player_direct=2; break; case 'q': quit = true; break; case 'p': timeout(55555); break; case 'o': timeout(75); break; } //player hits rock if (rock_x<=x && rock_y==y){ rock_direct=2; player_score=player_score+25; } if (rock_x<=x && rock_y==y-1){ rock_direct=3; player_score=player_score+25; } if (rock_x<=x && rock_y==y+1){ rock_direct=5; player_score=player_score+25; } //a.i. hits rock...sometimes doesn't have time to hit rock:-) if (rock_x>=ai_x && rock_y==ai_y){ rock_direct=1; ai_score=ai_score+25; } if (rock_x>=ai_x && rock_y==ai_y-1){ rock_direct=6; ai_score=ai_score+25; } if (rock_x>=ai_x && rock_y==ai_y+1){ rock_direct=8; ai_score=ai_score+25; } clock_1++; if (clock_1==5){ rock_direct=1+rand()%2;} erase();//keeps moving characters from leaving a trace on screen switch (player_direct) { case 1: y--; break; case 2: y++; break; } switch(ai_direct) { case 1: ai_y--; break; case 2: ai_y++; break; } switch (rock_direct){ case 1: rock_x--; break; case 2: rock_x++; break; case 3: rock_x++; rock_y--; break; case 4: rock_x++; break; case 5: rock_x++; rock_y++; break; case 6: rock_x--; rock_y--; break; case 7: rock_x--; break; case 8: rock_x--; rock_y++; break; } //ai (computer) hits target... if (rock_x>=18+rand()%10 && rock_y<=ai_y-1) ai_direct=1; if (rock_x>=18+rand()%10 && rock_y>=ai_y+1) ai_direct=2; if (ai_y<1) ai_y=1; if (ai_y>10) ai_y=10; //A.I. scores if (rock_x<0) { rock_x=14; rock_y=3+rand()%5; rock_direct=0; ai_points++; clock_1=0; } //player scores if (rock_x>29) { rock_x=14; rock_y=3+rand()%5; player_points++; clock_1=0; rock_direct=0; } //A.I. gets bonus points if (rock_x<0 && rock_direct==1) ai_score=ai_score+100; if (rock_x<0 && rock_direct==6 || rock_x<0 && rock_direct==8) ai_score=ai_score+50; if (rock_x>=ai_x && rock_y>=ai_y-1 && rock_y<=ai_y+1 && rock_direct==3 || rock_x>=ai_x && rock_y>=ai_y-1 && rock_y<=ai_y+1 && rock_direct==5) ai_score=ai_score+150; //player gets bonus points if (rock_x>29 && rock_direct==2) player_score=player_score+100; if (rock_x>29 && rock_direct==3 || rock_x>29 && rock_direct==5) player_score=player_score+50; //greater bonus points for player if ball rock is at an angle when hit:-) if (rock_x<=x && rock_y>=y-1 && rock_y<=y+1 && rock_direct==6 || rock_x<=x && rock_y>=y-1 && rock_y<=y+1 && rock_direct==8) player_score=player_score+150; if ( rock_direct==1 || rock_direct==2 || rock_direct==4 || rock_direct==7) timeout(60); if ( rock_direct==3 || rock_direct==5 || rock_direct==6 || rock_direct==8) timeout(100); if (ch=='p') timeout(55555); if (rock_y<0 && rock_direct==3) rock_direct=5; if (rock_y>10 && rock_direct==5) rock_direct=3; if (rock_y<0 && rock_direct==6) rock_direct=8; if (rock_y>10 && rock_direct==8) rock_direct=6; if (y<1) y=1; if (y>10) y=10; //start screen if (level==0 ){ attrset(COLOR_PAIR(3)); mvprintw(0,0,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); attrset(COLOR_PAIR(1)); mvprintw(2,12,"PONG"); mvprintw(4,9,"5-Point Game"); mvprintw(5,9,"20-Point Game"); mvprintw(6,9,"50-Point Game"); mvprintw(8,3,"(Press 'g' to select)"); attrset(COLOR_PAIR(10)); mvprintw(d,c,"=["); attrset(COLOR_PAIR(3)); mvprintw(11,0,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); clock_1=0; } if (level==0 && ch==KEY_DOWN) { d++; } if (level==0 && d>6 ) d=4; if (level==0 && d==4 && ch=='g') { level=1; max_points=5; } if (level==0 && d==5 && ch=='g'){ level=1; max_points=20; } if (level==0 && d==6 && ch=='g'){ level=1; max_points=50; } if (level==1){ attrset(COLOR_PAIR(1)); for (int a=1; a<12; a++){ mvprintw(a,14,":");} //player attrset(COLOR_PAIR(3)); mvprintw(y,x,"0"); mvprintw(y-1,x,"]"); mvprintw(y+1,x,"]"); //rock attrset(COLOR_PAIR(5)); mvprintw(rock_y,rock_x,"O"); attrset(COLOR_PAIR(4)); mvprintw(ai_y,ai_x,"#"); mvprintw(ai_y-1,ai_x,"0"); mvprintw(ai_y+1,ai_x,"0"); attrset(COLOR_PAIR(6)); mvprintw(0,1, "You: "); mvprintw(0,6,"%i", player_points); attrset(COLOR_PAIR(7)); mvprintw(0,20,"A.I.:"); mvprintw(0,26,"%i", ai_points); attrset(COLOR_PAIR(1)); mvprintw(0,14,"%i", max_points); }//level=1 closing brace if (player_points==max_points || ai_points==max_points) quit=true; }//while (!quit) bracket. Do not delete this brace //The area below here is for when quit=true, which stops the program. timeout(-1); erase();// clears screen bkgd(COLOR_PAIR(10)); if (player_points>=max_points){ mvprintw(2, 9, "Summary:"); mvprintw(4, 9, "You Won!"); mvprintw(6,9,"Your SCORE:"); mvprintw(6, 20," %i", player_score+500); mvprintw(8 ,9,"A.I. SCORE:"); mvprintw(8, 20," %i", ai_score); } if (ai_points>=max_points){ mvprintw(2, 9, "Summary:"); mvprintw(4, 9, "A.I. Won"); mvprintw(6 ,9,"A.I. SCORE:"); mvprintw(6, 20," %i", ai_score+500); mvprintw(8,9,"Your SCORE:"); mvprintw(8, 20," %i", player_score); } refresh(); getch(); endwin(); goto start;//back to home screen }//Closing bracket of main function #include <string>
#include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int main() { start: bool quit = false; int level=0; int c=6; int d=5; int max_points; int ch; int life=3; int ai_life=3; int player_points=0; int ai_points=0; int player_direct=0; int x=0; int y=6; int rock_direct=0; int rock_x=14; int rock_y=5; int ai_direct=0; int ai_x=29; int ai_y=6; int clock_1=0; int clock_2=0; srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(75); //initiate color function start_color(); bkgd(COLOR_PAIR(1)); //multiple color pairs to use init_pair(1, COLOR_WHITE, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(3, COLOR_BLUE, COLOR_CYAN); init_pair(4, COLOR_WHITE, COLOR_RED); init_pair(5, COLOR_MAGENTA, COLOR_MAGENTA); init_pair(6, COLOR_CYAN, COLOR_BLACK); init_pair(7, COLOR_RED, COLOR_BLACK); init_pair(8, COLOR_BLACK, COLOR_RED); while (!quit) { ch = getch(); switch (ch) { case KEY_UP: player_direct=1; break; case KEY_DOWN: player_direct=2; break; case 'q': quit = true; break; case '0': timeout(55555); break; case 'p': timeout(75); break; } //player hits rock if (rock_x<=x && rock_y==y) rock_direct=2; if (rock_x<=x && rock_y==y-1) rock_direct=3; if (rock_x<=x && rock_y==y+1) rock_direct=5; //a.i. hits rock...sometimes doesn't have time to hit rock:-) if (rock_x>=ai_x && rock_y==ai_y) rock_direct=1; if (rock_x>=ai_x && rock_y==ai_y-1) rock_direct=6; if (rock_x>=ai_x && rock_y==ai_y+1) rock_direct=8; clock_1++; if (clock_1==5){ rock_direct=1+rand()%2;} erase(); switch (player_direct) { case 1: y--; break; case 2: y++; break; } switch(ai_direct) { case 1: ai_y--; break; case 2: ai_y++; break; } switch (rock_direct){ case 1: rock_x--; break; case 2: rock_x++; break; case 3: rock_x++; rock_y--; break; case 4: rock_x++; break; case 5: rock_x++; rock_y++; break; case 6: rock_x--; rock_y--; break; case 7: rock_x--; break; case 8: rock_x--; rock_y++; break; } //ai (computer) hits target... if (rock_x>=18+rand()%10 && rock_y<=ai_y-1) ai_direct=1; if (rock_x>=18+rand()%10 && rock_y>=ai_y+1) ai_direct=2; if (ai_y<1) ai_y=1; if (ai_y>10) ai_y=10; if (rock_x<0) { rock_x=14; rock_y=3+rand()%5; rock_direct=0; ai_points++; clock_1=0; } if (rock_x>29) { rock_x=14; rock_y=3+rand()%5; player_points++; clock_1=0; rock_direct=0; } if ( rock_direct==1 || rock_direct==2 || rock_direct==4 || rock_direct==7) timeout(60); if ( rock_direct==3 || rock_direct==5 || rock_direct==6 || rock_direct==8) timeout(100); if (ch=='0') timeout(55555); if (rock_y<0 && rock_direct==3) rock_direct=5; if (rock_y>10 && rock_direct==5) rock_direct=3; if (rock_y<0 && rock_direct==6) rock_direct=8; if (rock_y>10 && rock_direct==8) rock_direct=6; if (y<1) y=1; if (y>10) y=10; //start screen if (level==0 ){ attrset(COLOR_PAIR(8)); mvprintw(0,0,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); attrset(COLOR_PAIR(1)); mvprintw(2,12,"PONG"); mvprintw(5,9,"20-Point Game"); mvprintw(6,9,"50-Point Game"); attrset(COLOR_PAIR(2)); mvprintw(d,c,"==["); attrset(COLOR_PAIR(8)); mvprintw(11,0,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); clock_1=0; } if (level==0 && ch==' ') { d++; } if (level==0 && d>6 ) d=5; if (level==0 && d==5 && ch==10) { level=1; max_points=20; } if (level==0 && d==6 && ch==10){ level=1; max_points=50; } if (level==1){ attrset(COLOR_PAIR(1)); for (int a=1; a<12; a++){ mvprintw(a,14,":");} //player attrset(COLOR_PAIR(3)); mvprintw(y,x,"0"); mvprintw(y-1,x,"]"); mvprintw(y+1,x,"]"); //rock attrset(COLOR_PAIR(5)); mvprintw(rock_y,rock_x,"O"); attrset(COLOR_PAIR(4)); mvprintw(ai_y,ai_x,"#"); mvprintw(ai_y-1,ai_x,"0"); mvprintw(ai_y+1,ai_x,"0"); attrset(COLOR_PAIR(6)); mvprintw(0,1,"Player"); mvprintw(0,8,"%i", player_points); attrset(COLOR_PAIR(7)); mvprintw(0,20,"A.I.:"); mvprintw(0,26,"%i", ai_points); attrset(COLOR_PAIR(1)); mvprintw(0,14,"%i", max_points); } if (player_points==max_points || ai_points==max_points) quit=true; }//while (!quit) bracket. Do not delete this bracket //The area below here is for when quit=true, which stops the program. timeout(-1); //erase();// clears screen if (player_points>=max_points) mvprintw(5, 9, "Player Won"); if (ai_points>=max_points) mvprintw(5, 9, "A.I. Won"); refresh(); getch(); endwin(); goto start; }//Closing bracket of main function //"R-Wing" game
//[email protected] #include <string> #include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> using namespace std; int main() { start: bool quit = false; int ch; int life=30; int points=0; int player_direct=0; int x=12; int y=6; int rock_direct=0; int rock_x=11; int rock_y=0; int bird_direct=0; int bird_x=20+rand()%9; int bird_y=3+rand()%5; int clock_1=0; srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(175); attron(A_BOLD); //initiate color function start_color(); bkgd(COLOR_PAIR(1)); init_pair(1, COLOR_BLACK, COLOR_YELLOW); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_BLACK, COLOR_RED); init_pair(4, COLOR_CYAN, COLOR_BLUE); while (!quit) { ch = getch(); switch (ch) { case KEY_LEFT: player_direct = 1; break; case KEY_RIGHT: player_direct = 2; break; case KEY_UP: player_direct = 3; break; case KEY_DOWN: player_direct = 4; break; case 'q': quit = true; break; case 'p': timeout(555555); break; case 'o': timeout(175); break; } clock_1++; if (clock_1>10+rand()%5) rock_direct=4; if (clock_1>20+rand()%5) bird_direct=1; //this erase function keep the moving characters from leaving a trace on the screen erase(); bool collision=false; //player gets hit in certain conditions if (x>=rock_x && x<=rock_x+4 && rock_y==y || x==bird_x && y==bird_y){ life=life-10; collision=true; } if (collision) { mvprintw(1,0,":-("); mvprintw(y-1,x,"x"); mvprintw(y+1,x,"x"); mvprintw(y,x-1,"x"); mvprintw(y,x+1,"x"); } //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>10){ rock_y=rand()%2; rock_x=rand()%24; points=points+50; life++ ;} //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) player_direct=2; if (x>29) player_direct=1; if (y<0) player_direct=4; if (y>11) player_direct=3; //game over if (life<=0) quit=true; //player attrset(COLOR_PAIR(4)); mvprintw(y,x,"O"); //rock attrset(COLOR_PAIR(2)); mvprintw(rock_y,rock_x,"#####"); //bird attrset(COLOR_PAIR(3)); mvprintw(bird_y,bird_x,"@"); attrset(COLOR_PAIR(1)); mvprintw(0,1,"life:"); mvprintw(0,6,"%i", life); mvprintw(0,22,"$"); 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();// clears screen mvprintw(5, 9, "GAME OVER"); mvprintw(7,9,"SCORE: $"); mvprintw(7,16,"%i", points); refresh(); getch(); endwin(); goto start; }//Closing bracket of main function /* "Snake_1.2" Edited by: Wilson Questions?...Email me at: [email protected]*/ #include <iostream> #include <ncurses.h> #include <list> #include <stdlib.h> #include <time.h> using namespace std; class snake { private: int x; int y; public: snake(int a, int b) { x = a; y = b; } int getX() { return x; } int getY() { return y; } }; int main() { start: bool quit = false; int rotate_right=0; int rotate_left=0; int clock_1 = 0; int clock_2 = 0; int clock_energy=0; int energy = 100; int points = 0; int background; int bird_column = 14+ rand() % 8+ 1; int bird_row= 3 + rand() % 2 + 1; int Player_direction = 2; int bird_direction=0; int food_x = 8+rand() % 12 + 1; int food_y =4+ rand() % 5+ 1; int ch; srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(200); attron(A_BOLD); start_color(); bkgd(COLOR_PAIR(5)); //initializing multiple color pairs:-)...Just for more options init_pair(1,COLOR_WHITE,COLOR_BLACK); init_pair(2,COLOR_BLACK,COLOR_WHITE); init_pair(3,COLOR_GREEN,COLOR_GREEN);//food init_pair(4,COLOR_BLACK,COLOR_MAGENTA); init_pair(5,COLOR_BLACK,COLOR_WHITE); init_pair(6,COLOR_RED,COLOR_CYAN); std::list < snake > snakes; std::list < snake >::iterator it; Location_1: for (int i = 0; i < 3; i++) // generate start snake //snakes location snakes.push_front(snake(5 + i, 6)); // ___________________________ while (!quit) { // input from keys pressed ch = getch(); switch (ch) { case KEY_LEFT: Player_direction = 1; break; case KEY_RIGHT: Player_direction = 2; break; case KEY_UP: Player_direction = 3; break; case KEY_DOWN: Player_direction = 4; break; case ' ': rotate_right++; break; //Pause case 'p': timeout(555555); break; //unpause (resume game) case 'o': timeout(200); break; } //timers clock_1++; clock_2++; if (rotate_right>1) rotate_right=0; if (rotate_left>1) rotate_left=0; if (Player_direction==1 && rotate_right==1) {Player_direction=3; rotate_right=0;} if (Player_direction==3 && rotate_right==1) {Player_direction=2; rotate_right=0;} if (Player_direction==2 && rotate_right==1) {Player_direction=4; rotate_right=0;} if (Player_direction==4 && rotate_right==1) {Player_direction=1; rotate_right=0;} if (Player_direction==1 && rotate_left==1) {Player_direction=4; rotate_left=0;} if (Player_direction==4 && rotate_left==1) {Player_direction=2; rotate_left=0;} if (Player_direction==2 && rotate_left==1) {Player_direction=3; rotate_left=0;} if (Player_direction==3 && rotate_left==1) {Player_direction=1; rotate_left=0;} snake logic = snakes.front(); int x = logic.getX(); int y = logic.getY(); switch (Player_direction){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } attron(A_BOLD); snakes.push_front(snake(x, y)); // _____________________ if (clock_1 >= 18 + rand() % 10 + 1) { food_x = rand() % 29 + 1; food_y =1+ rand() % 10 + 1; energy = energy - 10; clock_1 = 0; } // _______________________ // Below: food disappears and goes to random location on screen if (x == food_x && y == food_y) { food_x = rand() % 29 + 1; food_y = 1+rand() % 10 + 1; energy = energy + 10; points = points + 50; clock_1 = 0; } else snakes.pop_back(); erase();// keeps characters from leaving trace on screen // crash into walls if (x<=-1) {x=0; Player_direction=2; } if (x>=29){x=21; Player_direction=1; } if (y<=-1) {Player_direction=4; y=1; } if (y>=12) {Player_direction=3; y=10; } attrset(COLOR_PAIR(2)); mvprintw(0, 0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); mvprintw(11, 0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); attrset(COLOR_PAIR(3)); mvaddch(food_y, food_x, '¢'); attrset(COLOR_PAIR(1)); mvprintw(bird_row, bird_column, "("); if (clock_2>=7 && clock_2<=15){ mvprintw(1,11,"Level 1"); mvprintw(food_y,food_x-8,"(food->)");} if (clock_2>=30) bird_direction=1; // ______________________ switch (bird_direction){ case 1: bird_column--; break; case 2: bird_column++; break; case 3: bird_row--; break; case 4: bird_row++; break; } // _______________________ if (bird_column<= -1 && x<=12) { bird_column=4+rand()%12+1; bird_row = y; } if (bird_column<= -1 && x>=13) { bird_column=17+rand()%11+1; bird_row = y; } if (x == bird_column && y == bird_row || y<1 || y>10) { mvprintw(0,0,":-("); energy = energy - 5; } for (it = snakes.begin(); it != snakes.end(); it++) { if (energy <= 75) { mvprintw(1,7," LOW ENERGY!"); mvaddch((*it).getY(), (*it).getX(), 'o'); attrset(COLOR_PAIR(5)); } if (energy > 76 && energy <= 499) { mvaddch((*it).getY(), (*it).getX(), 'O'); attrset(COLOR_PAIR(6)); attron(A_BOLD); } if (energy >= 500 ) { mvaddch((*it).getY(), (*it).getX(), 'Q'); } } // Snake collide if ((*it).getY() == y && (*it).getX() == x && it != snakes.begin()) { mvprintw(0,1,":-("); energy = energy - 5; } if (energy <= 0) quit = true; // game over :-( }//<--while (!quit) closing bracket. Important. Do not delete this bracket. timeout(-1); erase(); mvprintw(4, 9, "GAME OVER"); mvprintw(6,9,"SCORE:"); mvprintw(6,15,"%i", points); attron(A_BOLD); bkgd(COLOR_PAIR(5)); refresh(); getch(); endwin(); goto start; }//end of main //SpaceBot //By: Wilson // http://programswithc.weebly.com #include <string> #include <ncurses.h> #include <stdlib.h> #include <time.h> #include <iostream> int main() { start: srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(150); int ch; bool quit = false; bool collision=false; bool collision_2=false; int level=0; int keys_need=15; int c=7; int d=4; int player_direct=0; int life=30; int points=0; int x=4; int y=4; int key_x=2+rand()%27; int key_y=2+rand()%6; int food_x=2+rand()%27; int food_y=2+rand()%6; int log_direct=4; int log_x=14+rand()%6; int log_y=3+rand()%3; int clock=0; int clock_1=0; int clock_2=0; //initiate color function start_color(); bkgd(COLOR_PAIR(1)); //multiple color pairs just gives you more options init_pair(1, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_MAGENTA, COLOR_BLACK); init_pair(3, COLOR_YELLOW, COLOR_BLACK); init_pair(4, COLOR_GREEN, COLOR_BLACK); init_pair(5, COLOR_RED, COLOR_BLACK); init_pair(6, COLOR_WHITE, COLOR_BLUE); init_pair(7, COLOR_WHITE, COLOR_MAGENTA); init_pair(8, COLOR_WHITE, COLOR_RED); init_pair(9, COLOR_BLACK, COLOR_GREEN); init_pair(10, COLOR_BLACK, COLOR_WHITE); init_pair(11, COLOR_RED, COLOR_BLUE); while (!quit) { ch = getch(); //keys pressed by player switch (ch) { case KEY_LEFT: player_direct = 1; break; case KEY_RIGHT: player_direct = 2; break; case KEY_UP: player_direct = 3; break; case KEY_DOWN: player_direct = 4; break; case 'q': quit = true; break; case 'p': timeout(555555); break; case 'o': timeout(150); break; } //start screen if (level==0 ){ erase(); attrset(COLOR_PAIR(6)); mvprintw(0,0,"oxoxoxoxoxoxoxoxoxoxooxoxoxoxo"); mvprintw(11,0,"oxoxoxooxoxoxoxoxoxoxoxoxoxoxo"); attrset(COLOR_PAIR(1)); mvprintw(0,8,":::SpaceBot:::"); mvprintw(4,9,"START"); mvprintw(5,9,"EXIT"); mvprintw(7,3, "(Press 'g' to select)"); mvprintw(d,c,">"); if ( ch==KEY_DOWN) d++; if (d>5 ) d=4; if (d==4 && ch=='g') { level=1; x=4; y=5; log_x=15+rand()%10; log_y=2+rand()%3;} if (d==5 && ch=='g') quit=1; }//homescreen closing brace int T=0; //level 1 if (level==1){ erase(); clock++; clock_2++; attrset(COLOR_PAIR(1)); if (clock>=3 && clock<=10) mvprintw(1,10,"Level 1"); attrset(COLOR_PAIR(2)); mvprintw(food_y, food_x, "@"); attrset(COLOR_PAIR(1)); for (int a=8; a<22; a++) mvprintw(6,a, "x"); mvprintw(1,1,"%i", keys_need); mvprintw(y,x,"O"); mvprintw(key_y, key_x,"1"); mvprintw(log_y, log_x,"[xxxx]"); attrset(COLOR_PAIR(7)); mvprintw(0,0, " xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); mvprintw(11,0, " xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); for (int a=0; a<12; a++) mvprintw(a, 0 ,"x"); for (int b=0; b<12; b++) mvprintw(b, 29, "x"); if ( x>=log_x && x<=log_x+5 && y==log_y){ collision_2=true; life=life-5;} else collision_2=false; if (y<1) { bkgd(COLOR_PAIR(5)); collision_2=1; player_direct=4; life--;} if (y>10) { bkgd(COLOR_PAIR(5)); collision_2=1; player_direct=3; life--;} if (x<1) { bkgd(COLOR_PAIR(5)); collision_2=1; player_direct=2; life--;} if (x>28) { bkgd(COLOR_PAIR(5)); collision_2=1; player_direct=1; life--;} if (x>=8 && x<=22 &&y==6){ collision_2=1; life--;} if (clock_2>=25+rand()%10 ){ log_x=1+rand()%23; food_x=1+rand()%28; food_y=1+rand()%9; clock_2=0; } if (x==food_x && y==food_y){ food_x=1+rand()%28; food_y=1+rand()%9; life=life+5;} if (x==key_x && y==key_y){ collision=1; key_x=2+rand()%27; key_y=2+rand()%5; log_x=1+rand()%23; clock_2=0; points=points+50; keys_need--; } else collision=0; if (log_y>10) log_direct=3; if (log_y<1) log_direct=4; if (keys_need<1){ log_x=30; log_y=0; key_x=30; key_y=0; log_direct=0; clock_2=0; attrset(COLOR_PAIR(6)); mvprintw(1, 14, "_"); } if (keys_need<1 && x==14 && y==1){ level=2; keys_need=25; key_x=5+rand()%20; key_y=3+rand()%3; } }//level 1 closing brace if (level==2){ erase(); clock++; clock_2++; attrset(COLOR_PAIR(1)); if (clock>=3 && clock<=10) mvprintw(1,10,"Level 2"); for (int a=5; a<25; a++) mvprintw(3,a, "x"); for (int b=5; b<25; b++) mvprintw(8,b, "x"); attrset(COLOR_PAIR(2)); mvprintw(food_y, food_x, "@"); attrset(COLOR_PAIR(1)); mvprintw(1,1,"%i", keys_need); mvprintw(y,x,"O"); mvprintw(key_y, key_x,"1"); mvprintw(log_y, log_x,"[==0==]"); attrset(COLOR_PAIR(8)); mvprintw(0,0, "ooooooooooooooooooooooooooooo"); mvprintw(11,0, "ooooooooooooooooooooooooooooo"); for (int a=0; a<12; a++) mvprintw(a, 0 ,"O"); for (int b=0; b<12; b++) mvprintw(b, 29, "O"); if ( x>=log_x && x<=log_x+6 && y==log_y){ collision_2=true; life=life-5;} else collision_2=false; if (x<1) { bkgd(COLOR_PAIR(5)); collision_2=1; player_direct=2; life--;} if (x>28) { bkgd(COLOR_PAIR(5)); collision_2=1; player_direct=1; life--;} if (y<1) { bkgd(COLOR_PAIR(5)); collision_2=1; player_direct=4; life--;} if (y>10) { bkgd(COLOR_PAIR(5)); collision_2=1; player_direct=3; life--;} if (x>=5 && x<=25 && y==3 || x>=5 && x<=25 && y==8){ collision_2=1; life--;} if (clock_2>=25+rand()%10 ){ log_x=1+rand()%22; food_x=1+rand()%28; food_y=1+rand()%9; clock_2=0; } if (x==food_x && y==food_y){ food_x=1+rand()%28; food_y=1+rand()%9; life=life+5;} if (x==key_x && y==key_y){ collision=1; key_x=2+rand()%27; key_y=2+rand()%5; log_x=1+rand()%22; clock_2=0; points=points+50; keys_need--; } else collision=0; if (log_y>10) log_direct=3; if (log_y<1) log_direct=4; if (keys_need<1){ log_x=30; log_y=0; key_x=30; key_y=0; log_direct=0; clock_2=0; attrset(COLOR_PAIR(6)); mvprintw(1, 14, "_"); } if (keys_need<1 && x==14 && y==1){ level=3; keys_need=35; key_x=5+rand()%20; key_y=3+rand()%3; } }//level 2 closing brace if (level==3){ erase(); clock++; clock_2++; attrset(COLOR_PAIR(1)); if (clock>=3 && clock<=10) mvprintw(1,10,"Level 3"); log_direct=4; attrset(COLOR_PAIR(2)); mvprintw(food_y, food_x, "@"); attrset(COLOR_PAIR(1)); mvprintw(1,1,"%i", keys_need); mvprintw(y,x,"O"); mvprintw(key_y, key_x,"1"); mvprintw(log_y, log_x,"[********]"); attrset(COLOR_PAIR(9)); mvprintw(0,0, "0###########################0"); mvprintw(11,0, "0###########################0"); for (int a=0; a<12; a++) mvprintw(a, 0 ,"O"); for (int b=0; b<12; b++) mvprintw(b, 29, "O"); if ( x>=log_x && x<=log_x+9 && y==log_y){ collision_2=true; life=life-10;} else collision_2=false; if (x<1) { attrset(COLOR_PAIR(5)); collision_2=1; player_direct=2; life--;} if (x>28) { attrset(COLOR_PAIR(5)); collision_2=1; player_direct=1; life--;} if (y<1) { attrset(COLOR_PAIR(5)); collision_2=1; player_direct=4; life--;} if (y>10) { attrset(COLOR_PAIR(5)); collision_2=1; player_direct=3; life--;} if (x==food_x && y==food_y){ food_x=1+rand()%28; food_y=1+rand()%9; life=life+5;} if (x==key_x && y==key_y){ collision=1; key_x=2+rand()%27; key_y=2+rand()%5; log_x=1+rand()%22; clock_2=0; points=points+50; keys_need--; } else collision=0; if (log_y>10){ log_y=rand()%2; log_x=1+rand()%18;} if (keys_need<1){ log_x=30; log_y=0; key_x=30; key_y=0; log_direct=0; clock_2=0; attrset(COLOR_PAIR(6)); mvprintw(1, 14, "_"); } if (keys_need<1 && x==14 && y==1){ log_direct=4; level=4; keys_need=50; key_x=5+rand()%20; key_y=3+rand()%3; } }//level 3 closing brace if (level==4){ erase(); clock++; clock_2++; attrset(COLOR_PAIR(1)); if (clock>=3 && clock<=10) mvprintw(1,10,"Level 4"); attrset(COLOR_PAIR(2)); mvprintw(food_y, food_x, "@"); attrset(COLOR_PAIR(1)); mvprintw(1,1,"%i", keys_need); mvprintw(y,x,"O"); mvprintw(key_y, key_x,"1"); mvprintw(log_y, log_x,"[==============]"); mvprintw(0,0, "#::::::::::::::::::::::::::::#"); mvprintw(11,0, "#::::::::::::::::::::::::::::#"); attrset(COLOR_PAIR(10)); for (int a=0; a<12; a++) mvprintw(a, 0 ,"*"); for (int b=0; b<12; b++) mvprintw(b, 29, "*"); if ( x>=log_x && x<=log_x+15 && y==log_y){ collision_2=true; life=life-7;} else collision_2=false; if (x<1) { attrset(COLOR_PAIR(5)); collision_2=1; player_direct=2; life--;} if (x>28) { attrset(COLOR_PAIR(5)); collision_2=1; player_direct=1; life--;} if (y<1) { attrset(COLOR_PAIR(5)); collision_2=1; player_direct=4; life--;} if (y>10) { attrset(COLOR_PAIR(5)); collision_2=1; player_direct=3; life--;} if (clock_2>=15+rand()%10 ){ log_x=1+rand()%13; food_x=1+rand()%28; food_y=1+rand()%9; clock_2=0; } if (x==food_x && y==food_y){ food_x=1+rand()%28; food_y=1+rand()%9; life=life+5;} if (x==key_x && y==key_y){ collision=1; key_x=2+rand()%27; key_y=2+rand()%5; log_x=1+rand()%22; clock_2=0; points=points+50; keys_need--; } else collision=0; if (log_y>10) log_direct=3; if (log_y<1) log_direct=4; if (keys_need<1){ log_x=30; log_y=0; key_x=30; key_y=0; log_direct=0; clock_2=0; attrset(COLOR_PAIR(6)); mvprintw(4, 14, "_"); } if (keys_need<1 && x==14 && y==4){ level=5;//final level key_x=5+rand()%20; key_y=3+rand()%3; } }//level 4 closing brace //final, eternal stage if (level==5){ erase(); log_direct=4; clock++; clock_2++; attrset(COLOR_PAIR(1)); if (clock>=3 && clock<=10) mvprintw(1,10,"Final Level"); attrset(COLOR_PAIR(2)); mvprintw(food_y, food_x, "@"); attrset(COLOR_PAIR(1)); mvprintw(y,x,"O"); mvprintw(key_y, key_x,"$"); mvprintw(log_y, log_x,"0++++++++0"); attrset(COLOR_PAIR(6)); mvprintw(0,0, "((((((((((((((+)))))))))))))))"); mvprintw(11,0, "((((((((((((((+)))))))))))))))"); attrset(COLOR_PAIR(3)); mvprintw(3,8,"*"); mvprintw(3,21,"*"); mvprintw(8,8,"*"); mvprintw(8,21,"*"); if ( x>=log_x && x<=log_x+9 && y==log_y || x==8 && y==3 || x==21 && y==3 || x==8 && y==8 || x==21 && y==8){ collision_2=true; life=life-7;} else collision_2=false; if (y<1) { collision=1; player_direct=4; } if (y>10) { collision=1; player_direct=3; } if (x<0) x=29; if (x>29) x=0; if (x==food_x && y==food_y){ collision=1; food_x=1+rand()%28; food_y=1+rand()%9; life=life+5;} if (x==key_x && y==key_y){ collision=1; key_x=1+rand()%28; key_y=2+rand()%5; clock_2=0; points=points+100; } else collision=0; if (log_y>10){ log_y=1; log_x=rand()%19; points=points+5; } }//level 5 closing brace switch (player_direct){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } switch (log_direct){ case 1: log_x--; break; case 2: log_x++; break; case 3: log_y--; break; case 4: log_y++; break; } if (collision ){ attrset(COLOR_PAIR(1)); mvprintw(y-1,x,"o"); mvprintw(y+1,x,"o"); mvprintw(y,x-1,"o"); mvprintw(y,x+1,"o");} if (collision_2 ){ attrset(COLOR_PAIR(1)); mvprintw(y-1,x,"x"); mvprintw(y+1,x,"x"); mvprintw(y,x-1,"x"); mvprintw(y,x+1,"x");} if (life<=0) quit=true; }//while (!quit) bracket. Do not delete this bracket //The area below here is for when quit is true, which stops the program. timeout(-1); erase();// clears screen attrset(COLOR_PAIR(1)); mvprintw(5, 10,"GAME OVER"); mvprintw(7,10,"SCORE: "); mvprintw(7,16," %i", points); refresh(); getch(); endwin(); //restart when any key is pressed, after game over goto start; }//Closing bracket of main function I use this compiler to write and run programs right on my Android phone.
I recommend this app to programmers who want to write programs on the go... |
Archives
March 2016
Categories |