Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. Ball ball;
  2. Paddle p1;
  3. Paddle p2;
  4.  
  5. import ddf.minim.*;
  6.  
  7. Minim minim;
  8. AudioPlayer song;
  9.  
  10. int p1Score = 0;
  11. int p2Score = 0;
  12.  
  13. void setup() {
  14. size(1000, 700, P3D);
  15. background(5);
  16. lights();
  17. stroke(#FFD900);
  18.  
  19. minim = new Minim(this);
  20.  
  21. ball = new Ball();
  22. p1 = new Paddle(1);
  23. p2 = new Paddle(2);
  24.  
  25. fill(255);
  26. rectMode(CENTER);
  27. textSize(80);
  28. }
  29.  
  30. void draw() {
  31. background(5);
  32.  
  33. if (p1Score >= 15) {
  34. gameover();
  35. textSize(48);
  36. text("Player 1 wins!!", width/2, height*.75);
  37. } else if (p2Score >= 15) {
  38. gameover();
  39. textSize(48);
  40. text("Player 2 wins!!", width/2, height*.75);
  41. }
  42.  
  43.  
  44. p1.update();
  45. p1.display();
  46. p2.update();
  47. p2.display();
  48. ball.update();
  49. ball.display();
  50.  
  51.  
  52. text(p1Score, width/2-100, 100);
  53. text(p2Score, width/2+50, 100);
  54. rect(width/2, height/2, 5, height);
  55. }
  56.  
  57. void gameover() {
  58. noLoop();
  59. fill(0);
  60. rect(width/2, height/2, width, height);
  61. fill(255);
  62. textSize(128);
  63. textAlign(CENTER, CENTER);
  64. text("GAME OVER", width/2, height/2);
  65. }
  66.  
  67.  
  68.  
  69. class Paddle {
  70. int player;
  71. int pWidth = 20;
  72. int pHeight = 100;
  73. float x;
  74. float y;
  75.  
  76. Paddle(int pNum) {
  77. player = pNum; //setting the which player value from the constructor value
  78. if (player == 1) {
  79. x = 30; //set player 1 to left side of the screen
  80. } else if (player == 2) {
  81. x = width-30; //set player 2 to right side of the screen
  82. }
  83. y = height/2;
  84. }
  85.  
  86. void update() {
  87. if (player == 2) {
  88. if (keyPressed) {
  89. if (key == CODED) {
  90. if (keyCode == UP) {
  91. y-=10;
  92. } else if (keyCode == DOWN) {
  93. y+=10;
  94. }
  95. }
  96. }
  97. } else if (player == 1) {
  98. if (keyPressed) {
  99. if (key == 'q' || key == 'Q') {
  100. y-=10;
  101. } else if (key == 'a' || key == 'A') {
  102. y+=10;
  103. }
  104. }
  105. }
  106.  
  107. if (ball.posX > x - pWidth/2 && ball.posX < x + pWidth/2) {
  108. if (ball.posY > y - pHeight/2 && ball.posY < y + pHeight/2) {
  109. ball.contact();
  110. }
  111. }
  112. }
  113.  
  114. void display() {
  115. pushMatrix();
  116. translate(x, y);
  117. fill(200);
  118. box(pWidth, pHeight, 20);
  119. x = constrain(x, width - width, width);
  120. y = constrain(y, height - height, height);
  121. popMatrix();
  122. }
  123. }
  124.  
  125. class Ball {
  126. float posX;
  127. float posY;
  128. float velX;
  129. float velY;
  130. float size;
  131.  
  132. Ball() {
  133. posX = width/2;
  134. posY = height/2;
  135. if (int(random(2)) == 0) {
  136. velX = 9;
  137. } else {
  138. velX = -9;
  139. }
  140. velY = random(-5, 5);
  141. size = 20;
  142. }
  143.  
  144. void update() {
  145. if (posX < 0) { //if the ball goes off the left side of the screen
  146. p2Score++; //increase player 2 score
  147. posX = width/2;
  148. posY = height/2;
  149. velX = -9;
  150. velY = random(-5, 5);
  151. } else if (posX > width) {
  152. p1Score++; //increase player 1 score
  153. posX = width/2;
  154. posY = height/2;
  155. velX = 9;
  156. velY = random(-5, 5);
  157. }
  158.  
  159. if (posY > height - size/2) { //bounce off the bottom
  160. posY = height - size/2;
  161. velY = -velY;
  162. } else if (posY < 0 + size/2) { //bounce off the top
  163. posY = 0 + size/2;
  164. velY = -velY;
  165. }
  166.  
  167. posX += velX;
  168. posY += velY;
  169. }
  170.  
  171. void display() {
  172. pushMatrix();
  173. translate(posX, posY);
  174. sphere(20);
  175. popMatrix();
  176. }
  177.  
  178. void contact() { //call this function when the ball contacts the paddle
  179. song = minim.loadFile("pongsound.mp3");
  180. song.play();
  181. posX = posX - velX; //step the ball back one frame
  182. velX = -velX;
  183. velY = random(-5, 5);
  184. velX += velX * 0.01; //increase the speed of the ball by 1% every time it hits a paddle
  185. velY += velY * 0.01;
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement