Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. boolean showTitleScreen = true;
  2. boolean playing = false;
  3. boolean gameOver = false;
  4.  
  5. float playerOneX = 25;
  6. float playerOneY = 250;
  7. float playerOneWidth = 10;
  8. float playerOneHeight = 50;
  9.  
  10. float playerTwoX = 465;
  11. float playerTwoY = 250;
  12. float playerTwoWidth = 10;
  13. float playerTwoHeight = 50;
  14.  
  15. float paddleSpeed = 5;
  16.  
  17. boolean upPressed = false;
  18. boolean downPressed = false;
  19. boolean wPressed = false;
  20. boolean sPressed = false;
  21.  
  22. int playerOneScore = 0;
  23. int playerTwoScore = 0;
  24.  
  25. float ballX = 250;
  26. float ballY = 250;
  27. float radius = 10;
  28. float ballDeltaX = -1;
  29. float ballDeltaY = 3;
  30.  
  31. void setup() {
  32. size(500, 500);
  33.  
  34. ellipseMode(CENTER);
  35. ellipseMode(RADIUS);
  36.  
  37. //draw with white
  38. stroke(255, 255, 255);
  39. }
  40.  
  41. void keyPressed() {
  42. if (showTitleScreen) {
  43. if (key == 'P' || key == 'p') {
  44. showTitleScreen = false;
  45. playing = true;
  46. }
  47. }
  48. else if (playing) {
  49. if (key == CODED) {
  50. if (keyCode == UP) {
  51. upPressed = true;
  52. }
  53. else if (keyCode == DOWN) {
  54. downPressed = true;
  55. }
  56. }
  57. else if (key == 'W' || key == 'w') {
  58. wPressed = true;
  59. }
  60. else if (key == 'S' || key == 's') {
  61. sPressed = true;
  62. }
  63. }
  64. else if (gameOver) {
  65. if (key == ' ') {
  66. gameOver = false;
  67. showTitleScreen = true;
  68. playerOneY = 250;
  69. playerTwoY = 250;
  70. ballX = 250;
  71. ballY = 250;
  72. playerOneScore = 0;
  73. playerTwoScore = 0;
  74. }
  75. }
  76. }
  77.  
  78. void keyReleased() {
  79. if (playing) {
  80. if (key == CODED) {
  81. if (keyCode == UP) {
  82. upPressed = false;
  83. }
  84. else if (keyCode == DOWN) {
  85. downPressed = false;
  86. }
  87. }
  88. else if (key == 'W' || key == 'w') {
  89. wPressed = false;
  90. }
  91. else if (key == 'S' || key == 's') {
  92. sPressed = false;
  93. }
  94. }
  95. }
  96.  
  97. void draw() {
  98. if (showTitleScreen) {
  99. background(0, 0, 0);
  100.  
  101. textSize(36);
  102. text("Pong", 165, 100);
  103.  
  104. textSize(18);
  105. text("Press 'P' to play.", 175, 400);
  106. }
  107. else if (playing) {
  108. background(0, 0, 0);
  109.  
  110. if (upPressed) {
  111. if (playerOneY-paddleSpeed > 0) {
  112. playerOneY -= paddleSpeed;
  113. }
  114. }
  115. if (downPressed) {
  116. if (playerOneY + paddleSpeed + playerOneHeight < height) {
  117. playerOneY += paddleSpeed;
  118. }
  119. }
  120. if (wPressed) {
  121. if (playerTwoY-paddleSpeed > 0) {
  122. playerTwoY -= paddleSpeed;
  123. }
  124. }
  125. if (sPressed) {
  126. if (playerTwoY + paddleSpeed + playerTwoHeight < height) {
  127. playerTwoY += paddleSpeed;
  128. }
  129. }
  130.  
  131. float nextBallLeft = ballX - radius + ballDeltaX;
  132. float nextBallRight = ballX + radius + ballDeltaX;
  133. float nextBallTop = ballY - radius + ballDeltaY;
  134. float nextBallBottom = ballY + radius + ballDeltaY;
  135.  
  136. float playerOneRight = playerOneX + playerOneWidth;
  137. float playerOneTop = playerOneY;
  138. float playerOneBottom = playerOneY + playerOneHeight;
  139.  
  140. float playerTwoLeft = playerTwoX;
  141. float playerTwoTop = playerTwoY;
  142. float playerTwoBottom = playerTwoY + playerTwoHeight;
  143.  
  144. //ball bounces off top and bottom of screen
  145. if (nextBallTop < 0 || nextBallBottom > height) {
  146. ballDeltaY *= -1;
  147. }
  148.  
  149. //will the ball go off the left side?
  150. if (nextBallLeft < playerOneRight) {
  151. //is it going to miss the paddle?
  152. if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) {
  153. playerTwoScore ++;
  154.  
  155. if (playerTwoScore == 3) {
  156. playing = false;
  157. gameOver = true;
  158. }
  159.  
  160. ballX = 250;
  161. ballY = 250;
  162. }
  163. else {
  164. ballDeltaX *= -1;
  165. }
  166. }
  167.  
  168. //will the ball intersect player two?
  169. if (nextBallRight > playerTwoLeft) {
  170. //is it going to miss the paddle?
  171. if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) {
  172. playerOneScore ++;
  173.  
  174. if (playerOneScore == 3) {
  175. playing = false;
  176. gameOver = true;
  177. }
  178.  
  179. ballX = 250;
  180. ballY = 250;
  181. }
  182. else {
  183. ballDeltaX *= -1;
  184. }
  185. }
  186.  
  187. ballX += ballDeltaX;
  188. ballY += ballDeltaY;
  189.  
  190. //draw dashed line down center
  191. for (int lineY = 0; lineY < height; lineY += 50) {
  192. line(250, lineY, 250, lineY+25);
  193. }
  194.  
  195. //draw "goal lines" on each side
  196. line(playerOneRight, 0, playerOneRight, height);
  197. line(playerTwoLeft, 0, playerTwoLeft, height);
  198.  
  199. textSize(36);
  200. text(playerOneScore, 100, 100);
  201. text(playerTwoScore, 400, 100);
  202.  
  203. rect(playerOneX, playerOneY, playerOneWidth, playerOneHeight);
  204. rect(playerTwoX, playerTwoY, playerTwoWidth, playerTwoHeight);
  205. ellipse(ballX, ballY, radius, radius);
  206. }
  207. else if (gameOver) {
  208. background(0, 0, 0);
  209.  
  210. textSize(36);
  211. text(playerOneScore, 100, 100);
  212. text(playerTwoScore, 400, 100);
  213.  
  214. textSize(36);
  215. if (playerOneScore > playerTwoScore) {
  216. text("Player 1 Wins!", 165, 200);
  217. }
  218. else {
  219. text("Player 2 Wins!", 165, 200);
  220. }
  221.  
  222. textSize(18);
  223. text("Press space to restart.", 150, 400);
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement