This program simulates pong on your screen using the sides as walls and the mouse as the ball. Have fun. By the way, does this forum allow malicious source codes?
Code:#include <stdio.h> #include <windows.h> int ScreenHeight = 0; int ScreenWidth = 0; INT CheckForXCollision(int x); INT CheckForYCollision(int y); void StartBounce(POINT *lpPoint); int main() { //Print text to screen printf("Press CTRL+C to quit\n"); //Obtain screen dimensions ScreenHeight = GetSystemMetrics(SM_CYSCREEN); ScreenWidth = GetSystemMetrics(SM_CXSCREEN); //Seed random number generator srand(GetTickCount()); //Get mouse location POINT lpPoint; GetCursorPos(&lpPoint); //Start bouncin' StartBounce(&lpPoint); return 0; } void StartBounce(POINT *lpPoint) { //Random number int r =0; //Variables for x and y values BOOL xSwitch = FALSE; BOOL ySwitch = FALSE; for(;;) { //Get random number r = rand() % 1000 + 100; //Checks for collsions and plays beep on event if(CheckForXCollision(lpPoint->x) == 1) { xSwitch = FALSE; Beep(r,30); }else if(CheckForXCollision(lpPoint->x) == -1) { xSwitch = TRUE; Beep(r,30); } if(CheckForYCollision(lpPoint->y) == 1) { ySwitch = FALSE; Beep(r,30); }else if(CheckForYCollision(lpPoint->y) == -1) { ySwitch = TRUE; Beep(r,30); } if(xSwitch == TRUE) { //Increase the x val by 10 lpPoint->x = lpPoint->x + 10; }else{ //Decrease the x val by 10 lpPoint->x = lpPoint->x - 10; } if(ySwitch == TRUE) { //Increase the y val by 10 lpPoint->y = lpPoint->y + 10; }else{ //Decrease the y val by 10 lpPoint->y = lpPoint->y - 10; } //Update position SetCursorPos(lpPoint->x,lpPoint->y); //Rest for 1 millisecond Sleep(1); } } //Checks for collision with the screen on the x axis INT CheckForXCollision(int x) { if( x >= ScreenWidth ) { return 1; } else if( x <= 0) { return -1; } return 0; } //Check for a collision with the screen on the y axis INT CheckForYCollision(int y) { if( y >= ScreenHeight ) { return 1; } else if ( y <= 0 ) { return -1; } return 0; }
Results 1 to 5 of 5
Thread: [C++]Screenpong
Threaded View
- 01 Jul. 2010 04:14pm #1
[C++]Screenpong