/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.9G-Hu-KK84R/rev.257
*
* authors:
* Nathaniel Phillips
* Kevin Crouch
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// Global variables for the ball
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 5; // Radius
float dy = 0; // Direction
// Global variables for the paddle
int paddle_width = 5;
int paddle_height = 20;
int dist_wall = 15;
void setup()
{
size(200, 200);
rectMode(CENTER_RADIUS);
ellipseMode(CENTER_RADIUS);
noStroke();
smooth();
ball_y = height/2;
ball_x = 1;
}
void draw()
{
background(255);
ball_x += ball_dir * 1.0;
ball_y += dy;
if(ball_x > width+ball_size) {
ball_x = -width/2 - ball_size;
ball_y = random(0, height);
dy = 0;
}
// Constrain paddle to screen
float paddle_y = constrain(mouseY, paddle_height, height-paddle_height);
// Test to see if the ball is touching the paddle
float py = width-dist_wall-paddle_width-ball_size;
if(ball_x == py
&& ball_y > paddle_y - paddle_height - ball_size
&& ball_y < paddle_y + paddle_height + ball_size) {
ball_dir *= -1;
if(mouseY != pmouseY) {
dy = (mouseY-pmouseY)/2.0;
if(dy > 5) { dy = 5; }
if(dy < -5) { dy = -5; }
}
}
// If ball hits paddle or back wall, reverse direction
if(ball_x < ball_size && ball_dir == -1) {
ball_dir *= -1;
}
// If the ball is touching top or bottom edge, reverse direction
if(ball_y > height-ball_size) {
dy = dy * -1;
resetrings();
}
if(ball_y < ball_size) {
dy = dy * -1;
}
// Draw ball
fill(0);
ellipse(ball_x, ball_y, ball_size, ball_size);
// Draw the paddle
fill(0);
rect(width-dist_wall, paddle_y, paddle_width, paddle_height);
}
/*
int ring_x;
int ring_y;
float ringSize;
int ringWidth = 5;
int lastClick;
void setup() {
size(500, 400);
resetRings(width/2, height/2, 0);
stroke(0);
noFill();
lastClick = millis();
}
void resetRings(int newX, int newY) {
ring_x = newX;
ring_y = newY;
ringSize = 0;
}
void mousePressed() {
lastClick = millis();
resetRings(mouseX, mouseY);
}
void circle(int x, int y, int d) {
ellipse(ring_x, ring_y, d, d);
}
void draw() {
if (millis() - 1000 > lastClick) {
resetRings(random(width), random(height));
lastClick = millis();
}
background(255);
ringSize = ringSize + 0.3;
int ringCount = 0;
while (ringCount < 5 && ringSize - ringCount * ringWidth*2 > 0) {
circle(ring_x, ring_y, ringSize - ringCount * ringWidth*2);
ringCount = ringCount + 1;
}
}
*/