Saturday, June 1, 2013

Fun with cui

/*  Here's a really, and I mean REALLY basic version of a dodging game that I did up just as an exercise.  It has an odd collision detection going on, and it's not really much of a game, but it does show how objects can interact with the cui.  If you decide you're going to make a little game with the same namespace and the bconsole/console files let me know, cause I'd love to see what people come up with.
The idea is: Every time a 'T' falls on you you loose a point, everytime a @ falls on you you gain one.  You are the '*' and you control, one step at a time, with the arrow keys.
 */
#include "console.h"
#include <cstdlib>
#include <time.h>
#include <stdio.h>
//#include <Windows.h> -- This is an early attempt at coloring the chars
using namespace cui;

class DropObj{
    char sym;
    int row,col;
    public:
        DropObj();
        DropObj(char);
        ~DropObj();
        void newLoc();
        void move();
        char disp();
        void place();
        int outside();
        int colcall();
        void set(char);
};
void DropObj::newLoc(){
    row=0;
    col=rand()%console.getCols();
}
int DropObj::colcall(){
    return col;
}
void DropObj::set(char t){
//    system("color 01");
    sym=t;
}
DropObj::DropObj(){
    sym='@';
    row=0;
    col=rand()%console.getCols();
}
DropObj::DropObj(char ty){
    sym=ty;
    row=0;
    col=rand()%console.getCols();
}
DropObj::~DropObj(){}
void DropObj::move(){
    row++;
    if(row>console.getRows()-1)row=0;
}
char DropObj::disp(){
    return sym;
}
void DropObj::place(){
    console.setPos(row,col);
    printf("%c",disp());
}
int DropObj::outside(){
    return row;
}

class BadGuy:public DropObj{
public:
    BadGuy();
    BadGuy(char);
};
BadGuy::BadGuy(){
    DropObj();
}
BadGuy::BadGuy(char n):DropObj(n){}

/*void hitCheck(DropObj star,BadGuy bads[],int i,int r,int c){
    for(int j=0;j<i;j++){
        if(bads[j].outside()==r+1&&bads[j].colcall()==c)printf("YOU'RE HIT!");
    }
}*/
int main(){
  srand(time(NULL));
  DropObj dropPoint;
  BadGuy Go[99];
  for(int j=0;j<99;j++)Go[j].set('T');
  bool done = false;
  int row = console.getRows()-1;
  int col = 0;
  int launch=0;
  int ii=0;
  int score=0;
  bool dead=false;
  int key;
  while(!done){
    launch=rand()%20;
    if(launch<5&&ii<99){
        Go[ii].place();
        ii++;
      }
    dropPoint.place();
    dropPoint.move();
    if(dropPoint.outside()==row+1&&dropPoint.colcall()==col){
      score++;
      dropPoint.newLoc();
    }
    for(int i=0;i<ii;i++){
      Go[i].place();
      Go[i].move();
      if(Go[i].outside()==row+1&&Go[i].colcall()==col){
        score--;
        Go[i].newLoc();
      }
    }
    if(row+1==console.getRows()&&col+2==console.getCols()||row+2==console.getRows()&&col+1==console.getCols())dead=true;
    else dead=false;
    console.setPos(row-1,40);
    printf("Score=%d",score);
    console.dspstr("*",row,col);
    key = console.getKey();
    switch(key){
    case DOWN:
      if(row+1<console.getRows()&&!dead){
        row++;
      }
      console.clear();
      break;
    case UP:
      if(row>0){
        row--;
      }
      console.clear();
      break;
    case RIGHT:
      if(col+1<console.getCols()&&!dead){
        col++;
      }
      console.clear();
      break;
    case LEFT:
      if(col>0){
        col--;
      }
      console.clear();
      break;
    case ESCAPE:
      done = true;
      break;
    default:
        console.clear();
        break;
    }
  }
  return 0;

}

No comments:

Post a Comment