int x=250; // Horizontal position of ball int direction_x=2; // Change in horizontal position each time draw() executed int y=150; // Vertical position of ball int direction_y=2; // Change in horizontal position each time draw() executed int lives=15; int score=0; void setup() { size(400,400); // Create a window 400x400 pixels } void draw() { background(255,255,255); // Clear screen to white fill(0,255,0); // Set fill colour to blue rect(0,mouseY-60,20,120); // Position rectangle using mouse fill(255,0,0); ellipse(x,y,20,20); // Draw blue disk centered on x,y diameter 20 x=x+direction_x; if(x<10) { direction_x=x; // Bounce x=30; // Force x to beyond the paddle on a restart lives--; // Reduce lives by one if(lives==0) exit(); // If lives is zero then quit } if(x>(width-10)) direction_x=-direction_x; //Right side //Vertical movement y=y+direction_y; if(y /10) direction_y=-direction_y; //Bottom side //Score if((x / 30) &&(abs(mouseY-y)< 60)) { x=30; direction_x=-direction_x; // Bounce score++; } if(y>(height-10)) direction_y=-direction_y; //Bottom side textSize(32); fill(0,0,255); text(score, 10, 30); // Display score text(lives,width-30, 30) // Display lives } (Q) Here is the code for Processing(4.2.1), There is some error in the code. I'm unable to run the code. Please debug the code and submit the output.
int x=250; // Horizontal position of ball
int direction_x=2; // Change in horizontal position each time draw() executed
int y=150; // Vertical position of ball
int direction_y=2; // Change in horizontal position each time draw() executed
int lives=15;
int score=0;
void setup()
{
size(400,400); // Create a window 400x400 pixels
}
void draw()
{
background(255,255,255); // Clear screen to white
fill(0,255,0); // Set fill colour to blue
rect(0,mouseY-60,20,120); // Position rectangle using mouse
fill(255,0,0);
ellipse(x,y,20,20); // Draw blue disk centered on x,y diameter 20
x=x+direction_x;
if(x<10)
{
direction_x=x; // Bounce
x=30; // Force x to beyond the paddle on a restart
lives--; // Reduce lives by one
if(lives==0) exit(); // If lives is zero then quit
}
if(x>(width-10)) direction_x=-direction_x; //Right side
//Vertical movement
y=y+direction_y;
if(y /10) direction_y=-direction_y; //Bottom side
//Score
if((x / 30) &&(abs(mouseY-y)< 60))
{
x=30;
direction_x=-direction_x; // Bounce
score++;
}
if(y>(height-10)) direction_y=-direction_y; //Bottom side
textSize(32);
fill(0,0,255);
text(score, 10, 30); // Display score
text(lives,width-30, 30) // Display lives
}
(Q) Here is the code for Processing(4.2.1), There is some error in the code.
I'm unable to run the code.
Please debug the code and submit the output.
int x=250; // Horizontal position of ball int direction_x=2; // Change in horizontal position each time draw() executed int y=150; // Vertical position of ball int direction_y=2; // Change in horizontal position each time dra) executed int lives=3; int score=0; void setup() { size(400,400); // Create a window 400x400 pixels } void draw() { background(255,255,255); // Clear screen to white fill(0,255,0); // Set fill colour to blue rect(0,mouseY-60,20,120); // Position rectangle using mouse fill(0,0,255); ellipse(x,y,20,20); // Draw blue disk centered on x,y diameter 20 x=x+direction_x; // Update position if(x<10) direction_x=-direction_x; // Reverse direction if hit boundary if(x>(width-10)) direction_x=-direction_x; y=y+direction_y; if(y<10) direction_y=-direction_y; // if(y>(height-10)) direction_y=-direction_y; if(y>(height-10)) // If ball bits bottom of screen then miss.. { direction_y=-direction_y; // Bounce lives--; // Reduce lives by one if(lives==0) exit(); // If lives is zero then quit } if(x<10) { direction_x=-direction_x; // Bounce x=30; // Force x to beyond the paddle on a restart lives--; // Reduce lives by one if(lives==0) exit(); // If lives is zero then quit } if((x<30)&&(abs(mouseY-y)<60)) // If ball has bit paddle then.. { direction_x=-direction_x; // Bounce score++; // Increase score by one } textSize(32); fill(0,0,255); text(score, 10, 30); // Display score text(lives,width-30, 30); // Display lives }
Step by step
Solved in 2 steps with 1 images