Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. //BoxallBenjaminA2Q5
  2. //By Ben Boxall COMP 1010 04-A
  3. //Due Friday, 19 October 2019
  4. //Pong Game
  5.  
  6. //Setting final variables first
  7.  
  8. final int BALL_DIAMETER = 20; //the Diameter of the ball (does not move in this program)
  9. final int PADDLE_HEIGHT = 100; // the height in pixels of both paddles.
  10. final int PADDLE_WIDTH = 20; //the width in pixels of both paddles.
  11. final int PADDLE_SPEED = 10; //speed in which the paddles move in pixels per frame
  12. final int MAX_BALL_SPEED = 12; //speed will not exceed this value
  13. final int MIN_BALL_SPEED = 3; //speed will not subceed this value
  14.  
  15. //Setting global variables for the score, as well as the coordinants of the paddles.
  16. int keyScore, mouseScore;
  17. int lPaddleX, lPaddleY, rPaddleX, rPaddleY;
  18.  
  19. //graphical variables
  20. int centreX, centreY;
  21.  
  22. //These represent the position of the ball
  23. float ballX, ballY; //physical position of balls
  24. float ballVX, ballVY; //Pixels per frame that the ball moves.
  25. float angle; //between 0 and TWO_PI, the direction that the ball will move.
  26. float ballSpeed; //current speed of ball
  27.  
  28. //while gameOver == false, the game will be paused until SPACE (keycode == 32) is pressed.
  29. boolean gameOver = false;
  30. int lastPoint; //1 and -1 respective for left and right. Toggles based on point.
  31.  
  32. //gameModes
  33. boolean randomCollisions; //gamemode for random ball bouncing.
  34. boolean ballSpeedIncreases; //gamemode for ball speeding up on collisions
  35.  
  36. void setup(){
  37. size(720, 480); //No size was specified, I chose this size because it is an excelent game size.
  38. frameRate(60); //As shown in the Ball Bouncing program example
  39. parameters(); //lets the user change some parameters
  40. keyScore = mouseScore = 0; //start of game sets both scores to 0.
  41. resetBall(); //This resets the ball to the centre
  42. checkAngles(); //gets ballVX and ballVY to initiate game.
  43. resetPaddles(); //This resets the paddles to the centre at the beginning and when a point is scored
  44. setGraphicalVariables();
  45. }
  46.  
  47.  
  48. void draw(){ //loops infinitely unless otherwise controlled
  49. if (!gameOver){ //
  50. background(100,155,200); //lovely blue.
  51. drawGame(); //calls visual functions
  52. checkIfScored(); //checks if the ball is out of bounds, then resets it and changes the score.
  53. }
  54. else {
  55. reset(); //waits for a reset key to be pressed (post game) to reset the game while displaying the score.
  56. }
  57. }
  58.  
  59. void drawGame(){
  60. //eventually this function will include commands to call the movement functions
  61. drawBall();
  62. drawPaddles();
  63. moveBall();
  64. checkColissions();
  65. movePaddles();
  66. drawScore();
  67. }
  68. //~~~~Functions to Draw ~~~~\\
  69.  
  70. void drawPaddles(){
  71. //passes the variables to PaddleDrawer for each side as to save space with code.
  72. paddleDrawer(lPaddleY, lPaddleX);
  73. paddleDrawer(rPaddleY, rPaddleX);
  74. }
  75.  
  76. void paddleDrawer(int paddleY, int paddleX){
  77. //Thsi function is passed variables so code can be reused;
  78. rect(paddleX, paddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
  79. }
  80.  
  81. void drawScore(){
  82. //the following code was copied and pasted directly as shown at /SPECIFIC INSTRUCTION/ of the assignment
  83. textSize(20);
  84. String toPrint = "Keyboard: " + keyScore;
  85. text(toPrint, width/4-textWidth(toPrint)/2, 50);
  86. toPrint = "Mouse: " + mouseScore;
  87. text(toPrint, width*3/4-textWidth(toPrint)/2, 50);
  88. }
  89.  
  90. void drawBall(){
  91. //This function draws the ball
  92. //I thought these colors were pretty cool
  93. fill(255);
  94. strokeWeight(4);
  95. ellipse(ballX, ballY, BALL_DIAMETER, BALL_DIAMETER);
  96. }
  97.  
  98. void moveBall(){
  99. ballX += ballVX;
  100. ballY += ballVY;
  101. }
  102.  
  103. void setGraphicalVariables(){
  104. centreX = width/2;
  105. centreY = height/2;
  106. }
  107.  
  108. //~~~~Functions to Calculate~~~~\\
  109.  
  110.  
  111. void movePaddles(){
  112. moveLeftPaddle();
  113. moveRightPaddle();
  114. }
  115.  
  116. //Moving Paddles could not be condensed further due to nature of controls being different inputs.
  117. void moveLeftPaddle(){ //Left paddle controlled by UP and DOWN (inversed)
  118. if (keyPressed){
  119. if (keyCode == UP && lPaddleY > 0){
  120. lPaddleY -= PADDLE_SPEED; //moves paddle up (negative y)
  121. }
  122. else if (keyCode == DOWN && lPaddleY+PADDLE_HEIGHT <height){
  123. lPaddleY += PADDLE_SPEED; //moves paddle down (positive y)
  124. }
  125. }
  126. }
  127.  
  128. void moveRightPaddle(){
  129. if (mouseY-PADDLE_HEIGHT/2 <= 0){
  130. rPaddleY = 0;
  131. }
  132. else if (mouseY+PADDLE_HEIGHT/2 >=height){
  133. rPaddleY = height-PADDLE_HEIGHT; //moves paddle down (positive y)
  134. }
  135. else{
  136. rPaddleY = mouseY-PADDLE_HEIGHT/2; //moves paddle up (negative y)
  137. }
  138. }
  139.  
  140.  
  141. //Gets random angle in a circle, but not one that is too crazy up or down.
  142. void getAngle(){
  143.  
  144. if (lastPoint == -1){ //if right paddle scored
  145. angle = random(3*PI/4, 5*PI/4);
  146. }
  147. else if (lastPoint == 1){ //if left paddle scored
  148. angle = random(-QUARTER_PI/4, QUARTER_PI/4);
  149. }
  150. else{
  151.  
  152. boolean angleChecker = true; //creates a loop that only returns legal angles.
  153.  
  154. while(angleChecker){ //creates loop to get legal angle >|<
  155. angle = random(0,2*PI);
  156. println(angle);
  157.  
  158. if (abs(sin(angle)) < sin(QUARTER_PI)){ //makes sure the angle is not super high or low.
  159. angleChecker = false; //breaks loop when angle is legal.
  160. }
  161. }
  162. }
  163. }
  164.  
  165. void checkColissions(){
  166.  
  167. //Y axis colissions (top and bottom)
  168. if (ballY <= 0+BALL_DIAMETER/2 || ballY >= height-BALL_DIAMETER/2){
  169. ballVY = -ballVY; //changes ball Y velocity upon contact with floor and ceiling
  170. }
  171.  
  172. //collisions for paddles
  173.  
  174. //left paddle
  175. if (ballY >= lPaddleY && ballY < lPaddleY+PADDLE_HEIGHT){ //if ball is within paddle height
  176. if (ballX < lPaddleX+PADDLE_WIDTH+BALL_DIAMETER/2 && ballX > lPaddleX+BALL_DIAMETER/2){//and within paddle width
  177.  
  178. if (randomCollisions){
  179. angle = random(-PI/3, PI/3);
  180. }
  181. else{
  182. //collide based on where the ball is on paddle. Uses percent of paddle for angle range.
  183. angle = (2*THIRD_PI*((ballY-lPaddleY)/PADDLE_HEIGHT))-THIRD_PI;
  184. }
  185. checkAngles(1); //1 = multiply cosX by 1 for x (left going right)
  186. }
  187. }
  188.  
  189. if (ballY > rPaddleY && ballY < rPaddleY+PADDLE_HEIGHT){
  190. if (ballX > rPaddleX-BALL_DIAMETER/2 && ballX < rPaddleX){
  191.  
  192. if (randomCollisions){
  193. angle = random(-PI/3, PI/3);
  194. }
  195. else{
  196. angle = (2*THIRD_PI*((ballY-rPaddleY)/PADDLE_HEIGHT))-THIRD_PI;
  197. }
  198. checkAngles(-1);//-1 = multiply cosX by -1 for x (right going left)
  199. }
  200. }
  201. }
  202.  
  203. void checkAngles(){//non-collision (new ball)
  204. ballVX = ballSpeed * cos(angle);
  205. ballVY = ballSpeed * sin(angle);
  206. }
  207.  
  208. void checkAngles(int paddle){ //collision
  209.  
  210. if (ballSpeedIncreases){
  211. ballSpeed *= 1.125; //raises ball speed by an eighth.
  212. }
  213. ballVX = ballSpeed*cos(angle)*paddle; //if right paddle, COS is made negative
  214. ballVY = ballSpeed*sin(angle);
  215. }
  216.  
  217. void checkIfScored(){
  218. if (ballX < 0){ //OPTION: MOUSE SCORES
  219. mouseScore ++;
  220. lastPoint = 1; //value to decide which way to spawn the ball
  221. resetBall();
  222. }
  223. else if (ballX > width){ //OPTION: KEY SCORES
  224. keyScore ++;
  225. lastPoint = -1; //value to decide which way to spawn the ball
  226. resetBall();
  227. }
  228.  
  229. //stops game once scores reach 11.
  230. if (mouseScore == 11){
  231. endGame("Mouse");
  232. }
  233. if (keyScore == 11){
  234. endGame("Key");
  235. }
  236. }
  237.  
  238. //~~~~Functions to Debut~~~~\\
  239.  
  240. void keyPressed(){ //spawns new ball when space is pressed.
  241. if (keyCode == 32){
  242. resetBall();
  243. }
  244. }
  245.  
  246. //~~~~Functions to Reset~~~~\\
  247. void resetBall(){
  248.  
  249. //sets initial Ball position to the centre of canvas
  250. ballSpeed = MIN_BALL_SPEED; //resets the ball speed
  251. ballX = centreX; //dead centre
  252. ballY = centreY; //dead centre
  253. getAngle(); //calculates angle of new ball.
  254. checkAngles();
  255. }
  256.  
  257. void resetPaddles(){
  258. //resets both paddles to exactly the centre
  259. rPaddleY = (height-PADDLE_HEIGHT)/2;
  260. lPaddleY = (height-PADDLE_HEIGHT)/2;
  261.  
  262. lPaddleX = PADDLE_WIDTH;
  263. rPaddleX = width-(PADDLE_WIDTH*2);
  264. }
  265.  
  266. void endGame(String winner){ //winner display
  267. gameOver = true; //changes boolean to stop game loop in draw()
  268. textSize(32);
  269. text(winner + " Wins!", (centreX-textWidth(winner)), centreY); //String
  270. }
  271. void reset(){
  272. println(keyCode);
  273. if (keyCode == 32 && gameOver){
  274. resetBall();
  275. keyScore = 0;
  276. mouseScore = 0;
  277. gameOver = false;
  278. }
  279. }
  280.  
  281. void parameters(){
  282. //random colisions:
  283. randomCollisions = false;
  284. ballSpeedIncreases = true;
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement