Question: i HAVE A BOUNCING BALL GAME AND IT IS WORKING GOOD. BUT MY PADDLE IS GOING UP AND DOWN I DONT KNOW WHY HOW TO
i HAVE A BOUNCING BALL GAME AND IT IS WORKING GOOD. BUT MY PADDLE IS GOING UP AND DOWN I DONT KNOW WHY HOW TO FIX THAT?
PLEASE MAKE SURE YOU FIX IT RIGHT AND RUN THE CODE BEFORE POSTING THE ANSWER TO MAKE SURE ITS FIXED THANK YOU!!!
#include
int main() { // Initialize the random number generator. srand(time(NULL));
// Initialize the ball position and velocity. int ball_x = SCREEN_WIDTH / 2; int ball_y = 1; int ball_velocity_x = BALL_VELOCITY; int ball_velocity_y = BALL_VELOCITY;
int paddle_x = SCREEN_WIDTH / 2 - PADDLE_WIDTH / 2; while (1) { // Clear the screen. system("cls");
// Draw the ceiling. for (int i = 0; i < SCREEN_WIDTH + 2; i++) { printf("x"); } printf(" ");
// Draw the ball. for (int i = 0; i < ball_y; i++) { printf(" "); } for (int i = 0; i < ball_x; i++) { if (i == SCREEN_WIDTH / 2 - 3 || i == SCREEN_WIDTH / 2 + 2) { printf(" "); } else { printf(" "); } } printf("O ");
// Draw the paddle. for (int i = 0; i < paddle_x; i++) { printf(" "); }
for (int i = 0; i < PADDLE_WIDTH; i++) { printf("_"); }
printf(" ");
// Check for user input to move the paddle. if (_kbhit()) { char key = _getch(); if (key == 77) // right arrow key { if (paddle_x < SCREEN_WIDTH - PADDLE_WIDTH) { paddle_x += 1; } } else if (key == 75) // left arrow key { if (paddle_x > 0) { paddle_x -= 1; } } }
// Draw the bottom border. for (int i = ball_y + 1; i < SCREEN_HEIGHT; i++) { printf(" "); } for (int i = 0; i < SCREEN_WIDTH + 2; i++) { printf("x"); } printf(" ");
// Update the ball position and velocity. ball_x += ball_velocity_x; ball_y += ball_velocity_y;
if (ball_x == 0 || ball_x == SCREEN_WIDTH - 1) { ball_velocity_x = -ball_velocity_x; } if (ball_y == 0 || ball_y == SCREEN_HEIGHT - 1) { ball_velocity_y = -ball_velocity_y; }
// Wait for a short time to control the game speed. for (int i = 0; i < 100000000; i++) {
} }
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
