C graphics programs
- Draw shapes Click here...
- Bar chart Click here...
- Pie chart Click here...
- 3d bar chart Click here...
- Smiling face animation Click here...
- captcha Click here...
- Circles in circles Click here...
- Countdown Click here...
- Paint program in c Click here...
- Press me button game Click here...
- Web browser program Click here...
- Traffic Light Simulation Click here...
- Mouse pointer restricted in circle Click here...
Draw shapes using c graphics
This c graphics program draws basic shapes such as circle, line, rectangle, ellipse and display text on screen using c graphics. This can be a first graphics program for a beginner.
Output of program:
C programming code
#include<graphics.h> #include<conio.h> main() { int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50; initgraph(&gd, &gm, "C:\\TC\\BGI"); rectangle(left, top, right, bottom); circle(x, y, radius); bar(left + 300, top, right + 300, bottom); line(left - 10, top + 150, left + 410, top + 150); ellipse(x, y + 200, 0, 360, 100, 50); outtextxy(left + 100, top + 325, "My First C Graphics Program"); getch(); closegraph(); return 0; }
Output of program:
c program draw bar chart
This program draws bar chart using c graphics. Chart is drawn using bars filled with different styles and in different colors.
Output of program:
C programming code
#include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TC\\BGI"); setcolor(YELLOW); rectangle(0,30,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,0,"Bar Chart"); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420); line(90,70,100,60); line(110,70,100,60); line(590,410,600,420); line(590,430,600,420); outtextxy(95,35,"Y"); outtextxy(610,405,"X"); outtextxy(85,415,"O"); setfillstyle(LINE_FILL,BLUE); bar(150,100,200,419); setfillstyle(XHATCH_FILL,RED); bar(225,150,275,419); setfillstyle(WIDE_DOT_FILL,GREEN); bar(300,200,350,419); setfillstyle(INTERLEAVE_FILL,MAGENTA); bar(375,125,425,419); setfillstyle(HATCH_FILL,BROWN); bar(450,175,500,419); getch(); return 0; }
Output of program:
c program to draw pie chart
This c program draws a pie chart showing percentage of various components drawn with different filling styles and colors.
Output of program:
C programming code
/* Program to draw a pie chart */#include<graphics.h> #include<conio.h> main() { int gd = DETECT, gm, midx, midy; initgraph(&gd, &gm, "C:\\TC\\BGI"); setcolor(MAGENTA); rectangle(0,40,639,450); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); setcolor(WHITE); outtextxy(275,10,"Pie Chart"); midx = getmaxx()/2; midy = getmaxy()/2; setfillstyle(LINE_FILL,BLUE); pieslice(midx, midy, 0, 75, 100); outtextxy(midx+100, midy - 75, "20.83%"); setfillstyle(XHATCH_FILL,RED); pieslice(midx, midy, 75, 225, 100); outtextxy(midx-175, midy - 75, "41.67%"); setfillstyle(WIDE_DOT_FILL,GREEN); pieslice(midx, midy, 225, 360, 100); outtextxy(midx+75, midy + 75, "37.50%"); getch(); return 0; }
Output of program:
c smiling face animation
This animation using c draws a smiling face which appears at random position on screen. See output below the code, it will help you in understanding the code easily.
Output of program:
C programming code
#include<graphics.h> #include<conio.h> #include<stdlib.h> main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(YELLOW); circle(50,100,25); setfillstyle(SOLID_FILL,YELLOW); floodfill(50,100,YELLOW); setcolor(BLACK); setfillstyle(SOLID_FILL,BLACK); fillellipse(44,85,2,6); fillellipse(56,85,2,6); ellipse(50,100,205,335,20,9); ellipse(50,100,205,335,20,10); ellipse(50,100,205,335,20,11); area = imagesize(left, top, left + 50, top + 50); p = malloc(area); setcolor(WHITE); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(155,451,"Smiling Face Animation"); setcolor(BLUE); rectangle(0,0,639,449); while(!kbhit()) { temp1 = 1 + random ( 588 ); temp2 = 1 + random ( 380 ); getimage(left, top, left + 50, top + 50, p); putimage(left, top, p, XOR_PUT); putimage(temp1 , temp2, p, XOR_PUT); delay(100); left = temp1; top = temp2; } getch(); closegraph(); return 0; }
Output of program:
captcha program in c
This program generates captcha, a captcha is a random code generated using some algorithm. We will use random function in our code. These are used in typing tutors and in website to check whether a human is operating on a website.
Output of program:
C programming code
#include<stdlib.h> #include<dos.h> #include<graphics.h> main() { int i = 0, key, num, midx, gd = DETECT, gm; char a[10]; initgraph(&gd,&gm,"C:\\TC\\BGI"); midx = getmaxx()/2; settextstyle(SCRIPT_FONT,HORIZ_DIR,5); settextjustify(CENTER_TEXT,CENTER_TEXT); setcolor(GREEN); outtextxy(midx,20,"CAPTCHA"); settextstyle(SCRIPT_FONT,HORIZ_DIR,2); outtextxy(midx,125,"Press any key to change the generated random code \"captcha\""); outtextxy(midx,150,"Press escape key to exit..."); setcolor(WHITE); setviewport(100,200,600,400,1); setcolor(RED); randomize(); while(1) { while(i<6) { num = random(3); if ( num == 0 ) a[i] = 65 + random(26); /* 65 is the ASCII value of A */ else if ( num == 1) a[i] = 97 + random(26); /* 97 is the ASCII value of a */ else a[i] = 48 + random(10); /* 48 is the ASCII value of 0 */ i++; } a[i] = '\0'; outtextxy(210,100,a); key = getch(); if( key == 27 ) /* escape key*/ exit(0); clearviewport(); i = 0; } }
Output of program:
c program to draw circles in circles
This program draws circles in circles in two different colors.
Output of program:
C programming code
#include<graphics.h> #include<conio.h> #include<dos.h> main() { int gd = DETECT, gm, x, y, color, angle = 0; struct arccoordstype a, b; initgraph(&gd, &gm, "C:\\TC\\BGI"); delay(2000); while(angle<=360) { setcolor(BLACK); arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100); setcolor(RED); getarccoords(&a); circle(a.xstart,a.ystart,25); setcolor(BLACK); arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150); getarccoords(&a); setcolor(GREEN); circle(a.xstart,a.ystart,25); angle = angle+5; delay(50); } getch(); closegraph(); return 0; }
Output of program:
c program countdown
This c graphics program performs countdown for 30 seconds.
C program countdown code
#include<graphics.h> #include<dos.h> #include<conio.h> main() { int gd = DETECT, gm, i; char a[5]; initgraph( &gd, &gm, "C:\\TC\\BGI"); settextjustify( CENTER_TEXT, CENTER_TEXT ); settextstyle(DEFAULT_FONT,HORIZ_DIR,3); setcolor(RED); for(i = 30 ; i >=0 ; i--) { sprintf(a,"%d",i); outtextxy(getmaxx()/2, getmaxy()/2, a); delay(1000); if ( i == 0 ) break; cleardevice(); } getch(); closegraph(); return 0; }
c program countdown
This c graphics program performs countdown for 30 seconds.
C program countdown code
#include<graphics.h> #include<dos.h> #include<conio.h> main() { int gd = DETECT, gm, i; char a[5]; initgraph( &gd, &gm, "C:\\TC\\BGI"); settextjustify( CENTER_TEXT, CENTER_TEXT ); settextstyle(DEFAULT_FONT,HORIZ_DIR,3); setcolor(RED); for(i = 30 ; i >=0 ; i--) { sprintf(a,"%d",i); outtextxy(getmaxx()/2, getmaxy()/2, a); delay(1000); if ( i == 0 ) break; cleardevice(); } getch(); closegraph(); return 0; }
paint program in c
Paint program in c:- This program can draw different shapes using mouse such as line, circle, pixel and many other shapes. You can also change the color, clear the screen. Code of paint program in c is given below:-
/* To understand the code see output below the code, it will help you in understanding the code. */
C programming code
#include<graphics.h> #include<dos.h> #include<math.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> union REGS i, o; int leftcolor[15]; int get_key() { union REGS i,o; i.h.ah = 0; int86(22,&i,&o); return ( o.h.ah ); } void draw_color_panel() { int left, top, c, color; left = 100; top = 436; color = getcolor(); setcolor(GREEN); rectangle(4,431,635,457); setcolor(RED); settextstyle(TRIPLEX_FONT,0,2); outtextxy(10,431,"Colors : "); for( c = 1 ; c <= 15 ; c++ ) { setfillstyle(SOLID_FILL, c); bar(left, top, left+16, top+16); leftcolor[c-1] = left; left += 26; } setcolor(color); } void draw_shape_panel() { int left, top, c, color; left = 529; top = 45; color = getcolor(); setcolor(GREEN); rectangle(525,40,633,255); for( c = 1 ; c <= 7 ; c++ ) { rectangle(left, top, left+100, top+25); top += 30; } setcolor(RED); outtextxy(530,45,"Bar"); outtextxy(530,75,"Line"); outtextxy(530,105,"Pixel"); outtextxy(530,135,"Ellipse"); outtextxy(530,165,"Freehand"); outtextxy(530,195,"Rectangle"); outtextxy(530,225,"Clear"); setcolor(color); } void change_color(int x, int y) { int c; for( c = 0 ; c <= 13 ; c++ ) { if( x > leftcolor[c] && x < leftcolor[c+1] && y > 437 && y < 453 ) setcolor(c+1); if( x > leftcolor[14] && x < 505 && y > 437 && y < 453 ) setcolor(WHITE); } } char change_shape(int x, int y) { if ( x > 529 && x < 625 && y > 45 && y < 70 ) return 'b'; else if ( x > 529 && x < 625 && y > 75 && y < 100 ) return 'l'; else if ( x > 529 && x < 625 && y > 105 && y < 130 ) return 'p'; else if ( x > 529 && x < 625 && y > 135 && y < 160 ) return 'e'; else if ( x > 529 && x < 625 && y > 165 && y < 190 ) return 'f'; else if ( x > 529 && x < 625 && y > 195 && y < 220 ) return 'r'; else if ( x > 529 && x < 625 && y > 225 && y < 250 ) return 'c'; } void showmouseptr() { i.x.ax = 1; int86(0x33,&i,&o); } void hidemouseptr() { i.x.ax = 2; int86(0x33,&i,&o); } void restrictmouseptr( int x1, int y1, int x2, int y2) { i.x.ax = 7; i.x.cx = x1; i.x.dx = x2; int86(0x33,&i,&o); i.x.ax = 8; i.x.cx = y1; i.x.dx = y2; int86(0x33,&i,&o); } void getmousepos(int *button,int *x,int *y) { i.x.ax = 3; int86(0x33,&i,&o); *button = o.x.bx; *x = o.x.cx; *y = o.x.dx; } main() { int gd = DETECT,gm; int maxx,maxy,x,y,button,prevx,prevy,temp1,temp2,key,color; char ch = 'f' ; // default free-hand drawing initgraph(&gd,&gm,"C:\\TC\\BGI"); maxx = getmaxx(); maxy = getmaxy(); setcolor(BLUE); rectangle(0,0,maxx,maxy); setcolor(WHITE); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(maxx/2-180,maxy-28,"www.programmingsimplified.com"); draw_color_panel(); draw_shape_panel(); setviewport(1,1,maxx-1,maxy-1,1); restrictmouseptr(1,1,maxx-1,maxy-1); showmouseptr(); rectangle(2,2,518,427); setviewport(1,1,519,428,1); while(1) { if(kbhit()) { key = get_key(); if( key == 1 ) { closegraph(); exit(0); } } getmousepos(&button,&x,&y); if( button == 1 ) { if( x > 4 && x < 635 && y > 431 && y < 457 ) change_color( x, y ); else if ( x > 529 && x < 625 && y > 40 && y < 250 ) ch = change_shape( x, y ); temp1 = x ; temp2 = y ; if ( ch == 'f' ) { hidemouseptr(); while( button == 1 ) { line(temp1,temp2,x,y); temp1 = x; temp2 = y; getmousepos(&button,&x,&y); } showmouseptr(); } while( button == 1) getmousepos(&button,&x,&y); /* to avoid interference of mouse while drawing */ hidemouseptr(); if( ch == 'p') putpixel(x,y,getcolor()); else if ( ch == 'b' ) { setfillstyle(SOLID_FILL,getcolor()); bar(temp1,temp2,x,y); } else if ( ch == 'l') line(temp1,temp2,x,y); else if ( ch == 'e') ellipse(temp1,temp2,0,360,abs(x-temp1),abs(y-temp2)); else if ( ch == 'r' ) rectangle(temp1,temp2,x,y); else if ( ch == 'c' ) { ch = 'f'; // setting to freehand drawing clearviewport(); color = getcolor(); setcolor(WHITE); rectangle(2,2,518,427); setcolor(color); } showmouseptr(); } } }
Download executable file of c paint program.
Press me button game in c, download press-me-button game
Press me game in C:- In this game when you try to bring mouse near a button it moves away from the mouse, so you keep on trying pressing the button. In this game we have the coordinates of current position of mouse pointer at every moment of time, whenever we find mouse pointer very close to button we move the button suitably.
C programming code
#include<stdio.h> #include<conio.h> #include<dos.h> #include<graphics.h> #include<stdlib.h> union REGS i, o; int left = 265, top = 250; void initialize_graphics_mode() { int gd = DETECT, gm, error; initgraph(&gd,&gm,"C:\\TC\\BGI"); error = graphresult(); if( error != grOk ) { perror("Error "); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } } void showmouseptr() { i.x.ax = 1; int86(0x33,&i,&o); } void hidemouseptr() { i.x.ax = 2; int86(0x33,&i,&o); } void getmousepos(int *x,int *y) { i.x.ax = 3; int86(0x33,&i,&o); *x = o.x.cx; *y = o.x.dx; } void draw_bar() { hidemouseptr(); setfillstyle(SOLID_FILL,CYAN); bar(190,180,450,350); showmouseptr(); } void draw_button(int x, int y) { hidemouseptr(); setfillstyle(SOLID_FILL,MAGENTA); bar(x,y,x+100,y+30); moveto(x+5,y); setcolor(YELLOW); outtext("Press me"); showmouseptr(); } void draw() { settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(155,451,"www.programmingsimplified.com"); setcolor(BLUE); rectangle(0,0,639,450); setcolor(RED); outtextxy(160,25,"Try to press the \"Press me\" button"); outtextxy(210,50,"Press escape key to exit"); setfillstyle(XHATCH_FILL,GREEN); setcolor(BLUE); bar(1,1,75,449); bar(565,1,638,449); showmouseptr(); draw_bar(); draw_button(left,top); } void initialize() { initialize_graphics_mode(); if( !initmouse() ) { closegraph(); printf("Unable to initialize the mouse"); printf("Press any key to exit...\n"); getch(); exit(EXIT_SUCCESS); } draw(); } int initmouse() { i.x.ax = 0; int86(0x33,&i,&o); return ( o.x.ax ); } void get_input() { int x, y; while(1) { getmousepos(&x,&y); /* mouse pointer in left of button */ if( x >= (left-3) && y >= (top-3) && y <= (top+30+3) && x < left ) { draw_bar(); left = left + 4; if( left > 350 ) left = 190; draw_button(left,top); } /* mouse pointer in right of button */ else if( x <= (left+100+3) && y >= (top-3) && y <= (top+30+3) && x>(left+100)) { draw_bar(); left = left - 4; if( left < 190 ) left = 350; draw_button(left,top); } /* mouse pointer above button */ else if( x >= (left-3) && y >= (top-3) && y < (top) && x<=(left+100+3)) { draw_bar(); top = top + 4; if( top > 320 ) top = 180; draw_button(left,top); } /* mouse pointer below button */ else if( x >= (left-3) && y > (top+30) && y <= (top+30+3) && x<=(left+100+3)) { draw_bar(); top = top - 4; if( top < 180 ) top = 320; draw_button(left,top); } if(kbhit()) { if( getkey() == 1 ) exit(EXIT_SUCCESS); } } } int getkey() { i.h.ah = 0; int86(22,&i,&o); return( o.h.ah ); } main() { initialize(); get_input(); return 0; }
Download executable file of press me button game.
web browser project in c, c program to open a website/url
This program opens a website entered by the user. User can open any website .It will launch Mozilla Firefox web browser to open a website so it should be installed on your computer, if you are using an another web browser then you can change the path in the program.
C programming code
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<graphics.h> #include<dos.h> #include<string.h> void initialize_graphics_mode(); int get_key(); void draw(); union REGS i, o; main() { int key, i = 0, xpos, ypos, button; char arr[200], temp[5], *ptr; char a[] = "C:\\Progra~1\\Mozill~1\\firefox "; strcpy(arr,a); i = strlen(a); initialize_graphics_mode(); draw(); while(1) { if(kbhit()) key = get_key(); if((key>=97&&key<=122)||(key>=65&&key<=90)||key==46||key==47||key==63) { arr[i] = key; sprintf(temp,"%c",arr[i]); outtext(temp); if(getx()>470) { clearviewport(); moveto(5,2); } i++; } else if ( key == 13 ) { arr[i] = '\0'; system(arr); break; } else if ( key == 27 ) { closegraph(); exit(EXIT_SUCCESS); } if(button==1&&xpos>=150&&xpos<=480&&ypos>=300&&ypos<=330) { system("C:\\Progra~1\\Mozill~1\\firefox programmingsimplified.com"); break; } key = -1; } closegraph(); return 0; } void initialize_graphics_mode() { int gd = DETECT, gm, errorcode; initgraph(&gd,&gm,"C:\\TC\\BGI"); errorcode = graphresult(); if( errorcode != grOk ) { printf("Graphics error : %s\n",grapherrormsg(errorcode)); printf("Press any key to exit...\n"); getch(); exit(EXIT_FAILURE); } } int get_key() { i.h.ah = 0; int86(22,&i,&o); return( o.h.al ); } void draw() { settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(275,11,"Web Browser"); outtextxy(155,451,"www.programmingsimplified.com"); outtextxy(5,105,"Enter URL : "); rectangle(120,100,600,130); setviewport(121,101,599,129,1); moveto(5,1); }
traffic light program in c, traffic light simulation
Traffic light Simulation: Traffic light program in c presents what happens in our daily life at traffic light signals. Firstly user will press a key to start the traffic light simulation.
Download Traffic Light Simulation.
C programming code
#include<graphics.h> #include<conio.h> #include<dos.h> #include<stdlib.h> main() { int gd = DETECT, gm, midx, midy; initgraph(&gd, &gm, "C:\\TC\\BGI"); midx = getmaxx()/2; midy = getmaxy()/2; setcolor(RED); settextstyle(SCRIPT_FONT, HORIZ_DIR, 3); settextjustify(CENTER_TEXT, CENTER_TEXT); outtextxy(midx, midy-10, "Traffic Light Simulation"); outtextxy(midx, midy+10, "Press any key to start"); getch(); cleardevice(); setcolor(WHITE); settextstyle(DEFAULT_FONT, HORIZ_DIR, 1); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy-50, 22); setfillstyle(SOLID_FILL,RED); floodfill(midx, midy-50,WHITE); setcolor(BLUE); outtextxy(midx,midy-50,"STOP"); delay(2000); graphdefaults(); cleardevice(); setcolor(WHITE); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy, 20); setfillstyle(SOLID_FILL,YELLOW); floodfill(midx, midy,WHITE); setcolor(BLUE); outtextxy(midx-18,midy-3,"READY"); delay(2000); cleardevice(); setcolor(WHITE); rectangle(midx-30,midy-80,midx+30,midy+80); circle(midx, midy+50, 22); setfillstyle(SOLID_FILL,GREEN); floodfill(midx, midy+50,WHITE); setcolor(BLUE); outtextxy(midx-7,midy+48,"GO"); setcolor(RED); settextstyle(SCRIPT_FONT, HORIZ_DIR, 4); outtextxy(midx-150, midy+100, "Press any key to exit..."); getch(); closegraph(); return 0; }
Download Traffic Light Simulation.
c program to restrict mouse pointer in a circle
This program restricts mouse pointer in a circle i.e you can't move mouse out of a circle. When you try to bring mouse pointer outside the circle, mouse pointer is moved to it's previous location which is inside the circle. Code to restrict mouse in circle is given below :-
C programming code
#include<graphics.h> #include<conio.h> #include<dos.h> #include<stdlib.h> #include<math.h> union REGS i, o; int initmouse() { i.x.ax = 0; int86(0X33, &i, &o); return ( o.x.ax ); } void showmouseptr() { i.x.ax = 1; int86(0X33, &i, &o); } void hidemopuseptr() { i.x.ax = 2; int86(0X33,&i,&o); } void getmousepos(int *x, int *y) { i.x.ax = 3; int86(0X33, &i, &o); *x = o.x.cx; *y = o.x.dx; } void movemouseptr(int x, int y) { i.x.ax = 4; i.x.cx = x; i.x.dx = y; int86(0X33, &i, &o); } main() { int gd = DETECT, gm, midx, midy, radius, x, y, tempx, tempy; radius = 100; initgraph(&gd, &gm, "C:\\TC\\BGI"); if(!initmouse()) { closegraph(); exit(1); } midx = getmaxx()/2; midy = getmaxy()/2; showmouseptr(); movemouseptr(midx, midy); circle(midx, midy, radius); x = tempx = midx; y = tempy = midy; while(!kbhit()) { getmousepos(&x, &y); if((pow(x-midx,2)+pow(y-midy,2)-pow(radius,2))>0) { movemouseptr(tempx, tempy); x = tempx; y = tempy; } tempx = x; tempy = y; } closegraph(); return 0; }
Hangman game source code for you.
============
----HANGMAN----
============
SOURCE CODE GIVEN BY HARISH
#include<conio.h> #include<string.h> main() { char word[30],getit[30],*found; int score,loop,len,num,lup,om,luptimes,times,fitimes,ondtimes,antimes; luptimes=0; fitimes=0; ondtimes=0; clrscr(); printf("\n ENTER THE WORD: "); gets(word); score=0; num=0; om=0; len=strlen(word); clrscr(); char gname[70]; printf("\n ENTER YOUR NAME: "); scanf("%s",gname); char h; printf("\n LET's START: "); h='_'; printf("\n LOOK HERE: "); for(loop=0;loop!=len;loop++) { printf("\n %c ",h); } for(loop=0;loop!=len;loop++) { om=1; num=num+1; fitimes=0; ondtimes=0; printf("\n\n %d LETTER",num); scanf("%s",&getit[num]); found=strchr(word,getit[num]); if(found) { for(luptimes=0;antimes=1;luptimes!=len;luptimes++,antimes++) { if(getit[num]==word[luptimes]) { fitimes++; } else { fitimes=fitimes; } } for(luptimes=num;luptimes!=0;luptimes--) { if(getit[num]==getit[luptimes]) { ondtimes++; } else { ondtimes=ondtimes; } } if(fitimes>=ondtimes) { score=score+1; printf("\n OK\n"); for(lup=0;getit[num]!=word[lup];lup++) { om++; } printf("\n POSITION: %d\n",om); printf("\n ===================\n"); } else { score=score; printf("\n NOT AGAIN!\n"); } } else { score=score; printf("\n NO\n"); } } printf("\n THE WORD IS '%s'\n"word); printf("\n YOUR SCORE IS %d OUT OF %d",score,len); getch(); return 0; }
SNAKE GAME
SOURCE CODE GIVEN BY HARISH
Snake Game Source Code in C #include<dos.h> #include<conio.h> #include<graphics.h> #include<stdio.h> #include<stdlib.h> #define UP 72 #define LEFT 75 #define RIGHT 77 #define DOWN 80 #define ESC 1 #ifdef __cplusplus #define __CPPARGS ... #else #define __CPPARGS #endif struct SnakeParts{ int x; int y; }box[50],target; void snake(int ,int ); void initialize(int,int); void interrupt (*prev)(__CPPARGS); void interrupt our(__CPPARGS); void snakeb(); int check(); void Rectangle(int ,int,int,int); int stop=0,blocks=4,direction=2,size=10,hit=1,snake_color=14,bk=7,target_color =4; int stage=1,speed=100,point=0; void main(){ int gd=DETECT,gm,x=100,y=100,i=0,j=0; char msg[50]; initgraph(&gd,&gm,""); /*animation*/ prev=getvect(9); /* setcolor(bk); rectangle(0,0,639,479); setfillstyle(SOLID_FILL,bk); floodfill(250,250,bk);*/ initialize(x,y); //getch(); while (1){ /*setcolor(bk); sprintf(msg,"POINT = %3d",point); settextstyle(0,HORIZ_DIR,1); outtextxy(550,100,msg); */ switch(direction){ case 0: j-=size;break; case 1: j+=size;break; case 2: i+=size;break; case 3: i-=size;break; } setcolor(snake_color); snake(x+i,y+j);//,x+15+i,y+15+j); delay(speed); setcolor(bk); snakeb(/*x+i,y+j*/);//,x+15+i,y+15+j); setvect(9,our); if(stop==1){break;} if(check()){break;} if(hit==1){ hit=0; target.x=size+size*random(25); target.y=size+size*random(20); setcolor(target_color); setfillstyle(SOLID_FILL,target_color); circle(target.x+size/2,target.y+size/2,size/2-1); floodfill(target.x+size/2,target.y+size/2,target_color); } setfillstyle(SOLID_FILL,target_color); setcolor(2); circle(target.x+size/2,target.y+size/2,size/2-1); floodfill(target.x+size/2,target.y+size/2,2); /*setcolor(11); sprintf(msg,"POINT = %3d",point); settextstyle(0,HORIZ_DIR,1); outtextxy(550,100,msg);*/ gotoxy(65,4); printf("POINT = %3d",point); //delay(3000); //cleardevice(); } closegraph(); } void interrupt our(__CPPARGS){ //if(inportb(0x60)==UP){ //i=0;j switch(inportb(0x60)){ case UP: if(direction==1)break;direction=0;break; case DOWN: if(direction==0)break;direction=1;break; case RIGHT: if(direction==3)break;direction=2;break; case LEFT: if(direction==2)break;direction=3;break; case 15:blocks++;break; case ESC: stop=1;break; } (*prev)(); } void snake(int x1,int y1){//,int x2,int y2){ int k=0; //if(getcolor()==15){ for(k=blocks-1;k>0;k--){ box[k].x=box[k-1].x; box[k].y=box[k-1].y; } box[0].x=x1; box[0].y=y1; //} //setfillstyle(SOLID_FILL,snake_color); for(k=0;k<blocks;k++){ Rectangle(box[k].x,box[k].y,box[k].x+size,box[k].y+size); //floodfill(box[k].x+size/2,box[k].y+size/2,snake_color); } } void snakeb(){ //setfillstyle(SOLID_FILL,bk); for(int k=0;k<blocks;k++){ Rectangle(box[k].x,box[k].y,box[k].x+size,box[k].y+size); //floodfill(box[k].x+size/2,box[k].y+size/2,bk); } } void initialize(int x,int y){ setcolor(bk); rectangle(0,0,639,479); setfillstyle(SOLID_FILL,bk); floodfill(250,250,bk); box[0].x=x; box[0].y=y; for(int i=1;i<blocks;i++){ box[i].x=x-size*i; box[i].y=y; } /*for(int k=0;k<blocks;k++) rectangle(box[k].x,box[k].y,box[k].x+15,box[k].y+15);*/ } int check(){ int i; char msg[50]; if(!(box[0].x>=0 && box[0].x<=639 && box[0].y>=0 && box[0].y<=479)){ for( i=100;i<700;i+=100){ sound(i); delay(100); nosound(); } sprintf(msg,"GAME OVER"); setcolor(4); settextstyle(1,HORIZ_DIR,6); outtextxy(150,200,msg); delay(3000); cleardevice(); return 1; } for( i=1;i<blocks;i++){ if(box[0].x==box[i].x && box[0].y==box[i].y){ sprintf(msg,"GAME OVER"); setcolor(4); settextstyle(1,HORIZ_DIR,6); outtextxy(150,200,msg); delay(3000); cleardevice(); return 1; } } if(box[0].x==target.x && box[0].y==target.y){ //for( i=50;i<400;i+=50){ sound(700); delay(100); nosound(); //} blocks++; point++; hit=1; setcolor(bk); //circle(target.x+size/2,target.y+size/2,size/2-1); setfillstyle(SOLID_FILL,bk); setcolor(bk); circle(target.x+size/2,target.y+size/2,size/2-1); floodfill(target.x+size/2,target.y+size/2,bk); } if(blocks==40){ stage++; blocks=4; sprintf(msg,"STAGE %d CLEARED",stage-1); setcolor(4); settextstyle(1,HORIZ_DIR,6); outtextxy(150,200,msg); delay(2000); cleardevice(); initialize(100,100); speed=100/stage; } return 0; } void Rectangle(int x1,int y1,int x2,int y2){ for(int i=0;i<size/2;i++) rectangle(x1+i,y1+i,x2-i,y2-i); }
SNAKE GAME - 2
SOURCE CODE GIVEN BY HARISH
# include <stdio.h>
# include <graphics.h>
# include <stdlib.h>
# include <dos.h>
# include <conio.h>
int gdriver=DETECT,gmode,x1=0,y1=0,x2,y2,k=0,c,a,i=0;
int bufx[150],bufy[150],dist=7,delayspeed=100,d; int rndx,rndy,tail=2,size,dash=0; int life=5;
int choice; void randomnumber(void);
void move(int a);
void statusbar(void);
void main()
{
// registerfarbgidriver(EGAVGA_driver_far);
initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); size=tail; cleardevice();
printf("Devlopped By IDEAL BERG");
printf("Enter the speed:: \n<1> for slow <2> for slower <3> for slowest\n<4> for normal\n<5> for fast <6> for faster <7> for fastest");
scanf("%d",&choice);
switch(choice)
{
case 1: delayspeed=250;break;
case 2: delayspeed=200;break;
case 3: delayspeed=160;break;
case 4: delayspeed=90;break;
case 5: delayspeed=60;break;
case 6: delayspeed=40;break;
case 7: delayspeed=20;break;
};
cleardevice();
randomnumber();
bar3d(rndx,rndy,rndx+dist,rndy+dist,0,0);
statusbar();
while((c=getch())!='\033')
{
if (c=='\115')
a=1;
if (c=='\113')
a=2;
if (c=='\120')
a=3;
if (c=='\110')
a=4; dash=0;
move(a);
} }
void move(int a)
{
void checkcollision(int x1,int y1);
void checkcapture(int x1,int y1);
while(!kbhit())
{
if (!dash)
{
k=k++%size;
i++;
setfillstyle(1,7);
bar3d(x1,y1,x1+dist,y1+dist,0,0);
if(i>=size) // Style start { setfillstyle(0,0);
setcolor(0);
bar3d(bufx[k],bufy[k],bufx[k]+dist,bufy[k]+dist,0,0);
bufx[k]=x1;
bufy[k]=y1; setcolor(15); checkcapture(x1,y1);
}
else
{
bufx[1]=x1; bufy[1]=y1;
}
if(a==1) x1=x1+dist;
if(a==2) x1=x1-dist;
if(a==3) y1=y1+dist;
if(a==4) y1=y1-dist;
checkcollision(x1,y1);
setfillstyle(1,14);
bar3d(x1,y1,x1+dist,y1+dist,0,0);
sound(300); delay(delayspeed);
nosound(); delay(delayspeed);
}
}
}
void checkcollision(int x1,int y1)
{
int i;
for(i=1;i<=size;i++)
if((bufx[i]==x1)&&(bufy[i]==y1))
dash=1;
if((x1<0)||(x1>=400)||(y1<0)||(y1>400)) dash=1;
if(dash)
{
if(--life<0) exit(0);
sound(700);
delay(300);
nosound();
if(size>25) life++;
size=tail;
cleardevice();
} statusbar();
}
void checkcapture(int x1,int y1)
{
int i; if((x1==rndx)&&(y1==rndy))
{
size++; sound(500);
delay(100);
randomnumber();
}
setfillstyle(11,14);
bar3d(rndx,rndy,rndx+dist,rndy+dist,0,0);
}
void statusbar()
{
char s[10];
s[1]=life;
setcolor(15);
rectangle(0,0,400,400);
gotoxy(55,2);
printf("Life: %d size: %d",life,size);
gotoxy(55,5);
printf("Down: ");
putchar(31);
printf("\tUP");
putchar(30);
gotoxy(55,6);
printf("RIGHT :");
putchar(16);
printf("\tLEFT :");
putchar(17);
}
void randomnumber()
{
int i,j; randomize(); i=random(10000)%40;
i=i*dist;
j=random(10000)%40;
j=j*dist;
rndx=i; rndy=j;
}
TOWERS OF HANOI
SOURCE CODE GIVEN BY HARISH
#ident "$Id: game.c,v 1.5 2006/06/28 19:31:03 pwh Rel $"
/*
* Interactive Tower of Hanoi puzzle logic.
*/
#include <stdio.h>
#include <errno.h>
#include "hanoi.h"
#define CONGRATULATIONS "CONGRATULATIONS!!!"
#define CONFIRM "Exit game (Y/N)?"
#define TAUNT "See, wasn't that easy?"
/* Move stack. */
#define STACK_SIZE 32767
typedef struct {
int src;
int dest;
} MOVE;
typedef struct {
int top;
int contents;
MOVE moves [STACK_SIZE];
} MOVE_STACK;
static void resetMoveStack ( MOVE_STACK *stack )
{
stack->top = stack->contents = 0;
}
static void pushMove ( MOVE_STACK *stack, int src, int dest )
{
stack->moves [stack->top].src = src;
stack->moves [stack->top].dest = dest;
if ( ++stack->top == STACK_SIZE ) stack->top = 0;
if ( stack->contents < STACK_SIZE ) ++stack->contents;
}
static const MOVE *popMove ( MOVE_STACK *stack )
{
const MOVE *move = NULL;
if ( stack->contents ) {
--stack->contents;
if ( --stack->top < 0 ) stack->top += STACK_SIZE;
move = stack->moves + stack->top;
}
return ( move );
}
static int cursorVisible ( PUZZLE *puzzle, int peg )
{
return ( ! puzzle->xDisk || puzzle->xDisk->y != puzzle->y0 + 2
|| puzzle->xp [peg] < puzzle->xDisk->x - puzzle->xDisk->size - 1
|| puzzle->xp [peg] > puzzle->xDisk->x + puzzle->xDisk->size + 1 );
}
int playGame ( PUZZLE *puzzle )
{
int status;
int yt = puzzle->y0 + 2;
int c = 'r';
while ( c == 'r' ) {
int peg = LEFT_PEG;
int src = -1;
int dest = -1;
int drop = -1;
int undo = -1;
int solveIt = 0;
int moveDone = 1;
int done = 0;
MOVE_STACK moveHistory;
const MOVE *lastMove;
status = 1;
resetMoveStack ( &moveHistory );
move ( yt, puzzle->xp [LEFT_PEG] );
addch ( ACS_DIAMOND );
move ( 0, 0 );
refresh ();
while ( ! done ) {
switch ( c = getch () ) {
case 'v':
case 'j':
case KEY_DOWN: /* Drop the disk. */
if ( src != -1 && drop == -1 ) {
drop = peg;
moveDone = 0;
if ( src != peg ) pushMove ( &moveHistory,
src, peg );
} else beep ();
break;
case '^':
case 'k':
case KEY_UP: /* Pickup a disk. */
if ( src == -1 ) {
src = peg;
moveDone = 0;
} else beep ();
break;
case '<':
case 'h':
case KEY_LEFT: /* Move to the left. */
if ( peg > LEFT_PEG ) {
if ( cursorVisible ( puzzle, peg ) ) {
move ( yt, puzzle->xp [peg] );
addch ( ' ' );
}
--peg;
if ( src != -1 ) {
dest = peg;
moveDone = 0;
}
} else beep ();
break;
case '>':
case 'l':
case KEY_RIGHT: /* Move to the right. */
if ( peg < RIGHT_PEG ) {
if ( cursorVisible ( puzzle, peg ) ) {
move ( yt, puzzle->xp [peg] );
addch ( ' ' );
}
++peg;
if ( src != -1 ) {
dest = peg;
moveDone = 0;
}
} else beep ();
break;
case 's': /* Solve the puzzle. */
src = dest = drop = undo = -1;
solveIt = 1;
moveDone = 0;
break;
case 'u': /* Undo the last move. */
if ( undo != -1 ) {
beep ();
break;
} else if ( src == -1 ) {
if ( lastMove = popMove ( &moveHistory ) ) {
int dest;
dest = lastMove->dest;
undo = lastMove->src;
if ( undoMove ( puzzle, undo, dest ) ) {
src = dest;
drop = undo;
moveDone = 0;
} else {
pushMove ( &moveHistory,
undo, dest );
undo = -1;
beep ();
}
} else beep();
break;
}
case ESC: /* Interrupt the current move. */
if ( src != -1 && undo == -1 ) {
if ( drop != -1 ) popMove ( &moveHistory );
undo = drop = src;
moveDone = 0;
break;
}
case 'q': /* Quit the game. */
move ( yt - 1, puzzle->xc
- ( sizeof ( CONFIRM ) >> 1 ) );
{
char *p = CONFIRM;
while ( *p ) addch ( *p++ );
}
refresh ();
while ( ( done = getch () ) == ERR );
if ( done == 'y' || done == 'Y' ) {
puzzle->moves = 0;
if ( c == 'q' ) status = 0;
} else {
done = sizeof ( CONFIRM );
move ( yt - 1, puzzle->xc
- ( sizeof ( CONFIRM ) >> 1 ) );
do { addch ( ' ' ); } while ( --done > 0 );
break;
}
case 'r': /* Restart the game. */
solveIt = 0;
moveDone = 1;
done = 1;
break;
case 'S': /* Slow. */
setSpeed ( puzzle, SLOW_SPEED );
break;
case 'N': /* Normal speed. */
setSpeed ( puzzle, NORMAL_SPEED );
break;
case 'F': /* Fast. */
setSpeed ( puzzle, HIGH_SPEED );
break;
case 'X': /* Extremely fast. */
setSpeed ( puzzle, XTREME_SPEED );
break;
case 'W': /* Warp speed. */
setSpeed ( puzzle, WARP_SPEED );
break;
case ERR: /* No input. */
break;
default: /* Invalid input. */
beep ();
break;
}
if ( ! done && ! solveIt && cursorVisible ( puzzle, peg ) ) {
move ( yt, puzzle->xp [peg] );
addch ( ACS_DIAMOND );
}
if ( ! moveDone ) {
if ( solveIt ) {
if ( cursorVisible ( puzzle, peg ) ) {
move ( yt, puzzle->xp [peg] );
addch ( ' ' );
}
done = 1;
if ( ( status
= solvePuzzle ( puzzle, WARP_SPEED ) )
&& isSolved ( puzzle ) ) {
char *p = TAUNT;
move ( yt, puzzle->xc
- ( sizeof ( TAUNT ) >> 1 ) );
while ( *p ) addch ( *p++ );
c = ERR;
moveDone = 1;
} else if ( errno == EINTR ) {
done = 0;
status = 1;
}
} else if ( drop != -1 ) {
if ( moveDone = dropDisk ( puzzle, drop ) ) {
if ( isSolved ( puzzle ) ) {
char *p = CONGRATULATIONS;
move ( yt, puzzle->xp [peg] );
addch ( ' ' );
move ( yt, puzzle->xc
- ( sizeof ( CONGRATULATIONS )
>> 1 ) );
while ( *p ) addch ( *p++ );
done = 1;
}
src = -1;
dest = -1;
drop = -1;
undo = -1;
} else if ( ! errno ) {
beep ();
popMove ( &moveHistory );
moveDone = slideDisk ( puzzle, drop );
drop = -1;
}
} else if ( dest != -1 ) moveDone
= slideDisk ( puzzle, dest );
else if ( src != -1 ) {
if ( ! ( moveDone = pickupDisk ( puzzle, src ) )
&& ! errno ) {
beep ();
src = -1;
moveDone = 1;
}
}
if ( ! done && ! solveIt
&& cursorVisible ( puzzle, peg ) ) {
move ( yt, puzzle->xp [peg] );
addch ( ACS_DIAMOND );
}
}
move ( 0, 0 );
refresh ();
}
if ( c != 'q' && c != 'r' && c != ESC )
while ( ( c = getch() ) == ERR );
if ( c == 'r' ) {
if ( ! resetPuzzle ( puzzle ) ) c = EOF;
} else ungetch ( c );
}
return ( status );
}
No comments:
Post a Comment