Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. //Liz Rossman
  2. //Ma
  3. //Code
  4. ///Code Sketch 9: Fix Pong!
  5.  
  6. //Pong is broken :( we must fix him/her/they!
  7.  
  8. Ball ball;
  9. Paddle p1;
  10. Paddle p2;
  11.  
  12. int p1Score = 0;
  13. int p2Score = 0;
  14.  
  15. void setup() {
  16. size(1000, 700);
  17. background(255);
  18. noStroke();
  19.  
  20. ball = new Ball();
  21. p1 = new Paddle(1);
  22. p2 = new Paddle(2);
  23.  
  24. fill(255);
  25. rectMode(CENTER);
  26. textSize(80);
  27. }
  28.  
  29. void draw() {
  30. background(5);
  31.  
  32. p1.update();
  33. p1.display();
  34. p2.update();
  35. p2.display();
  36. ball.update();
  37. ball.display();
  38.  
  39. text(p1Score, width/2-120, 100);
  40. text(p2Score, width/2+50, 100);
  41. rect(width/2, height/2, 20, height);
  42. }
  43.  
  44. class Paddle {
  45. int player;
  46. int pWidth = 20;
  47. int pHeight = 100;
  48. float x;
  49. float y;
  50.  
  51. Paddle(int pNum) {
  52. player = pNum; //setting the which player value from the constructor value
  53. if (player == 1) {
  54. x = 30; //set player 1 to left side of the screen
  55. } else if (player == 2) {
  56. x = width-30; //set player 2 to right side of the screen
  57. }
  58. y = height/2;
  59. }
  60.  
  61. ///PADDLE CONTROL
  62.  
  63. void update() {
  64. if (player == 1) {
  65. if (keyPressed) {
  66. if (key == CODED) {
  67. if (keyCode == UP && y > 60) {
  68. y-=10;
  69. } else if (keyCode == DOWN && y < 640) {
  70. y+=10;
  71. }
  72. }
  73. }
  74. } else if (player == 2) {
  75. if (keyPressed) {
  76. if (key == CODED) {
  77. if (keyCode == UP && y > 60) {
  78. y-=10;
  79. } else if (keyCode == DOWN && y < 640) {
  80. y+=10;
  81. }
  82. }
  83. }
  84. }
  85.  
  86. if (ball.posX > x - pWidth/2 && ball.posX < x + pWidth/2) {
  87. if (ball.posY > y - pHeight/2 && ball.posY < y + pHeight/2) {
  88. ball.contact();
  89. }
  90. }
  91. }
  92.  
  93. void display() {
  94. rect(x, y, pWidth, pHeight);
  95. }
  96. }
  97.  
  98. class Ball {
  99. float posX;
  100. float posY;
  101. float velX;
  102. float velY;
  103. float size;
  104.  
  105. Ball() {
  106. posX = width/2;
  107. posY = height/2;
  108.  
  109. if (int(random(2)) == 0) {
  110. velX = 4;
  111. } else {
  112. velX = -4;
  113. }
  114. velY = random(-4, 4);
  115. }
  116.  
  117. void update() {
  118.  
  119. ///INCREASES PLAYER SCORES
  120. if (posX < 0) { //if the ball goes off the left side of the screen
  121. p2Score++; //increase player 1 score
  122. posX = width/2;
  123. posY = height/2;
  124. velX = -9;
  125. velY = random(-5, 5);
  126. fill(255, 0, 0); //MODIFICATION: if player wins background color changes
  127. } else if (posX > width) {
  128. p1Score++; //increase player 2 score
  129. posX = width/2;
  130. posY = height/2;
  131. velX = 9;
  132. velY = random(-5, 5);
  133. fill(0, 0, 255);
  134. }
  135.  
  136.  
  137. ///BALL BOUNCE
  138. if (posY > height - size/2) { //bounce off the bottom
  139. posY = height - size/2;
  140. velY = -velY;
  141. } else if (posY < 0 + size/2) { //bounce off the top
  142. posY = 0 + size/2;
  143. velY = -velY;
  144. }
  145.  
  146. posX += velX;
  147. posY += velY;
  148. }
  149.  
  150. void display() {
  151. ellipse(posX, posY, 30, 30);
  152. }
  153.  
  154. void contact() { //call this function when the ball contacts the paddle
  155. posX = posX - velX; //step the ball back one frame
  156. velX = -velX;
  157. velY = random(-5, 5);
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement