Wednesday, July 19, 2023

GRAPICS

 

GRAPICS

graphicsDriver - DETECT 

graphicsMode 

driverDirectoryPath - BGI files

Colors

16 colors declared in C Graphics

COLOR MACRO

INTEGER VALUE

BLACK

0

BLUE

1

GREEN

2

CYAN

3

RED

4

MAGENTA

5

BROWN

6

LIGHTGRAY

7

DARKGRAY

8

LIGHTBLUE

9

LIGHTGREEN

10

LIGHTCYAN

11

LIGHTRED

12

LIGHTMAGENTA

13

YELLOW

14

WHITE

15

 

/* C graphics program to draw a line */

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

int main() {

    int gd = DETECT, gm;

    /* initialization of graphic mode */

    initgraph(&gd, &gm, "C:\\Turboc3\\BGI ");

    line(100,100,200, 200);

    getch();

    closegraph();

    getch();

}

Output

 


 

RAINBOW  PROGRAM

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

void main()

{

int gdriver = DETECT,gmode;

int x,y,i;

    initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");

   x=getmaxx()/2;

   y=getmaxy()/2;

   for(i=30;i<200;i++)

   {

       delay(100);

       setcolor(i/10);

       arc(x,y,0,180,i-10);

   }

getch();

}

Output



CLOCK

#include<conio.h>

#include<graphics.h>

#include<math.h>

#include<dos.h>

 

#define WBC 5

//^watchbackcolor

#define X 200

#define Y 200

 

void dial(int x, int y);

void sechand(int timeminute);

 

void minhand(int t)

{

int x1,y1;

setlinestyle(0,0,3);

 

x1= X+ (80 * cos(t*0.1047));

y1= Y+ (80 * sin(t*0.1047));

 

setcolor(BLACK);

line( X, Y, x1, y1);

 

setcolor(WBC+1);

line( X, Y, X+ 80 * cos((t-1)*0.1047),Y+ 80 * sin((t-1)*0.1047));

circle(X,Y,4);

}

 

void sechand(int t)

{

int x1,y1;

setlinestyle(0,0,3);

 

x1= X+(100 * cos(t*0.1047));

y1= Y+(100 * sin(t*0.1047));

 

setcolor(RED);

line(X, Y, x1, y1);

 

setcolor(WBC+1);

line(X, Y, X+ 100 * cos((t-1)*0.1047),Y+ 100 * sin((t-1)*0.1047));

 

circle(X,Y,4);

}

 

void dial(int x,int y)

{

int const size=200;

 

setfillstyle(1,WBC);

fillellipse(x,y,size,size);

 

setfillstyle(1,WBC+1);

fillellipse(x,y,size-20,size-20);

 

outtextxy(x,y-(size-40),"12");

outtextxy(x,y+(size-40),"6");

outtextxy(x+(size-40),y,"3");

outtextxy(x-(size-40),y,"9");

outtextxy(x+size/3,y-2*size/3,"1");

outtextxy(x+2*size/3,y-size/3,"2");

outtextxy(x+2*size/3,y+size/3,"4");

outtextxy(x+size/3,y+2*size/3,"5");

outtextxy(x-size/3,y+2*size/3,"7");

outtextxy(x-2*size/3,y+size/3,"8");

outtextxy(x-size/3,y-2*size/3,"11");

outtextxy(x-2*size/3,y-size/3,"10");

 

circle(x,y,4);

}

 

void main()

{

int gd=DETECT, gm,i,j, flag=1;

initgraph(&gd,&gm,"C:\\turboc3\\bgi");

 

dial(200,200);

do

{

minhand(i);

for(j=0;j<60;j++)

{

sechand(j);

delay(1000

);

if(kbhit()) {

flag =0;

break;

}

}

i++;

}while(flag);

closegraph();

}

Output



C Graphics Programs

 

CIRCLE

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

 

main(){

   int gd = DETECT,gm;

   int x ,y ,radius=80;

   initgraph(&gd, &gm, " C:\\Turboc3\\BGI ");

   /* Initialize center of circle with center of screen */

   x = getmaxx()/2;

   y = getmaxy()/2;

 

   outtextxy(x-100, 50, "CIRCLE Using Graphics in C");

   /* Draw circle on screen */

   circle(x, y, radius);

 

   closegraph();

   getch();

}

Output


 


 

RECTANGLE & SOLID BAR

#include <stdio.h>

#include<conio.h>

#include<graphics.h>

 

main(){

   int gd = DETECT,gm;

   initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 

   /* Draw rectangle on screen */

   rectangle(150, 50, 400, 150);

 

   /* Draw Bar on screen */

   bar(150, 200, 400, 350);

 

   closegraph();

   getch();

}

 Output




 

 

ECLIPSE

 

#include <stdio.h>

#include<conio.h>

#include<graphics.h>

 

main(){

   int gd = DETECT,gm;

   int x ,y;

   initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

   /* Initialize center of ellipse with center of screen */

   x = getmaxx()/2;

   y = getmaxy()/2;

 

   outtextxy(x-100, 50, "ELLIPSE Using Graphics in C");

   /* Draw ellipse on screen */

   ellipse(x, y, 0, 360, 120, 60);

 

   closegraph();

   getch();

}

 Output




 

CONCENTRIC CIRCLES

#include <stdio.h>

#include<conio.h>

#include<graphics.h>

 

main(){

   int gd = DETECT,gm;

   int x ,y;

   initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

   /* Initialize center of circle with center of screen */

   x = getmaxx()/2;

   y = getmaxy()/2;

 

   outtextxy(240, 50, "Concentric Circles");

   /* Draw circles on screen */

   setcolor(RED);

   circle(x, y, 30);

   setcolor(GREEN);

   circle(x, y, 50);

   setcolor(YELLOW);

   circle(x, y, 70);

   setcolor(BLUE);

   circle(x, y, 90);

 

   closegraph();

getch();

}

 Output




  

BAR GRAPH

#include <stdio.h>

#include<conio.h>

#include <graphics.h>

 

main() {

   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 

   settextstyle(BOLD_FONT,HORIZ_DIR,2);

   outtextxy(275,0,"BAR GRAPH");

 

   setlinestyle(SOLID_LINE,0,2);

   /* Draw X and Y Axis */

   line(90,410,90,50);

   line(90,410,590,410);

   line(85,60,90,50);

   line(95,60,90,50);

   line(585,405,590,410);

   line(585,415,590,410);

 

   outtextxy(65,60,"Y");

   outtextxy(570,420,"X");

   outtextxy(70,415,"O");

   /* Draw bars on screen */

   setfillstyle(XHATCH_FILL, RED);

   bar(150,80,200,410);

   bar(225,100,275,410);

   bar(300,120,350,410);

   bar(375,170,425,410);

   bar(450,135,500,410);

 

   closegraph();

getch();

}

 Output




 

 

3D BAR GRAPH

#include <stdio.h>

#include<conio.h>

#include <graphics.h>

  

main() {

   int gd = DETECT, gm;

   initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 

   settextstyle(BOLD_FONT,HORIZ_DIR,2);

   outtextxy(275,0,"3D BAR GRAPH");

 

   setlinestyle(SOLID_LINE,0,2);

   /* Print X and Y Axis */

   line(90,410,90,50);

   line(90,410,590,410);

   line(85,60,90,50);

   line(95,60,90,50);

   line(585,405,590,410);

   line(585,415,590,410);

 

   outtextxy(65,60,"Y");

   outtextxy(570,420,"X");

   outtextxy(70,415,"O");

 

   /* Print 3D bars */

   setfillstyle(XHATCH_FILL, RED);

   bar3d(150,80,200,410, 15, 1);

   bar3d(225,100,275,410, 15, 1);

   bar3d(300,120,350,410, 15, 1);

   bar3d(375,170,425,410, 15, 1);

   bar3d(450,135,500,410, 15, 1);

 

   closegraph();

getch();

}

 Output





 

SINE WAVE

#include <stdio.h>

#include<conio.h>

#include <math.h>

#include <graphics.h>

#include <dos.h>

 

main() {

    int gd = DETECT, gm;

    int angle = 0;

    double x, y;

 

    initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 

 line(0, getmaxy() / 2, getmaxx(), getmaxy() / 2);

 /* generate a sine wave */

 for(x = 0; x < getmaxx(); x+=3) {

 

     /* calculate y value given x */

     y = 50*sin(angle*3.141/180);

     y = getmaxy()/2 - y;

 

     /* color a pixel at the given position */

  putpixel(x, y, 15);

  delay(100);

 

  /* increment angle */

  angle+=5;

 }

 

 closegraph();

 

getch();

}

 Output




PIE CHART

 

#include <stdio.h>

#include<conio.h>

 

#include<graphics.h>

 

main() {

   int gd = DETECT, gm, x, y;

   initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 

   settextstyle(BOLD_FONT,HORIZ_DIR,2);

   outtextxy(220,10,"PIE CHART");

   /* Setting cordinate of center of circle */

   x = getmaxx()/2;

   y = getmaxy()/2;

 

   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,1);

   setfillstyle(SOLID_FILL, RED);

   pieslice(x, y, 0, 60, 120);

   outtextxy(x + 140, y - 70, "FOOD");

 

   setfillstyle(SOLID_FILL, YELLOW);

   pieslice(x, y, 60, 160, 120);

   outtextxy(x - 30, y - 170, "RENT");

 

   setfillstyle(SOLID_FILL, GREEN);

   pieslice(x, y, 160, 220, 120);

   outtextxy(x - 250, y, "ELECTRICITY");

 

   setfillstyle(SOLID_FILL, BROWN);

   pieslice(x, y, 220, 360, 120);

   outtextxy(x, y + 150, "SAVINGS");

 

   closegraph();

getch();

}

 Output



 

DIGITAL CLOCK

#include <stdio.h>

#include<conio.h>

#include <graphics.h>

#include <time.h>

#include <dos.h>

#include <string.h>

 

main() {

    int gd = DETECT, gm;

    int midx, midy;

    long current_time;

    char timeStr[256];

 

    initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 

    /* mid pixel in horizontal and vertical axis */

    midx = getmaxx() / 2;

    midy = getmaxy() / 2;

 

    while (!kbhit()) {

        cleardevice();

        setcolor(WHITE);

        setfillstyle(SOLID_FILL, WHITE);

        rectangle(midx - 250, midy - 40, midx + 250, midy + 40);

        floodfill(midx, midy, WHITE);

        /* Get Current epoch time in seconds */

        current_time = time(NULL);

        /* store the date and time in string */

        strcpy(timeStr, ctime(&current_time));

        setcolor(RED);

        settextjustify(CENTER_TEXT, CENTER_TEXT);

        settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 4);

 

        moveto(midx, midy);

        /* print current time */

        outtext(timeStr);

        /* Add delay of 1000 milliseconds(1 second) */

        delay(1000);

    }

 

    closegraph();

    getch();

}

 Output




 

BOUNCING BALL ANIMATION

#include <stdio.h>

#include<conio.h>

#include <graphics.h>

#include <dos.h>

 main() {

 int gd = DETECT, gm;

 int i, x, y, flag=0;

 initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 

 /* get mid positions in x and y-axis */

 x = getmaxx()/2;

 y = 30;

 

 

 while (!kbhit()) {

  if(y >= getmaxy()-30 || y <= 30)

     flag = !flag;

     /* draws the gray board */

     setcolor(RED);

     setfillstyle(SOLID_FILL, RED);

     circle(x, y, 30);

     floodfill(x, y, RED);

 

 /* delay for 50 milli seconds */

 delay(50);

 

 /* clears screen */

 cleardevice();

 if(flag){

     y = y + 5;

 } else {

     y = y - 5;

 }

    }

 

    closegraph();

getch();

}

Output


 


MOVING CAR ANIMATION


#include <stdio.h>

#include<conio.h>

#include <graphics.h>

#include <dos.h>

 

main() {

    int gd = DETECT, gm;

    int i, maxx, midy;

 

    /* initialize graphic mode */

    initgraph(&gd, &gm, "X:\\TC\\BGI");

    /* maximum pixel in horizontal axis */

    maxx = getmaxx();

    /* mid pixel in vertical axis */

    midy = getmaxy()/2;

 

    for (i=0; i < maxx-150; i=i+5) {

        /* clears screen */

        cleardevice();

 

        /* draw a white road */

        setcolor(WHITE);

        line(0, midy + 37, maxx, midy + 37);

 

        /* Draw Car */

        setcolor(YELLOW);

        setfillstyle(SOLID_FILL, RED);

 

        line(i, midy + 23, i, midy);

        line(i, midy, 40 + i, midy - 20);

        line(40 + i, midy - 20, 80 + i, midy - 20);

        line(80 + i, midy - 20, 100 + i, midy);

        line(100 + i, midy, 120 + i, midy);

        line(120 + i, midy, 120 + i, midy + 23);

        line(0 + i, midy + 23, 18 + i, midy + 23);

        arc(30 + i, midy + 23, 0, 180, 12);

        line(42 + i, midy + 23, 78 + i, midy + 23);

        arc(90 + i, midy + 23, 0, 180, 12);

        line(102 + i, midy + 23, 120 + i, midy + 23);

        line(28 + i, midy, 43 + i, midy - 15);

        line(43 + i, midy - 15, 57 + i, midy - 15);

        line(57 + i, midy - 15, 57 + i, midy);

        line(57 + i, midy, 28 + i, midy);

        line(62 + i, midy - 15, 77 + i, midy - 15);

        line(77 + i, midy - 15, 92 + i, midy);

        line(92 + i, midy, 62 + i, midy);

        line(62 + i, midy, 62 + i, midy - 15);

        floodfill(5 + i, midy + 22, YELLOW);

        setcolor(BLUE);

        setfillstyle(SOLID_FILL, DARKGRAY);

        /*  Draw Wheels */

        circle(30 + i, midy + 25, 9);

        circle(90 + i, midy + 25, 9);

        floodfill(30 + i, midy + 25, BLUE);

        floodfill(90 + i, midy + 25, BLUE);

        /* Add delay of 0.1 milli seconds */

        delay(100);

    }

 

    closegraph();

    getch();

}

 

Output


Snake game in C

#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <time.h>

check();
end();
win();
int m[500],n[500],con=20;
clock_t start,stop;
void main(void)
{

int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100,temp,a=0,i=0,j,t;

initgraph(&gd,&gm,"c:\\Turboc3\\BGI");

setcolor(WHITE);
settextstyle(3,0,6);
outtextxy(200,2," SNAKE 2 ");
settextstyle(6,0,2);
outtextxy(20,80," Use Arrow Keys To Direct The Snake ");
outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of Snake");
outtextxy(20,160," Pick The Beats Untill You Win The Game ");
outtextxy(20,200," Press 'Esc' Anytime To Exit ");
outtextxy(20,220," Press Any Key To Continue ");
ch=getch();
if(ch==27) exit(0);
cleardevice();
maxx=getmaxx();
maxy=getmaxy();

randomize();

p=random(maxx);
temp=p%13;
p=p-temp;
q=random(maxy);
temp=q%14;
q=q-temp;



 start=clock();
while(1)
{

 setcolor(WHITE);
 setfillstyle(SOLID_FILL,con+5);
 circle(p,q,5);
 floodfill(p,q,WHITE);

   if( kbhit() )
   {
     ch=getch(); if(ch==0) ch=getch();
     if(ch==72&& a!=2) a=1;
     if(ch==80&& a!=1) a=2;
     if(ch==75&& a!=4) a=3;
     if(ch==77&& a!=3) a=4;
      }
       else
     {
     if(ch==27
     ) break;
     }

       if(i<20){
   m[i]=x;
   n[i]=y;
   i++;
       }

if(i>=20)

{
  for(j=con;j>=0;j--){
  m[1+j]=m[j];
  n[1+j]=n[j];
  }
   m[0]=x;
   n[0]=y;

   setcolor(WHITE);
   setfillstyle(SOLID_FILL,con);
   circle(m[0],n[0],8);
   floodfill(m[0],n[0],WHITE);

   setcolor(WHITE);
   for(j=1;j<con;j++){
  setfillstyle(SOLID_FILL,con+j%3);
   circle(m[j],n[j],5);
   floodfill(m[j],n[j],WHITE);
      }
       delay(spd);

  setcolor(BLACK);
  setfillstyle(SOLID_FILL,BLACK);
   circle(m[0],n[0],8);
   floodfill(m[0],n[0],BLACK);

   setcolor(BLACK);
  setfillstyle(SOLID_FILL,BLACK);
   circle(m[j],n[j],5);
   floodfill(m[j],n[j],BLACK);

   }
     stop=clock();
     t=(stop-start)/CLK_TCK;
   //  printf(" TIME %d sec   ",t);
    // printf("SCORE %d",con-5);
     check();

    if(x==p&&y==q) { con=con+5; if(spd>=5) spd=spd-5; else spd=5;
      if(con>490) win();
     p=random(maxx); temp=p%13;  p=p-temp;
     q=random(maxy); temp=q%14;   q=q-temp;
    }
    if(a==1)  y =  y-14; if(y<0) { temp=maxy%14;y=maxy-temp;}
    if(a==2)  y =  y+14; if(y>maxy) y=0;
    if(a==3)  x =  x-13; if(x<0) { temp=maxx%13;x=maxx-temp;}
    if(a==4)  x =  x+13; if(x>maxx) x=0;
    if(a==0){  y = y+14 ;  x=x+13; }
}

 }


check(){
   int a;
   for(a=1;a<con;a++)

if(m[0]==m[a] && n[0]==n[a]) end();
   else continue;

   }
end()

{

    int j,i;
   setcolor(WHITE);
   for(i=0;i<5;i++){
   delay(500);
    cleardevice();
    delay(500);
   for(j=0;j<=con;j++){
   setfillstyle(SOLID_FILL,RED);
   circle(m[j],n[j],5);
   floodfill(m[j],n[j],WHITE);
  }

}

    settextstyle(3,0,4);
    outtextxy(150,150,"    GAME OVER ");
    getch();
    getch();
    exit(0);
    }

win()
{
int j,i;
setcolor(WHITE);
   for(i=0;i<5;i++){
   for(j=0;j<=con;j++){
   setfillstyle(SOLID_FILL,con);
   circle(m[j],n[j],5);
   floodfill(m[j],n[j],WHITE);
  }
    delay(500);
    cleardevice();
    delay(500);
}
  settextstyle(3,0,4);
  outtextxy(210,320," YOU WIN ");
  getch();
  exit(0);
}
Output







Tetris game in C
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<time.h>
//The tetris matrix (2d array)
int board[35][13];
//Different versions of T block (rotated)
int t_[8]={1,0,1,1,1,2,2,1};
int t_90[8]={0,1,1,0,1,1,2,1};
int t_180[8]={0,1,1,0,1,1,1,2};
int t_270[8]={0,1,1,1,1,2,2,1};
//Different versions of L block (rotated)
int l_[8]={0,2,1,0,1,1,1,2};
int l_90[8]={0,1,1,1,2,1,2,2};
int l_180[8]={1,0,1,1,1,2,2,0};
int l_270[8]={0,0,0,1,1,1,2,1};
//S block

int s_[8]={0,0,0,1,1,0,1,1};
//Different versions of Z block
int z_[8]={1,1,1,2,2,0,2,1};
int z_90[8]={0,1,1,1,1,2,2,2};
//Different versions of I block

int i_ver[8]={0,1,1,1,2,1,3,1};
int i_hor[8]={1,0,1,1,1,2,1,3};
//score as string
char * scorestr;
/*
Numbering for type of blocks:
(values of fallingBlockNumber)
0=T
1=L
2=S
3=Z
4=I
*/
//pointer to current falling block type array
int *blockarray;
int fallingblockNum;
int fallingBlockVersion=0;//values 0 to 4
int fallingBlockRow=0;//the current row of falling block
int fallingBlockCol=0;//the current column of falling block
int startdelay=200;
int motiondelay;
int scoreInc=5;//score for each line clearance
int myscore=0;//score
//spawning next falling block
void NextBlock()
{
fallingblockNum=rand()%5;
if(fallingblockNum==0||fallingblockNum==1)
 fallingBlockVersion=rand()%4;
else if(fallingblockNum==4||fallingblockNum==3)
 fallingBlockVersion=rand()%2;
else
 fallingBlockVersion=0;
fallingBlockRow=0;
fallingBlockCol=5;
}
//store pointer to current falling block's array in *blockarray;
int *getFallingBlockArray()
{
int a=fallingblockNum*10+fallingBlockVersion;
switch(a)
    {
    case 0:return ((int*)&t_);
    case 1:return ((int*)&t_90);
    case 2:return ((int*)&t_180);
    case 3:return ((int*)&t_270);
    case 10:return ((int*)&l_);
    case 11:return ((int*)&l_90);
    case 12:return ((int*)&l_180);
    case 13:return ((int*)&l_270);
    case 20:return ((int*)&s_);
    case 30:return ((int*)&z_);
    case 31:return ((int*)&z_90);
    case 40:return ((int*)&i_hor);
    case 41:return ((int*)&i_ver);
    }
return ((int*)&i_ver);
}
/* To check if moving to left or right
or rotation of falling block is feasible.
i.e to check whether there is enough space around it.
*/
int isDrawable(int newrow,int newcol,int blockversion)
{
int i,tempversion,flag=1;;
tempversion=fallingBlockVersion;
fallingBlockVersion=blockversion;
blockarray=getFallingBlockArray();
for(i=0;i<8;i+=2)
    {
//check if block goes out of the matrix (up or down)

if(newrow+blockarray[i]>34||newrow+blockarray[i]<0)
 {
 flag=0;
 break;
 }
//check if block goes out of the matrix (left or right)

if(newcol+blockarray[i+1]>12||newcol+blockarray[i+1]<0)
 {
 flag=0;
 break;
 }
//check if there is any obstacle on the proposed position

if(board[(newrow+blockarray[i])][(newcol+blockarray[i+1])]==2)
 {
 flag=0;
 break;
 }
    }
fallingBlockVersion=tempversion;
blockarray=getFallingBlockArray();
return flag;
}

/*
To clear the old position of falling block
after it moves to a new position (row/column change)
or after it is rotated
*/
void clearOldBlockVersion()
{
int i,r,c;
for(i=0;i<8;i+=2)
 {
 r=fallingBlockRow+blockarray[i];
 c=fallingBlockCol+blockarray[i+1];
 board[r][c]=0;
 setfillstyle(EMPTY_FILL,BLACK);
 setcolor(BLACK);
 bar(2+c*13,2+r*13,2+c*13+13,2+r*13+13);
 rectangle(2+c*13,2+r*13,2+c*13+13,2+r*13+13);
 }
}
/*

To draw the new picture

after it moves to a new position (row/column change)

or after it is rotated

*/

void drawNewBlockVersion()
{
int i,r,c;
for(i=0;i<8;i+=2)
 {
 r=fallingBlockRow+blockarray[i];
 c=fallingBlockCol+blockarray[i+1];
 board[r][c]=1;
 setfillstyle(XHATCH_FILL,CYAN);
 setcolor(RED);
 bar(2+c*13,2+r*13,2+c*13+13,2+r*13+13);
 rectangle(2+c*13,2+r*13,2+c*13+13,2+r*13+13);
 }
}

/*

To check whether game is over.

called after every move.

Game is over when tetris matrix is filled.

*/


int isGameOver()
{
if(isDrawable(0,5,fallingBlockVersion)==0)
    return 1;
drawNewBlockVersion();
if(isAtBottom())
    return 1;
return 0;
}
/*
To check if falling block reached the bottom
*/
int isAtBottom()
{
int i,max=0,ti,tj;
for(i=0;i<8;i+=2)
    if(*(blockarray+i)>max)
 max=*(blockarray+i);
if(fallingBlockRow+max>=34)
 return 1;
for(i=0;i<8;i+=2)
    {
    ti=*(blockarray+i)+fallingBlockRow;
    tj=*(blockarray+i+1)+fallingBlockCol;
    if(board[ti+1][tj]==2)
       return 1;
    }
return 0;
}
/*
Draw score on screen
*/
void showScore()
{
int left,top;
setcolor(BLACK);
setfillstyle(EMPTY_FILL,BLACK);
left=getmaxx()-100;
top=getmaxy()/2;
bar3d(left,top,left+60,top+70,2,1);
setcolor(CYAN);
settextstyle(TRIPLEX_FONT, HORIZ_DIR,2);
outtextxy(getmaxx()-100,getmaxy()/2,"Score: ");
outtextxy(getmaxx()-100,getmaxy()/2+20,itoa(myscore,scorestr,10));
}
/*

To clear any row that is fully filled and increment score.

Also settles rows over the cleared row down.

*/

void CollapseFullRow()
{
int i,j,k,sum,copyskipover=0,r;
for(i=34;i>=0;)
    {
    sum=0;//full flag
    for(j=0;j<13;j++)
 sum+=board[i][j];
    if(sum==2*13)//row full
 {
 myscore+=scoreInc;
 copyskipover++;
 }
    if(sum==0)
 break;
    i--;
    if(copyskipover>0)
 {
 for(j=0;j<13;j++)
     {
     r=i+copyskipover;
     board[r][j]=board[i][j];
     if(board[i][j]==0)
  {
  setfillstyle(SOLID_FILL, RED);//empty yo hatch
  setcolor(BLACK);
  bar(2+j*13,2+r*13,2+j*13+13,2+r*13+13);
  }
     else
  {
  setfillstyle(SOLID_FILL, RED);
  setcolor(WHITE);
  bar(2+j*13,2+r*13,2+j*13+13,2+r*13+13);
  rectangle(2+j*13,2+r*13,2+j*13+13,2+r*13+13);
  }
     }
 }
    }
for(k=0;k<copyskipover;k++)
    {
    r=i+k;
    for(j=0;j<13;j++)
 {
 board[r][j]=0;
 setfillstyle(XHATCH_FILL,BLACK);
 setcolor(BLACK);
 bar(2+j*13,2+r*13,2+j*13+13,2+r*13+13);
 }
    }
showScore();
}

//function to display game over and end game
void GameOver();

void main()
{
int scorespeedctrl=0;
char arrowpre,ch,timehalving=0;
int gd=DETECT,gm,i,j,k=1,spawn=1,tversion;
motiondelay=startdelay;
scorestr=(char*)malloc(sizeof(char)*10);
for(i=0;i<35;i++)
    for(j=0;j<13;j++)
 board[i][j]=0;
/*
to get the ascii characters for up arrow key.
When any of arrow key is pressed, two characters
are sent to buffer. The first one may be different
on different compilers and systems.
The second one is of value 72 for up arrow.
*/
while(k!=72)
{
printf("press 'up' arrow key to start\n");
arrowpre=getch();
if(kbhit())
 k=getch();
}

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI\\");
cleardevice();
setcolor(YELLOW);
rectangle(0,0,13*13+6,35*13+6);
srand(time(NULL));
showScore();
setcolor(RED);
settextstyle(SMALL_FONT, HORIZ_DIR,6);
outtextxy(getmaxx()-200,getmaxy()/2-100,"Press 'Q' to quit.");

while(ch!='q'&&ch!='Q'&&k!='q'&&k!='Q')
    {
    if(kbhit()&&spawn==0)
 {
 ch=getch();
 if(ch==arrowpre)//if an arrow key pressed
     {
     k=getch();
     if(k==72)//if up arrow key is pressed
  {
  if(fallingblockNum==0||fallingblockNum==1)
      tversion=(fallingBlockVersion+1)%4;
  else if(fallingblockNum==4||fallingblockNum==3)
      tversion=(fallingBlockVersion+1)%2;

  if(fallingblockNum!=2&&isDrawable(fallingBlockRow,fallingBlockCol,tversion))
      {
      clearOldBlockVersion();
      fallingBlockVersion=tversion;
      blockarray=getFallingBlockArray();
      drawNewBlockVersion();
      }
  }
     else if(k==75)//if left arrow key is pressed

  {
  if(isDrawable(fallingBlockRow,fallingBlockCol-1,fallingBlockVersion))
      {
      clearOldBlockVersion();
      fallingBlockCol--;
      drawNewBlockVersion();
      }
  }
     else if(k==77)//if right arrow key is pressed
  {
  if(isDrawable(fallingBlockRow,fallingBlockCol+1,fallingBlockVersion))
      {
      clearOldBlockVersion();
      fallingBlockCol++;
      drawNewBlockVersion();
      }
  }
     else if(k==80)//if down arrow key is pressed
  {
  if(isDrawable(fallingBlockRow+1,fallingBlockCol,fallingBlockVersion))
      {
      clearOldBlockVersion();
      fallingBlockRow++;
      drawNewBlockVersion();
      }
  }
     while(kbhit())
  getch();
     }
 }
    if(isAtBottom()&&spawn==0)
 {
 for(i=0;i<8;i+=2)
     {
     board[fallingBlockRow+blockarray[i]][fallingBlockCol+blockarray[i+1]]=2;
     }
 spawn=1;
 CollapseFullRow();
 }
    if(spawn)
 {
 NextBlock();
 blockarray=getFallingBlockArray();
 spawn=0;
 if(isGameOver())
     {
     GameOver();
     return;
     }
 }
    else
 {
 timehalving=(timehalving+1)%3;
 if(timehalving==2)
  {
  clearOldBlockVersion();
  fallingBlockRow++;
  drawNewBlockVersion();
  }
 }
    scorespeedctrl=(scorespeedctrl+1)%280;
    if(scorespeedctrl==0)
 {
 motiondelay-=8;
 scoreInc++;
 }
    delay(motiondelay);
    }
}

void GameOver()
{
static int style=TRIPLEX_FONT;
static int size = 4;
setcolor(YELLOW);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(style, HORIZ_DIR, size);
outtextxy(getmaxx()/2-10,getmaxy()/2,"Game Over.\n");
getch();
main();
}

Output




0 comments:

Post a Comment