Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ***************************
- Challenge 3: Motion
- Designed By:
- Creates a ball that bounces off the edges of the canvas
- ****************************** */
- final int BACKGROUND_COLOR =255;
- float ballX;
- float ballY;
- float ballXVelocity = random(1,10);
- float ballYVelocity = random(1,10);
- final int BALL_COLOR = 200;
- final float BALL_SIZE=80.0;
- void setup() {
- size(1280, 800);
- ballX = width/2;
- ballY=height/2;
- }
- void draw() {
- background(BACKGROUND_COLOR);
- fill(BALL_COLOR);
- ellipse( ballX, ballY, BALL_SIZE, BALL_SIZE);
- //use velocity to simulate motion
- ballX+=ballXVelocity;
- ballY+=ballYVelocity;
- //reverse direction when the ball hits the left/right edges of the canvas
- if (ballX >= width || ballX <= 0){
- ballXVelocity *= -1;
- }
- //reverse direction when the ball hits the top/bottom edges of the canvas
- if (ballY >= height || ballY <= 0){
- ballYVelocity *= -1;
- }
- }
Add Comment
Please, Sign In to add comment