Advertisement
nawamkihafahd

Untitled

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