Simple Games using ncurses
Menu
//"Space Shooter"
//By: Wilson #include <ncurses.h> #include <stdlib.h> #include <time.h> bool player_hit=false; bool enemy_hit=false; enum Colors { BK = 0, RD = 1, GN = 2, YW = 3, BE = 4, MA = 5, CN = 6, WE = 7 }; enum Move { dir_REST, dir_LEFT, dir_RIGHT, dir_UP, dir_DOWN }; struct Weapon { int col, row, amount, dir; }; Weapon bullets= { 40, 0, 100, 3 }; struct Fuel { int col, row, amount, timer; }; Fuel capsule={ 20, 0, 1, 0 }; struct User { int col, row, a, b, points, energy, dir; }; User player={ 16, 14, 40, 0, 0, 50, dir_REST }; struct Limit_Area{ int left, right; }; Limit_Area wall={ 5, 30 }; struct Object { int col,row, dir; }; Object meteor={ 15, 5, dir_DOWN }; struct Object_2 { int col,row, dir; }; Object_2 meteor_2={ 15, 5, dir_DOWN, }; void start_curses() { srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(1000/8); start_color(); bkgd(COLOR_PAIR(1)); init_pair(1,WE, BK); init_pair(2,RD, RD); init_pair(3,RD, BK); init_pair(4,WE, BK); init_pair(5,WE, BE); }; void draw_level(){ erase(); attron(COLOR_PAIR(5)); mvprintw(capsule.row, capsule.col, "E"); attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(player.row,player.col, "=O="); mvprintw(bullets.row, bullets.col, "! !"); if (enemy_hit){ attron(COLOR_PAIR(3)); attron(A_BOLD); mvprintw(meteor.row,meteor.col, "########"); mvprintw(meteor_2.row,meteor_2.col, "########"); }; if (!enemy_hit){ attron(COLOR_PAIR(4)); attron(A_BOLD); mvprintw(meteor.row,meteor.col, "[xxxxxx]"); mvprintw(meteor_2.row,meteor_2.col, "(++++++)"); }; attron(COLOR_PAIR(1)); mvprintw(17,0,"Score: %i", player.points); mvprintw(17,24,"Energy: %i", player.energy); attron(COLOR_PAIR(2)); attron(A_BOLD); for (int j=0; j<17; j++){ mvprintw(j, wall.left,"@"); mvprintw(j, wall.right,"@"); }; if (player_hit){ attron(COLOR_PAIR (1)); mvprintw(player.row,player.col, "x"); mvprintw(player.row,player.col+2, "x"); mvprintw(player.row-1,player.col+1, "x"); mvprintw(player.row+1,player.col+1, "x"); }; if (enemy_hit){ attron(COLOR_PAIR (3)); attron(A_BOLD); mvprintw(bullets.row, bullets.col, ":::"); player.energy+=1; }; }; void gameover(){ erase(); mvprintw(6,14,"GAME OVER"); mvprintw(8, 14, "SCORE: %i", player.points); timeout(-1); refresh(); getch(); endwin(); }; void loop(){ capsule.timer++; int getkey=getch(); switch (getkey) { case KEY_LEFT: case 'q': case 'w': case 'a': case 's': case 'd': player.dir = dir_LEFT; break; case KEY_RIGHT: case 'i': case 'o': case 'p': case 'j': case 'k': case 'l': player.dir = dir_RIGHT; break; case KEY_UP: case 'r': case 't': case 'y': case 'u': player.dir = dir_UP; break; case KEY_DOWN: case 'c': case 'v': case 'b': case ' ': player.dir = dir_DOWN; break; case 10: attrset(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(7, 15, "PAUSED"); refresh(); while (1) { getkey = getch(); if (getkey== 10) { break; } } break; } if (player.col+2>=meteor.col && player.col<=meteor.col+7 && meteor.row==player.row || player.col+2>=meteor_2.col && player.col<=meteor_2.col+7 && meteor_2.row==player.row){ player.energy-=3; player_hit=true; }; if (bullets.col+2>=meteor.col && bullets.col<=meteor.col+7 && bullets.row==meteor.row-1 || bullets.col+2>=meteor_2.col && bullets.col<=meteor_2.col+7 && bullets.row==meteor_2.row) enemy_hit=true; if (meteor.row>16){ meteor.row=1+rand()%5; meteor.col=6+rand()%18; player.points+=5+rand()%15; }; if (meteor_2.row>16){ meteor_2.row=rand()%3; meteor_2.col=6+rand()%18;player.points+=15+rand()%35; }; if (player.col<=wall.left){ player.dir=dir_RIGHT; player_hit=true; player.energy--; }; if (player.col+2>=wall.right){ player.dir=dir_LEFT; player_hit=true; player.energy--; }; if (player.row<1) player.dir=dir_DOWN; if (player.row>16) player.row=16; if (bullets.row<=0){ bullets.row=player.row+1;bullets.col=player.col; }; if (capsule.timer>=10+rand()%10){ capsule.timer=0-rand()%20; capsule.col=rand()%40; capsule.row=rand()%6; }; if (capsule.col>=player.col && capsule.col<=player.col+2 && capsule.row==player.row) { capsule.timer=0-(20+rand()%20); capsule.col=rand()%40; capsule.row=rand()%6; player.energy+=3; player.points+=25; }; if (player.energy<=0) mvprintw(4,14,"GAME OVER"); switch (player.dir) { case dir_LEFT: player.col--; break; case dir_RIGHT: player.col++; break; case dir_UP: player.row--; break; case dir_DOWN: player.row++; break; } switch (meteor.dir) { case dir_LEFT: meteor.col--; break; case dir_RIGHT: meteor.col++; break; case dir_UP: meteor.row--; break; case dir_DOWN: meteor.row++; break; } switch (meteor_2.dir) { case dir_LEFT: meteor_2.col--; break; case dir_RIGHT: meteor_2.col++; break; case dir_UP: meteor_2.row--; break; case dir_DOWN: meteor_2.row++; break; } switch (bullets.dir) { case dir_UP: bullets.row--; break; }; draw_level(); player_hit=0; enemy_hit=0; }; int main() { start_curses (); while (player.energy>0) { loop(); } gameover(); }
15 Comments
// 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(); } /* "Snake_5.3" Game [email protected] */ #include <ncurses.h> #include <list> #include <stdlib.h> #include <time.h> class Snake { private: int p_x, p_y; public: Snake(int play_col, int play_row) { p_x = play_col; p_y = play_row; } int locate_col() { return p_x; } int locate_row() { return p_y; } }; void ncurses_activate() { srand(time(NULL)); initscr(); noecho(); curs_set(0); keypad(stdscr, TRUE); timeout(1000 / 4); } struct Timers { int clock_1, clock_2; }; Timers T = { 0, 0 }; int clock_1 = 0; int clock_2 = 0; struct Player { int energy, points, dir, miss, hit, crash, getkey, food_x, food_y; }; Player p = { 50, 0, 2, 0, 0, 0, 0, 14, 4 }; struct Machine { int x, y, dir; }; Machine jet = { 14, 1, 0 }; void timers_on() { T.clock_1++; T.clock_2++; } enum Border { B_LEFT = 0, B_RIGHT = 34, B_TOP = 0, B_BOTTOM = 14 }; enum Colors { BK = 0, RD = 1, GN = 2, YW = 3, BE = 4, MA = 5, CN = 6, WE = 7 }; void add_color() { /* COLOR_BLACK 0 COLOR_RED 1 COLOR_GREEN 2 COLOR_YELLOW 3 COLOR_BLUE 4 COLOR_MAGENTA 5 COLOR_CYAN 6 COLOR_WHITE 7 */ start_color(); bkgd(COLOR_PAIR(1)); init_pair(1, WE, BK); init_pair(2, BK, RD); init_pair(3, RD, BK); init_pair(4, CN, BK); init_pair(5, GN, BK); init_pair(6, WE, BE); } void display() { attrset(COLOR_PAIR(3)); attron(A_BOLD); mvprintw(p.food_y, p.food_x, "@"); attrset(COLOR_PAIR(4)); attron(A_BOLD); mvprintw(jet.y, jet.x, "[********]"); attrset(COLOR_PAIR(2)); mvprintw(0, 0, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); mvprintw(15, 0, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); for (int wall = 0; wall < 15; wall++) { mvprintw(wall, 0, "X"); mvprintw(wall, 35, "X"); } attrset(COLOR_PAIR(4)); attron(A_BOLD); mvprintw(16, 0, "Energy: %i", p.energy); attrset(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(16, 18, "Score: %i", p.points); } void game_over() { erase(); attrset(COLOR_PAIR(6)); for (int c = 2; c < 31; c++) { mvprintw(0, c, "@"); mvprintw(14, c, "@"); for (int r = 1; r < 15; r++) { mvprintw(r, 2, "@"); mvprintw(r, 30, "@"); } } attrset(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(2, 10, "GAME OVER"); mvprintw(4, 10, " Summary:"); mvprintw(6, 10, "Collisions: %i", p.crash); mvprintw(7, 10, "missed_food: %i", p.miss); mvprintw(8, 10, "Hits by jet: %i", p.hit); mvprintw(12, 10, "Score: %i", p.points); mvprintw(10, 4, "///////////////////////"); refresh(); getch(); timeout(-1); endwin(); } int main() { ncurses_activate(); 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) { p.getkey = getch(); switch (p.getkey) { case 'a': case 's': case 'd': p.dir = 1; break; case 'j': case 'k': case 'l': p.dir = 2; break; case 'r': case 't': case 'y': case 'u': p.dir = 3; break; case 'c': case 'v': case 'b': case 'n': case ' ': p.dir = 4; break; case 10: attron(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(7, 12, "PAUSED"); refresh(); while (1) { p.getkey = getch(); if (p.getkey == 10) { break; } } break; } Snake logic = snakes.front(); int p_x = logic.locate_col(); int p_y = logic.locate_row(); enum Move { NONE = 0, LEFT = 1, RIGHT = 2, UP = 3, DOWN = 4 }; switch (p.dir) { case LEFT: p_x--; break; case RIGHT: p_x++; break; case UP: p_y--; break; case DOWN: p_y++; break; } switch (jet.dir) { case LEFT: jet.x--; break; case RIGHT: jet.x++; break; case UP: jet.y--; break; case DOWN: jet.y++; break; } erase(); timers_on(); snakes.push_front(Snake(p_x, p_y)); if (T.clock_1 >= 22 + rand() % 10 + 1) { p.food_x = rand() % 29 + 1; p.food_y = 1 + rand() % 10 + 1; p.energy -= 3 + rand() % 6; T.clock_1 = 0; p.miss++; } if (p_x == p.food_x && p_y == p.food_y) { p.food_x = rand() % 29 + 1; p.food_y = 1 + rand() % 10 + 1; p.energy += 5 + rand() % 15; p.points += 100 + rand() % 400; T.clock_1 = 0; } else snakes.pop_back(); if (p_x < B_LEFT) { p.dir = RIGHT; } if (p_x > B_RIGHT) { p.dir = LEFT; } if (p_y < B_LEFT) { p.dir = DOWN; } if (p_y > B_BOTTOM) { p.dir = UP; } display(); if (T.clock_2 >= 5 + rand() % 5) jet.dir = DOWN; if (jet.y > B_BOTTOM) { jet.y = 1 + rand() % 2; jet.x = 1 + rand() % 15; T.clock_2 = 0; jet.dir = 0; } if (p_x >= jet.x && p_x <= jet.x + 10 && p_y == jet.y) { for (int r = 0; r < 15; r++) { mvprintw(r, p_x, ":"); p.energy -= 2; } p.hit++; } for (it = snakes.begin(); it != snakes.end(); it++) { if (p.energy < 25) { attrset(COLOR_PAIR(1)); attron(A_BOLD); mvprintw(2, 6, "WARNING: LOW ENERGY!!"); } attron(COLOR_PAIR(5)); attron(A_BOLD); mvprintw((*it).locate_row(), (*it).locate_col(), "O"); // Self destruction if ((*it).locate_row() == p_y && (*it).locate_col() == p_x && it != snakes.begin()) { attron(COLOR_PAIR(1)); mvprintw(0, 0, "Self Harm :-("); p.energy -= 10; p.crash++; } if (p.energy <= 0) game_over(); } } } Question:
what does -> really mean? In basic calculator programming, 21->A means 21 if assigned to A. I know that it is a pointer related thing but just not really getting it. Answer: Forget what you know about calculator programming.-> means something completely different in C. -> is a token (don’t think of it as “minus-greater-than”, think of it as “arrow”) that takes a pointer on its left side and a sub-field on its right side: instance -> member It’s like instance is a “container” while member is a variable within that container. Also, -> is syntax sugar for dereferencing a pointer and accessing its field, i.e. instance -> member is the same as *(instance) . member Generally, both -> and . will only work on structs/unions and objects (class instances). #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 //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; */ } } |
Archives
March 2016
Categories |