Answered Essay: Hello. I am trying to figure out how to make a bouncing ball but

Hello. I am trying to figure out how to make a bouncing ball but can’t seem to figure it out. It’s like the bouncing ball in Breakout. I am coding in Processing.

// Bounce aka pong exercise
// Animate a ball that bounces all the edges of the canvas
// Complete this exercise by adding code where you see “ADD CODE HERE”

// The variables x,y give the center of the ball
float x = 200;
float y = 200;

// The variables dx,dy give the velocity of the ball
float dx, dy;

void setup() {
size(400,400);
// Start the ball moving in a random direction
// with a speed of 4 pixels/frame
float angle = random(TWO_PI);
dx = cos(angle)*4;
dy = sin(angle)*4;
}

void draw() {

background(0); // erase canvas

// draw ball at location x,y
// ADD CODE HERE

// update the position of the ball
// ADD CODE HERE

// check to see if the ball has moved past the edge of the canvas.
// If so, negate the appropriate velocity to make the ball bounce
// ADD CODE HERE
}

Expert Answer

 

import java.awt.*;

import java.util.formatter;

import javax.swing.*;

public class BouncingBallSample extends Jpanel{

private static final int BOX_WIDTH=400;

private static final int BOX_HEIGHT=400;

private float ballRadius=200;

private float ballX=ballRadius+50;

private float ballY=ballRadius+20;

private float ballSpeedX=3;

private float ballSpeedY=2;

private static final int UPDATE-RATE=30;

public BouncingBallSimple{

this.setpreffered(new dimension(BOX_WIDTH,BOX_HEIGHT));

thread gamethread=new thread(){

public void run(){

while(true){

ballX=ballX+ballspeedX;

ballY=ballY=ballspeedY;

if(ballX-ballradius<0){

ballspeedX=-ballspeedX;

ballX=ballRadius;

}elseif(ballX+ballRadius>BOX_WIDTH){

ballspeedX=-ballspeedX;

ballX=BOX_WIDTH-ballradius;

}

if(ballY-ballradius<0)

{

ballspeedY=-ballspeedY;

ballY=ballRadius;

}elseif(ballY+ballRadius>BOX_HEIGHT){

ballY=BOX_HEIGHT-ballRadius;

}

repaint();

try

{

Thread.sleep(1000/UPDATE_RATE){

catch(InterruptedException ex){}
}

}

}

};

public void paintcomponent(Graphics g){

super.paint(component(g)){

g.setcolor(color.BLACK);

g.fillRect(0,0,BOX_WIDTH<BOX_HEIGHT);

g.setcolor(color.BLUE);

g.filloval((int)(ballX-ballradius),(int)(ballY-ballradius),(int)(2*ballradius),(int)(2*ballradius));

public static void main(sring[] args){

javax.swing.swingutilities.invokelater(new Runnable()){

publiv void run(){

jframe frame=new jframe(“A BOUNCING BALL”);

frame.setDefaultcloseoperation(jframe.EXIT_ON_CLOSE);

frame.setcontentpanel(new BouncingBallsimple());

frame.Pack();

frame.setVisible(true);

}

}

}

}

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.