i have this c code its a bouncing ball game and it has a paddle to make the ball bounce back but it is not working. how to fix it? #include #include #include // Define constants for the screen size and ball velocity. #define SCREEN_WIDTH 60 #define SCREEN_HEIGHT 20 #define BALL_VELOCITY 1 #define CENTER // Variables to keep track of position and speed of ball int x = 0; int y = 100; int x_speed = 5; int y_speed = 5; // Variables to keep track of paddle int x_paddle = 250, y_paddle = 370; int paddle_width_half = 40; // score keeping int score = 0; int width; int key; int LEFT; int RIGHT; int keyPressed; int keyCode; // Called every re-draw, defaul 30 times per second void draw() { // Update position by adding speed x = x + x_speed; y = y + y_speed; if (y < 0) y_speed = -y_speed; if (x > width || x < 0) x_speed = -x_speed; // Check if keys are pressed if (keyPressed) { if (keyCode == RIGHT || key == 'd') { // Move paddle right x_paddle = x_paddle + 8; } else if (keyCode == LEFT || key == 'a') { // Move paddle left x_paddle = x_paddle - 8; } else if (key == ' ') { // Restart x = 0; y = 100; x_paddle = 250; score = 0; } } } 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; // Game loop. while (1) { // Clear the screen. system("cls"); // Draw the ceiling. for (int i = 0; i < SCREEN_WIDTH + 2; i++) { printf("-"); } printf("\n"); // Draw the ball. for (int i = 0; i < ball_y; i++) { printf("\n"); } for (int i = 0; i < ball_x; i++) { if (i == SCREEN_WIDTH / 2 - 3 || i == SCREEN_WIDTH / 2 + 2) { printf(" "); } else { printf(" "); } } printf("O\n"); for (int i = ball_y + 1; i < SCREEN_HEIGHT; i++) { printf("\n"); } for (int i = 0; i < SCREEN_WIDTH + 2; i++) { printf("_"); } printf("\n"); // 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; } // Check if the ball goes into the hole on top or bottom and make it come out as two balls. if (ball_x == SCREEN_WIDTH / 2 - 2 && ball_y == 0) { ball_velocity_y = -ball_velocity_y; ball_y += ball_velocity_y; printf("OO\n"); } else if (ball_x == SCREEN_WIDTH / 2 - 2 && ball_y == SCREEN_HEIGHT - 1) { ball_velocity_y = -ball_velocity_y; ball_y += ball_velocity_y; printf("OO\n"); } // Wait for a short time to control the game speed. for (int i = 0; i < 100000000; i++) { // Do nothing. } } return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

i have this c code its a bouncing ball game and it has a paddle to make the ball bounce back but it is not working. how to fix it?


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Define constants for the screen size and ball velocity.
#define SCREEN_WIDTH 60
#define SCREEN_HEIGHT 20
#define BALL_VELOCITY 1
#define CENTER


// Variables to keep track of position and speed of ball
int x = 0;
int y = 100;
int x_speed = 5;
int y_speed = 5;
// Variables to keep track of paddle
int x_paddle = 250, y_paddle = 370;
int paddle_width_half = 40;
// score keeping
int score = 0;
int width;
int key;
int LEFT;
int RIGHT;
int keyPressed;
int keyCode;

// Called every re-draw, defaul 30 times per second
void draw() {

// Update position by adding speed
x = x + x_speed;
y = y + y_speed;
if (y < 0)
y_speed = -y_speed;
if (x > width || x < 0)
x_speed = -x_speed;

// Check if keys are pressed
if (keyPressed) {
if (keyCode == RIGHT || key == 'd') {
// Move paddle right
x_paddle = x_paddle + 8;
}
else if (keyCode == LEFT || key == 'a') {
// Move paddle left
x_paddle = x_paddle - 8;
}
else if (key == ' ') {
// Restart
x = 0;
y = 100;
x_paddle = 250;
score = 0;
}
}
}


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;

// Game loop.
while (1)
{
// Clear the screen.
system("cls");

// Draw the ceiling.
for (int i = 0; i < SCREEN_WIDTH + 2; i++)
{
printf("-");
}
printf("\n");


// Draw the ball.
for (int i = 0; i < ball_y; i++)
{
printf("\n");
}
for (int i = 0; i < ball_x; i++)
{
if (i == SCREEN_WIDTH / 2 - 3 || i == SCREEN_WIDTH / 2 + 2) {
printf(" ");
}
else {
printf(" ");
}
}
printf("O\n");

for (int i = ball_y + 1; i < SCREEN_HEIGHT; i++)
{
printf("\n");
}
for (int i = 0; i < SCREEN_WIDTH + 2; i++)
{
printf("_");
}
printf("\n");

// 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;
}

// Check if the ball goes into the hole on top or bottom and make it come out as two balls.
if (ball_x == SCREEN_WIDTH / 2 - 2 && ball_y == 0) {
ball_velocity_y = -ball_velocity_y;
ball_y += ball_velocity_y;
printf("OO\n");
}
else if (ball_x == SCREEN_WIDTH / 2 - 2 && ball_y == SCREEN_HEIGHT - 1) {
ball_velocity_y = -ball_velocity_y;
ball_y += ball_velocity_y;
printf("OO\n");
}

// Wait for a short time to control the game speed.
for (int i = 0; i < 100000000; i++)
{
// Do nothing.
}
}

return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Introduction to Coding
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education