Write a program that simulates a ball bouncing within a rectangle.
Your rectangle will be the size of the standard MS-DOS screen: 25 rows by 80 columns (X = 0-79, and Y = 0-24). The rectangle should contain an internal vertical wall consisting of a single column of block characters (ASCII code 0DBh) starting at row 5, column 40, extending down to row 19, column 40. The background should be blue, and the wall and ball should be white.
The ball travels at a constant speed, determined by a delay value of 20 milliseconds inside the loop that displays, erases, and moves the ball. The following sequence of actions will be in the loop:
display, delay, erase, move
The cursor itself should be invisible. This is accomplished by executing the following 32-bit code:
;----- hides the cursor ---------------------------------------- .data cursorInfo CONSOLE_CURSOR_INFO <> outHandle DWORD ? .code INVOKE GetStdHandle, STD_OUTPUT_HANDLE mov outHandle,eax INVOKE GetConsoleCursorInfo, outHandle, ADDR cursorInfo mov cursorInfo.bVisible,0 INVOKE SetConsoleCursorInfo, outHandle, ADDR cursorInfo ;---------------------------------------------------------------
Suppose you have a current location (X, Y), and two direction values, dX, and dY. The latter control the ball's movement. For example, if dX =1 and dY = 1, the ball is moving downward and to the right.
Evaluate the potential new X and Y location:
Next, add dX and dY to your current X and Y coordinates, generating the ball's new location. Redraw the ball at this new location.
|
Optional enhancement: Turn this program into a game by allowing a single player to move the wall up and down using the keyboard. The wall should only be about 1/4 of its current size. Each time the ball bounces off the left wall, the player gains one point. When the ball strikes the right wall, the player loses one point. Display the current number of points while the game is running. |