How to Make Colorful Snake in C++ using Turbo c++ Graphics
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <dos.h>
int main() {
/* request auto detection */
int gdriver = DETECT, gmode, err;
int i = 0, j = 0, fq, amp, flag = 1;
double x, y;
/* initialize graphic mode */
initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
err = graphresult();
if (err != grOk) {
/* error occurred */
printf("Graphics Error: %s\n",
grapherrormsg(err));
return 0;
}
/* drawing snake with different colors */
for (i = 1; i <= 15; i++) {
x = 0, flag = 1, j = 0;
fq = 2;
amp = 50;
setcolor(i);
while (x + 5 < getmaxx()) {
/* tail/head size is less than body size */
if (j < 12 && flag) {
/* logic to draw tail */
j++;
} else if (((int)x + 12) > getmaxx()) {
/* logic to draw head part */
flag = 0;
j--;
if (j == 6) {
setlinestyle(SOLID_LINE, 1, 4);
putpixel(x - 1, y, BLACK);
}
}
/* draw the snake structure part by part */
y = amp * sin((3.14 * fq * x) / 180);
y = y + getmaxy() / 2;
putpixel(x, y, 15);
line(x, y, x, y - j);
line(x, y, x, y + j);
/* sleep for 5 micro seconds */
delay(5);
/* increment x */
x = x + 1;
}
/* drawing eyes and mouth of snake */
setfillstyle(SOLID_FILL, i);
circle(x - 1, y, 3);
floodfill(x - 1, y, i);
setcolor(BLACK);
circle(x - 3, y, 2);
putpixel(x - 3, y, BLACK);
delay(100);
}
getch();
/* deallocate memory allocated for graphic screen */
closegraph();
return 0;
}
0 comments:
Post a Comment