Advertisement
Guest User

Untitled

a guest
Sep 6th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1.  
  2. import java.awt.Font;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.util.Random;
  10.  
  11. import javax.swing.JPanel;
  12. import javax.swing.Timer;
  13.  
  14. public class PongPanel extends JPanel implements ActionListener, KeyListener {
  15.  
  16. private Game game;
  17.  
  18. private boolean running = true;
  19.  
  20. private boolean upPressed = false, downPressed = false, wPressed = false, sPressed = false;
  21.  
  22. private int ballX = 400, ballY = 200;
  23. private int diameter = 20;
  24. private int ballDeltaX = -5, ballDeltaY = 5;
  25.  
  26. private int playerOneX = 25, playerOneY = 220;
  27. private int playerOneWidth = 20, playerOneHeight = 60;
  28.  
  29. private int playerTwoX = 750, playerTwoY = 220;
  30. private int playerTwoWidth = 20, playerTwoHeight = 60;
  31.  
  32. private int paddleSpeed = 10;
  33.  
  34. Random random = new Random();
  35. private final float hue = random.nextFloat();
  36. private final float saturation = 0.9f;
  37. private float luminance = 1.0f;
  38.  
  39. private int deaths;
  40. private int rallyScore = 0;
  41. private int rallyInterval = 5;
  42.  
  43. private long currentTime = System.currentTimeMillis();
  44. public static Thread thread;
  45.  
  46. private int[] Colors = new int[getWidth() * getHeight()];
  47.  
  48. public PongPanel() {
  49. Random rand = new Random();
  50. setBackground(new Color(rand.nextInt(0xFF4AF6)));
  51.  
  52. setFocusable(true);
  53. addKeyListener(this);
  54.  
  55. Timer timer = new Timer(1000 / 60, this);
  56. timer.start();
  57. }
  58.  
  59. public void actionPerformed(ActionEvent e) {
  60. step();
  61. }
  62.  
  63. public void step() {
  64.  
  65. if (upPressed) {
  66. if (playerTwoY - paddleSpeed > 0) {
  67. playerTwoY -= paddleSpeed;
  68. }
  69. }
  70. if (downPressed) {
  71. if (playerTwoY + paddleSpeed + playerTwoHeight < getHeight()) {
  72. playerTwoY += paddleSpeed;
  73. }
  74. }
  75.  
  76. if (wPressed) {
  77. if (playerOneY - paddleSpeed > 0) {
  78. playerOneY -= paddleSpeed;
  79. }
  80. }
  81. if (sPressed) {
  82. if (playerOneY + paddleSpeed + playerOneHeight < getHeight()) {
  83. playerOneY += paddleSpeed;
  84. }
  85. }
  86.  
  87. int nextBallLeft = ballX + ballDeltaX;
  88. int nextBallRight = ballX + diameter + ballDeltaX;
  89. int nextBallTop = ballY + ballDeltaY;
  90. int nextBallBottom = ballY + diameter + ballDeltaY;
  91.  
  92. int playerOneRight = playerOneX + playerOneWidth;
  93. int playerOneTop = playerOneY;
  94. int playerOneBottom = playerOneY + playerOneHeight;
  95.  
  96. float playerTwoLeft = playerTwoX;
  97. float playerTwoTop = playerTwoY;
  98. float playerTwoBottom = playerTwoY + playerTwoHeight;
  99.  
  100. if (nextBallTop < 0 || nextBallBottom > getHeight()) {
  101. ballDeltaY *= -1;
  102.  
  103. }
  104.  
  105. if (nextBallLeft < playerOneRight) {
  106.  
  107. if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) {
  108.  
  109. System.out.println("You died. Your rally was " + rallyScore + ".");
  110. deaths++;
  111. ballX = 400;
  112. ballY = 200;
  113. } else {
  114. rallyScore++;
  115. ballDeltaX *= -1;
  116. }
  117. }
  118.  
  119. if (nextBallRight > playerTwoLeft) {
  120.  
  121. if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) {
  122.  
  123. System.out.println("You died. Your rally was " + rallyScore + ".");
  124.  
  125. deaths++;
  126.  
  127. ballX = 400;
  128. ballY = 200;
  129. } else {
  130. rallyScore++;
  131. ballDeltaX *= -1;
  132. }
  133. }
  134.  
  135. ballX += ballDeltaX;
  136. ballY += ballDeltaY;
  137.  
  138. if (rallyScore % rallyInterval == 1) {
  139. ballDeltaX = ballDeltaX - 2;
  140. ballDeltaY = ballDeltaY + 2;
  141. paddleSpeed = paddleSpeed + 2;
  142. }
  143.  
  144. repaint();
  145. }
  146.  
  147. public synchronized void Score() {
  148.  
  149. }
  150.  
  151. public void RandomColor() {
  152.  
  153. }
  154.  
  155. public void paintComponent(Graphics g) {
  156.  
  157. super.paintComponent(g);
  158. Random rand = new Random();
  159.  
  160. for (int lineY = 0; lineY < getHeight(); lineY += 50) {
  161. g.setColor(new Color(rand.nextInt(0xFF4AF6)));
  162. g.fillRoundRect(400, lineY, 20, 800, 0, 0);
  163. }
  164.  
  165. g.setColor(Color.WHITE);
  166. g.fillRoundRect(ballX, ballY, 25, 25, 5, 25);
  167. for (int tick = 0; tick > getHeight(); tick--) {
  168.  
  169. g.setColor(new Color(rand.nextInt(0xFF4AF6)));
  170. }
  171. g.setColor(new Color(rand.nextInt(0xFF4AF6)));
  172. g.fillRoundRect(playerOneX, playerOneY, playerOneWidth,
  173. playerOneHeight, 50, 5);
  174.  
  175. g.fillRoundRect(playerTwoX, playerTwoY, playerTwoWidth,
  176. playerTwoHeight, 50, 5);
  177.  
  178. g.setColor(Color.WHITE);
  179. g.fillRoundRect(335, 180, 150, 150, 50, 50);
  180. g.setFont(new Font(Font.DIALOG, Font.BOLD, 150));
  181. String rally = " " + rallyScore;
  182. g.setColor(new Color(0xFF2172));
  183. g.drawString(rally, 328, 310);
  184. g.dispose();
  185. }
  186.  
  187. public void keyTyped(KeyEvent e) {
  188. }
  189.  
  190. public void keyPressed(KeyEvent e) {
  191. if (e.getKeyCode() == KeyEvent.VK_UP) {
  192. upPressed = true;
  193. } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
  194. downPressed = true;
  195. } else if (e.getKeyCode() == KeyEvent.VK_W) {
  196. wPressed = true;
  197. } else if (e.getKeyCode() == KeyEvent.VK_S) {
  198. sPressed = true;
  199. }
  200. }
  201.  
  202. public void keyReleased(KeyEvent e) {
  203. if (e.getKeyCode() == KeyEvent.VK_UP) {
  204. upPressed = false;
  205. } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
  206. downPressed = false;
  207. } else if (e.getKeyCode() == KeyEvent.VK_W) {
  208. wPressed = false;
  209. } else if (e.getKeyCode() == KeyEvent.VK_S) {
  210. sPressed = false;
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement