Simple Games using ncurses
Menu

Build a game :-)

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

using namespace std;

class snake
{

  private:
int x;
    int y;

  public:
  snake(int a, int b)
{
x = a;
y = b;
}

int getX()
{
return x;
}

int getY()
{
return y;
}
};


int main()
{
  
bool quit = false;
int rotate_right=0;
int rotate_left=0;
int clock_1 = 0;
int clock_2 = 0;
int clock_energy=0;
int energy = 100;
int points = 0;
int background;
int bird_column = 14+ rand() % 8+ 1;
int bird_row= 3 + rand() % 2 + 1;
int Player_direction = 2;
int bird_direction=0;
int food_x = 8+rand() % 12 + 1;
int food_y =4+ rand() % 5+ 1;

int ch;

srand(time(NULL));
initscr();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
timeout(200);

attron(A_BOLD);

start_color();


bkgd(COLOR_PAIR(5));


//initializing multiple color pairs:-)...Just for more options

init_pair(1,COLOR_WHITE,COLOR_BLACK);                               

init_pair(2,COLOR_BLACK,COLOR_WHITE);


init_pair(3,COLOR_GREEN,COLOR_GREEN);//food

init_pair(4,COLOR_BLACK,COLOR_MAGENTA);

init_pair(5,COLOR_BLACK,COLOR_WHITE);

init_pair(6,COLOR_RED,COLOR_CYAN);


std::list < snake > snakes;
std::list < snake >::iterator it;



Location_1:

for (int i = 0; i < 3; i++)
// generate start snake

//snakes location
snakes.push_front(snake(5 + i, 6));

// ___________________________

while (!quit)
{
// input from keys pressed
ch = getch();
switch (ch)
{
case KEY_LEFT:
Player_direction = 1;
break;
case KEY_RIGHT:
Player_direction = 2;
break;
case KEY_UP:
Player_direction = 3;
break;
case KEY_DOWN:
Player_direction = 4;
break;

case ' ': rotate_right++; break;

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

//timers
clock_1++;

clock_2++;


if (rotate_right>1) rotate_right=0;

if (rotate_left>1)
rotate_left=0;

if (Player_direction==1 && rotate_right==1) {Player_direction=3; rotate_right=0;}

if (Player_direction==3 && rotate_right==1) {Player_direction=2; rotate_right=0;}

if (Player_direction==2 && rotate_right==1) {Player_direction=4; rotate_right=0;}

if (Player_direction==4 && rotate_right==1) {Player_direction=1; rotate_right=0;}

if (Player_direction==1 && rotate_left==1) {Player_direction=4; rotate_left=0;}

if (Player_direction==4 && rotate_left==1) {Player_direction=2; rotate_left=0;}

if (Player_direction==2 && rotate_left==1) {Player_direction=3; rotate_left=0;}

if (Player_direction==3 && rotate_left==1) {Player_direction=1; rotate_left=0;}


snake logic = snakes.front();

int x = logic.getX();
int y = logic.getY();


switch (Player_direction){
case 1: x--; break;
case 2: x++; break;
case 3: y--; break;
case 4: y++; break;
}


attron(A_BOLD);


snakes.push_front(snake(x, y));

// _____________________

if (clock_1 >= 18 + rand() % 10 + 1)

{
food_x = rand() % 29 + 1;
food_y =1+ rand() % 10 + 1;
energy = energy - 10;
clock_1 = 0;
}

// _______________________

// Below: food disappears and goes to random location on screen

if (x == food_x && y == food_y)
{
food_x = rand() % 29 + 1;
food_y = 1+rand() % 10 + 1;
energy = energy + 10;
points = points + 50;

clock_1 = 0;
}
else
snakes.pop_back();



erase();// keeps characters from leaving trace on screen

// crash into walls

if (x<=-1) {x=0; Player_direction=2; }

if (x>=29){x=21; Player_direction=1; }

if (y<=-1) {Player_direction=4; y=1; }

if (y>=12) {Player_direction=3;  y=10; }


attrset(COLOR_PAIR(2));

mvprintw(0, 0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
mvprintw(11, 0, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");

attrset(COLOR_PAIR(3));

mvaddch(food_y, food_x, '¢');

attrset(COLOR_PAIR(1));

mvprintw(bird_row, bird_column, "(");


if (clock_2>=7 && clock_2<=15){ mvprintw(1,11,"Level 1");
mvprintw(food_y,food_x-8,"(food->)");}

if (clock_2>=30) bird_direction=1;

// ______________________

switch (bird_direction){
case 1: bird_column--; break;
case 2: bird_column++; break;
case 3: bird_row--; break;
case 4: bird_row++; break;
}

// _______________________

if (bird_column<= -1 && x<=12)
{
bird_column=4+rand()%12+1;
bird_row = y;
}

if (bird_column<= -1 && x>=13)
{
bird_column=17+rand()%11+1;
bird_row = y;
}


if (x == bird_column && y == bird_row ||  y<1 || y>10)
{

mvprintw(0,0,":-(");
energy = energy - 5;

}

for (it = snakes.begin(); it != snakes.end(); it++)
{

if (energy <= 75)
{
mvprintw(1,7," LOW ENERGY!");
mvaddch((*it).getY(), (*it).getX(), 'o');
attrset(COLOR_PAIR(5));


}

if (energy > 76 && energy <= 499)
{

mvaddch((*it).getY(), (*it).getX(), 'O');
attrset(COLOR_PAIR(6));

attron(A_BOLD);
}

if (energy >= 500 )
{
mvaddch((*it).getY(), (*it).getX(), 'Q');

}


}

// Snake collide

if ((*it).getY() == y && (*it).getX() == x && it != snakes.begin())
{
mvprintw(0,1,":-(");

energy = energy - 5;
}

if (energy <= 0)
quit = true;

// game over :-(




}//<--while (!quit) closing bracket. Important. Do not delete this bracket.

timeout(-1);

erase();

mvprintw(4, 9, "GAME OVER");
mvprintw(6,9,"SCORE:");
mvprintw(6,15,"%i", points);


attron(A_BOLD);

bkgd(COLOR_PAIR(5));


refresh();
getch();

endwin();

}
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
✕