Question: I have this code written but it is not running how it suppose to be. how to fix it? #include #include #include #include // Define
I have this code written but it is not running how it suppose to be. how to fix it?
#include
// Define constants for the screen size and ball velocity. #define SCREEN_WIDTH 60 #define SCREEN_HEIGHT 20 #define BALL_VELOCITY 1
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;
// Initialize the paddle position. int paddle_x = SCREEN_WIDTH / 2;
// Game loop. while (1) { // Clear the screen. system("cls");
// Draw the ceiling. for (int i = 0; i < SCREEN_WIDTH + 2; i++) { printf("-"); } printf(" ");
// Draw the ball. for (int i = 0; i < ball_y; i++) { printf(" "); } for (int i = 0; i < ball_x; i++) { printf(" "); } printf("O ");
for (int i = ball_y + 1; i < SCREEN_HEIGHT; i++) { printf(" "); } for (int i = 0; i < SCREEN_WIDTH + 2; i++) { printf("_"); } printf(" ");
// Draw the paddle. for (int i = 0; i < SCREEN_WIDTH; i++) { if (i == paddle_x) { printf("|"); } else { printf(" "); } } 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_velocity_y = -ball_velocity_y; } if (ball_y == SCREEN_HEIGHT - 1) { printf("Game over! "); break; }
// Reverse the horizontal velocity if the ball hits the paddle. if (ball_y == SCREEN_HEIGHT - 2 && ball_velocity_y > 0 && ball_x == paddle_x) { ball_velocity_y = -ball_velocity_y; }
// Update the paddle position. if (_kbhit()) { char key = _getch(); if (key == 'a' && paddle_x > 0) { paddle_x--; } else if (key == 'd' && paddle_x < SCREEN_WIDTH - 1); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
