Simple Games using ncurses
Menu

"Ghost" (Upgrade)

10/25/2015

0 Comments

 
//"Ghost
//W.Perez
//wperez274@gmail.com

#include <string>
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;

int main()
{

home:

bool quit = false;

int life=75;
int points=0;
int x=7;
int y=4;
int food_x=15+rand()%8;
int food_y=3+rand()%5;
int ghost_x=18;
int ghost_y=2+rand()%3;
int ghost_direct=0;

int player_direct=0;

int clock=0;
int clock_1=0;
int clock_2=0;
int clock_3=0;
int ch;



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_WHITE, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_WHITE, COLOR_BLUE);

init_pair(4, COLOR_GREEN, COLOR_BLACK);

init_pair(5, COLOR_CYAN, COLOR_BLACK);

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;

//Pause game
case '0': timeout(555555); break;
//unpause (resume game)
case 'p': timeout(200); break;}

attron(A_BOLD);
clock++;
clock_1++;
clock_2++;
clock_3++;
erase();

bool collision=false;
bool collision_2=false;
bool chased=false;
bool star=false;


if (clock_3>=10+rand()%5 && clock_3 <=20+rand()%10) star=1;

if (clock_3>=21) clock_3=0-rand()%5;

if (x==food_x && y==food_y) collision_2=true;

if (x==ghost_x && y==ghost_y && star==false || y==1 && star==false || y==9 && star==false){

collision=true;

life--;
}

if (clock_1>=10  ) {

chased=true;  

}
if (clock_1>=21){

clock_1=rand()%5;

chased=false; ghost_direct=0;}

attrset(COLOR_PAIR(1));
if (collision) {
mvprintw(0,12,":-(");
mvprintw(y-1,x,"x");
mvprintw(y+1,x,"x");
mvprintw(y,x-1,"x");
mvprintw(y,x+1,"x");}

if (collision_2) {

mvprintw(0,12,":-)");
mvprintw(y-1,x,"o");
mvprintw(y+1,x,"o");
mvprintw(y,x-1,"o");
mvprintw(y,x+1,"o");}

  if (x==food_x && y==food_y){
 
collision_2=true;
 
life=life+10;
 
food_x=1+rand()%28;

food_y=2+rand()%7;

points=points+25;
  }

switch (player_direct){

case 1: x--; break;
case 2: x++; break;
case 3: y--; break;
case 4: y++; break;
}

switch (ghost_direct){

case 1: ghost_x--; break;
case 2: ghost_x++; break;
case 3: ghost_y--; break;
case 4: ghost_y++; break;}

//this code makes bird more likely to hit player
if (clock>=25) {

food_x=1+rand()%25;

food_y=2+rand()%7;

clock=rand()%7;

life=life-5;
}

if (x<0) x=25;
if (x>25) x=0;
if (y<2) player_direct=4;
if (y>8) player_direct=3;
 


if (chased==1){


if (ghost_x>x)  ghost_direct=1;
if (ghost_x<x )  ghost_direct=2;
if (ghost_y>y ) ghost_direct=3;
if (ghost_y<y )  ghost_direct=4;

}

if (ghost_x<1) ghost_x=25;
if (ghost_x>25) ghost_x=0;
if (ghost_y<2) ghost_y=9;
if (ghost_y>9) ghost_y=2;



//game over
if (life<=0) quit=true;

attrset(COLOR_PAIR(3));
mvprintw(1, 0, "((((((((((((()))))))))))))");

mvprintw(9,0,"((((((((((((()))))))))))))");


attrset(COLOR_PAIR(5));

if (life<50)
mvprintw(y,x,"o");


if (life>=51)
mvprintw(y,x,"0");


if (star== true ) mvprintw(y, x, "=");

attrset(COLOR_PAIR(2));
mvprintw(food_y,food_x,"$");
attrset(COLOR_PAIR(1));

if (chased==1)
mvprintw(ghost_y,ghost_x,"&");
if (chased==0)
mvprintw(ghost_y,ghost_x,"_");

attrset(COLOR_PAIR(1));
mvprintw(0,1,"life:");
mvprintw(0,6,"%i", life);
mvprintw(0,22,"$");
mvprintw(0,23,"%i", points);

}//while !quit closing brace

timeout(-1);

attrset(COLOR_PAIR(5));
mvprintw(4, 8, "GAME OVER");
mvprintw(6,8,"SCORE: $");
mvprintw(6,15,"%i", points);

mvprintw(8, 4, "wperez274@gmail.com");

refresh();
getch();
endwin();

goto home;

}
Picture
0 Comments



Leave a Reply.

    Archives

    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015
    March 2015
    January 2015
    December 2014

    Categories

    All

    RSS Feed

Powered by
  • Blog
    • "Snake_1.2"
  • \\"Raider Bot\\"
  • "Pong"
  • "Pong_0.1"
  • "Cave Escape"
  • Tips & Notes
  • YouTube links:
  • Cave Escape
  • Blog
    • "Snake_1.2"
  • \\"Raider Bot\\"
  • "Pong"
  • "Pong_0.1"
  • "Cave Escape"
  • Tips & Notes
  • YouTube links:
  • Cave Escape
✕