Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ***************************
- Challenge 3: Motion
- Designed By: Andy Grove
- Creates a ball that starts in the middle of the canvas and
- bounces off the edges of the canvas
- What needs to be fixed to get this to work?
- What changes could be made to make this code more professional?
- ****************************** */
- int backgroundColor =255;
- float BALLX=width/2;
- float BALLY=height/2;
- float ballXVelocity = random(1,10);
- float ballYVelocity = random(1,10);
- final int ball_color = 200;
- float ball_size=80.0;
- void setup() {
- size(1280, 800);
- }
- void draw() {
- background(backgroundColor);
- fill(ball_color);
- ellipse( BALLX, BALLY, ball_size, ball_size);
- //use velocity to simulate motion
- BALLX = BALLX + ballXVelocity;
- BALLY = BALLY+ ballYVelocity;
- //reverse direction when the ball hits the left/right edges of the canvas
- if (BALLX >= width - 40 || BALLX <= 0 + 40){
- ballXVelocity = ballXVelocity * (1);
- ballYVelocity = ballYVelocity * (-1);
- }
- //reverse direction when the ball hits the top/bottom edges of the canvas
- if (BALLY >= height - 40 || BALLY <= 0 + 40){
- ballXVelocity = ballXVelocity * (-1);
- ballYVelocity = ballYVelocity * (1);
- }
- }
Add Comment
Please, Sign In to add comment