Friday, June 21, 2013

Fruits in the basket Turbo C game

/*

    Fruits in the basket is a fun and addicting game. Catch falling

    fruits and score points.

    Copyright (C) 2013 Abhishek Baddi

    This program is free software: you can redistribute it and/or 

    modify it under the terms of the GNU General Public License as

    published by the Free Software Foundation, either version 3 of

    the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see http://www.gnu.org/licenses/.

*/


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ENTER 13

int r=20, coorx[9], x, midx, midy, cellspc=50;

int score=0, miss=0, speed=10;

char cha[8];

void drawboard(struct basket );
void drawfruit(struct fruit * );

int fruity;

struct fruit {
 int x, y;
};

struct basket {
 int x, y, rad;
};

int main() {
 int gd=DETECT, gm, i;
 char ch;
 struct basket one;
 struct fruit f1;
 randomize();
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 midx=getmaxx()/2;
 midy=getmaxy()/2;
 one.y=getmaxy()-40;
 coorx[0]=170-100;
 coorx[1]=220-100;
 coorx[2]=270-100;
 coorx[3]=320-100;
 coorx[4]=370-100;
 coorx[5]=420-100;
 coorx[6]=470-100;
 one.x=coorx[random(7)];
 one.rad=20;
 f1.x=coorx[random(7)];
 f1.y=0;
 settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
 outtext("Welcome to ");
 setcolor(MAGENTA);
 outtext("F");
 setcolor(GREEN);
 outtext("r");
 setcolor(RED);
 outtext("u");
 setcolor(YELLOW);
 outtext("i");
 setcolor(CYAN);
 outtext("t");
 setcolor(BROWN);
 outtext("s");
 setcolor(WHITE);
 outtext(" in the basket.");
 outtextxy(0,40,"Use \'a\' & \'d\' or the arrow keys to move");
 outtextxy(0,80,"the basket.");
 outtextxy(0,120,"Press \'q\' to quit anytime.");
 getch();

 while(1) {
  if(kbhit()) {
   ch=getch();
   switch(ch) {
    case 72:
     //up arrow pressed

     break;

    case 75:
     //left arrow pressed
     one.x-=cellspc;
     if(one.x<coorx[0])
      one.x+=cellspc;
     break;
    case 77:
     //right arrow pressed
     one.x+=cellspc;
     if(one.x>coorx[6])
      one.x-=cellspc;
     break;
    case 80:
     //down arrow pressed

     break;


case 'a':

     //'a' = left arrow pressed


one.x-=cellspc;
     if(one.x<coorx[0])
      one.x+=cellspc;
     break;
    case 'd':

     //'d' = right arrow pressed


one.x+=cellspc;
     if(one.x>coorx[6])
      one.x-=cellspc;
     break;
    case '\r':
     goto end;
    case 'q':
     goto end;
    default:
     break;
   }
  }
  delay(50);
  drawboard(one);
  drawfruit(&f1);

  if(((f1.y+50)>=one.y)&&(f1.x==one.x)) {
   ++score;
   f1.x=coorx[random(7)];
   f1.y=0;
  }
  if(f1.y>one.y) {
   ++miss;
   f1.x=coorx[random(7)];
   f1.y=0;
  }
  speed=0.2*score+10;
  if(speed<10)
   speed=10;
  if(speed>30)
   speed=30;
  if(miss>10) {
   end:
   clearviewport();
   setcolor(WHITE);
   itoa(score,cha,10);
   outtextxy(2,10,"Your score is:");
   setcolor(GREEN);
   outtextxy(2,40,cha);
   getch();
   return 0;
  }
 }
}

void drawboard(struct basket one) {
 char st[8];
 clearviewport();
 setcolor(WHITE);
 line(440,0,440,480);
 itoa(miss,st,10);
 outtextxy(450,20,"Your misses are:");
 setcolor(RED);
 outtextxy(450,50,st);
 itoa(score,st,10);
 setcolor(WHITE);
 outtextxy(450,80,"Your score is:");
 setcolor(GREEN);
 outtextxy(450,110,st);
 setcolor(YELLOW);
 setfillstyle(XHATCH_FILL, YELLOW);
 pieslice(one.x, one.y, 180, 360, one.rad);
 setcolor(WHITE);
 itoa(one.x,st,10);
 outtextxy(550,130,"x=");
 outtextxy(565,130,st);
 itoa(one.y,st,10);
 outtextxy(550,150,"y=");
 outtextxy(565,150,st);
}

void drawfruit(struct fruit *f) {
 char st[8];
 setcolor(RED);
 setfillstyle(SOLID_FILL, RED);
 pieslice(f->x, f->y, 0, 360, r-2);
 f->y+=speed;
 setcolor(WHITE);
 itoa(f->x,st,10);
 outtextxy(450,130,"x=");
 outtextxy(465,130,st);
 itoa(f->y,st,10);
 outtextxy(450,150,"y=");
 outtextxy(465,150,st);

}

No comments:

Post a Comment