pls someone explain this program....

#include"graphics.h"
#include"dos.h"
#include"conio.h"
#include"stdlib.h"
#define DELAY 1
#define SOUND 3500
void state(int x,int y,int mode);
int i;
void *ptr1[4],*ptr2[4];
/* 3d Ball */
char ball[20][20]=
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,12,12,12,12,12,12,12,12,0,0,0,0,0,0},
{0,0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,12,12,12,12,12,15,15,12,12,12,12,12,12,12,12,12,0,0},
{0,12,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,15,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,15,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0},
{0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0},
{0,0,0,0,12,12,12,12,12,12,12,12,12,12,12,12,0,0,0,0},
{0,0,0,0,0,0,12,12,12,12,12,12,12,12,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
/* Condition to check out of range */
int COND(int x,int y)
{
if (x>10 && x<getmaxx()-10 && y>10 && y<getmaxy()-10)
return 1;
else
return 0;
}
/* This sub-routine will check which path to follow on stiking the wall
of
the container */
void state(int x,int y,int mode)
{
while(COND(x,y)&&!kbhit())
{
putimage(x,y,ptr1[4],COPY_PUT);
putimage(getmaxx()-x,getmaxy()-y,ptr1[4],COPY_PUT);
switch(mode)
{
case 0:
x++;
y++;
break;
case 1:
x++;
y--;
break;
case 2:
x--;
y++;
break;
case 3:
x--;
y--;
break;
}
delay(DELAY);
nosound();
putimage(x,y,ptr2[4],COPY_PUT);
putimage(getmaxx()-x,getmaxy()-y,ptr2[4],COPY_PUT);
}
cleardevice();
if(x>=(getmaxx()-10)||x<=10)
{
sound(SOUND);
switch(mode)
{
case 0:
state(--x,--y,2);
break;
case 1:
state(--x,++y,3);
break;
case 2:
state(++x,--y,0);
break;
case 3:
state(++x,++y,1);
break;
}
}
else
if(y>=getmaxy()-10||y<=10)
{
sound(SOUND);
switch(mode)
{
case 0:
state(--x,--y,1);
break;
case 1:
state(--x,++y,0);
break;
case 2:
state(++x,--y,3);
break;
case 3:
state(++x,++y,2);
break;
}
}
else
exit(0);
}
void main()
{
int gm,gd=DETECT;
int i,j;
initgraph(&gd,&gm,"\tc\tc\bgi");
for(i=0;i<20;i++)
for(j=0;j<20;j++)
if(ball[i][j]!='0')
putpixel(10+i,10+j,ball[j][i]);
getimage(10,10,30,30,ptr1[4]);
cleardevice();
getimage(10,10,30,30,ptr2[4]);
/* start with (20,20) */
state(20,20,0);
getch();
}

i know graphics a little i know the functions but i cant understand this programme fully...well mostly...please i really wanna understand it...someone pls explain ....thanks a million!
Well what is it you are wanting to know? I mean in simple terms it appears to draw a ball to the screen and move it around. If you are wanting to have every things explained as to what it is doing then that will take a little while for anyone to type up.
Hi! Thanks for your answer BHXSpecter.

This is what i know about the programme:
There is a ball which is made by putpixel.
Then we copied the ball by getimage in ptr1. isnt it?
Then in ptr2 a black portion is copied.

Then I don't understand much,
I get that the ball is put on screen till max xy-10 and cleared again again
But i dont understand rest.
What is the concept of mode?
Last edited on
I'm not a 100% positive, but it almost looks like mode is the equivalent of direction, but it looks like the code mostly keeps track of that. Someone will have to correct me if I'm wrong because I'm truly not sure. Sorry I can't be of more help.
Can someone pls pls pls explain this line

putimage(x,y,ptr2[4],COPY_PUT);
putimage(getmaxx()-x,getmaxy()-y,ptr2[4],COPY_PUT);

What does it do?
in ptr2 back img is stored isnt it?
if instead of x id x+100 or anything (and similarly with y)
there is no change

Pls someone tell
In all honesty I wouldn't bother trying to decipher this code...

1
2
#include"graphics.h"
#include"dos.h" 


These 2 headers are non-standard and haven't been in widespread use in maybe 15 years. This code is extremely old and wherever you got it from is an outdated resource and/or is not worth reading.
Thank you Disch for your answer but still i want to understand it . :)

I have simplified the code and it is a follws:

#include"graphics.h"
#include"dos.h"
#include"conio.h"
#include"stdlib.h"
void *p1;
int area;
int cond(int x,int y)
{
if(x>10 && x<getmaxx()-10 && y>10 && y<getmaxy()-10)
return 1;
else
return 0;
}
void ball(int x,int y,int mode)
{
while(cond(x,y)&&!kbhit())
{
putimage(x,y,p1,COPY_PUT);
putimage(getmaxx()-x,getmaxy()-y,p1,COPY_PUT);
switch(mode)
{
case 0:
x++;
y++;
break;
case 1:
x++;
y--;
break;
case 2:
x--;
y++;
break;
case 3:
x--;
y--;
break;
}
delay(1);
nosound();
cleardevice();

}
cleardevice();
if(x>=(getmaxx()-10)||x<=10)
{
sound(3500);
switch(mode)
{
case 0:
ball(--x,--y,2);
break;
case 1:
ball(--x,++y,3);
break;
case 2:
ball(++x,--y,0);
break;
case 3:
ball(++x,++y,1);
break;
}
}
else
if(y>=getmaxy()-10||y<=10)
{
sound(3500);
switch(mode)
{
case 0:
ball(--x,--y,1);
break;
case 1:
ball(--x,++y,0);
break;
case 2:
ball(++x,--y,3);
break;
case 3:
ball(++x,++y,2);
break;
}
}
else
exit(0);
}
void main()
{

int gm,gd=DETECT;
int i,j;
initgraph(&gd,&gm,"c:\\bgi");
setcolor(YELLOW);
circle(20,20,10);
setfillstyle(SOLID_FILL,YELLOW);
floodfill(20,20,YELLOW);
area = imagesize(10,10,30,30);
p1 = malloc(area);

getimage(10,10,30,30,p1);

ball(20,20,0);
getch();
}


just someone pls explain how to change the color of the balls when hey touch the screen.thnx a million :)
Topic archived. No new replies allowed.