www.pudn.com > screensaver.rar > DoBouncingBall.c


#include "DoBouncingBall.h" 
#include "graphics.h" 
#include "extgraph.h" 
#include "commonlib.h" 
 
#define BALL_SIZE 0.3 
#define X_SPEED 0.03 
#define Y_SPEED 0.03 
 
void DrawBall (double x, double y, string color); 
void MoveToNextStep (double * dblCurX, double * dblCurY, double * dblXSpeed, double * dblYSpeed, string * strColor); 
 
void DoBouncingBall (void) 
{ 
	double dblCurX, dblCurY; 
	double dblXSpeed, dblYSpeed; 
	string strColor; 
 
	dblXSpeed = X_SPEED; 
	dblYSpeed = Y_SPEED; 
 
	dblCurX = BALL_SIZE; 
	dblCurY = BALL_SIZE; 
 
	strColor = "Blue"; 
 
	while(!MouseButtonIsDown()) 
	{ 
		MoveToNextStep(&dblCurX, &dblCurY, &dblXSpeed, &dblYSpeed, &strColor); 
 
		DrawBall (dblCurX, dblCurY, strColor ); 
	} 
} 
 
void DrawBall (double x, double y, string color) 
{ 
	CleanScreen(); 
 
	SetPenColor(color); 
	StartFilledRegion(1.0); 
	 
	MovePen(x, y); 
	DrawArc(BALL_SIZE, 0, 360); 
 
	EndFilledRegion(); 
 
	UpdateDisplay(); 
} 
 
void MoveToNextStep (double * dblCurX, double * dblCurY, double * dblXSpeed, double * dblYSpeed, string * strColor) 
{ 
	*dblCurX = (*dblCurX) + (*dblXSpeed); 
	*dblCurY = (*dblCurY) + (*dblYSpeed); 
 
	if ( ( (*dblCurX) <= 0 ) || ((*dblCurX) >= GetWindowWidth()) ) 
	{ 
		*dblXSpeed = - *dblXSpeed; 
		*strColor = GetRandomColor(); 
	} 
 
	if ( ( (*dblCurY) <= 0 ) || ((*dblCurY) >= GetWindowHeight()) ) 
	{ 
		*dblYSpeed = - *dblYSpeed; 
		*strColor = GetRandomColor(); 
	} 
}