Simple Games using ncurses
Menu
#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();
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=='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(1,12,"PONG");
mvprintw(4,9,"5-Point Game");
mvprintw(5,9,"20-Point Game");
mvprintw(6,9,"50-Point Game");
attrset(COLOR_PAIR(10));
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=4;
if (level==0 && d==4 && ch==10) {
level=1;
max_points=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, "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);
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);
mvprintw(8,9,"Your SCORE:");
mvprintw(8, 20," %i", player_score);
}
refresh();
getch();
endwin();
goto start;
}//Closing bracket of main function
#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();
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=='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(1,12,"PONG");
mvprintw(4,9,"5-Point Game");
mvprintw(5,9,"20-Point Game");
mvprintw(6,9,"50-Point Game");
attrset(COLOR_PAIR(10));
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=4;
if (level==0 && d==4 && ch==10) {
level=1;
max_points=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, "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);
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);
mvprintw(8,9,"Your SCORE:");
mvprintw(8, 20," %i", player_score);
}
refresh();
getch();
endwin();
goto start;
}//Closing bracket of main function