ilham_syamsuddin

Untitled

Dec 1st, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. //package pong;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12. import java.util.Random;
  13.  
  14. import javax.swing.JFrame;
  15. import javax.swing.Timer;
  16.  
  17. public class Pong implements ActionListener, KeyListener
  18. {
  19.  
  20. public static Pong pong;
  21.  
  22. public int width = 700, height = 700;
  23.  
  24. public Renderer renderer;
  25.  
  26. public Paddle player1;
  27.  
  28. public Paddle player2;
  29.  
  30. public Ball ball;
  31.  
  32. public boolean bot = false, selectingDifficulty;
  33.  
  34. public boolean w, s, up, down;
  35.  
  36. public int gameStatus = 0, scoreLimit = 7, playerWon; //0 = Menu, 1 = Paused, 2 = Playing, 3 = Over
  37.  
  38. public int botDifficulty, botMoves, botCooldown = 0;
  39.  
  40. public Random random;
  41.  
  42. public JFrame jframe;
  43.  
  44. public Pong()
  45. {
  46. Timer timer = new Timer(20, this);
  47. random = new Random();
  48.  
  49. jframe = new JFrame("Pong");
  50.  
  51. renderer = new Renderer();
  52.  
  53. jframe.setSize(width + 15, height + 35);
  54. jframe.setVisible(true);
  55. jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  56. jframe.add(renderer);
  57. jframe.addKeyListener(this);
  58.  
  59. timer.start();
  60. }
  61.  
  62. public void start()
  63. {
  64. gameStatus = 2;
  65. player1 = new Paddle(this, 1);
  66. player2 = new Paddle(this, 2);
  67. ball = new Ball(this);
  68. }
  69.  
  70. public void update()
  71. {
  72. if (player1.score >= scoreLimit)
  73. {
  74. playerWon = 1;
  75. gameStatus = 3;
  76. }
  77.  
  78. if (player2.score >= scoreLimit)
  79. {
  80. gameStatus = 3;
  81. playerWon = 2;
  82. }
  83.  
  84. if (w)
  85. {
  86. player1.move(true);
  87. }
  88. if (s)
  89. {
  90. player1.move(false);
  91. }
  92.  
  93. if (!bot)
  94. {
  95. if (up)
  96. {
  97. player2.move(true);
  98. }
  99. if (down)
  100. {
  101. player2.move(false);
  102. }
  103. }
  104. else
  105. {
  106. if (botCooldown > 0)
  107. {
  108. botCooldown--;
  109.  
  110. if (botCooldown == 0)
  111. {
  112. botMoves = 0;
  113. }
  114. }
  115.  
  116. if (botMoves < 10)
  117. {
  118. if (player2.y + player2.height / 2 < ball.y)
  119. {
  120. player2.move(false);
  121. botMoves++;
  122. }
  123.  
  124. if (player2.y + player2.height / 2 > ball.y)
  125. {
  126. player2.move(true);
  127. botMoves++;
  128. }
  129.  
  130. if (botDifficulty == 0)
  131. {
  132. botCooldown = 20;
  133. }
  134. if (botDifficulty == 1)
  135. {
  136. botCooldown = 15;
  137. }
  138. if (botDifficulty == 2)
  139. {
  140. botCooldown = 10;
  141. }
  142. }
  143. }
  144.  
  145. ball.update(player1, player2);
  146. }
  147.  
  148. public void render(Graphics2D g)
  149. {
  150. g.setColor(Color.BLACK);
  151. g.fillRect(0, 0, width, height);
  152. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  153.  
  154. if (gameStatus == 0)
  155. {
  156. g.setColor(Color.WHITE);
  157. g.setFont(new Font("Arial", 1, 50));
  158.  
  159. g.drawString("PONG", width / 2 - 75, 50);
  160.  
  161. if (!selectingDifficulty)
  162. {
  163. g.setFont(new Font("Arial", 1, 30));
  164.  
  165. g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
  166. g.drawString("Press Shift to Play with Bot", width / 2 - 200, height / 2 + 25);
  167. g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
  168. }
  169. }
  170.  
  171. if (selectingDifficulty)
  172. {
  173. String string = botDifficulty == 0 ? "Easy" : (botDifficulty == 1 ? "Medium" : "Hard");
  174.  
  175. g.setFont(new Font("Arial", 1, 30));
  176.  
  177. g.drawString("<< Bot Difficulty: " + string + " >>", width / 2 - 180, height / 2 - 25);
  178. g.drawString("Press Space to Play", width / 2 - 150, height / 2 + 25);
  179. }
  180.  
  181. if (gameStatus == 1)
  182. {
  183. g.setColor(Color.WHITE);
  184. g.setFont(new Font("Arial", 1, 50));
  185. g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
  186. }
  187.  
  188. if (gameStatus == 1 || gameStatus == 2)
  189. {
  190. g.setColor(Color.WHITE);
  191.  
  192. g.setStroke(new BasicStroke(5f));
  193.  
  194. g.drawLine(width / 2, 0, width / 2, height);
  195.  
  196. g.setStroke(new BasicStroke(2f));
  197.  
  198. g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
  199.  
  200. g.setFont(new Font("Arial", 1, 50));
  201.  
  202. g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
  203. g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
  204.  
  205. player1.render(g);
  206. player2.render(g);
  207. ball.render(g);
  208. }
  209.  
  210. if (gameStatus == 3)
  211. {
  212. g.setColor(Color.WHITE);
  213. g.setFont(new Font("Arial", 1, 50));
  214.  
  215. g.drawString("PONG", width / 2 - 75, 50);
  216.  
  217. if (bot && playerWon == 2)
  218. {
  219. g.drawString("The Bot Wins!", width / 2 - 170, 200);
  220. }
  221. else
  222. {
  223. g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
  224. }
  225.  
  226. g.setFont(new Font("Arial", 1, 30));
  227.  
  228. g.drawString("Press Space to Play Again", width / 2 - 185, height / 2 - 25);
  229. g.drawString("Press ESC for Menu", width / 2 - 140, height / 2 + 25);
  230. }
  231. }
  232.  
  233. @Override
  234. public void actionPerformed(ActionEvent e)
  235. {
  236. if (gameStatus == 2)
  237. {
  238. update();
  239. }
  240.  
  241. renderer.repaint();
  242. }
  243.  
  244. public static void main(String[] args)
  245. {
  246. pong = new Pong();
  247. }
  248.  
  249. @Override
  250. public void keyPressed(KeyEvent e)
  251. {
  252. int id = e.getKeyCode();
  253.  
  254. if (id == KeyEvent.VK_W)
  255. {
  256. w = true;
  257. }
  258. else if (id == KeyEvent.VK_S)
  259. {
  260. s = true;
  261. }
  262. else if (id == KeyEvent.VK_UP)
  263. {
  264. up = true;
  265. }
  266. else if (id == KeyEvent.VK_DOWN)
  267. {
  268. down = true;
  269. }
  270. else if (id == KeyEvent.VK_RIGHT)
  271. {
  272. if (selectingDifficulty)
  273. {
  274. if (botDifficulty < 2)
  275. {
  276. botDifficulty++;
  277. }
  278. else
  279. {
  280. botDifficulty = 0;
  281. }
  282. }
  283. else if (gameStatus == 0)
  284. {
  285. scoreLimit++;
  286. }
  287. }
  288. else if (id == KeyEvent.VK_LEFT)
  289. {
  290. if (selectingDifficulty)
  291. {
  292. if (botDifficulty > 0)
  293. {
  294. botDifficulty--;
  295. }
  296. else
  297. {
  298. botDifficulty = 2;
  299. }
  300. }
  301. else if (gameStatus == 0 && scoreLimit > 1)
  302. {
  303. scoreLimit--;
  304. }
  305. }
  306. else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3))
  307. {
  308. gameStatus = 0;
  309. }
  310. else if (id == KeyEvent.VK_SHIFT && gameStatus == 0)
  311. {
  312. bot = true;
  313. selectingDifficulty = true;
  314. }
  315. else if (id == KeyEvent.VK_SPACE)
  316. {
  317. if (gameStatus == 0 || gameStatus == 3)
  318. {
  319. if (!selectingDifficulty)
  320. {
  321. bot = false;
  322. }
  323. else
  324. {
  325. selectingDifficulty = false;
  326. }
  327.  
  328. start();
  329. }
  330. else if (gameStatus == 1)
  331. {
  332. gameStatus = 2;
  333. }
  334. else if (gameStatus == 2)
  335. {
  336. gameStatus = 1;
  337. }
  338. }
  339. }
  340.  
  341. @Override
  342. public void keyReleased(KeyEvent e)
  343. {
  344. int id = e.getKeyCode();
  345.  
  346. if (id == KeyEvent.VK_W)
  347. {
  348. w = false;
  349. }
  350. else if (id == KeyEvent.VK_S)
  351. {
  352. s = false;
  353. }
  354. else if (id == KeyEvent.VK_UP)
  355. {
  356. up = false;
  357. }
  358. else if (id == KeyEvent.VK_DOWN)
  359. {
  360. down = false;
  361. }
  362. }
  363.  
  364. @Override
  365. public void keyTyped(KeyEvent e)
  366. {
  367.  
  368. }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment