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
|
Archives
March 2016
Categories |