/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://cst1101.sketchpad.cc/sp/pad/view/ro.$FearrclflO/rev.908
*
* authors:
* Joe Perrino
* Jeff Hammer
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/* pong
by joe perrino and jeff hammer */
int bumperOffset = 15;
int play1 = 0;
int play2 = 0;
Ball ball;
Bumper bumper1;
Bumper bumper2;
void setup()
{
size(500, 500);
smooth();
background(255);
textSize(50);
fill(0);
ball = new Ball(width/2, height/2);
bumper1 = new Bumper(width-width+bumperOffset, (height/2));
bumper2 = new Bumper(width-bumperOffset, (height/2));
}
void draw()
{
background(255);
bumper1.display();
bumper2.display();
ball.bounce();
text(play1, 200, 50);
text(play2, 300, 50);
}
void keyPressed()
{
if (key == 'q' || key == 'Q')
{
bumper1.move('U');
}
if (key == 'z' || key == 'Z')
{
bumper1.move('D');
}
}
void mouseMoved()
{
bumper2.allign();
}
//*************************************************
class Ball
{
float xpos; // ball x-coord
float ypos; // ball y-coord
float xspeed = 3; // ball x-speed
float yspeed = 3; // ball y-speed
int radius = 25; // ball radius
float xdir = random(-1, 1); // ball x-dir
float ydir = random(-1, 1); // ball y-dir
color col = color(255, 0, 0); // ball color
Ball(int _xpos, int _ypos)
{
xpos = _xpos;
ypos = _ypos;
}
void bounce()
{
xpos += (xspeed * xdir); // combine x position with x direction
ypos += (yspeed * ydir); // combine y position with y direction
if (xpos < 0 || (xpos+radius) > (width-radius))
{
xdir *= -1;
if (xpos < 0) {
play2++;
}
else {
play1++;
}
}
if (ypos < 0 || (ypos+radius) > (height-radius))
{
ydir *= -1;
}
if (xpos >= 15 && xpos <= 20 && ypos >= (bumper1.ypos - 55) && ypos <= (bumper1.ypos + 55))
{
xdir = abs(xdir);
}
if (xpos >= 430 && xpos <= 435 && ypos >= (bumper2.ypos - 55) && ypos <= (bumper2.ypos + 55))
{
xdir = -1 * abs(xdir);
}
noStroke();
fill(col);
ellipse(xpos+radius, ypos+radius, radius*2, radius*2);
}
void paddleStrike()
{
xdir *= -1;
ydir = random(-1, 1);
}
}
//***************************************************
class Bumper
{
int length = 100; // bumper length
int width = 10; // bumper width
int xpos; // bumper x pos
int ypos; // bumper y pos
int speed = 10; // bumper move speed
Bumper(int _xpos, int _ypos)
{
xpos = _xpos;
ypos = _ypos;
}
void allign() {
ypos = mouseY;
}
void display()
{
noStroke();
fill(0);
rectMode(CENTER);
rect(xpos, ypos, width, length);
}
void move(char dir)
{
if (dir == 'U')
{
ypos -= speed;
}
if (dir == 'D')
{
ypos += speed;
}
}
}