Crenox

Pong in Java (CLASS)

May 27th, 2014
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. /*
  2. * This code is protected under the Gnu General Public License (Copyleft), 2005 by
  3. * IBM and the Computer Science Teachers Association. It may be freely
  4. * modified and redistributed under educational fair use.
  5. */
  6.  
  7. import csta.ibm.pong.Game;
  8.  
  9. public class Pong extends Game
  10. {
  11. // Add any state variables or object references here
  12. Ball b = new Ball();
  13. Paddle p1 = new Paddle();
  14. Paddle p2 = new Paddle();
  15.  
  16. private static int ballSize;
  17. private static int paddleHeight;
  18. private static int width;
  19.  
  20. private static int x, y;
  21.  
  22. /**
  23. * Fill in this method with code that tells the game what to do
  24. * before actual play begins
  25. */
  26. public void setup()
  27. {
  28. ballSize = 10;
  29. paddleHeight = 10;
  30. width = 100;
  31.  
  32. b.setColor(java.awt.Color.red);
  33. p1.setColor(java.awt.Color.green);
  34. p2.setColor(java.awt.Color.green);
  35.  
  36. b.setX(185);
  37. b.setY(150);
  38. p1.setX(0);
  39. p1.setY(145);
  40. p2.setX(375);
  41. p2.setY(145);
  42.  
  43. b.setSize(10, 10);
  44. p1.setSize(10, 50);
  45. p2.setSize(10, 50);
  46.  
  47. add(b);
  48. add(p1);
  49. add(p2);
  50.  
  51. setBackground(java.awt.Color.blue);
  52.  
  53. getContentPane().repaint();
  54. }
  55.  
  56. /**
  57. * Fill in this method with code that tells the playing field what to do
  58. * from one moment to the next
  59. */
  60. public void act()
  61. {
  62. if(ZKeyPressed())
  63. {
  64. p1.moveUp();
  65. }
  66. if(XKeyPressed())
  67. {
  68. p1.moveDown();
  69. }
  70. if(MKeyPressed())
  71. {
  72. p2.moveUp();
  73. }
  74. if(NKeyPressed())
  75. {
  76. p2.moveDown();
  77. }
  78. getContentPane().repaint();
  79. }
  80.  
  81. // Add any additional methods here
  82.  
  83. /**
  84. * This code has been provided for you, and should not be modified
  85. */
  86. public static void main(String[] args)
  87. {
  88. Pong p = new Pong();
  89. p.setVisible(true);
  90. p.initComponents();
  91. }
  92. }
  93.  
  94. -----------------------------------------------------------------------------------------------------------------------------------
  95. /*
  96. * This code is protected under the Gnu General Public License (Copyleft), 2005 by
  97. * IBM and the Computer Science Teachers Association. It may be freely
  98. * modified and redistributed under educational fair use.
  99. */
  100.  
  101. import csta.ibm.pong.GameObject;
  102.  
  103. public class Paddle extends GameObject
  104. {
  105. // Add any state variables here
  106.  
  107. /**
  108. * Fill in this method with code that describes the behavior
  109. * of a paddle from one moment to the next
  110. */
  111. public void act()
  112. {
  113.  
  114. }
  115.  
  116. // Add any additional methods here
  117. public void moveUp()
  118. {
  119. setY(5);
  120. }
  121.  
  122. public void moveDown()
  123. {
  124. setY(-5);
  125. }
  126. }
  127.  
  128. -----------------------------------------------------------------------------------------------------------------------------------
  129. /*
  130. * This code is protected under the Gnu General Public License (Copyleft), 2005 by
  131. * IBM and the Computer Science Teachers Association. It may be freely
  132. * modified and redistributed under educational fair use.
  133. */
  134.  
  135. import csta.ibm.pong.GameObject;
  136.  
  137. public class Ball extends GameObject
  138. {
  139. // Add any state variables here
  140. private static int xVelocity;
  141. private static int yVelocity;
  142.  
  143. private static int MAXX;
  144. private static int MAXY;
  145.  
  146. int randomNumber;
  147.  
  148. /**
  149. * Fill in this method with code that describes the behavior
  150. * of a ball from one moment to the next
  151. */
  152. public void act()
  153. {
  154.  
  155. }
  156.  
  157. // Add any additional methods here
  158. }
Advertisement
Add Comment
Please, Sign In to add comment