Simple Games using ncurses
Menu
#include <string>
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
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);
//initiate color function
start_color();
bkgd(COLOR_PAIR(1));
init_pair(1, COLOR_BLACK, COLOR_WHITE);
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;
//Pause game
case 'p': timeout(555555); break;
//unpause (resume game)
case 'o': timeout(200); 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
mvprintw(y,x,"O");
//rock
mvprintw(rock_y,rock_x,"oxoxo");
//bird
mvprintw(bird_y,bird_x,"]");
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();
}//Closing bracket of main function
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
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);
//initiate color function
start_color();
bkgd(COLOR_PAIR(1));
init_pair(1, COLOR_BLACK, COLOR_WHITE);
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;
//Pause game
case 'p': timeout(555555); break;
//unpause (resume game)
case 'o': timeout(200); 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
mvprintw(y,x,"O");
//rock
mvprintw(rock_y,rock_x,"oxoxo");
//bird
mvprintw(bird_y,bird_x,"]");
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();
}//Closing bracket of main function