SHARE
TWEET

Untitled

a guest Feb 15th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //pong game was created by joel comeau 7873238 for assingment 2 of comp 1010
  3.  
  4.  
  5.  int x; //x coordinate
  6.  int y; //y coordinate
  7.  int w; //width
  8.  int h; //height
  9.  int speedX; //speed variables
  10.  int speedY;
  11.  int paddleXL;//padle variables
  12.  int paddleYL;
  13.  int paddleWidth;
  14.  int paddleHeight;
  15.  int paddleSpeed;
  16.  int paddleXR;
  17.  int  paddleYR;
  18.  boolean downL;
  19.  boolean downR;
  20.  boolean upL;
  21.  boolean upR;
  22. //color for the paddles
  23.  
  24. int paddleColorR = color (0,40,255);
  25. int paddleColorL = color (0,40,255);
  26. //score
  27. int scoreL = 0;
  28. int scoreR = 0;
  29. //final win score
  30. int winScore = 10;
  31.  
  32. //Sets the game and window size up, along with the background and other important things
  33. //happens only once; at the beginning of the game
  34.  
  35. void setup() {
  36.   //this sets the window size
  37.   size(650,500);
  38.   //the x,y,w,h get the ball to begin at the center of the game
  39.   x = width/2;
  40.   y = height/2;
  41.   w = 35;
  42.   h = 35;
  43.   //this allows the ball to bounce
  44.   speedX = 2;
  45.   speedY = 2;
  46.   //this creates a text
  47.   textSize(30);
  48.   textAlign(CENTER, CENTER); //this centers the height and width of the text
  49.   rectMode(CENTER); //this allows both paddles to be centered
  50.   //This sets the paddles to a certain place on the window, and makes them visible
  51.   paddleXL = 20;
  52.   paddleYL = height/2;
  53.   paddleXR = width-40;
  54.   paddleYR = height/2;
  55.   paddleWidth = 15;
  56.   paddleHeight = 100;
  57.   paddleSpeed = 5;
  58.  
  59. }
  60.  
  61. //Draws the canvas for the game
  62.  
  63. void draw() {
  64.   background(0,22,139);
  65.   drawCircle();
  66.   moveCircle();
  67.   bounceOff();
  68.   drawPaddles();
  69.   movePaddle();
  70.   restrictPaddle();
  71.   contactPaddle();
  72.   drawScore();
  73.   gameOver();
  74. }
  75.  
  76.  void drawScore() {
  77.  textSize(20);
  78.  fill (255);
  79.  String toPrint = "PLAYER 2: "+scoreR;text(toPrint, width/4-textWidth(toPrint)/2, 50);
  80.  toPrint = "PLAYER 1: "+scoreL;
  81.  text(toPrint, width*3/4-textWidth(toPrint)/2, 50);
  82.  
  83.  }
  84.  
  85. //creates paddles
  86.  
  87. void drawPaddles() {
  88.   //left paddle
  89.   fill(paddleColorL);
  90.   rect(paddleXL, paddleYL, paddleWidth, paddleHeight);
  91.  //right paddle
  92.   fill(paddleColorR);
  93.   rect(paddleXR, paddleYR, paddleWidth, paddleHeight);
  94. }
  95.  
  96. //Moves the paddles
  97.  
  98. void movePaddle() {
  99.  
  100.   if (downL) {
  101.     paddleYL = paddleYL + paddleSpeed;
  102.   }
  103.   if (upL) {
  104.     paddleYL = paddleYL - paddleSpeed;
  105.   }
  106.   if (downR) {
  107.     paddleYR = paddleYR + paddleSpeed;
  108.   }
  109.   if (upR) {
  110.     paddleYR = paddleYR - paddleSpeed;
  111.   }
  112. }
  113.  
  114. //detects when a paddle has hit the wall
  115.  
  116. void restrictPaddle() {
  117.   if (paddleYL - paddleHeight/2 < 0) {
  118.     paddleYL = paddleYL + paddleSpeed;
  119.   }
  120.   if (paddleYL + paddleHeight/2 > height) {
  121.     paddleYL = paddleYL - paddleSpeed;
  122.   }
  123.   if (paddleYR - paddleHeight/2 < 0) {
  124.     paddleYR = paddleYR + paddleSpeed;
  125.   }
  126.   if (paddleYR + paddleHeight/2 > height) {
  127.     paddleYR = paddleYR - paddleSpeed;
  128.   }
  129. }
  130.  
  131. //This detects left and right collision; respectively
  132.  
  133. void contactPaddle() {
  134.   if (x - w/2 < paddleXL + paddleWidth/2 && y - h/2 < paddleYL + paddleHeight/2 && y + h/2 > paddleYL - paddleHeight/2 ) {
  135.     if (speedX < 0) {
  136.       speedX = -speedX*1;
  137.     }
  138.   }
  139.   else if (x + w/2 > paddleXR - paddleWidth/2 && y - h/2 < paddleYR + paddleHeight/2 && y + h/2 > paddleYR - paddleHeight/2 ) {
  140.     if (speedX > 0) {
  141.       speedX = -speedX*1;
  142.     }
  143.   }
  144. }
  145.  
  146. //creates circle
  147. void drawCircle() {
  148.   fill(255);
  149.   ellipse(x, y, w, h);
  150. }
  151. //this allows the circle to bounce
  152. void moveCircle() {  
  153.   x = x + speedX*2;
  154.   y = y + speedY*2;
  155. }
  156.  
  157. //This detects if the ball has bounced or not
  158.  
  159. void bounceOff() {
  160.  if ( x > width - w/2) {
  161.     setup();
  162.     speedX = -speedX;
  163.     scoreL = scoreL + 1;
  164.   } else if ( x < 0 + w/2) {
  165.     setup();
  166.     scoreR = scoreR + 1;
  167.   }
  168.   if ( y > height - h/2) {
  169.     speedY = -speedY;
  170.   } else if ( y < 0 + h/2) {
  171.     speedY = -speedY;
  172.   }
  173. }
  174.  
  175.  
  176. //This determines who wins
  177.  
  178. void gameOver() {
  179.   if(scoreL == winScore) {
  180.     gameOverPage("PLAYER 1 wins!", paddleColorL);
  181.   }
  182.   if(scoreR == winScore) {
  183.     gameOverPage("PLAYER 2 wins!", paddleColorR);
  184.   }
  185. }
  186.  
  187. //This stops the ball from bouncing around, gets the user to know that the game is over
  188.  
  189. void gameOverPage(String text, color c) {
  190.   speedX = 0;
  191.   speedY = 0;
  192.   fill(255);
  193.   text("Game over", width/2, height/3 - 40);
  194.   text("Click to play again", width/2, height/3 + 40);
  195.   fill(c);
  196.   text(text, width/2, height/3);
  197.   //this gets the user to click on the screen to play again
  198.   if(mousePressed) {
  199.   //this sets the score to zero
  200.     scoreR = 0;
  201.     scoreL = 0;
  202.     //this gets the speed to a 1
  203.     speedX = 1;
  204.     speedY = 1;
  205.   }
  206. }
  207.  
  208. //this gets the user to press a key and allow the paddle(s) to move
  209.  //the second set assumes if caps lock is on for s and w
  210. void keyPressed() {
  211. if (key == 'w' || key == 'W') {
  212.     upL = true;
  213.   }
  214.   if (key == 's' || key == 'S') {
  215.     downL = true;
  216.   }
  217.   if (keyCode == UP) {
  218.     upR = true;
  219.   }
  220.   if (keyCode == DOWN) {
  221.     downR = true;
  222.   }
  223. }
  224.  
  225. //this creates a false key interaction
  226.  
  227. void keyReleased() {
  228.   if (key == 'w' || key == 'W') {
  229.     upL = false;
  230.   }
  231.   if (key == 's' || key == 'S') {
  232.     downL = false;
  233.   }
  234.   if (keyCode == UP) {
  235.     upR = false;
  236.   }
  237.   if (keyCode == DOWN) {
  238.     downR = false;
  239.   }
  240. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top