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