Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Start: Jordan's Variables */
- float width = 1280; //debug manual declaration to enable usage of width and height here
- float height = 720;
- float ballX = width/2; //Ball is placed in the middle, for now
- float ballY = height/2;
- float friction = 2; //rate for the Ball to slow down over time
- float bounceFriction = 0.8; //multiplier for the Ball to lose speed when hit, 1 for no effect, speed loss increases as this decreases to 0 [can be higher than 1 to gain speed instead] [can split this for both players, potentially]
- float ballRadius = 2; //allows Ball radius to be modified live, potentially
- float ballSpeedX = 0; //placeholder because speed is calculated by coordinates elsewhere
- float ballSpeedY = 0;
- float hitDelay1 = 0; //prevent multiple micro-collisions with a paddle
- float hitDelay2 = 0;
- float paddle_X1 = 0; //Paddle coordinate for player One. placeholder x and y, because they are mapped to hands
- float paddle_Y1 = 0;
- //float paddle_X2 = 0; //Paddle coordinate for player Two
- //float paddle_Y2 = 0;
- float paddle_X2 = width-2*(width/10); //placeholder coordinates for player Two
- float paddle_Y2 = height/2;
- int p2direction = 0; //placeholder for paddle movement for player Two
- float prev_X1 = 0; //store the coordinate in the previous frame, to calculate speed
- float prev_Y1 = 0;
- float prev_X2 = 0; //player Two
- float prev_Y2 = 0;
- float prevTick = 0; //placeholder used in the process of storing previous X and Y
- float Xspeed1 = 0; //placeholder variable for speed calculation
- float Yspeed1 = 0;
- float Xspeed2 = 0; //player Two
- float Yspeed2 = 0;
- float paddleSize_1 = 1; //allows the paddle size to be modified live, potentially
- float paddleSize_2 = 1; //allows one specific player's size to be modified, instead of only both
- //float paddlePower_1 = 1; //how fast the Ball bounces off the paddle, potentially [can be used alongside bounceFriction]
- //float paddlePower_2 = 1;
- //boolean punchOne = false; //potential feature, player can press a button to hit harder
- //boolean punchTwo = false;
- float goal_X1 = width/10; //coordinate of goal for player One. allows the goal to move, potentially
- float goal_Y1 = height/2;
- float goal_X2 = 9*(width/10); //coordinate of goal for player Two
- float goal_Y2 = height/2;
- float goalSize_1 = 1; //allows the goal size to be modified live, potentially
- float goalSize_2 = 1;
- //float goal1_Height = 0;
- int goalState = 0; //0 for no goal, 1 for player 1 goal, 2 for player 2 goal, reset to 0 after the timer
- int goalScores_1 = 0; //score tracker for player 1
- int goalScores_2 = 0;
- float goalDelay = 0; //goal score animation timer
- /* End: Jordan's Variables */
- /* Start: Micaela's Variables */
- // for Fade-In Fade-Out Text
- float timeInterval;
- float timePast;
- int textAlpha = 100;
- int textFade = 2;
- // Initialising Board Theme
- int board_theme = 0;
- // OBJECT RGB COLOURS
- // Board Background Colour
- int colour_bgR = 0;
- int colour_bgG = 0;
- int colour_bgB = 0;
- // Puck Colour
- int colour_ballR = 255;
- int colour_ballG = 255;
- int colour_ballB = 255;
- // Player 1 Paddle
- int colour_paddleR1 = 255;
- int colour_paddleG1 = 255;
- int colour_paddleB1 = 255;
- // Player 2 Paddle
- int colour_paddleR2 = 255;
- int colour_paddleG2 = 255;
- int colour_paddleB2 = 255;
- // Ring Colour
- int colour_ringR = 255;
- int colour_ringB = 255;
- int colour_ringG = 255;
- int opacity_ring = 255;
- // MidLine Colour
- int colour_midLineR = 255;
- int colour_midLineG = 255;
- int colour_midLineB = 255;
- int radii_midLine = 0;
- // Boundary Line Colours
- int colour_boundLinesR = 255;
- int colour_boundLinesG = 255;
- int colour_boundLinesB = 255;
- // Goal 1 Colours
- int goal1_R;
- int goal1_G;
- int goal1_B;
- // Goal 2 Colours
- int goal2_R;
- int goal2_G;
- int goal2_B;
- // Stage Variable
- int stage; // Variable for switching screens
- // Theme Fonts
- PFont font;
- // Goal Animation Placeholder
- float placeholderAnimationY = 0;
- /* End: Micaela's Variables */
- /* Start: Vicky's Variables */
- PImage[] hFX = new PImage[13];
- PImage[] gFX = new PImage[10];
- PImage[] match = new PImage[5] ;
- // Player 1
- PImage[] p1MatchOver = new PImage[5] ;
- PImage[] p1ScoreBoard = new PImage[5] ;
- // Player 2
- PImage[] p2MatchOver = new PImage[5] ;
- PImage[] p2ScoreBoard = new PImage[5] ;
- // Global Scales
- float objScale = 0.3 ;
- float scaleWin = 0.2 ;
- float scaleWinX = 0.2 ;
- float aniY = 200 ;
- float aniY2 = 200 ;
- float aniY3 = 200 ;
- float aniX = 0 ;
- float squish = 1 ;
- // Universal:
- // Reactions
- int test = 0 ;
- float hitfx = 0 ;
- // Game Set Match
- float matchState = 0 ;
- int matchExtend = 0 ;
- int visible = 0 ;
- int visible2 = 0 ;
- // Player 1
- int p1GoalState = 0 ;
- int p1GoalExtend = 0 ;
- // Player 2
- int p2GoalState = 0 ;
- int p2GoalExtend = 0 ;
- //global style change
- int r = 184 ;
- int g = 91 ;
- int b = 171 ;
- int alph = 255 ; //0-255 (none-visible)
- /* End: Vicky's Variables */
- import ddf.minim.*;
- import ddf.minim.spi.*;
- import ddf.minim.ugens.*;
- Minim minim;
- FilePlayer filePlayer;
- AudioOutput out;
- Sampler collision, goal, border, applause;
- Sampler collision2, goal2, border2, applause2;
- Sampler collision3, goal3, border3, applause3;
- String fileName = "sfx/sounds_back2_final.mp3";
- void setup(){
- size(1280,720);
- background(colour_bgR, colour_bgG, colour_bgB);
- fill(255,255,255);
- //Vicky
- smooth();
- frameRate(30);
- bothPlayers();
- player1Load();
- player2Load();
- // End: Vicky
- timePast = millis();
- timeInterval = 2000.0f; // every two seconds
- stage = 1; // Title Screen
- // Sound
- minim = new Minim(this);
- out = minim.getLineOut();
- filePlayer = new FilePlayer( minim.loadFileStream("sfx/sounds_back2_final.mp3") );
- filePlayer.loop();
- filePlayer.patch(out);
- collision = new Sampler( "sfx/vanilla_collision.mp3", 1, minim ); //SFX Edited/Provided by Justin Abellera - 13044173 (Source: YouTube)
- collision.patch(out);
- applause = new Sampler( "sfx/vanilla_applause.mp3", 1, minim ); //SFX Edited/Provided by Justin Abellera - 13044173 (Source: YouTube)
- applause.patch(out);
- collision2 = new Sampler( "sfx/jungle_collision.mp3", 1, minim ); //SFX Edited/Provided by Christian Leona - 13044235 (Source: FreeSound.org)
- collision2.patch(out);
- applause2 = new Sampler( "sfx/jungle_applause.mp3", 1, minim ); //SFX Edited/Provided by Christian Leona - 13044235 (Source: FreeSound.org)
- applause2.patch(out);
- collision3 = new Sampler( "sfx/neon_collision.mp3", 1, minim ); //SFX Edited/Provided by Justin Abellera - 13044173 (Source: YouTube)
- collision3.patch(out);
- applause3 = new Sampler( "sfx/vanilla_applause.mp3", 1, minim ); //SFX Edited/Provided by Christian Leona - 13044235 (Source: YouTube)
- applause3.patch(out);
- border = new Sampler( "sfx/border.mp3", 1, minim ); //SFX Edited/Provided by Justin Abellera - 13044173 (Source: YouTube)
- border.patch(out);
- goal = new Sampler( "sfx/goal.mp3", 1, minim ); //SFX Edited/Provided by Christian Leona - 13044235 (Source: FreeSound.org)
- goal.patch(out);
- // End Sound
- }
- void textFade() {
- if (millis() > timeInterval + timePast) {
- timePast = millis();
- textFade *= -1.25;
- }
- textAlpha += textFade;
- }
- void draw(){
- if (stage == 1) {
- background(0, 0, 0);
- fill(255,255,255);
- font = loadFont("superstar.vlw");
- textFont(font, 60);
- // Drop Text Shadow
- fill(184,91,171);
- text("TOTALLY NOT", width/2-180+3, height/3+3);
- textSize(80);
- text("PONG", width/2-100+3, height/3+50+3);
- // Title Text
- fill(255);
- textSize(60);
- text("TOTALLY NOT", width/2-180, height/3);
- textSize(80);
- text("PONG", width/2-100, height/3+50);
- // Text Below Title
- textFade();
- textSize(20);
- fill(255, 255, 255, textAlpha);
- text("Press any key (for now) to start!", width/2-180, height/2);
- textSize(12);
- if (keyPressed) { // note: change this later pls
- stage = 2;
- }
- }
- if (stage == 2) {
- background(0, 0, 0);
- textSize(50);
- fill(184,91,171);
- text("MAIN MENU", width/2-130+2, height/3+52+3);
- fill(255);
- text("MAIN MENU", width/2-130, height/3+52);
- // "Play" Nav. Button
- fill(255); // Menu Nav. colour
- rect(width/2-100, height/2, 150, 40);
- fill(0);
- textSize(30);
- text("P L A Y", width/2-70, height/2+30);
- // Hover
- if (mouseX>(width/2-100) && mouseX<((width/2)+150) && mouseY>(height/2) && mouseY<((height/2)+40)) {
- fill(184,91,171);
- rect(width/2-100, height/2, 150, 40);
- fill(255);
- textSize(30);
- text("P L A Y", width/2-70, height/2+30);
- }
- fill(255); //reset
- // Pressed
- if (mousePressed) {
- if (mouseX>(width/2-100) && mouseX<((width/2)+150) && mouseY>(height/2) && mouseY<((height/2)+40)) {
- stage = 3;
- }
- }
- // "Options" Nav. Button
- rect(width/2-100, height/2+60, 150, 40);
- fill(0);
- textSize(22);
- text("O P T I O N S", width/2-90, height/2+87);
- // Hover
- if (mouseX>(width/2-100) && mouseX<((width/2)+150) && mouseY>(height/2+60) && mouseY<(((height/2)+60)+40)) {
- fill(184,91,171);
- rect(width/2-100, height/2+60, 150, 40);
- fill(255);
- textSize(22);
- text("O P T I O N S", width/2-90, height/2+87);
- }
- fill(255); //reset
- // Pressed
- if (mousePressed) {
- if (mouseX>(width/2-100) && mouseX<((width/2)+150) && mouseY>(height/2+60) && mouseY<(((height/2)+60)+40)) {
- stage = 4;
- }
- }
- }
- if (stage == 3) { // Gameplay Screen
- frameRate(60);
- background(colour_bgR, colour_bgG, colour_bgB);
- textAlign(CENTER);
- text("var " + aniY3, width/2, 50);
- textAlign(LEFT);
- fill(255, 255, 255); //reset colours
- textSize(12); //reset font
- //Return to Menu (3 >> 2)
- noStroke();
- fill(255);
- rect(20,650,80,40);
- textSize(20);
- fill(0);
- text("M E N U", 30, 675);
- if (mouseX>20 && mouseX<100 && mouseY>650 && mouseY<690) {
- fill(230);
- rect(20,650,80,40);
- fill(0);
- text("M E N U", 30, 675);
- if (mousePressed) {
- stage = 2;
- }
- }
- stroke(0);
- draw_theme(); // Moved Draw Method here before Goal Delay because the "Goal" animation was not on "top".
- countHitDelay(); // Vicky "animation"
- matchOver();
- hitEffect();
- playerScored();
- setMatch();
- scoredGoal();
- playerWinIndicator();
- countGoalDelay();
- draw_paddle_1();
- draw_paddle_2();
- draw_goal_1();
- draw_goal_2();
- draw_ball();
- applyFriction();
- speedCap();
- borderPrevention(); //prevent Ball from leaving the border
- //debugText(); //debug note: display variable values to check functionality
- p2move(); //debug note: placeholder for player 2 paddle
- paddleSpeed_1();
- paddleSpeed_2();
- checkHitboxes();
- updateBallPosition();
- checkGoals();
- //put this AFTER paddleSpeed_One(), so that prev_X1 is not the same as paddleX_One
- prevCoordinateStorage();
- //debug note: does the order of these methods matter?
- }
- if (stage == 4) {
- background(0, 0, 0);
- textSize(50);
- // Drop Shadow
- fill(184, 91, 171);
- text("OPTIONS", width/2-90+2, height/3-50+3);
- fill(255);
- text("OPTIONS", width/2-90, height/3-50);
- //Box
- stroke(255);
- noFill();
- rect(width/4, height/4+50, 600, 350);
- fill(255);
- textSize(22);
- // "Choose Your Theme!" Text
- text("Select your board theme!", width/2-140, height/2-70);
- textSize(18);
- // "Chosen Theme" Text
- text("Current theme : ", width/2-140, height-250);
- if (board_theme == 0) {
- text("Classic Pong", width/2, height-250);
- }
- if (board_theme == 1) {
- text("Jungle Theme", width/2, height-250);
- }
- if (board_theme == 2) {
- text("Arcade Hockey", width/2, height-250);
- }
- noStroke();
- fill(255);
- textSize(40); // For box numbers
- // ThemeBtn1
- rect(width/2 - 250, height/2-30, 60, 60);
- fill(0);
- text("1", width/2 - 225, height/2+10);
- if (mouseX>(width/2 - 250) && mouseX<((width/2 - 250)+60) && mouseY>(height/2-30) && mouseY<((height/2-30)+60)) {
- fill(184, 71, 191);
- rect(width/2 - 250, height/2-30, 60, 60);
- fill(255);
- text("1", width/2 - 225, height/2+10);
- textSize(20);
- text("Classic Pong", width/2 - 280, height/2+60);
- textSize(40); // Reset
- if (mousePressed) {
- board_theme = 0;
- }
- }
- fill(255); // reset
- // ThemeBtn2
- rect(width/2 - 150, height/2-30, 60, 60);
- fill(0);
- text("2", width/2 - 127, height/2+10);
- if (mouseX>(width/2 - 150) && mouseX<((width/2 - 150)+60) && mouseY>(height/2-30) && mouseY<((height/2-30)+60)) {
- fill(184, 71, 191);
- rect(width/2 - 150, height/2-30, 60, 60);
- fill(255);
- text("2", width/2 - 127, height/2+10);
- textSize(20);
- text("Jungle Theme", width/2 - 180, height/2+60);
- textSize(40); // Reset
- if (mousePressed) {
- board_theme = 1;
- }
- }
- fill(255); // reset
- // ThemeBtn3
- rect(width/2 - 50, height/2-30, 60, 60);
- fill(0);
- text("3", width/2 - 30, height/2+10);
- if (mouseX>(width/2 - 50) && mouseX<((width/2 - 50)+60) && mouseY>(height/2-30) && mouseY<((height/2-30)+60)) {
- fill(184, 71, 191);
- rect(width/2 - 50, height/2-30, 60, 60);
- fill(255);
- text("3", width/2 - 30, height/2+10);
- textSize(20);
- text("Arcade Hockey", width/2 - 80, height/2+60);
- textSize(40); // Reset
- if (mousePressed) {
- board_theme = 2;
- }
- }
- fill(255); // reset
- // ThemeBtn4 – INACTIVE
- rect(width/2 + 50, height/2-30, 60, 60);
- fill(0);
- text("4", width/2 + 71, height/2+10);
- fill(255); // reset
- // ThemeBtn5 - INACTIVE
- rect(width/2 + 150, height/2-30, 60, 60);
- fill(0);
- text("5", width/2 + 172, height/2+10);
- fill(255); // reset
- // "Return to Menu" Button
- fill(255);
- rect(width/2-90,500,150,50);
- textSize(22);
- fill(0);
- text("G O B A C K", width/2-75, 530);
- if (mousePressed) {
- if (mouseX>(width/2-90) && mouseX<((width/2-90)+150) && mouseY>500 && mouseY<550) {
- stage = 2;
- }
- }
- // Return Hover
- if (mouseX>(width/2-90) && mouseX<((width/2-90)+150) && mouseY>500 && mouseY<550) {
- fill(184, 91, 171);
- rect(width/2-90,500,150,50);
- fill(255);
- text("G O B A C K", width/2-75, 530);
- }
- }
- fill(255); //reset
- }
- //##### DRAW METHODS #####//
- void draw_paddle_1(){ //draw the paddle for player One
- pushMatrix();
- noStroke();
- translate(paddle_X1,paddle_Y1);
- fill(colour_paddleR1, colour_paddleG1, colour_paddleB1); //chosen colour, fixed for now
- //paddle_X1 = map(mouseX, 0, width, 0, width); //the paddle can follow the mouse, mapped to a specific area
- //paddle_Y1 = map(mouseY, 0, height, 0, height);
- paddle_X1 = mouseX; //placeholder updating paddle position
- paddle_Y1 = mouseY;
- ellipse(0,0,20*paddleSize_1,20*paddleSize_1); //the drawn paddle
- fill(255,255,255); //reset colour
- stroke(0);
- popMatrix();
- }
- void draw_paddle_2(){ //draw the paddle for player Two
- pushMatrix();
- noStroke();
- translate(paddle_X2,paddle_Y2);
- fill(colour_paddleR2, colour_paddleG2, colour_paddleB2);
- ellipse(0,0,20*paddleSize_2,20*paddleSize_2);
- fill(255,255,255); //reset colour
- stroke(0);
- popMatrix();
- }
- void draw_ball(){
- pushMatrix();
- translate(ballX,ballY);
- noStroke();
- fill(colour_ballR, colour_ballG, colour_ballB);
- ellipse(0,0,40*ballRadius,40*ballRadius);
- ellipse(-2,-2,4,4); //debug midpoint
- fill(255,255,255); //reset colour
- stroke(0);
- popMatrix();
- }
- void draw_goal_1(){
- noStroke();
- pushMatrix();
- fill(goal1_R, goal1_G, goal1_B);
- translate(goal_X1,goal_Y1);
- rect(-15*goalSize_1,-60*goalSize_1,30*goalSize_1,120*goalSize_1);
- ellipse(-2,-2,4,4); //debug midpoint
- popMatrix();
- stroke(0); //reset
- fill(255);
- }
- void draw_goal_2(){
- noStroke();
- pushMatrix();
- fill(goal2_R, goal2_G, goal2_B);
- translate(goal_X2,goal_Y2);
- rect(-15*goalSize_2,-60*goalSize_2,30*goalSize_2,120*goalSize_2);
- ellipse(-2,-2,4,4); //debug midpoint
- popMatrix();
- stroke(0); //reset
- fill(255);
- }
- //##### BACKGROUND THEMES #####//
- void draw_theme(){
- if (board_theme == 0) { // PONG THEME
- colour_bgR = 0;
- colour_bgG = 0;
- colour_bgB = 0;
- //Ring Colour
- colour_ringR = 0;
- colour_ringG = 0;
- colour_ringB = 0;
- opacity_ring = 0;
- //Boundary Lines
- colour_boundLinesR = 255;
- colour_boundLinesG = 255;
- colour_boundLinesB = 255;
- // Middle Line
- colour_midLineR = 255;
- colour_midLineG = 255;
- colour_midLineB = 255;
- radii_midLine = 0;
- goal1_R = 255;
- goal1_G = 255;
- goal1_B = 255;
- goal2_R = 255;
- goal2_G = 255;
- goal2_B = 255;
- colour_ballR = 255;
- colour_ballG = 255;
- colour_ballB = 255;
- colour_paddleR1 = 255;
- colour_paddleG1 = 255;
- colour_paddleB1 = 255;
- colour_paddleR2 = 255;
- colour_paddleG2 = 255;
- colour_paddleB2 = 255;
- }
- else if (board_theme == 1) { // JUNGLE THEME
- colour_bgR = 110;
- colour_bgG = 157;
- colour_bgB = 118;
- //Ring Colour
- colour_ringR = 159;
- colour_ringG = 155;
- colour_ringB = 128;
- opacity_ring = 255;
- //Boundary Lines
- colour_boundLinesR = 126;
- colour_boundLinesG = 128;
- colour_boundLinesB = 118;
- // Middle Line
- colour_midLineR = 126;
- colour_midLineG = 128;
- colour_midLineB = 118;
- radii_midLine = 20;
- goal1_R = 126;
- goal1_G = 128;
- goal1_B = 118;
- goal2_R = 126;
- goal2_G = 128;
- goal2_B = 118;
- colour_ballR = 100;
- colour_ballG = 100;
- colour_ballB = 100;
- colour_paddleR1 = 94;
- colour_paddleG1 = 95;
- colour_paddleB1 = 87;
- colour_paddleR2 = 94;
- colour_paddleG2 = 95;
- colour_paddleB2 = 87;
- }
- else if (board_theme == 2) { // ARCADE HOCKEY THEME
- colour_bgR = 255;
- colour_bgG = 255;
- colour_bgB = 240;
- //Ring Colour
- colour_ringR = 213;
- colour_ringG = 71;
- colour_ringB = 41;
- opacity_ring = 100;
- //Boundary Lines
- colour_boundLinesR = 213;
- colour_boundLinesG = 71;
- colour_boundLinesB = 41;
- // Middle Line
- colour_midLineR = 213;
- colour_midLineG = 71;
- colour_midLineB = 41;
- radii_midLine = 5;
- goal1_R = 102;
- goal1_G = 108;
- goal1_B = 205;
- goal2_R = 102;
- goal2_G = 108;
- goal2_B = 205;
- colour_ballR = 69;
- colour_ballG = 69;
- colour_ballB = 69;
- colour_paddleR1 = 0;
- colour_paddleG1 = 0;
- colour_paddleB1 = 0;
- colour_paddleR2 = 0;
- colour_paddleG2 = 0;
- colour_paddleB2 = 0;
- noStroke();
- fill(102,108,205);
- rect(width/2-100, 40, 12, 645);
- rect(width/2+100, 40, 12, 645);
- }
- pushMatrix();
- //middle line (25 squares down the middle) ['25' value or rect value can be changed, because the Y values are scaled to height]
- fill(colour_midLineR, colour_midLineG, colour_midLineB);
- noStroke();
- pushMatrix();
- translate(width/2,height/23-((height/140)));
- pushMatrix();
- for (int i = 0; i < 25; i++) {
- translate(0,height/28);
- rect(-8,-8,16,16, radii_midLine); //raw pixel values
- }
- popMatrix();
- popMatrix();
- // GOAL RINGS
- pushMatrix(); //player One
- translate(goal_X1+width/100,goal_Y1);
- noFill();
- stroke(colour_ringR, colour_ringG, colour_ringB, opacity_ring); //
- strokeWeight(4);
- arc(0, 0, width/8, width/8, radians(270), radians(450)); //inner ring
- strokeWeight(8);
- arc(0, 0, width/4, width/4, radians(270), radians(450)); //outer ring
- popMatrix();
- pushMatrix(); //player Two
- translate(goal_X2-width/100,goal_Y2);
- stroke(colour_ringR, colour_ringG, colour_ringB, opacity_ring);
- strokeWeight(4);
- arc(0, 0, width/8, width/8, radians(90), radians(270)); //inner ring
- strokeWeight(8);
- arc(0, 0, width/4, width/4, radians(90), radians(270)); //outer ring
- stroke(255,255,255); //reset
- popMatrix();
- // BOUNDARY LINES
- stroke(colour_boundLinesR, colour_boundLinesG, colour_boundLinesB);
- line(width/10, height/20, width/10*9, height/20); //top line
- line(width/10, height/20*19, width/10*9, height/20*19); //bottom line
- line(width/10, height/20, width/10, height/20*7); //left, up line
- line(width/10, height/20*13, width/10, height/20*19); //left, down line
- line(width/10*9, height/20, width/10*9, height/20*7); //right, up line
- line(width/10*9, height/20*13, width/10*9, height/20*19); //right, down line
- stroke(0,0,0); //reset
- strokeWeight(1);
- fill(0,0,0);
- popMatrix();
- }
- //##### FUNCTIONAL METHODS #####//
- void borderPrevention(){
- //prevent the Ball from leaving the border
- if (ballX > width/10 * 9 -40){ //horizontal speed [moving right]
- ballSpeedX = abs(ballSpeedX) * -1;
- border.trigger(); // SOUND
- } //force move away from border
- if (ballX < width/10 + 40){ //horizontal speed [moving left]
- ballSpeedX = abs(ballSpeedX);
- border.trigger(); // SOUND
- }
- if (ballY > height/20 * 19 -40){ //vertical speed [moving down]
- ballSpeedY = abs(ballSpeedY) * -1;
- border.trigger(); // SOUND
- }
- if (ballY < height/20 + 40){ //vertical speed [moving up]
- ballSpeedY = abs(ballSpeedY);
- border.trigger(); // SOUND
- }
- }
- void countHitDelay(){
- //if the Ball is hit, it cannot be hit again until some time later
- if (hitDelay1 > 0 && hitDelay1 < 17){ //Ball cannot be hit until 15 frames [17 because it starts at 1 and is immediately incremented to 2, probably]
- hitDelay1++;
- }
- if (hitDelay1 > 15){ //reset
- hitDelay1 = 0;
- } //debug note: can this be improved using >= and <= ??? check frame by frame
- if (hitDelay2 > 0 && hitDelay2 < 17){ //for player Two
- hitDelay2++; }
- if (hitDelay2 > 15){ //reset
- hitDelay2 = 0; }
- if (test > 0 && test < 14) {test++; }
- if (test > 13) {test = 0;}
- }
- void countGoalDelay(){ //can be merged with countHitDelay() [and every other method that is used on every frame]
- //if a goal has been scored, goal animation plays and the Ball is removed until some time later
- if (goalDelay > 0 && goalDelay < 121){ //goal animation ends after 120 frames
- goalDelay++;
- ballX = -5000; //move the Ball off the screen while the goal animation plays
- ballY = -5000;
- fill(0,255,0);
- //begin placeholder goal animation
- //pushMatrix();
- // translate(width/2-100,height/8);
- // textSize(60);
- // fill(255,255,255);
- // text("G O A L !", 0, placeholderAnimationY); //placeholder goal animation
- // textSize(12);
- //popMatrix();
- fill(255,255,255);
- placeholderAnimationY = placeholderAnimationY + 3; //placeholder goal animation
- }
- if (goalDelay > 119){ //reset
- goalDelay = 0;
- goalState = 0; //allow goal to be scored again
- ballSpeedX = 0; //reset Ball speed for when it is moved back to the middle
- ballSpeedY = 0;
- ballX = width/2; //move the Ball back to the middle
- ballY = height/2;
- placeholderAnimationY = 0; //placeholder goal animation
- }
- }
- void checkHitboxes(){
- //check if the paddle is overlapping the Ball, by using radial distance
- if (dist(paddle_X1, paddle_Y1, ballX, ballY) < (25*ballRadius)) {
- //check if Ball is not affected by hitDelay
- if (hitDelay1 == 0){ //DEBUG NOTE: make a variable to prevent a player from hitting the Ball while they are over the middle boundary
- hitDelay1 = 1;
- test = 1;
- collisionSound(); // SOUND
- hitDelay2 = 0; //allow player Two to hit the Ball immediately by resetting hitDelay2
- //if the Ball is moving fast, bounce off the paddle to simulate realistic ricochet/deflection
- if (ballSpeedX >= 10 || ballSpeedX <= -10 || ballSpeedY >= 10 || ballSpeedY <= -10){
- if (ballX > paddle_X1 && ballY < paddle_Y1){ //approximate deflection based on quadrant, top right
- ballSpeedX = abs(ballSpeedX)*bounceFriction; //force move to the right [and also lose some speed]
- ballSpeedY = (abs(ballSpeedY)*-1)*bounceFriction; } //force move up
- if (ballX > paddle_X1 && ballY > paddle_Y1){ //quadrant, bottom right
- ballSpeedX = abs(ballSpeedX)*bounceFriction; //force move right
- ballSpeedY = abs(ballSpeedY)*bounceFriction; } //force move down
- if (ballX < paddle_X1 && ballY > paddle_Y1){ //quadrant, bottom left
- ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; //force move left
- ballSpeedY = abs(ballSpeedY)*bounceFriction; } //force move down
- if (ballX <= paddle_X1 && ballY <= paddle_Y1){ //quadrant, top left
- ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; //force move left
- ballSpeedY = (abs(ballSpeedY)*-1)*bounceFriction; } //force move up
- }
- //if the Ball is moving VERY fast, bounce away from the paddle regardless of quadrant
- //[due to large differences in pixels distances for each frame, the Ball can skip over the paddle and be placed in the wrong quadrant, resulting in an unexpected ricochet
- //(in most cases, the Ball would be forced in the same direction after teleporting to the opposite side of the paddle)]
- if (ballSpeedX >= 25 || ballSpeedX <= -25 || ballSpeedY >= 25 || ballSpeedY <= -25){ //25 can be changed to scale with ballRadius and paddleSize, to increase the range of speeds that maintain the realistic ricochet
- if (ballSpeedX > 0){ //force flip X direction
- ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; } //move left
- if (ballSpeedX < 0){
- ballSpeedX = abs(ballSpeedX)*bounceFriction; } //move right
- if (ballSpeedY > 0){ //force flip Y direction
- ballSpeedY = (abs(ballSpeedX)*-1)*bounceFriction; } //move up
- if (ballSpeedX > 0){
- ballSpeedY = abs(ballSpeedY)*bounceFriction; } //move down
- }
- //otherwise if the Ball is slow, it will copy the speed and direction of the paddle instead
- if (ballSpeedX < 10 && ballSpeedX > -10 && ballSpeedY < 10 && ballSpeedY > -10){
- ballSpeedX = Xspeed1;
- ballSpeedY = Yspeed1;
- }
- }
- //hitDelay = 1; //uncomment this if you want the delay to reset UNTIL the paddle is not on the Ball [potential issues if the player tracks the Ball with the paddle]
- }
- //for player Two
- if (dist(paddle_X2, paddle_Y2, ballX, ballY) < (25*ballRadius)) {
- if (hitDelay2 == 0){
- hitDelay2 = 1;
- test = 1;
- collisionSound(); // SOUND
- hitDelay1 = 0; //allow player One to hit the Ball immediately
- //Ball is moving fast
- if (ballSpeedX >= 10 || ballSpeedX <= -10 || ballSpeedY >= 10 || ballSpeedY <= -10){
- if (ballX > paddle_X2 && ballY < paddle_Y2){ //quadrant, top right
- ballSpeedX = abs(ballSpeedX)*bounceFriction; ballSpeedY = (abs(ballSpeedY)*-1)*bounceFriction; }
- if (ballX > paddle_X2 && ballY > paddle_Y2){ //quadrant, bottom right
- ballSpeedX = abs(ballSpeedX)*bounceFriction; ballSpeedY = abs(ballSpeedY)*bounceFriction; }
- if (ballX < paddle_X2 && ballY > paddle_Y2){ //quadrant, bottom left
- ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; ballSpeedY = abs(ballSpeedY)*bounceFriction; }
- if (ballX <= paddle_X2 && ballY <= paddle_Y2){ //quadrant, top left
- ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; ballSpeedY = (abs(ballSpeedY)*-1)*bounceFriction; } }
- //Ball is moving VERY fast
- if (ballSpeedX >= 25 || ballSpeedX <= -25 || ballSpeedY >= 25 || ballSpeedY <= -25){
- if (ballSpeedX > 0){ ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; } //move left
- if (ballSpeedX < 0){ ballSpeedX = abs(ballSpeedX)*bounceFriction; } //move right
- if (ballSpeedY > 0){ ballSpeedY = (abs(ballSpeedX)*-1)*bounceFriction; } //move up
- if (ballSpeedX > 0){ ballSpeedY = abs(ballSpeedY)*bounceFriction; } //move down
- }
- //Ball is slow
- if (ballSpeedX < 10 && ballSpeedX > -10 && ballSpeedY < 10 && ballSpeedY > -10){
- ballSpeedX = Xspeed2; ballSpeedY = Yspeed2; }
- }
- }
- }
- void checkGoals(){ //check if the Ball is within a goal
- if (goalState == 0){
- if (ballX > goal_X1-(15*goalSize_1)-20*ballRadius && ballX < goal_X1+(15*goalSize_1)+20*ballRadius ){ //Ball is within goal 1
- if (ballY > goal_Y1-(60*goalSize_1)-18*ballRadius && ballY < goal_Y1+(60*goalSize_1)+18*ballRadius ){ //the hitbox is slightly generous around the corners because the Ball is a circle and dist() is not used
- //taken from draw_goal_1(): rect(-15*goalSize_1,-60*goalSize_1,30*goalSize_1,120*goalSize_1)
- goalState = 2; //goal has been scored for player 2
- goalScores_2 = goalScores_2 + 1; //increase score for player 2
- goalDelay = 1; //begin goal animation timer
- // Vicky
- p2GoalState = 1;
- // Christian and Justin
- goal.trigger();
- if (board_theme == 0){
- applause.trigger();
- }
- if (board_theme == 1){
- applause2.trigger();
- }
- if (board_theme == 2){
- applause3.trigger();
- }
- // SOUND
- }
- }
- if (ballX > goal_X2-(15*goalSize_2)-20*ballRadius && ballX < goal_X2+(15*goalSize_2)+20*ballRadius ){ //Ball is within goal 2
- if (ballY > goal_Y2-(60*goalSize_2)-18*ballRadius && ballY < goal_Y2+(60*goalSize_2)+18*ballRadius ){
- goalState = 1; //goal has been scored for player 1
- goalScores_1 = goalScores_1 + 1; //increase score for player 1
- goalDelay = 1; //begin goal animation timer
- p1GoalState = 1;
- goal.trigger();
- if (board_theme == 0){
- applause.trigger();
- }
- if (board_theme == 1){
- applause2.trigger();
- }
- if (board_theme == 2){
- applause3.trigger();
- }
- // SOUND
- }
- }
- //debug note: how wide does the goal need to be to prevent a player from camping?
- }
- }
- void applyFriction(){
- //reduce the speed of the Ball while friction is active
- //DEBUG NOTE: the '4' here must be changed if friction is greater than 8 [applyFriction() -> 0.1*friction cannot exceed the range of 8 from 4>x>-4 because the stopping range will be skipped]
- if (ballSpeedX > 4){ //the Ball is moving RIGHT
- ballSpeedX = ballSpeedX - (0.1*friction); }
- if (ballSpeedX < -4){ //the Ball is moving LEFT
- ballSpeedX = ballSpeedX + (0.1*friction); }
- if (ballSpeedY > 4){ //the Ball is moving DOWN
- ballSpeedY = ballSpeedY - (0.1*friction); }
- if (ballSpeedY < -4){ //the Ball is moving UP
- ballSpeedY = ballSpeedY + (0.1*friction); }
- }
- void speedCap(){
- if (ballSpeedX > 80){ ballSpeedX = 80; }
- if (ballSpeedX < -80){ ballSpeedX = 80; }
- if (ballSpeedY > 80){ ballSpeedY = 80; }
- if (ballSpeedY < -80){ ballSpeedY = 80; }
- }
- void updateBallPosition(){
- ballX = ballX + ballSpeedX;
- ballY = ballY + ballSpeedY;
- }
- void paddleSpeed_1(){
- //compare coordinates of the paddle to calculate the speed
- Xspeed1 = paddle_X1 - prev_X1; //if moved to the right, speed is positive
- Yspeed1 = paddle_Y1 - prev_Y1; //if moved down, speed is positive
- }
- void paddleSpeed_2(){ //can be merged with paddleSpeed_1()
- Xspeed2 = paddle_X2 - prev_X2; //if moved to the right, speed is positive
- Yspeed2 = paddle_Y2 - prev_Y2; //if moved down, speed is positive
- }
- void prevCoordinateStorage(){ //functions similarly to pmouseX, but can be used for more than just one frame, potentially
- prevTick++;
- if (prevTick > 0){ //delayed by one frame
- prevTick = 0;
- prev_X1 = paddle_X1;
- prev_Y1 = paddle_Y1;
- prev_X2 = paddle_X2;
- prev_Y2 = paddle_Y2;
- }
- }
- void collisionSound(){
- if (board_theme == 0){
- collision.trigger();
- }
- if (board_theme == 1){
- collision2.trigger();
- }
- if (board_theme == 2){
- collision3.trigger();
- }
- }
- // //##### debug methods for testing purposes #####//
- //void debugText(){
- // pushMatrix();
- // fill(255,165,0);
- // text(Xspeed1+" Xspeed1",20,20);
- // text(Yspeed1+" Yspeed1",20,20+20);
- // text(hitDelay1+" hitDelay1",20,20+40);
- // text(paddle_X1+" paddle_X1",20,20+60);
- // text(prev_X1+" prev_X1",20,20+80);
- // text(ballSpeedX+" ballSpeedX",20,20+100);
- // text(ballX+" ballX",20,20+120);
- // text("goalState "+goalState,20,20+160);
- // text("p1 score "+goalScores_1,20,20+180);
- // text("p2 score "+goalScores_2,20,20+200);
- // text("ball dist paddle "+dist(paddle_X1, paddle_Y1, ballX, ballY),20,20+250);
- // text(hitDelay2+" hitDelay2",20,20+270);
- // text(Xspeed2+" Xspeed2",1160,20);
- // text(Yspeed2+" Yspeed2",1160,40);
- // text(paddle_X2+" paddle_X2",1160,60);
- // text("placeholder controls:",1160,100);
- // text("mouse for p1",1160,120);
- // text("and CLICK for ball",1160,135);
- // text("arrow keys for p2",1160,160);
- // text("and ENTER to reset",1160,175);
- // //text(var+" var",20,20+);
- // fill(255,255,255);
- // popMatrix();
- //}
- void mousePressed() { //debug note: teleport the ball to the mouse
- /*
- if (stage == 2) {
- if (mouseButton == LEFT) {
- ballX=mouseX;
- ballY=mouseY;
- }
- } */
- }
- void keyReleased() { //debug note: placeholder for player 2 paddle movement, stops movement after releasing ANY key
- p2direction = 0;
- if (keyCode == ENTER) { //reset paddle position
- paddle_X2 = width-2*(width/10);
- paddle_Y2 = height/2;
- }
- }
- void p2move(){ //debug note: placeholder for player 2 paddle movement, forces movement until a different direction is pressed
- if (p2direction != 0){ //this line is redundant
- if (p2direction == 4){
- paddle_Y2 = paddle_Y2 - 25; //up
- } else if (p2direction == 2){
- paddle_Y2 = paddle_Y2 + 25; //down
- } else if (p2direction == 3){
- paddle_X2 = paddle_X2 - 25; //left
- } else if (p2direction == 1){
- paddle_X2 = paddle_X2 + 25; //right
- }
- }
- }
- //debug issue: a paddle can move through the Ball without triggering the hitbox, if the paddle is moving very fast and cannot keep up with the movement within 1 frame
- //(e.g. paddle moves 160 pixels within 1 frame, skipping over the Ball hitbox) [partially solved using speed cap]
- void mouseClicked(){
- //test = 1;
- // reset
- gameReset(); //place this where ever you need to reset the game
- }
- void keyPressed() { //debug note: placeholder for player 2 paddle movement
- if (key == CODED) {
- if (keyCode == UP) {
- p2direction = 4;
- } else if (keyCode == DOWN) {
- p2direction = 2;
- } else if (keyCode == LEFT) {
- p2direction = 3;
- } else if (keyCode == RIGHT) {
- p2direction = 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement