Code:
#include <stdio.h>
#include <graphics.h>
#include <math.h>
using namespace std;
char scoretext[20];
int score = 0;
void button(int x,int y);
void rectangle( int left, int top, int right, int bottom );
int main(int argc, char *argv[])
{
int tag = -1;
int i=69,j=239,delay,xdir=1,ydir=1;
signed int x2,y2;
int distance,oldi,oldj,k;
initwindow(540,500);
setcolor (LIGHTGREEN);
rectangle (4,4,536,416);
setcolor (LIGHTBLUE);
rectangle (8,8,532,412);
printf("Score = %d",score);
int btns[] = {150, 45, 430, 350, 250, 250, 500, 150, 75, 150, 100, 370, 350, 100};
int num_btns = 7;
while(1)
{
button(btns[0],btns[1]);
button(btns[2],btns[3]);
button(btns[4],btns[5]);
button(btns[6],btns[7]);
button(btns[8],btns[9]);
button(btns[10],btns[11]);
button(btns[12],btns[13]);
//Edge detections, these are the walls of the box
oldi=i;
oldj=j;
if (i>=522)
{xdir = -(rand()%2)-1; }
if (i<=20)
{xdir = (rand()%2)+1; }
i=i+xdir;
if (j>=402)
{ydir = -(rand()%2)-1;}
if (j<=20)
{ydir = (rand()%2)+1;}
j=j+ydir;
//Collision Detect
for(k=0;k<(num_btns * 2);k=k+2)
{
x2=btns[k]-i;
y2=btns[k+1]-j;
distance=(int)sqrt((float)(x2*x2)+(y2*y2));
if((distance<20) && (tag == -1))
{
tag = k;
score = score+10;
printf("Score = %d",score);
sprintf(scoretext,"Score = %d",score);
setcolor(LIGHTBLUE);
settextstyle(4,0,2);
outtextxy(10,425,scoretext);
if (ydir>=1 && xdir>=1)
{xdir = -1;}
else
if (ydir>=1 && xdir<=-1)
{xdir = 1;}
else
if (ydir<=-1 && xdir>=1)
{ydir = 1;}
else
if (ydir<=-1 && xdir<=-1)
{ydir = 1;}
}
if ((distance >80)&&(tag ==k))
{ tag = -1;}
}
setcolor(BLACK);
setfillstyle(1,BLACK);
fillellipse(oldi,oldj,10,10);
setfillstyle(1,LIGHTBLUE);
fillellipse(i,j,10,10);
for(delay=0;delay<250000;delay++);
//getch();
}
closegraph();
return 0;
}
void button(int x,int y)
{
int i;
i=y;
setcolor(LIGHTGREEN);
setfillstyle (SOLID_FILL, LIGHTGREEN);
fillellipse(x,y,10,10);
//setcolor(SOLID_LIGHTGREEN);
//fillellipse(x,y,5,5);
}