//PING//
/*
INTERACTIVE MEDIA COMPUTATION
DEAN ELLIOTT
OCT. 3, 2017
Pressing "START" initiates a pong-esque interactive toy. Movement of the cursor readjusts the racket position. Use this mechanic to keep the target up!
*/
// Variables
// Racket
float racketWidth = 50;
float racketHeight = 7;
float racketX = 200;
float racketY = 175+160;
float leeway = 6;
// Target
float gravity = .1;
float fallspeed = 5;
float velocity = 8;
float xspeed = 5;
float xspeedInc = .6;
float xPos = 75;
float yPos = 0;
float trgtX = (int) random (0, 400);
color trgtClr=255;
int trgtSizeL = 25;
int trgtSizeReg = 15;
int trgtSize = trgtSizeReg;
int trgtSizeInc = 1;
// Score
int score = 0;
// Menu
boolean menu = false;
boolean play = false;
// Setup
void setup() {
size(400, 400);
frameRate(60);
rectMode(CENTER);
ellipseMode(CENTER);
noStroke();
}
// Draw
void draw() {
if (menu == false) {
title();
}
if (play == true) {
drawBackground();
updateRacket();
drawRacket();
checkCollisions();
drawTrgt();
updateTrgt();
drawScore();
respawn();
}
}
// Background
void drawBackground() {
background(172, 215, 243);
rectMode(CENTER);
fill (255);
rect(200, 180, 250, 300);
fill(124, 173, 145);
rect(140, 180, 115, 290);
rect(260, 180, 115, 290);
fill(205);
rectMode(CORNER);
rect(75, 330, 250, 15);
fill(194, 154, 95);
rect(125, 345, 15, 45);
rect(270, 345, 15, 45);
rectMode(CENTER);
rect(200, 365, 150, 5);
}
// Update Racket
void updateRacket() {
if (mouseX < .5 * racketWidth) {
racketX = .5 * racketWidth;
} else if (mouseX > width - (.5 * racketWidth)) {
racketX = width - (.5 * racketWidth);
} else {
racketX = mouseX;
}
}
// Draw Racket
void drawRacket() {
rectMode(CENTER);
noStroke();
fill(244, 219, 174);
rect(mouseX-15, racketY, 65, 15);
fill(226, 151, 139);
rect(mouseX, racketY, 30, 45);
rect(mouseX, racketY, 45, 30);
}
// Update Target
void updateTrgt() {
fallspeed += gravity;
yPos += fallspeed;
xPos += xspeed;
if (trgtSize != trgtSizeReg) {
trgtSize -= trgtSizeInc;
}
}
// Draw Target
void drawTrgt() {
fill(trgtClr);
rect(xPos, yPos, trgtSize, trgtSize);
}
// Collisions
void checkCollisions() {
if (
//Target hits racket
yPos > racketY && yPos < racketY + (fallspeed * 2)
&& xPos < racketX + (racketWidth/2) + leeway && xPos > racketX - (racketWidth/2) - leeway // if the X coordinates match
) {
yPos = racketY - (racketHeight/2);
xspeed += xspeedInc; // increase speed
fallspeed = -velocity; // reverse direction
score ++; // increase score
trgtSize = trgtSizeL; // Target increases when hit by racket
trgtClr = color (random(100, 233), random(134, 143), random(100, 233));
} else if (
// if the ball hits the ground
yPos > height
) {
}
//When the target hits any wall, the direction is reversed, speed is inverted.
if (xPos > width || xPos < 0) {
xspeed *= -1; // direction
xspeedInc *= -1; // speed
// invert x position
if (xspeed < 0) {
xPos = (width - (xPos-width));
} else if (xspeed > 0) {
xPos = -xPos;
}
}
}
// Score
void drawScore() {
textSize(40);
text(0+score, 20, 200);
}
// Respawn. When the target passes the racket, targets position and score are reset.
void respawn() {
if (yPos >= height) {
xPos=(int) random (0, 400); //Target falls from random postions along X-axis.
yPos=0;
fallspeed = 5;
xspeed = 5;
xspeedInc = .6;
score = 0;
trgtClr = 255;
}
}
// Title screen that begins the project.
void title() {
background(172, 215, 243);
textSize(150);
text ("PING", 30, 150);
rectMode(CORNERS);
fill(205);
rect(120, 260, 280, 310);
fill(255);
textSize(40);
text ("START", 140, 300);
}
// Hit box that initiates the game.
void mouseClicked() {
if (mouseX >= 120 && mouseX <= 260 && mouseY >= 280 && mouseY <= 310) {
menu = false;
play = true;
}
}
/*
REFERENCES
The projects listed below are credited to their creators, respectively. They assisted by demonstrating various function abilities.
//Establishing variables, movement/collision (Used for Racket, Target)
//http://www-acad.sheridanc.on.ca/PROG14998/2014/interactive-toy/interactive_toy_66/index.html
//Random colours (Used for Target, Score)
http://www-acad.sheridanc.on.ca/PROG14998/2016/interactive-toy/jordan_goulbourne_interactive_toy/index.html
//Gravity, velocity, movement (Used for Racket)
http://www-acad.sheridanc.on.ca/PROG14998/2015/interactive-toy/interactive_toy_19/index.html#
//Score counter
http://www-acad.sheridanc.on.ca/PROG14998/2014/interactive-toy/interactive_toy_06/index.html
//Hit box registration (Used for Menu/Title)
http://www-acad.sheridanc.on.ca/PROG14998/2015/interactive-toy/interactive_toy_30/index.html
*/