Advertisement
Guest User

Pong

a guest
Dec 28th, 2012
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. //we need the applet methods and the MouseMotionListener interface
  7. //(used for the human controlled paddle)
  8.  
  9. public class pongMain extends Applet implements MouseMotionListener,
  10. ActionListener
  11. {
  12. //declare an instance of the ball and two paddles
  13. Ball ball;
  14. PaddleLeft pLeft;
  15. PaddleRight pRight;
  16. // a font used to display the score
  17. Font newFont = new Font("sansserif", Font.BOLD, 20);
  18. //The image is going to be double buffered to avoid flicker
  19. Graphics bufferGraphics;
  20. //the Image will contain everything drawn on bufferGraphics
  21. Image offscreen;
  22. //variables used to set the width and height of the applet
  23. final int WIDTH = 500, HEIGHT = 300;
  24. //variable used to record the time the game has proceeded to
  25. //to tell how long the player has lasted
  26. long currentTime;
  27.  
  28. //I am going to use a timer to do a certain list of tasks
  29. //every 15 milliseconds (67 FPS)
  30. Timer time = new Timer(15, this);
  31.  
  32. public void init()
  33. {
  34. //set the applet to be 500*300
  35. setSize(WIDTH, HEIGHT);
  36. //we now instantiate our ball and two paddles
  37. ball = new Ball();
  38. pLeft = new PaddleLeft();
  39. //pRight is set to current ball position -35 because it is 70 pixels long
  40. pRight = new PaddleRight(ball.getY() - 35);
  41.  
  42. //mouseMotionListener allows the player to control their paddle
  43. addMouseMotionListener(this);
  44. setBackground(Color.black);
  45. //create offscreen image to draw on
  46. offscreen = createImage(WIDTH, HEIGHT);
  47. bufferGraphics = offscreen.getGraphics();
  48.  
  49. time.start();
  50.  
  51. }
  52.  
  53. //every 15 milliseconds the timer triggers the actionPerformed method
  54. public void actionPerformed(ActionEvent arg0)
  55. {
  56. if(pRight.getScore() == 10 || pLeft.getScore() == 10)
  57. {
  58. //after the game needs to end we stop the timer,
  59. //and calculate how long the player lasted
  60. //(current time - initial time)
  61. time.stop();
  62. currentTime = System.currentTimeMillis() - currentTime;
  63. repaint();
  64. }
  65. else
  66. {
  67. //move the ball
  68. ball.move();
  69. //lines the computer paddle up
  70. pRight.setPos(ball.getY() - 35);
  71. //checks the ball for a collision
  72. checkCollision();
  73. //repaints the applet
  74. repaint();
  75. }
  76. }
  77.  
  78. public void checkCollision()
  79. {
  80. //remember the ball is 10*10, x and y is the top left corner
  81. //if the top left corner y pos is 0 or 290 we reverse its direction
  82. //by multiplying ball.dy by -1
  83. if(ball.getY() <= 0 || ball.getY() >= 290)
  84. {
  85. ball.dy = (ball.dy * -1);
  86. }
  87.  
  88. //if the ball is at the right hand edge of the human paddle
  89. //and the boolean method hitPaddle() is true, then we reverse dx
  90. if((ball.getX() == 40) && hitLeftPaddle())
  91. {
  92. ball.dx = (ball.dx * -1);
  93. }
  94.  
  95. //the computer paddle can't miss so if the ball is at its left edge
  96. //it must rebound
  97. if(ball.getX() == 460 && hitRightPaddle())
  98. {
  99. ball.dx = (ball.dx * -1);
  100. }
  101.  
  102. //If the ball reaches the edge of the applet on the human side,
  103. //missing the paddle reset the ball and icrement the score
  104. if(ball.getX() == 0)
  105. {
  106. pRight.setScore(pRight.getScore() + 1);
  107. ball.reset();
  108. }
  109.  
  110. if(ball.getX() == WIDTH)
  111. {
  112. pLeft.setScore(pLeft.getScore() + 1);
  113. ball.reset();
  114. }
  115. }
  116.  
  117. public boolean hitLeftPaddle()
  118. {
  119. boolean didHit = false;
  120. //this just checks if the ball has hit the paddle
  121. if((pLeft.getPos() - 10) <= ball.getY() && (pLeft.getPos() + 70) > ball.getY())
  122. {
  123. if(pLeft.getPos() + 35 < ball.getY())
  124. {
  125. ball.dy = 5;
  126. }
  127. else
  128. {
  129. ball.dy = -5;
  130. }
  131. didHit = true;
  132. }
  133. return didHit;
  134. }
  135.  
  136. public boolean hitRightPaddle()
  137. {
  138. boolean didHit = false;
  139. //this just checks if the ball has hit the paddle
  140. if((pRight.getPos() - 10) <= ball.getY() && (pRight.getPos() + 70) > ball.getY())
  141. {
  142. if(pRight.getPos() + 35 < ball.getY())
  143. {
  144. ball.dy = 5;
  145. }
  146. else
  147. {
  148. ball.dy = -5;
  149. }
  150. didHit = true;
  151. }
  152. return didHit;
  153. }
  154.  
  155. public void paint(Graphics g)
  156. {
  157. //instead of using the typical graphics, we are going to
  158. //use bufferGraphics (which we declared at the beginning
  159. //of the class) to draw onto our off-screen image
  160.  
  161. // first clear off the image
  162. bufferGraphics.clearRect(0,0,WIDTH,HEIGHT);
  163.  
  164. //Now draw the paddles in white
  165. bufferGraphics.setColor(Color.white);
  166. //xPos never changes, yPos does. make the paddles 10*70
  167. //left
  168. bufferGraphics.fillRect(pLeft.XPOS,pLeft.getPos(),10,70);
  169. //Right
  170. bufferGraphics.fillRect(pRight.XPOS, pRight.getPos(),10,70);
  171.  
  172. //this draws our mid court lines and scores in grey
  173. bufferGraphics.setColor(Color.lightGray);
  174. bufferGraphics.setFont(newFont);
  175. //show players hopeless circumstance
  176. bufferGraphics.drawString(" " + pLeft.getScore(), 150, 15);
  177. //get the score from paddleright
  178. bufferGraphics.drawString(" "+ pRight.getScore(),300,15);
  179. //mid court divider
  180. //bufferGraphics.fillRect(240,0,20,300);
  181.  
  182. //Remember, we painted one last time after the computer won
  183. if(pRight.getScore() == 10)
  184. {
  185. bufferGraphics.drawString("You LOSE",40,150);
  186. }
  187.  
  188. if(pLeft.getScore() == 10)
  189. {
  190. bufferGraphics.drawString("You WIN",40,150);
  191. }
  192.  
  193. //draw the ball
  194. bufferGraphics.setColor(Color.white);
  195. bufferGraphics.fillRect(ball.getX(),ball.getY(),10,10);
  196. //finally draw the offscreen image to the applet
  197. g.drawImage(offscreen,0,0,this);
  198.  
  199. //this line makes sure all the monitors are up to date before proceeding
  200. Toolkit.getDefaultToolkit().sync();
  201. }
  202.  
  203. public void update(Graphics g)
  204. {
  205. paint(g);
  206. }
  207.  
  208. public void mouseMoved(MouseEvent evt)
  209. {
  210. pLeft.setPos(evt.getY() -35);
  211. }
  212.  
  213. public void mouseDragged(MouseEvent evt)
  214. {
  215. //this is placeholder
  216. }
  217.  
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement