// // Project Name: Bouncing Ball // // Author: Matthew Weathers // Modified by: // // Date : 14-Oct-2004 #include <iostream> #include <stdlib.h> #include <stdio.h> #include <math.h> #include <time.h> #include <string.h> #include "graphics2.h" using namespace std; #define ESC 0x1b void main() { srand((unsigned int)time(NULL)); // Seed the random number generator int GraphDriver=0,GraphMode=0; initgraph( &GraphDriver, &GraphMode, "", 800, 600 ); // Start Window char tempstring[80]="unused"; //Variable Declarations bool KeepGoing=true; char KeyPressed; float ballX, ballY; float prevX, prevY; float XVel, YVel; // Initial Position ballX = 100.0; ballY = 40.0; // Initial Velocity XVel = 4.0; YVel = 5.0; //Main Loop while ( KeepGoing ) { delay( 50 ); // Remember previous position prevX = ballX; prevY = ballY; // Move the ball (Add Velocity to Position) ballX += XVel; ballY += YVel; // Erase old Ball setcolor(BLACK); circle( prevX, prevY, 20); // Draw New ball setcolor(RED); circle( ballX, ballY, 20); // Bounch off left/right walls if ( ballX > 770 || ballX < 30) { XVel *= -1.0; } // Bounch off top/bottom walls if ( ballY > 570 || ballY < 30) { YVel *= -1.0; } // Check to see if a key has been pressed if (kbhit()) { KeyPressed = getch(); if (KeyPressed == 'q') { // q - quit KeepGoing = false; } }//end if kbhit() } // end while kbhit //getch(); //Wait for a key. (When main function ends, the window will close) } //end of main()