/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://bard117s14.sketchpad.cc/sp/pad/view/ro.wHJP-iMpl4T/rev.13
*
* authors:
* Keith O'Hara
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/*
* Skeleton of a ball bouncing /game/.
*
* Keith O'Hara <[email protected]>
* Feb 2014
*
*/
float paddle_x;
float paddle_step;
float paddle_size = 16;
float ball_x, ball_y;
float ball_x_step, ball_y_step;
float ball_r = 13;
void setup() {
size(600, 300);
smooth();
paddle_x = width/2;
paddle_step = 0;
reset();
}
void draw() {
background(196);
// move paddle
paddle_x = paddle_x + paddle_step;
//paddle_x = mouseX;
// is the ball hitting the right or left wall?
if (ball_x - ball_r < 0 || ball_x + ball_r > width) {
ball_x_step = -ball_x_step;
}
if (ball_y - ball_r < 0) {
ball_y_step = -ball_y_step;
}
if (ball_y + ball_r > height) {
ball_y_step = -ball_y_step;
}
// move ball by ball_x_step and ball_y_step
ball_x = ball_x + ball_x_step;
ball_y = ball_y + ball_y_step;
//draw ball
noStroke();
fill(196, 0, 0);
ellipse(ball_x, ball_y, ball_r*2, ball_r*2);
}
void keyPressed() {
if (keyCode == LEFT) {
paddle_step = -3;
}
else if (keyCode == RIGHT) {
paddle_step = 3;
}
else if (key == ' ') {
reset();
}
}
void keyReleased() {
paddle_step = 0;
}
void reset() {
ball_x = random(ball_r, width - ball_r);
ball_y = random(ball_r, height/2 );
ball_x_step = random(-3, 3);
ball_y_step = random(1, 3);
}
void drawPaddle() {
// draw paddle
stroke(24);
fill(64);
rect(paddle_x, height-paddle_size, paddle_size*3, paddle_size);
}