VB icon

graphics demo

Email
Submitted on: 1/1/2015 7:51:00 PM
By: Doug Jones (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: C++ (general), Borland C++
Views: 722
 
     It draws lines on the screen. Looks like a screensaver. Shows some basic C++ coding techniques.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: graphics demo
// Description:It draws lines on the screen. Looks like a screensaver. Shows some basic C++ coding techniques.
// By: Doug Jones (from psc cd)
//**************************************

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main(void)
{
	int x1, y1, x2, y2;
	int a1, b1, a2, b2;
	int xadd1 = 3, yadd1 =1, xadd2 = 5, yadd2 =2;
	int whichColor=1;
	int arx1[101] = {0};
	int arx2[101] = {0};
	int ary1[101] = {0};
	int ary2[101] = {0};
	int ara1[101] = {0};
	int ara2[101] = {0};
	int arb1[101] = {0};
	int arb2[101] = {0};
	int counter = 0;
	int counter2 = 0;
	/* request auto detection */
	int gdriver = DETECT, gmode, errorcode;
	/* initialize graphics mode */
	initgraph(&gdriver, &gmode, "c:\\tc\\bgi\\");/* path is the location of the egavga.bgi file, might be different on your comp...if you get an error about this file not being found you know why */
	/* read result of initialization */
	errorcode = graphresult();
	if (errorcode != grOk) /* an error occurred */
	{
		printf("Graphics error: %s\n", grapherrormsg(errorcode));
		printf("Press any key to halt:");
		getch();
		exit(1);/* return with error code */
	}
		/* draw a line */
	x1 = 0;
	y1 = 0;
	x2= 0;
	y2 = 0;
	a1 = getmaxx();
	b1 = 0;
	a2 = getmaxx();
	b2 = 0;
	while(!kbhit())
	{
		line(x1, y1, x2, y2);
		line(a1, b1, a2, b2);
		arx1[counter] = x1;
		ary1[counter] = y1;
		arx2[counter] = x2;
		ary2[counter] = y2;
		ara1[counter] = a1;
		ara2[counter] = a2;
		arb1[counter] = b1;
		arb2[counter] = b2;
		if (counter2 > 100)
		{
			setcolor(0);
			if (counter < 100)
			{
				line(arx1[counter + 1], ary1[counter + 1], arx2[counter + 1], ary2[counter + 1]);
				line(ara1[counter + 1], arb1[counter + 1], ara2[counter + 1], arb2[counter + 1]);
			}
			if (counter == 100)
			{
				line(arx1[0], ary1[0], arx2[0], ary2[0]);
				line(ara1[0], arb1[0], ara2[0], arb2[0]);
			}
			setcolor(whichColor);
		}
		if (x1 > (getmaxx()/2) || x1 <0)
			xadd1 *= -1;
		if (x2 > (getmaxx()/2) || x2 < 0)
			xadd2 *= -1;
		if (y1 > getmaxy() || y1 < 0)
			yadd1 *= -1;
		if (y2 > getmaxy() || y2 < 0)
			yadd2 *= -1;
			x1 += xadd1;
			x2 += xadd2;
			y1 += yadd1;
			y2 += yadd2;
			a1 += xadd1 * -1;
			a2 += xadd2 * -1;
			b1 += yadd1;
			b2 += yadd2;
			counter++;
			if(counter2 < 101)
				counter2++;
			if (counter % 100 == 0)
			{
				if(whichColor > getmaxcolor())
					whichColor = 1;
				whichColor += 1;
				setcolor(whichColor);
			}
			if (counter > 100)
				counter = 0;
			delay(10);
	}
 /* clean up */
 getch();
 closegraph();
 return 0;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.