Advertisement
Guest User

soundfix

a guest
Oct 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.01 KB | None | 0 0
  1.  
  2. /* Start: Jordan's Variables */
  3. float width = 1280; //debug manual declaration to enable usage of width and height here
  4. float height = 720;
  5. float ballX = width/2; //Ball is placed in the middle, for now
  6. float ballY = height/2;
  7. float friction = 2; //rate for the Ball to slow down over time
  8. 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]
  9. float ballRadius = 2; //allows Ball radius to be modified live, potentially
  10. float ballSpeedX = 0; //placeholder because speed is calculated by coordinates elsewhere
  11. float ballSpeedY = 0;
  12. float hitDelay1 = 0; //prevent multiple micro-collisions with a paddle
  13. float hitDelay2 = 0;
  14.  
  15. float paddle_X1 = 0; //Paddle coordinate for player One. placeholder x and y, because they are mapped to hands
  16. float paddle_Y1 = 0;
  17. //float paddle_X2 = 0; //Paddle coordinate for player Two
  18. //float paddle_Y2 = 0;
  19. float paddle_X2 = width-2*(width/10); //placeholder coordinates for player Two
  20. float paddle_Y2 = height/2;
  21. int p2direction = 0; //placeholder for paddle movement for player Two
  22.  
  23. float prev_X1 = 0; //store the coordinate in the previous frame, to calculate speed
  24. float prev_Y1 = 0;
  25.  
  26. float prev_X2 = 0; //player Two
  27. float prev_Y2 = 0;
  28. float prevTick = 0; //placeholder used in the process of storing previous X and Y
  29. float Xspeed1 = 0; //placeholder variable for speed calculation
  30. float Yspeed1 = 0;
  31. float Xspeed2 = 0; //player Two
  32. float Yspeed2 = 0;
  33.  
  34. float paddleSize_1 = 1; //allows the paddle size to be modified live, potentially
  35. float paddleSize_2 = 1; //allows one specific player's size to be modified, instead of only both
  36. //float paddlePower_1 = 1; //how fast the Ball bounces off the paddle, potentially [can be used alongside bounceFriction]
  37. //float paddlePower_2 = 1;
  38. //boolean punchOne = false; //potential feature, player can press a button to hit harder
  39. //boolean punchTwo = false;
  40.  
  41. float goal_X1 = width/10; //coordinate of goal for player One. allows the goal to move, potentially
  42. float goal_Y1 = height/2;
  43.  
  44. float goal_X2 = 9*(width/10); //coordinate of goal for player Two
  45. float goal_Y2 = height/2;
  46.  
  47. float goalSize_1 = 1; //allows the goal size to be modified live, potentially
  48. float goalSize_2 = 1;
  49. //float goal1_Height = 0;
  50.  
  51. int goalState = 0; //0 for no goal, 1 for player 1 goal, 2 for player 2 goal, reset to 0 after the timer
  52. int goalScores_1 = 0; //score tracker for player 1
  53. int goalScores_2 = 0;
  54. float goalDelay = 0; //goal score animation timer
  55.  
  56. /* End: Jordan's Variables */
  57.  
  58. /* Start: Micaela's Variables */
  59.  
  60. // for Fade-In Fade-Out Text
  61. float timeInterval;
  62. float timePast;
  63.  
  64. int textAlpha = 100;
  65. int textFade = 2;
  66.  
  67. // Initialising Board Theme
  68. int board_theme = 0;
  69.  
  70. // OBJECT RGB COLOURS
  71.  
  72. // Board Background Colour
  73. int colour_bgR = 0;
  74. int colour_bgG = 0;
  75. int colour_bgB = 0;
  76.  
  77. // Puck Colour
  78. int colour_ballR = 255;
  79. int colour_ballG = 255;
  80. int colour_ballB = 255;
  81.  
  82. // Player 1 Paddle
  83. int colour_paddleR1 = 255;
  84. int colour_paddleG1 = 255;
  85. int colour_paddleB1 = 255;
  86.  
  87. // Player 2 Paddle
  88. int colour_paddleR2 = 255;
  89. int colour_paddleG2 = 255;
  90. int colour_paddleB2 = 255;
  91.  
  92. // Ring Colour
  93. int colour_ringR = 255;
  94. int colour_ringB = 255;
  95. int colour_ringG = 255;
  96. int opacity_ring = 255;
  97.  
  98. // MidLine Colour
  99. int colour_midLineR = 255;
  100. int colour_midLineG = 255;
  101. int colour_midLineB = 255;
  102. int radii_midLine = 0;
  103.  
  104. // Boundary Line Colours
  105. int colour_boundLinesR = 255;
  106. int colour_boundLinesG = 255;
  107. int colour_boundLinesB = 255;
  108.  
  109. // Goal 1 Colours
  110. int goal1_R;
  111. int goal1_G;
  112. int goal1_B;
  113.  
  114. // Goal 2 Colours
  115. int goal2_R;
  116. int goal2_G;
  117. int goal2_B;
  118.  
  119. // Stage Variable
  120. int stage; // Variable for switching screens
  121.  
  122. // Theme Fonts
  123. PFont font;
  124.  
  125. // Goal Animation Placeholder
  126. float placeholderAnimationY = 0;
  127.  
  128. /* End: Micaela's Variables */
  129.  
  130. /* Start: Vicky's Variables */
  131.  
  132. PImage[] hFX = new PImage[13];
  133. PImage[] gFX = new PImage[10];
  134. PImage[] match = new PImage[5] ;
  135.  
  136. // Player 1
  137. PImage[] p1MatchOver = new PImage[5] ;
  138. PImage[] p1ScoreBoard = new PImage[5] ;
  139.  
  140. // Player 2
  141. PImage[] p2MatchOver = new PImage[5] ;
  142. PImage[] p2ScoreBoard = new PImage[5] ;
  143.  
  144. // Global Scales
  145. float objScale = 0.3 ;
  146. float scaleWin = 0.2 ;
  147. float scaleWinX = 0.2 ;
  148. float aniY = 200 ;
  149. float aniY2 = 200 ;
  150. float aniY3 = 200 ;
  151. float aniX = 0 ;
  152. float squish = 1 ;
  153.  
  154. // Universal:
  155. // Reactions
  156. int test = 0 ;
  157. float hitfx = 0 ;
  158.  
  159. // Game Set Match
  160. float matchState = 0 ;
  161. int matchExtend = 0 ;
  162. int visible = 0 ;
  163. int visible2 = 0 ;
  164.  
  165. // Player 1
  166. int p1GoalState = 0 ;
  167. int p1GoalExtend = 0 ;
  168.  
  169. // Player 2
  170. int p2GoalState = 0 ;
  171. int p2GoalExtend = 0 ;
  172.  
  173. //global style change
  174. int r = 184 ;
  175. int g = 91 ;
  176. int b = 171 ;
  177. int alph = 255 ; //0-255 (none-visible)
  178.  
  179. /* End: Vicky's Variables */
  180.  
  181.  
  182. import ddf.minim.*;
  183. import ddf.minim.spi.*;
  184. import ddf.minim.ugens.*;
  185. Minim minim;
  186. FilePlayer filePlayer;
  187. AudioOutput out;
  188. Sampler collision, goal, border, applause;
  189. Sampler collision2, goal2, border2, applause2;
  190. Sampler collision3, goal3, border3, applause3;
  191.  
  192. String fileName = "sfx/sounds_back2_final.mp3";
  193.  
  194. void setup(){
  195. size(1280,720);
  196. background(colour_bgR, colour_bgG, colour_bgB);
  197. fill(255,255,255);
  198.  
  199. //Vicky
  200. smooth();
  201. frameRate(30);
  202.  
  203. bothPlayers();
  204. player1Load();
  205. player2Load();
  206. // End: Vicky
  207.  
  208. timePast = millis();
  209. timeInterval = 2000.0f; // every two seconds
  210.  
  211. stage = 1; // Title Screen
  212.  
  213.  
  214. // Sound
  215. minim = new Minim(this);
  216. out = minim.getLineOut();
  217.  
  218. filePlayer = new FilePlayer( minim.loadFileStream("sfx/sounds_back2_final.mp3") );
  219. filePlayer.loop();
  220. filePlayer.patch(out);
  221.  
  222. collision = new Sampler( "sfx/vanilla_collision.mp3", 1, minim ); //SFX Edited/Provided by Justin Abellera - 13044173 (Source: YouTube)
  223. collision.patch(out);
  224. applause = new Sampler( "sfx/vanilla_applause.mp3", 1, minim ); //SFX Edited/Provided by Justin Abellera - 13044173 (Source: YouTube)
  225. applause.patch(out);
  226.  
  227. collision2 = new Sampler( "sfx/jungle_collision.mp3", 1, minim ); //SFX Edited/Provided by Christian Leona - 13044235 (Source: FreeSound.org)
  228. collision2.patch(out);
  229. applause2 = new Sampler( "sfx/jungle_applause.mp3", 1, minim ); //SFX Edited/Provided by Christian Leona - 13044235 (Source: FreeSound.org)
  230. applause2.patch(out);
  231.  
  232. collision3 = new Sampler( "sfx/neon_collision.mp3", 1, minim ); //SFX Edited/Provided by Justin Abellera - 13044173 (Source: YouTube)
  233. collision3.patch(out);
  234. applause3 = new Sampler( "sfx/vanilla_applause.mp3", 1, minim ); //SFX Edited/Provided by Christian Leona - 13044235 (Source: YouTube)
  235. applause3.patch(out);
  236.  
  237. border = new Sampler( "sfx/border.mp3", 1, minim ); //SFX Edited/Provided by Justin Abellera - 13044173 (Source: YouTube)
  238. border.patch(out);
  239. goal = new Sampler( "sfx/goal.mp3", 1, minim ); //SFX Edited/Provided by Christian Leona - 13044235 (Source: FreeSound.org)
  240. goal.patch(out);
  241.  
  242. // End Sound
  243. }
  244.  
  245. void textFade() {
  246. if (millis() > timeInterval + timePast) {
  247. timePast = millis();
  248. textFade *= -1.25;
  249. }
  250. textAlpha += textFade;
  251. }
  252.  
  253. void draw(){
  254. if (stage == 1) {
  255. background(0, 0, 0);
  256.  
  257. fill(255,255,255);
  258. font = loadFont("superstar.vlw");
  259. textFont(font, 60);
  260.  
  261. // Drop Text Shadow
  262. fill(184,91,171);
  263. text("TOTALLY NOT", width/2-180+3, height/3+3);
  264.  
  265. textSize(80);
  266. text("PONG", width/2-100+3, height/3+50+3);
  267.  
  268. // Title Text
  269. fill(255);
  270. textSize(60);
  271. text("TOTALLY NOT", width/2-180, height/3);
  272.  
  273. textSize(80);
  274. text("PONG", width/2-100, height/3+50);
  275.  
  276. // Text Below Title
  277. textFade();
  278. textSize(20);
  279. fill(255, 255, 255, textAlpha);
  280. text("Press any key (for now) to start!", width/2-180, height/2);
  281.  
  282. textSize(12);
  283.  
  284. if (keyPressed) { // note: change this later pls
  285. stage = 2;
  286. }
  287. }
  288.  
  289. if (stage == 2) {
  290. background(0, 0, 0);
  291.  
  292. textSize(50);
  293. fill(184,91,171);
  294. text("MAIN MENU", width/2-130+2, height/3+52+3);
  295. fill(255);
  296. text("MAIN MENU", width/2-130, height/3+52);
  297.  
  298. // "Play" Nav. Button
  299. fill(255); // Menu Nav. colour
  300. rect(width/2-100, height/2, 150, 40);
  301. fill(0);
  302. textSize(30);
  303. text("P L A Y", width/2-70, height/2+30);
  304.  
  305. // Hover
  306. if (mouseX>(width/2-100) && mouseX<((width/2)+150) && mouseY>(height/2) && mouseY<((height/2)+40)) {
  307. fill(184,91,171);
  308. rect(width/2-100, height/2, 150, 40);
  309.  
  310. fill(255);
  311. textSize(30);
  312. text("P L A Y", width/2-70, height/2+30);
  313. }
  314. fill(255); //reset
  315.  
  316. // Pressed
  317. if (mousePressed) {
  318. if (mouseX>(width/2-100) && mouseX<((width/2)+150) && mouseY>(height/2) && mouseY<((height/2)+40)) {
  319. stage = 3;
  320. }
  321. }
  322.  
  323. // "Options" Nav. Button
  324. rect(width/2-100, height/2+60, 150, 40);
  325. fill(0);
  326. textSize(22);
  327. text("O P T I O N S", width/2-90, height/2+87);
  328.  
  329. // Hover
  330. if (mouseX>(width/2-100) && mouseX<((width/2)+150) && mouseY>(height/2+60) && mouseY<(((height/2)+60)+40)) {
  331. fill(184,91,171);
  332. rect(width/2-100, height/2+60, 150, 40);
  333. fill(255);
  334. textSize(22);
  335. text("O P T I O N S", width/2-90, height/2+87);
  336.  
  337. }
  338. fill(255); //reset
  339.  
  340. // Pressed
  341. if (mousePressed) {
  342. if (mouseX>(width/2-100) && mouseX<((width/2)+150) && mouseY>(height/2+60) && mouseY<(((height/2)+60)+40)) {
  343. stage = 4;
  344. }
  345. }
  346. }
  347.  
  348. if (stage == 3) { // Gameplay Screen
  349. frameRate(60);
  350. background(colour_bgR, colour_bgG, colour_bgB);
  351.  
  352. textAlign(CENTER);
  353. text("var " + aniY3, width/2, 50);
  354.  
  355. textAlign(LEFT);
  356. fill(255, 255, 255); //reset colours
  357. textSize(12); //reset font
  358.  
  359. //Return to Menu (3 >> 2)
  360. noStroke();
  361. fill(255);
  362. rect(20,650,80,40);
  363. textSize(20);
  364. fill(0);
  365. text("M E N U", 30, 675);
  366.  
  367. if (mouseX>20 && mouseX<100 && mouseY>650 && mouseY<690) {
  368. fill(230);
  369. rect(20,650,80,40);
  370. fill(0);
  371. text("M E N U", 30, 675);
  372. if (mousePressed) {
  373. stage = 2;
  374. }
  375. }
  376. stroke(0);
  377.  
  378. draw_theme(); // Moved Draw Method here before Goal Delay because the "Goal" animation was not on "top".
  379.  
  380. countHitDelay(); // Vicky "animation"
  381. matchOver();
  382.  
  383.  
  384. hitEffect();
  385. playerScored();
  386. setMatch();
  387.  
  388. scoredGoal();
  389.  
  390. playerWinIndicator();
  391.  
  392.  
  393. countGoalDelay();
  394.  
  395. draw_paddle_1();
  396. draw_paddle_2();
  397. draw_goal_1();
  398. draw_goal_2();
  399. draw_ball();
  400.  
  401. applyFriction();
  402. speedCap();
  403. borderPrevention(); //prevent Ball from leaving the border
  404.  
  405. //debugText(); //debug note: display variable values to check functionality
  406. p2move(); //debug note: placeholder for player 2 paddle
  407.  
  408. paddleSpeed_1();
  409. paddleSpeed_2();
  410. checkHitboxes();
  411.  
  412. updateBallPosition();
  413.  
  414. checkGoals();
  415.  
  416. //put this AFTER paddleSpeed_One(), so that prev_X1 is not the same as paddleX_One
  417. prevCoordinateStorage();
  418.  
  419. //debug note: does the order of these methods matter?
  420. }
  421.  
  422. if (stage == 4) {
  423. background(0, 0, 0);
  424.  
  425. textSize(50);
  426.  
  427. // Drop Shadow
  428. fill(184, 91, 171);
  429. text("OPTIONS", width/2-90+2, height/3-50+3);
  430.  
  431. fill(255);
  432. text("OPTIONS", width/2-90, height/3-50);
  433.  
  434. //Box
  435. stroke(255);
  436. noFill();
  437. rect(width/4, height/4+50, 600, 350);
  438.  
  439. fill(255);
  440. textSize(22);
  441. // "Choose Your Theme!" Text
  442. text("Select your board theme!", width/2-140, height/2-70);
  443.  
  444. textSize(18);
  445. // "Chosen Theme" Text
  446. text("Current theme : ", width/2-140, height-250);
  447.  
  448. if (board_theme == 0) {
  449. text("Classic Pong", width/2, height-250);
  450.  
  451. }
  452. if (board_theme == 1) {
  453. text("Jungle Theme", width/2, height-250);
  454. }
  455. if (board_theme == 2) {
  456. text("Arcade Hockey", width/2, height-250);
  457. }
  458.  
  459. noStroke();
  460. fill(255);
  461. textSize(40); // For box numbers
  462.  
  463. // ThemeBtn1
  464. rect(width/2 - 250, height/2-30, 60, 60);
  465. fill(0);
  466. text("1", width/2 - 225, height/2+10);
  467. if (mouseX>(width/2 - 250) && mouseX<((width/2 - 250)+60) && mouseY>(height/2-30) && mouseY<((height/2-30)+60)) {
  468. fill(184, 71, 191);
  469. rect(width/2 - 250, height/2-30, 60, 60);
  470. fill(255);
  471. text("1", width/2 - 225, height/2+10);
  472.  
  473. textSize(20);
  474. text("Classic Pong", width/2 - 280, height/2+60);
  475. textSize(40); // Reset
  476.  
  477. if (mousePressed) {
  478. board_theme = 0;
  479. }
  480. }
  481. fill(255); // reset
  482.  
  483. // ThemeBtn2
  484. rect(width/2 - 150, height/2-30, 60, 60);
  485. fill(0);
  486. text("2", width/2 - 127, height/2+10);
  487. if (mouseX>(width/2 - 150) && mouseX<((width/2 - 150)+60) && mouseY>(height/2-30) && mouseY<((height/2-30)+60)) {
  488. fill(184, 71, 191);
  489. rect(width/2 - 150, height/2-30, 60, 60);
  490. fill(255);
  491. text("2", width/2 - 127, height/2+10);
  492.  
  493. textSize(20);
  494. text("Jungle Theme", width/2 - 180, height/2+60);
  495. textSize(40); // Reset
  496.  
  497. if (mousePressed) {
  498. board_theme = 1;
  499. }
  500. }
  501. fill(255); // reset
  502.  
  503. // ThemeBtn3
  504. rect(width/2 - 50, height/2-30, 60, 60);
  505. fill(0);
  506. text("3", width/2 - 30, height/2+10);
  507. if (mouseX>(width/2 - 50) && mouseX<((width/2 - 50)+60) && mouseY>(height/2-30) && mouseY<((height/2-30)+60)) {
  508. fill(184, 71, 191);
  509. rect(width/2 - 50, height/2-30, 60, 60);
  510. fill(255);
  511. text("3", width/2 - 30, height/2+10);
  512.  
  513. textSize(20);
  514. text("Arcade Hockey", width/2 - 80, height/2+60);
  515. textSize(40); // Reset
  516.  
  517. if (mousePressed) {
  518. board_theme = 2;
  519. }
  520. }
  521. fill(255); // reset
  522.  
  523. // ThemeBtn4 – INACTIVE
  524. rect(width/2 + 50, height/2-30, 60, 60);
  525. fill(0);
  526. text("4", width/2 + 71, height/2+10);
  527.  
  528. fill(255); // reset
  529.  
  530. // ThemeBtn5 - INACTIVE
  531. rect(width/2 + 150, height/2-30, 60, 60);
  532. fill(0);
  533. text("5", width/2 + 172, height/2+10);
  534.  
  535. fill(255); // reset
  536.  
  537. // "Return to Menu" Button
  538. fill(255);
  539. rect(width/2-90,500,150,50);
  540. textSize(22);
  541. fill(0);
  542. text("G O B A C K", width/2-75, 530);
  543.  
  544. if (mousePressed) {
  545. if (mouseX>(width/2-90) && mouseX<((width/2-90)+150) && mouseY>500 && mouseY<550) {
  546. stage = 2;
  547. }
  548. }
  549. // Return Hover
  550. if (mouseX>(width/2-90) && mouseX<((width/2-90)+150) && mouseY>500 && mouseY<550) {
  551. fill(184, 91, 171);
  552. rect(width/2-90,500,150,50);
  553.  
  554. fill(255);
  555. text("G O B A C K", width/2-75, 530);
  556. }
  557. }
  558.  
  559. fill(255); //reset
  560. }
  561.  
  562. //##### DRAW METHODS #####//
  563. void draw_paddle_1(){ //draw the paddle for player One
  564. pushMatrix();
  565. noStroke();
  566. translate(paddle_X1,paddle_Y1);
  567. fill(colour_paddleR1, colour_paddleG1, colour_paddleB1); //chosen colour, fixed for now
  568. //paddle_X1 = map(mouseX, 0, width, 0, width); //the paddle can follow the mouse, mapped to a specific area
  569. //paddle_Y1 = map(mouseY, 0, height, 0, height);
  570. paddle_X1 = mouseX; //placeholder updating paddle position
  571. paddle_Y1 = mouseY;
  572. ellipse(0,0,20*paddleSize_1,20*paddleSize_1); //the drawn paddle
  573. fill(255,255,255); //reset colour
  574. stroke(0);
  575. popMatrix();
  576. }
  577.  
  578. void draw_paddle_2(){ //draw the paddle for player Two
  579. pushMatrix();
  580. noStroke();
  581. translate(paddle_X2,paddle_Y2);
  582. fill(colour_paddleR2, colour_paddleG2, colour_paddleB2);
  583. ellipse(0,0,20*paddleSize_2,20*paddleSize_2);
  584. fill(255,255,255); //reset colour
  585. stroke(0);
  586. popMatrix();
  587. }
  588.  
  589. void draw_ball(){
  590. pushMatrix();
  591. translate(ballX,ballY);
  592. noStroke();
  593. fill(colour_ballR, colour_ballG, colour_ballB);
  594. ellipse(0,0,40*ballRadius,40*ballRadius);
  595. ellipse(-2,-2,4,4); //debug midpoint
  596. fill(255,255,255); //reset colour
  597. stroke(0);
  598. popMatrix();
  599. }
  600.  
  601. void draw_goal_1(){
  602. noStroke();
  603. pushMatrix();
  604. fill(goal1_R, goal1_G, goal1_B);
  605. translate(goal_X1,goal_Y1);
  606. rect(-15*goalSize_1,-60*goalSize_1,30*goalSize_1,120*goalSize_1);
  607. ellipse(-2,-2,4,4); //debug midpoint
  608. popMatrix();
  609.  
  610. stroke(0); //reset
  611. fill(255);
  612. }
  613.  
  614. void draw_goal_2(){
  615. noStroke();
  616. pushMatrix();
  617. fill(goal2_R, goal2_G, goal2_B);
  618. translate(goal_X2,goal_Y2);
  619. rect(-15*goalSize_2,-60*goalSize_2,30*goalSize_2,120*goalSize_2);
  620. ellipse(-2,-2,4,4); //debug midpoint
  621. popMatrix();
  622.  
  623. stroke(0); //reset
  624. fill(255);
  625. }
  626.  
  627. //##### BACKGROUND THEMES #####//
  628.  
  629. void draw_theme(){
  630. if (board_theme == 0) { // PONG THEME
  631. colour_bgR = 0;
  632. colour_bgG = 0;
  633. colour_bgB = 0;
  634.  
  635. //Ring Colour
  636. colour_ringR = 0;
  637. colour_ringG = 0;
  638. colour_ringB = 0;
  639. opacity_ring = 0;
  640.  
  641. //Boundary Lines
  642. colour_boundLinesR = 255;
  643. colour_boundLinesG = 255;
  644. colour_boundLinesB = 255;
  645.  
  646. // Middle Line
  647. colour_midLineR = 255;
  648. colour_midLineG = 255;
  649. colour_midLineB = 255;
  650. radii_midLine = 0;
  651.  
  652. goal1_R = 255;
  653. goal1_G = 255;
  654. goal1_B = 255;
  655.  
  656. goal2_R = 255;
  657. goal2_G = 255;
  658. goal2_B = 255;
  659.  
  660. colour_ballR = 255;
  661. colour_ballG = 255;
  662. colour_ballB = 255;
  663.  
  664. colour_paddleR1 = 255;
  665. colour_paddleG1 = 255;
  666. colour_paddleB1 = 255;
  667.  
  668. colour_paddleR2 = 255;
  669. colour_paddleG2 = 255;
  670. colour_paddleB2 = 255;
  671. }
  672.  
  673. else if (board_theme == 1) { // JUNGLE THEME
  674. colour_bgR = 110;
  675. colour_bgG = 157;
  676. colour_bgB = 118;
  677.  
  678. //Ring Colour
  679. colour_ringR = 159;
  680. colour_ringG = 155;
  681. colour_ringB = 128;
  682. opacity_ring = 255;
  683.  
  684. //Boundary Lines
  685. colour_boundLinesR = 126;
  686. colour_boundLinesG = 128;
  687. colour_boundLinesB = 118;
  688.  
  689. // Middle Line
  690. colour_midLineR = 126;
  691. colour_midLineG = 128;
  692. colour_midLineB = 118;
  693. radii_midLine = 20;
  694.  
  695. goal1_R = 126;
  696. goal1_G = 128;
  697. goal1_B = 118;
  698.  
  699. goal2_R = 126;
  700. goal2_G = 128;
  701. goal2_B = 118;
  702.  
  703. colour_ballR = 100;
  704. colour_ballG = 100;
  705. colour_ballB = 100;
  706.  
  707. colour_paddleR1 = 94;
  708. colour_paddleG1 = 95;
  709. colour_paddleB1 = 87;
  710.  
  711. colour_paddleR2 = 94;
  712. colour_paddleG2 = 95;
  713. colour_paddleB2 = 87;
  714. }
  715.  
  716. else if (board_theme == 2) { // ARCADE HOCKEY THEME
  717. colour_bgR = 255;
  718. colour_bgG = 255;
  719. colour_bgB = 240;
  720.  
  721. //Ring Colour
  722. colour_ringR = 213;
  723. colour_ringG = 71;
  724. colour_ringB = 41;
  725. opacity_ring = 100;
  726.  
  727. //Boundary Lines
  728. colour_boundLinesR = 213;
  729. colour_boundLinesG = 71;
  730. colour_boundLinesB = 41;
  731.  
  732. // Middle Line
  733. colour_midLineR = 213;
  734. colour_midLineG = 71;
  735. colour_midLineB = 41;
  736. radii_midLine = 5;
  737.  
  738. goal1_R = 102;
  739. goal1_G = 108;
  740. goal1_B = 205;
  741.  
  742. goal2_R = 102;
  743. goal2_G = 108;
  744. goal2_B = 205;
  745.  
  746. colour_ballR = 69;
  747. colour_ballG = 69;
  748. colour_ballB = 69;
  749.  
  750. colour_paddleR1 = 0;
  751. colour_paddleG1 = 0;
  752. colour_paddleB1 = 0;
  753.  
  754. colour_paddleR2 = 0;
  755. colour_paddleG2 = 0;
  756. colour_paddleB2 = 0;
  757.  
  758. noStroke();
  759. fill(102,108,205);
  760. rect(width/2-100, 40, 12, 645);
  761. rect(width/2+100, 40, 12, 645);
  762. }
  763.  
  764. pushMatrix();
  765. //middle line (25 squares down the middle) ['25' value or rect value can be changed, because the Y values are scaled to height]
  766. fill(colour_midLineR, colour_midLineG, colour_midLineB);
  767. noStroke();
  768. pushMatrix();
  769. translate(width/2,height/23-((height/140)));
  770. pushMatrix();
  771. for (int i = 0; i < 25; i++) {
  772. translate(0,height/28);
  773. rect(-8,-8,16,16, radii_midLine); //raw pixel values
  774. }
  775. popMatrix();
  776. popMatrix();
  777.  
  778. // GOAL RINGS
  779. pushMatrix(); //player One
  780. translate(goal_X1+width/100,goal_Y1);
  781. noFill();
  782. stroke(colour_ringR, colour_ringG, colour_ringB, opacity_ring); //
  783. strokeWeight(4);
  784. arc(0, 0, width/8, width/8, radians(270), radians(450)); //inner ring
  785. strokeWeight(8);
  786. arc(0, 0, width/4, width/4, radians(270), radians(450)); //outer ring
  787. popMatrix();
  788.  
  789. pushMatrix(); //player Two
  790. translate(goal_X2-width/100,goal_Y2);
  791. stroke(colour_ringR, colour_ringG, colour_ringB, opacity_ring);
  792. strokeWeight(4);
  793. arc(0, 0, width/8, width/8, radians(90), radians(270)); //inner ring
  794. strokeWeight(8);
  795. arc(0, 0, width/4, width/4, radians(90), radians(270)); //outer ring
  796. stroke(255,255,255); //reset
  797. popMatrix();
  798.  
  799. // BOUNDARY LINES
  800. stroke(colour_boundLinesR, colour_boundLinesG, colour_boundLinesB);
  801. line(width/10, height/20, width/10*9, height/20); //top line
  802. line(width/10, height/20*19, width/10*9, height/20*19); //bottom line
  803. line(width/10, height/20, width/10, height/20*7); //left, up line
  804. line(width/10, height/20*13, width/10, height/20*19); //left, down line
  805. line(width/10*9, height/20, width/10*9, height/20*7); //right, up line
  806. line(width/10*9, height/20*13, width/10*9, height/20*19); //right, down line
  807.  
  808. stroke(0,0,0); //reset
  809. strokeWeight(1);
  810. fill(0,0,0);
  811.  
  812. popMatrix();
  813. }
  814.  
  815. //##### FUNCTIONAL METHODS #####//
  816. void borderPrevention(){
  817. //prevent the Ball from leaving the border
  818. if (ballX > width/10 * 9 -40){ //horizontal speed [moving right]
  819. ballSpeedX = abs(ballSpeedX) * -1;
  820. border.trigger(); // SOUND
  821. } //force move away from border
  822.  
  823.  
  824.  
  825. if (ballX < width/10 + 40){ //horizontal speed [moving left]
  826. ballSpeedX = abs(ballSpeedX);
  827. border.trigger(); // SOUND
  828. }
  829.  
  830. if (ballY > height/20 * 19 -40){ //vertical speed [moving down]
  831. ballSpeedY = abs(ballSpeedY) * -1;
  832. border.trigger(); // SOUND
  833. }
  834.  
  835. if (ballY < height/20 + 40){ //vertical speed [moving up]
  836. ballSpeedY = abs(ballSpeedY);
  837. border.trigger(); // SOUND
  838. }
  839. }
  840.  
  841. void countHitDelay(){
  842. //if the Ball is hit, it cannot be hit again until some time later
  843. 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]
  844. hitDelay1++;
  845. }
  846. if (hitDelay1 > 15){ //reset
  847. hitDelay1 = 0;
  848. } //debug note: can this be improved using >= and <= ??? check frame by frame
  849. if (hitDelay2 > 0 && hitDelay2 < 17){ //for player Two
  850. hitDelay2++; }
  851. if (hitDelay2 > 15){ //reset
  852. hitDelay2 = 0; }
  853.  
  854. if (test > 0 && test < 14) {test++; }
  855. if (test > 13) {test = 0;}
  856.  
  857. }
  858.  
  859. void countGoalDelay(){ //can be merged with countHitDelay() [and every other method that is used on every frame]
  860. //if a goal has been scored, goal animation plays and the Ball is removed until some time later
  861. if (goalDelay > 0 && goalDelay < 121){ //goal animation ends after 120 frames
  862. goalDelay++;
  863. ballX = -5000; //move the Ball off the screen while the goal animation plays
  864. ballY = -5000;
  865. fill(0,255,0);
  866. //begin placeholder goal animation
  867. //pushMatrix();
  868. // translate(width/2-100,height/8);
  869. // textSize(60);
  870. // fill(255,255,255);
  871. // text("G O A L !", 0, placeholderAnimationY); //placeholder goal animation
  872. // textSize(12);
  873. //popMatrix();
  874.  
  875. fill(255,255,255);
  876. placeholderAnimationY = placeholderAnimationY + 3; //placeholder goal animation
  877. }
  878.  
  879. if (goalDelay > 119){ //reset
  880. goalDelay = 0;
  881. goalState = 0; //allow goal to be scored again
  882. ballSpeedX = 0; //reset Ball speed for when it is moved back to the middle
  883. ballSpeedY = 0;
  884. ballX = width/2; //move the Ball back to the middle
  885. ballY = height/2;
  886. placeholderAnimationY = 0; //placeholder goal animation
  887. }
  888. }
  889.  
  890. void checkHitboxes(){
  891. //check if the paddle is overlapping the Ball, by using radial distance
  892. if (dist(paddle_X1, paddle_Y1, ballX, ballY) < (25*ballRadius)) {
  893. //check if Ball is not affected by hitDelay
  894. if (hitDelay1 == 0){ //DEBUG NOTE: make a variable to prevent a player from hitting the Ball while they are over the middle boundary
  895. hitDelay1 = 1;
  896.  
  897. test = 1;
  898.  
  899. collisionSound(); // SOUND
  900.  
  901. hitDelay2 = 0; //allow player Two to hit the Ball immediately by resetting hitDelay2
  902. //if the Ball is moving fast, bounce off the paddle to simulate realistic ricochet/deflection
  903. if (ballSpeedX >= 10 || ballSpeedX <= -10 || ballSpeedY >= 10 || ballSpeedY <= -10){
  904. if (ballX > paddle_X1 && ballY < paddle_Y1){ //approximate deflection based on quadrant, top right
  905. ballSpeedX = abs(ballSpeedX)*bounceFriction; //force move to the right [and also lose some speed]
  906. ballSpeedY = (abs(ballSpeedY)*-1)*bounceFriction; } //force move up
  907. if (ballX > paddle_X1 && ballY > paddle_Y1){ //quadrant, bottom right
  908. ballSpeedX = abs(ballSpeedX)*bounceFriction; //force move right
  909. ballSpeedY = abs(ballSpeedY)*bounceFriction; } //force move down
  910. if (ballX < paddle_X1 && ballY > paddle_Y1){ //quadrant, bottom left
  911. ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; //force move left
  912. ballSpeedY = abs(ballSpeedY)*bounceFriction; } //force move down
  913. if (ballX <= paddle_X1 && ballY <= paddle_Y1){ //quadrant, top left
  914. ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; //force move left
  915. ballSpeedY = (abs(ballSpeedY)*-1)*bounceFriction; } //force move up
  916. }
  917. //if the Ball is moving VERY fast, bounce away from the paddle regardless of quadrant
  918. //[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
  919. //(in most cases, the Ball would be forced in the same direction after teleporting to the opposite side of the paddle)]
  920. 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
  921. if (ballSpeedX > 0){ //force flip X direction
  922. ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; } //move left
  923. if (ballSpeedX < 0){
  924. ballSpeedX = abs(ballSpeedX)*bounceFriction; } //move right
  925. if (ballSpeedY > 0){ //force flip Y direction
  926. ballSpeedY = (abs(ballSpeedX)*-1)*bounceFriction; } //move up
  927. if (ballSpeedX > 0){
  928. ballSpeedY = abs(ballSpeedY)*bounceFriction; } //move down
  929. }
  930. //otherwise if the Ball is slow, it will copy the speed and direction of the paddle instead
  931. if (ballSpeedX < 10 && ballSpeedX > -10 && ballSpeedY < 10 && ballSpeedY > -10){
  932. ballSpeedX = Xspeed1;
  933. ballSpeedY = Yspeed1;
  934. }
  935. }
  936. //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]
  937. }
  938.  
  939. //for player Two
  940. if (dist(paddle_X2, paddle_Y2, ballX, ballY) < (25*ballRadius)) {
  941. if (hitDelay2 == 0){
  942. hitDelay2 = 1;
  943.  
  944. test = 1;
  945.  
  946. collisionSound(); // SOUND
  947.  
  948. hitDelay1 = 0; //allow player One to hit the Ball immediately
  949. //Ball is moving fast
  950. if (ballSpeedX >= 10 || ballSpeedX <= -10 || ballSpeedY >= 10 || ballSpeedY <= -10){
  951. if (ballX > paddle_X2 && ballY < paddle_Y2){ //quadrant, top right
  952. ballSpeedX = abs(ballSpeedX)*bounceFriction; ballSpeedY = (abs(ballSpeedY)*-1)*bounceFriction; }
  953. if (ballX > paddle_X2 && ballY > paddle_Y2){ //quadrant, bottom right
  954. ballSpeedX = abs(ballSpeedX)*bounceFriction; ballSpeedY = abs(ballSpeedY)*bounceFriction; }
  955. if (ballX < paddle_X2 && ballY > paddle_Y2){ //quadrant, bottom left
  956. ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; ballSpeedY = abs(ballSpeedY)*bounceFriction; }
  957. if (ballX <= paddle_X2 && ballY <= paddle_Y2){ //quadrant, top left
  958. ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; ballSpeedY = (abs(ballSpeedY)*-1)*bounceFriction; } }
  959. //Ball is moving VERY fast
  960. if (ballSpeedX >= 25 || ballSpeedX <= -25 || ballSpeedY >= 25 || ballSpeedY <= -25){
  961. if (ballSpeedX > 0){ ballSpeedX = (abs(ballSpeedX)*-1)*bounceFriction; } //move left
  962. if (ballSpeedX < 0){ ballSpeedX = abs(ballSpeedX)*bounceFriction; } //move right
  963. if (ballSpeedY > 0){ ballSpeedY = (abs(ballSpeedX)*-1)*bounceFriction; } //move up
  964. if (ballSpeedX > 0){ ballSpeedY = abs(ballSpeedY)*bounceFriction; } //move down
  965. }
  966. //Ball is slow
  967. if (ballSpeedX < 10 && ballSpeedX > -10 && ballSpeedY < 10 && ballSpeedY > -10){
  968. ballSpeedX = Xspeed2; ballSpeedY = Yspeed2; }
  969. }
  970. }
  971. }
  972.  
  973. void checkGoals(){ //check if the Ball is within a goal
  974. if (goalState == 0){
  975.  
  976. if (ballX > goal_X1-(15*goalSize_1)-20*ballRadius && ballX < goal_X1+(15*goalSize_1)+20*ballRadius ){ //Ball is within goal 1
  977. 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
  978. //taken from draw_goal_1(): rect(-15*goalSize_1,-60*goalSize_1,30*goalSize_1,120*goalSize_1)
  979. goalState = 2; //goal has been scored for player 2
  980. goalScores_2 = goalScores_2 + 1; //increase score for player 2
  981. goalDelay = 1; //begin goal animation timer
  982.  
  983. // Vicky
  984. p2GoalState = 1;
  985.  
  986. // Christian and Justin
  987. goal.trigger();
  988. if (board_theme == 0){
  989. applause.trigger();
  990. }
  991. if (board_theme == 1){
  992. applause2.trigger();
  993. }
  994. if (board_theme == 2){
  995. applause3.trigger();
  996. }
  997. // SOUND
  998.  
  999. }
  1000. }
  1001.  
  1002. if (ballX > goal_X2-(15*goalSize_2)-20*ballRadius && ballX < goal_X2+(15*goalSize_2)+20*ballRadius ){ //Ball is within goal 2
  1003. if (ballY > goal_Y2-(60*goalSize_2)-18*ballRadius && ballY < goal_Y2+(60*goalSize_2)+18*ballRadius ){
  1004. goalState = 1; //goal has been scored for player 1
  1005. goalScores_1 = goalScores_1 + 1; //increase score for player 1
  1006. goalDelay = 1; //begin goal animation timer
  1007.  
  1008. p1GoalState = 1;
  1009.  
  1010. goal.trigger();
  1011. if (board_theme == 0){
  1012. applause.trigger();
  1013. }
  1014. if (board_theme == 1){
  1015. applause2.trigger();
  1016. }
  1017. if (board_theme == 2){
  1018. applause3.trigger();
  1019. }
  1020. // SOUND
  1021.  
  1022. }
  1023. }
  1024. //debug note: how wide does the goal need to be to prevent a player from camping?
  1025. }
  1026. }
  1027.  
  1028. void applyFriction(){
  1029. //reduce the speed of the Ball while friction is active
  1030. //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]
  1031. if (ballSpeedX > 4){ //the Ball is moving RIGHT
  1032. ballSpeedX = ballSpeedX - (0.1*friction); }
  1033. if (ballSpeedX < -4){ //the Ball is moving LEFT
  1034. ballSpeedX = ballSpeedX + (0.1*friction); }
  1035. if (ballSpeedY > 4){ //the Ball is moving DOWN
  1036. ballSpeedY = ballSpeedY - (0.1*friction); }
  1037. if (ballSpeedY < -4){ //the Ball is moving UP
  1038. ballSpeedY = ballSpeedY + (0.1*friction); }
  1039. }
  1040.  
  1041. void speedCap(){
  1042. if (ballSpeedX > 80){ ballSpeedX = 80; }
  1043. if (ballSpeedX < -80){ ballSpeedX = 80; }
  1044. if (ballSpeedY > 80){ ballSpeedY = 80; }
  1045. if (ballSpeedY < -80){ ballSpeedY = 80; }
  1046. }
  1047.  
  1048. void updateBallPosition(){
  1049. ballX = ballX + ballSpeedX;
  1050. ballY = ballY + ballSpeedY;
  1051. }
  1052.  
  1053. void paddleSpeed_1(){
  1054. //compare coordinates of the paddle to calculate the speed
  1055. Xspeed1 = paddle_X1 - prev_X1; //if moved to the right, speed is positive
  1056. Yspeed1 = paddle_Y1 - prev_Y1; //if moved down, speed is positive
  1057. }
  1058.  
  1059. void paddleSpeed_2(){ //can be merged with paddleSpeed_1()
  1060. Xspeed2 = paddle_X2 - prev_X2; //if moved to the right, speed is positive
  1061. Yspeed2 = paddle_Y2 - prev_Y2; //if moved down, speed is positive
  1062. }
  1063.  
  1064. void prevCoordinateStorage(){ //functions similarly to pmouseX, but can be used for more than just one frame, potentially
  1065. prevTick++;
  1066. if (prevTick > 0){ //delayed by one frame
  1067. prevTick = 0;
  1068. prev_X1 = paddle_X1;
  1069. prev_Y1 = paddle_Y1;
  1070. prev_X2 = paddle_X2;
  1071. prev_Y2 = paddle_Y2;
  1072. }
  1073. }
  1074.  
  1075.  
  1076.  
  1077. void collisionSound(){
  1078. if (board_theme == 0){
  1079. collision.trigger();
  1080. }
  1081. if (board_theme == 1){
  1082. collision2.trigger();
  1083. }
  1084. if (board_theme == 2){
  1085. collision3.trigger();
  1086. }
  1087. }
  1088.  
  1089.  
  1090.  
  1091.  
  1092. // //##### debug methods for testing purposes #####//
  1093. //void debugText(){
  1094. // pushMatrix();
  1095. // fill(255,165,0);
  1096. // text(Xspeed1+" Xspeed1",20,20);
  1097. // text(Yspeed1+" Yspeed1",20,20+20);
  1098. // text(hitDelay1+" hitDelay1",20,20+40);
  1099. // text(paddle_X1+" paddle_X1",20,20+60);
  1100. // text(prev_X1+" prev_X1",20,20+80);
  1101. // text(ballSpeedX+" ballSpeedX",20,20+100);
  1102. // text(ballX+" ballX",20,20+120);
  1103. // text("goalState "+goalState,20,20+160);
  1104. // text("p1 score "+goalScores_1,20,20+180);
  1105. // text("p2 score "+goalScores_2,20,20+200);
  1106.  
  1107. // text("ball dist paddle "+dist(paddle_X1, paddle_Y1, ballX, ballY),20,20+250);
  1108. // text(hitDelay2+" hitDelay2",20,20+270);
  1109.  
  1110. // text(Xspeed2+" Xspeed2",1160,20);
  1111. // text(Yspeed2+" Yspeed2",1160,40);
  1112. // text(paddle_X2+" paddle_X2",1160,60);
  1113.  
  1114. // text("placeholder controls:",1160,100);
  1115. // text("mouse for p1",1160,120);
  1116. // text("and CLICK for ball",1160,135);
  1117. // text("arrow keys for p2",1160,160);
  1118. // text("and ENTER to reset",1160,175);
  1119. // //text(var+" var",20,20+);
  1120. // fill(255,255,255);
  1121. // popMatrix();
  1122. //}
  1123.  
  1124. void mousePressed() { //debug note: teleport the ball to the mouse
  1125. /*
  1126. if (stage == 2) {
  1127. if (mouseButton == LEFT) {
  1128. ballX=mouseX;
  1129. ballY=mouseY;
  1130. }
  1131. } */
  1132.  
  1133. }
  1134.  
  1135. void keyReleased() { //debug note: placeholder for player 2 paddle movement, stops movement after releasing ANY key
  1136. p2direction = 0;
  1137. if (keyCode == ENTER) { //reset paddle position
  1138. paddle_X2 = width-2*(width/10);
  1139. paddle_Y2 = height/2;
  1140. }
  1141. }
  1142. void p2move(){ //debug note: placeholder for player 2 paddle movement, forces movement until a different direction is pressed
  1143. if (p2direction != 0){ //this line is redundant
  1144. if (p2direction == 4){
  1145. paddle_Y2 = paddle_Y2 - 25; //up
  1146. } else if (p2direction == 2){
  1147. paddle_Y2 = paddle_Y2 + 25; //down
  1148. } else if (p2direction == 3){
  1149. paddle_X2 = paddle_X2 - 25; //left
  1150. } else if (p2direction == 1){
  1151. paddle_X2 = paddle_X2 + 25; //right
  1152. }
  1153. }
  1154. }
  1155.  
  1156. //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
  1157. //(e.g. paddle moves 160 pixels within 1 frame, skipping over the Ball hitbox) [partially solved using speed cap]
  1158.  
  1159. void mouseClicked(){
  1160. //test = 1;
  1161. // reset
  1162. gameReset(); //place this where ever you need to reset the game
  1163. }
  1164.  
  1165. void keyPressed() { //debug note: placeholder for player 2 paddle movement
  1166. if (key == CODED) {
  1167. if (keyCode == UP) {
  1168. p2direction = 4;
  1169. } else if (keyCode == DOWN) {
  1170. p2direction = 2;
  1171. } else if (keyCode == LEFT) {
  1172. p2direction = 3;
  1173. } else if (keyCode == RIGHT) {
  1174. p2direction = 1;
  1175. }
  1176. }
  1177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement