Hello firends , i am new here.. am interested in programming languages...
here am sharing some useful graphics codes
1.solar system using C

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>


void main()
{
	int gd=DETECT,gm;
	float i=0,j=100,me=75,ve=23,ma=105,ju=175,sa=10,ur=300,ne=200,pl=175,s=0;
	int x,y,k,si,sx=random(300),sy=random(400),X,Y,ss;
	unsigned int earth,moon,mercury,venus,mars,jupiter,saturn,uranus,neptune,pluto,sunsize;
	void *ear,*moo,*mer,*ven,*mar,*jup,*sat,*ura,*nep,*plu,*sun,*ships,*ships2;
	x=300;
	y=210;
	initgraph(&gd,&gm,"d:\\tc\\bgi");

	setfillstyle(1,10);
	setcolor(2);
	fillellipse(50,50,13,13);
	earth=imagesize(35,35,65,65);
	ear=malloc(earth);
	getimage(35,35,65,65,ear);
	cleardevice();

	setfillstyle(1,15);
	setcolor(15);
	fillellipse(25,25,5,5);
	moon=imagesize(15,15,35,35);
	moo=malloc(moon);
	getimage(15,15,35,35,moo);
	cleardevice();

	setfillstyle(1,4);
	setcolor(4);
	fillellipse(15,15,4,4);
	mercury=imagesize(10,10,20,20);
	mer=malloc(mercury);
	getimage(10,10,20,20,mer);
	cleardevice();


	setfillstyle(1,11);
	setcolor(11);
	fillellipse(15,15,7,7);
	venus=imagesize(7,7,23,23);
	ven=malloc(venus);
	getimage(7,7,23,23,ven);
	cleardevice();


	setfillstyle(1,6);
	setcolor(6);
	fillellipse(20,20,14,14);
	mars=imagesize(5,5,35,35);
	mar=malloc(mars);
	getimage(5,5,35,35,mar);
	cleardevice();


	setfillstyle(1,7);
	setcolor(7);
	fillellipse(25,25,18,18);
	jupiter=imagesize(5,5,45,45);
	jup=malloc(jupiter);
	getimage(5,5,45,45,jup);
	cleardevice();


	setfillstyle(1,8);
	setcolor(8);
	fillellipse(50,50,12,12);
	uranus=imagesize(35,35,65,65);
	ura=malloc(uranus);
	getimage(35,35,65,65,ura);
	cleardevice();

	setfillstyle(1,12);
	setcolor(12);
	fillellipse(50,50,11,11);
	neptune=imagesize(35,35,65,65);
	nep=malloc(neptune);
	getimage(35,35,65,65,nep);
	cleardevice();

	setfillstyle(1,1);
	setcolor(1);
	fillellipse(50,50,8,8);
	pluto=imagesize(35,35,65,65);
	plu=malloc(pluto);
	getimage(35,35,65,65,plu);
	cleardevice();


	sunsize=imagesize(0,0,80,80);
	sun=malloc(sunsize);
	getimage(0,0,80,80,sun);

	cleardevice();
	setfillstyle(1,9);
	setcolor(9);
	fillellipse(45,45,16,16);
	setcolor(8);
	ellipse(45,45,125,390,20,7);
	setcolor(1);
	ellipse(45,45,120,400,23,8);
	setcolor(4);
	ellipse(45,45,120,400,25,9);
	setcolor(5);
	ellipse(45,45,120,424,28,10);
	setcolor(6);
	ellipse(45,45,115,425,30,11);
	saturn=imagesize(0,0,50,50);
	sat=malloc(saturn);
	getimage(0,0,75,75,sat);
	cleardevice();
	setcolor(15);
	cleardevice();
	outtextxy(200,60,"SOLAR SYSTEM ca-217");
	delay(300);
	setlinestyle(0,0,3);
	rectangle(0,0,getmaxx(),getmaxy());
	delay(700);
	for(i=0; i<1000; i++)
	{
		putpixel(random(630),random(530),15);
		putpixel(random(630),random(530),11);
		delay(5);
	}
	setlinestyle(3,0,1);
	setlinestyle(0,0,1);
	for(k=0; k<200; k++)
	{
		i=i+0.2;
		j=j+0.7;
		me=me+0.1;
		ve=ve+0.3;
		ma=ma+0.1;
		ju=ju+0.08;
		sa=sa+0.07;
		ur=ur+0.06;
		ne=ne+0.05;
		pl=pl+0.04;
		putpixel(x+100*cos(i)+13,y+80*sin(i)+13,10);
		putpixel(x+(100*cos(i))+(30*cos(j))+5,y+(80*sin(i))+(25*sin(j))+5,1);
		putpixel(x+60*cos(me)+4,y+40*sin(me)+4,4);
		putpixel(x+80*cos(ve)+7,y+60*sin(ve)+7,3);
		putpixel(x+125*cos(ma)+14,y+110*sin(ma)+14,6);
		putpixel(x+165*cos(ju)+18,y+130*sin(ju)+18,7);
		putpixel(x+220*cos(sa)+30+16,y+170*sin(sa)+30+16,9);
		putpixel(x+250*cos(ur)+12,y+200*sin(ur)+12,8);
		putpixel(x+280*cos(ne)+11,y+230*sin(ne)+11,12);
		putpixel(x+310*cos(pl)+8,y+250*sin(pl)+8,1);


	}


		setfillstyle(1,14);
		setcolor(14);
		fillellipse(x,y,25,25);
		delay(250);
		putimage(x-40,y-40,sun,XOR_PUT);
		setlinestyle(0,0,1);
		setfillstyle(1,14);
		setcolor(14);
		fillellipse(x,y,25,25);
		delay(250);
		putimage(x+100*cos(i),y+80*sin(i),ear,XOR_PUT);
		delay(250);
		putimage(x+(100*cos(i))+(30*cos(j)),y+(80*sin(i))+(25*sin(j)),moo,XOR_PUT);
		delay(250);
		putimage(x+60*cos(me),y+40*sin(me),mer,XOR_PUT);
		delay(250);
		putimage(x+80*cos(ve),y+60*sin(ve),ven,XOR_PUT);
		delay(250);
		putimage(x+125*cos(ma),y+110*sin(ma),mar,XOR_PUT);
		delay(250);
		putimage(x+165*cos(ju),y+130*sin(ju),jup,XOR_PUT);
		delay(250);
		putimage(x+220*cos(sa),y+170*sin(sa),sat,XOR_PUT);
		delay(250);
		putimage(x+250*cos(ur),y+200*sin(ur),ura,XOR_PUT);
		delay(250);
		putimage(x+280*cos(ne),y+230*sin(ne),nep,XOR_PUT);
		delay(250);
		putimage(x+310*cos(pl),y+250*sin(pl),plu,XOR_PUT);


		putimage(x-40,y-40,sun,XOR_PUT);
		setlinestyle(0,0,1);
		setfillstyle(1,14);
		setcolor(14);
		fillellipse(x,y,25,25);
		putimage(x+100*cos(i),y+80*sin(i),ear,XOR_PUT);
		putimage(x+(100*cos(i))+(30*cos(j)),y+(80*sin(i))+(25*sin(j)),moo,XOR_PUT);
		putimage(x+60*cos(me),y+40*sin(me),mer,XOR_PUT);
		putimage(x+80*cos(ve),y+60*sin(ve),ven,XOR_PUT);
		putimage(x+125*cos(ma),y+110*sin(ma),mar,XOR_PUT);
		putimage(x+165*cos(ju),y+130*sin(ju),jup,XOR_PUT);
		putimage(x+220*cos(sa),y+170*sin(sa),sat,XOR_PUT);
		putimage(x+250*cos(ur),y+200*sin(ur),ura,XOR_PUT);
		putimage(x+280*cos(ne),y+230*sin(ne),nep,XOR_PUT);
		putimage(x+310*cos(pl),y+250*sin(pl),plu,XOR_PUT);

		for(k=0; k<200; k++)
		{
			i=i+0.2;
			j=j+0.7;
			me=me+0.1;
			ve=ve+0.3;
			ma=ma+0.1;
			ju=ju+0.08;
			sa=sa+0.07;
			ur=ur+0.06;
			ne=ne+0.05;
			pl=pl+0.04;
			putpixel(x+100*cos(i)+13,y+80*sin(i)+13,10);

			putpixel(x+(100*cos(i))+(30*cos(j))+5,y+(80*sin(i))+(25*sin(j))+5,1);
			putpixel(x+60*cos(me)+4,y+40*sin(me)+4,4);
			putpixel(x+80*cos(ve)+7,y+60*sin(ve)+7,3);
			putpixel(x+125*cos(ma)+14,y+110*sin(ma)+14,6);
			putpixel(x+165*cos(ju)+18,y+130*sin(ju)+18,7);
			putpixel(x+220*cos(sa)+30+16,y+170*sin(sa)+30+16,9);
			putpixel(x+250*cos(ur)+12,y+200*sin(ur)+12,8);
			putpixel(x+280*cos(ne)+11,y+230*sin(ne)+11,12);
			putpixel(x+310*cos(pl)+8,y+250*sin(pl)+8,1);

		}

	i=0;j=100;me=75;ve=23;ma=105;ju=175;sa=10;ur=300;ne=200;pl=175;


	while(!kbhit())

	{
		i=i+0.2;
		j=j+0.7;
		me=me+0.1;
		ve=ve+0.3;
		ma=ma+0.1;
		ju=ju+0.08;
		sa=sa+0.07;
		ur=ur+0.06;
		ne=ne+0.05;
		pl=pl+0.04;

		putimage(x-40,y-40,sun,XOR_PUT);
		setlinestyle(0,0,1);
		setfillstyle(1,14);
		setcolor(14);
		fillellipse(x,y,25,25);

		putimage(x+100*cos(i),y+80*sin(i),ear,XOR_PUT);

putimage(x+(100*cos(i))+(30*cos(j)),y+(80*sin(i))+(25*sin(j)),moo,XOR_PUT)
;
		putimage(x+60*cos(me),y+40*sin(me),mer,XOR_PUT);
		putimage(x+80*cos(ve),y+60*sin(ve),ven,XOR_PUT);
		putimage(x+125*cos(ma),y+110*sin(ma),mar,XOR_PUT);
		putimage(x+165*cos(ju),y+130*sin(ju),jup,XOR_PUT);
		putimage(x+220*cos(sa),y+170*sin(sa),sat,XOR_PUT);
		putimage(x+250*cos(ur),y+200*sin(ur),ura,XOR_PUT);
		putimage(x+280*cos(ne),y+230*sin(ne),nep,XOR_PUT);
		putimage(x+310*cos(pl),y+250*sin(pl),plu,XOR_PUT);

		for(ss=0; ss<7; ss++)
		{
			X=random(10);
			Y=random(10);

			if(X>3)
				sx=sx+5;
			else
				sx=sx-5;

			if(Y>3)
				sy=sy+5;
			else
				sy=sy-5;

			if(ss%2==0)
			{
				putimage(sx,sy,ships,XOR_PUT);
				delay(100);
				putimage(sx,sy,ships,XOR_PUT);
			}
			else
			{
				putimage(sx,sy,ships2,XOR_PUT);
				delay(100);
				putimage(sx,sy,ships2,XOR_PUT);
			}

		}
		if(sx>getmaxx() || sx<=0)
			sx=random(300);

		if(sy>getmaxy() || sy<=0)
			sy=random(300);
		putimage(x+100*cos(i),y+80*sin(i),ear,XOR_PUT);

		putimage(x+(100*cos(i))+(30*cos(j)),y+(80*sin(i))+(25*sin(j)),moo,XOR_PUT);
		putimage(x+60*cos(me),y+40*sin(me),mer,XOR_PUT);
		putimage(x+80*cos(ve),y+60*sin(ve),ven,XOR_PUT);
		putimage(x+125*cos(ma),y+110*sin(ma),mar,XOR_PUT);
		putimage(x+165*cos(ju),y+130*sin(ju),jup,XOR_PUT);
		putimage(x+220*cos(sa),y+170*sin(sa),sat,XOR_PUT);
		putimage(x+250*cos(ur),y+200*sin(ur),ura,XOR_PUT);
		putimage(x+280*cos(ne),y+230*sin(ne),nep,XOR_PUT);
		putimage(x+310*cos(pl),y+250*sin(pl),plu,XOR_PUT);


	} 


	getch();
	closegraph();
}

Recommended Answers

All 14 Replies

2.AQUARIUM

/*AQUARIUM*/
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{

	int gd=DETECT,gm,i=0,j=0,r=3,c=0,x;
	clrscr();
	initgraph(&gd,&gm,"D:\\tc\\bgi");
	for(i=0;i<500;i++)
	{
		cleardevice();
		setbkcolor(BLUE);
		c++;
		setcolor(GREEN);
	setfillstyle(1,2);
	fillellipse(0,410,45,50);
	fillellipse(0,439,90,50);
	fillellipse(40,439,45,50);
	fillellipse(30,439,45,50);
	fillellipse(70,429,45,50);
	fillellipse(120,469,45,50);
	fillellipse(150,410,45,50);
	fillellipse(180,439,45,50);
	fillellipse(200,420,45,50);
	fillellipse(240,469,45,50);

	fillellipse(270,410,45,50);
	fillellipse(320,469,45,50);
	fillellipse(350,429,45,50);
	fillellipse(400,469,45,50);
	setcolor(WHITE);
	setbkcolor(BLUE);

		ellipse(320,15,180,360,320,40);

		ellipse(320,15,180,360,320,30);

		arc(400-i,300,0,140,90);
		arc(400-i,200,218,360,90);    //body

		arc(450-i,250,90,150,80);  //main upper wing
		arc(450-i,250,210,260,80); //main lower wings

		arc(430-i,250,190,220,80);

		arc(460-i,250,115,150,80);
		arc(470-i,250,125,150,80);
		arc(480-i,250,135,150,80);
		arc(480-i,250,145,150,80); //upper wings

		arc(460-i,250,210,240,80);
		arc(470-i,250,210,230,80);
		arc(480-i,250,210,220,80); //lower wings

		line(490-i,300,490-i,200);
		line(490-i,250,480-i,250);

		circle(355-i,245,5);
		circle(355-i,245,2);
		circle(350-i,263,1);

		line(330-i,243,340-i,255);
		line(330-i,255,340-i,255);
		line(333-i,243,343-i,255);
		line(333-i,255,343-i,255);

		if((243-i)>48)
		{       delay(10);
			circle(330,243-i,r);
			circle(290,239-i,r);
		}

		delay(50);

	}

	//fish2

	for(i=0;i<350;i++)
	{
	cleardevice();
	setcolor(14);
	setbkcolor(BLUE);
	setcolor(GREEN);
	setfillstyle(1,2);
	fillellipse(0,410,45,50);
	fillellipse(0,439,90,50);
	fillellipse(40,439,45,50);
	fillellipse(30,439,45,50);
	fillellipse(70,429,45,50);
	fillellipse(120,469,45,50);
	fillellipse(150,410,45,50);
	fillellipse(180,439,45,50);
	fillellipse(200,420,45,50);
	fillellipse(240,469,45,50);

	fillellipse(270,410,45,50);
	fillellipse(320,469,45,50);
	fillellipse(350,429,45,50);
	fillellipse(400,469,45,50);
	setcolor(WHITE);
	ellipse(320,15,180,360,320,40);
	ellipse(320,15,180,360,320,30);


 //fish1..
 arc(400+i,300,45,167,85);
 arc(400+i,187,195,315,85);

 arc(390+i,245,155,205,80);

 arc(390+i,233,35,100,30);
 arc(390+i,255,255,325,30);

 arc(350+i,220,8,32,30);
 arc(350+i,220,180,230,30);
 arc(390+i,285,147,167,75);

 arc(375+i,280,353,40,10);
 line(450+i,247,459+i,247);
 line(450+i,247,459+i,240);
 line(375+i,204,383+i,204);
 line(310+i,244,337+i,244);
 arc(500+i,225,173,200,90);//separation
 if(i<=165)
 {
 circle(460,200-i,4);//bubbles for fish1..
 circle(490,210-i,4);
 circle(470,220-i,4);
 }
 circle(435+i,238,5);
 circle(438+i,239,2);
delay(50);
}
	getch();
	closegraph();
}

3.BALL IN BOWL

/*BALL IN BOWL-ca-217*/
#include<math.h>
#include<graphics.h>
#include<conio.h>

void main()
{
	int gd=DETECT,gm,rb;
	int x1,y1,arx[550],ary[550],a=0,b=0,mov;
	int x=300,y=200,r=150,r1=138,xc,yc,i,j,t1,t2;
	initgraph(&gd,&gm,"d:\\tc\\bgi");
	outtextxy(350,400,"Ball inside the Bowl");
	//drawing arc
	for(i=0;i<3000;i++)
	{
		x1=x+r*cos(i);
		y1=y+r*sin(i);
		if(y1>200)
		putpixel(x1,y1,GREEN);
	}
	for(i=0;i<500;i++)
	{
		x1=x+r1*cos(i);
		y1=y+r1*sin(i);
		if(y1>200)
		{
			arx[a++]=x1;
			ary[b++]=y1;
		}
	}

	for(i=0;i<a;i++)
	{
		for(j=0;j<a;j++)
		{
			if(arx[j]>arx[j+1])
			{
				t1=arx[j];
				arx[j]=arx[j+1];
				arx[j+1]=t1;
				t2=ary[j];
				ary[j]=ary[j+1];
				ary[j+1]=t2;
			}
		}
	}
	//movement of the ball starts here
	mov=0;
	while(mov<5)
	{
		if(i!=a)
		{
			for(i=0;i<a;i+=10)
			{
				setcolor(RED);
				setfillstyle(8,RED);
				fillellipse(arx[i],ary[i],10,10);
				delay(150);
				setcolor(BLACK);
				setfillstyle(SOLID_FILL,BLACK);
				fillellipse(arx[i],ary[i],10,10);
			}

		}
		else
		{
			for(i=a;i>0;i-=10)
			{
				setcolor(RED);
				setfillstyle(8,RED);
				fillellipse(arx[i],ary[i],10,10);
				delay(150);
				setcolor(BLACK);
				setfillstyle(SOLID_FILL,BLACK);
				fillellipse(arx[i],ary[i],10,10);
			}
		}
		mov++;
	}

	getch();
	closegraph();
}

Initially there was a error of Graphics Driver Not Found . . . :(
After that the graphics file was missing.
Some how able to solve all errors and finally lots of balls dancing on my screen . . .
Sees like I have done a wonderful animation. :)

If you don't have a question, then this should go under code snippets. Also, it would be best to stay away from non-portable deprecated libraries such as graphics.h and conio.h

Thanks for share the no of code which are related to graphics in C thanks a lot it help full for me

commented: fake signature -4

I'm interested in graphics programming. what library do I use for graphics programming in C if graphics.h is deprecated??

I'm interested in graphics programming. what library do I use for graphics programming in C if graphics.h is deprecated??

graphics.h is not a standard C header and is probably a part of a custom library. If you can track down that original library and provide more info, i can probably help you get it set up.
To run the program, you need graphics.h header file, graphics.lib library file and Graphics driver (BGI file) in the program folder. These files are part of Turbo C package.

under which platform u are working ??

I'm interested in graphics programming. what library do I use for graphics programming in C if graphics.h is deprecated??

On MS-Windows use a modern compiler such as VC++ 2010 and win32 api graphics functions. c++ gives you many more options.

under which platform u are working ??

I'm using Code Blocks (but I also have Turbo C). I'm completely a novice when it comes to graphics. thanks for helping..

On MS-Windows use a modern compiler such as VC++ 2010 and win32 api graphics functions. c++ gives you many more options.

I'll have to learn how to use win32 api..thanks again.

@Soubhik

Check THIS out. Hope that helps.

Thanks myk45..

I've heard of OpenGL but always thought of it as way out of the reach of a beginner.

all graphics are out of reach of beginners (< 6 months studies). You better have a firm foundation in c and/or c++ before starting down that road.

Not that I wanna start off right now..! I'm learning as I go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.