Advertisement
Crenox

Jaryt Pong Java Tutorial Code

Jul 1st, 2015
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.63 KB | None | 0 0
  1. import java.awt.BasicStroke;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.RenderingHints;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.event.KeyListener;
  10. import java.util.Random;
  11. import javax.swing.JFrame;
  12. import javax.swing.Timer;
  13.  
  14. public class Pong implements ActionListener, KeyListener
  15. {
  16. public static Pong pong;
  17. public Renderer renderer;
  18. public Paddle player1;
  19. public Paddle player2;
  20. public Ball ball;
  21. public Random random;
  22.  
  23. // Game Status: 0 = Stopped, 1 = Paused, 2 = Playing, 3 = Over
  24. public int width = 700, height = 700, scoreLimit, playerWon, gameStatus = 0, botCooldown = 0, botMoves, botDifficulty;;
  25. public boolean bot = false, selectingDifficulty, w, s, up, down;
  26.  
  27. public Pong()
  28. {
  29. Timer timer = new Timer(20, this);
  30. JFrame jframe = new JFrame("Pong");
  31. random = new Random();
  32. renderer = new Renderer();
  33.  
  34. jframe.setSize(width + 15, height + 35);
  35. jframe.setVisible(true);
  36. jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37. jframe.setLocationRelativeTo(null);
  38. jframe.setTitle("Pong");
  39. jframe.add(renderer);
  40. jframe.addKeyListener(this);
  41.  
  42. timer.start();
  43. }
  44.  
  45. public void start()
  46. {
  47. gameStatus = 2;
  48. player1 = new Paddle(this, 1);
  49. player2 = new Paddle(this, 2);
  50. ball = new Ball(this);
  51. }
  52.  
  53. public void update()
  54. {
  55. if(player1.score >= scoreLimit)
  56. {
  57. playerWon = 1;
  58. gameStatus = 3;
  59. }
  60.  
  61. if (player2.score >= scoreLimit)
  62. {
  63. gameStatus = 3;
  64. playerWon = 2;
  65. }
  66.  
  67. if (w)
  68. {
  69. player1.move(true);
  70. }
  71.  
  72. if (s)
  73. {
  74. player1.move(false);
  75. }
  76.  
  77. if (!bot)
  78. {
  79. if (up)
  80. {
  81. player2.move(true);
  82. }
  83. if (down)
  84. {
  85. player2.move(false);
  86. }
  87. }
  88. else
  89. {
  90. if(botCooldown > 0)
  91. {
  92. botCooldown--;
  93. if (botCooldown == 0)
  94. {
  95. botMoves = 0;
  96. }
  97. }
  98.  
  99. if (botMoves < 10)
  100. {
  101. if (player2.y + player2.height / 2 < ball.y)
  102. {
  103. player2.move(false);
  104. botMoves++;
  105. }
  106.  
  107. if (player2.y + player2.height / 2 > ball.y)
  108. {
  109. player2.move(true);
  110. botMoves++;
  111. }
  112.  
  113. // difficulty levels
  114. if (botDifficulty == 0)
  115. {
  116. botCooldown = 20;
  117. }
  118. if (botDifficulty == 1)
  119. {
  120. botCooldown = 15;
  121. }
  122. if (botDifficulty == 2)
  123. {
  124. botCooldown = 10;
  125. }
  126. }
  127. }
  128.  
  129. ball.update(player1, player2);
  130. }
  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.  
  138. if (gameStatus == 0)
  139. {
  140. g.setColor(Color.WHITE);
  141. g.setFont(new Font("Arial", 1, 50));
  142.  
  143. g.drawString("PONG", width / 2 - 75, 50);
  144.  
  145. if (!selectingDifficulty)
  146. {
  147. g.setFont(new Font("Arial", 1, 30));
  148.  
  149. g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
  150. g.drawString("Press Shift to Play with Bot", width / 2 - 200, height / 2 + 25);
  151. g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
  152. }
  153. }
  154.  
  155. if (selectingDifficulty)
  156. {
  157. String string = botDifficulty == 0 ? "Easy" : (botDifficulty == 1 ? "Medium" : "Hard");
  158.  
  159. g.setFont(new Font("Arial", 1, 30));
  160.  
  161. g.drawString("<< Bot Difficulty: " + string + " >>", width / 2 - 180, height / 2 - 25);
  162. g.drawString("Press Space to Play", width / 2 - 150, height / 2 + 25);
  163. }
  164.  
  165. if (gameStatus == 1)
  166. {
  167. g.setColor(Color.WHITE);
  168. g.setFont(new Font("Arial", 1, 50));
  169. g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
  170. }
  171.  
  172. if (gameStatus == 1 || gameStatus == 2)
  173. {
  174. g.setColor(Color.WHITE);
  175.  
  176. g.setStroke(new BasicStroke(5f));
  177.  
  178. g.drawLine(width / 2, 0, width / 2, height);
  179.  
  180. g.setStroke(new BasicStroke(2f));
  181.  
  182. g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
  183.  
  184. g.setFont(new Font("Arial", 1, 50));
  185.  
  186. g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
  187. g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
  188.  
  189. player1.render(g);
  190. player2.render(g);
  191. ball.render(g);
  192. }
  193.  
  194. if (gameStatus == 3)
  195. {
  196. g.setColor(Color.WHITE);
  197. g.setFont(new Font("Arial", 1, 50));
  198.  
  199. g.drawString("PONG", width / 2 - 75, 50);
  200.  
  201. if (bot && playerWon == 2)
  202. {
  203. g.drawString("The Bot Wins!", width / 2 - 170, 200);
  204. }
  205. else
  206. {
  207. g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
  208. }
  209.  
  210. g.setFont(new Font("Arial", 1, 30));
  211.  
  212. g.drawString("Press Space to Play Again", width / 2 - 185, height / 2 - 25);
  213. g.drawString("Press ESC for Menu", width / 2 - 140, height / 2 + 25);
  214. }
  215. }
  216.  
  217. @Override
  218. public void actionPerformed(ActionEvent e)
  219. {
  220. if (gameStatus == 2)
  221. {
  222. update();
  223. }
  224.  
  225. renderer.repaint();
  226. }
  227.  
  228. public static void main(String[] args)
  229. {
  230. pong = new Pong();
  231. }
  232.  
  233. @Override
  234. public void keyPressed(KeyEvent e)
  235. {
  236. int id = e.getKeyCode();
  237.  
  238. if (id == KeyEvent.VK_W)
  239. {
  240. w = true;
  241. }
  242. else if (id == KeyEvent.VK_S)
  243. {
  244. s = true;
  245. }
  246. else if (id == KeyEvent.VK_UP)
  247. {
  248. up = true;
  249. }
  250. else if (id == KeyEvent.VK_DOWN)
  251. {
  252. down = true;
  253. }
  254. else if (id == KeyEvent.VK_RIGHT)
  255. {
  256. if (selectingDifficulty)
  257. {
  258. if (botDifficulty < 2)
  259. {
  260. botDifficulty++;
  261. }
  262. else
  263. {
  264. botDifficulty = 0;
  265. }
  266. }
  267. else if (gameStatus == 0)
  268. {
  269. scoreLimit++;
  270. }
  271. }
  272. else if (id == KeyEvent.VK_LEFT)
  273. {
  274. if (selectingDifficulty)
  275. {
  276. if (botDifficulty > 0)
  277. {
  278. botDifficulty--;
  279. }
  280. else
  281. {
  282. botDifficulty = 2;
  283. }
  284. }
  285. else if (gameStatus == 0 && scoreLimit > 1)
  286. {
  287. scoreLimit--;
  288. }
  289. }
  290. else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3))
  291. {
  292. gameStatus = 0;
  293. }
  294. else if (id == KeyEvent.VK_SHIFT && gameStatus == 0)
  295. {
  296. bot = true;
  297. selectingDifficulty = true;
  298. }
  299. else if (id == KeyEvent.VK_SPACE)
  300. {
  301. if (gameStatus == 0 || gameStatus == 3)
  302. {
  303. if (!selectingDifficulty)
  304. {
  305. bot = false;
  306. }
  307. else
  308. {
  309. selectingDifficulty = false;
  310. }
  311.  
  312. start();
  313. }
  314. else if (gameStatus == 1)
  315. {
  316. gameStatus = 2;
  317. }
  318. else if (gameStatus == 2)
  319. {
  320. gameStatus = 1;
  321. }
  322. }
  323. }
  324.  
  325. @Override
  326. public void keyReleased(KeyEvent e)
  327. {
  328. int id = e.getKeyCode();
  329.  
  330. if (id == KeyEvent.VK_W)
  331. {
  332. w = false;
  333. }
  334. else if (id == KeyEvent.VK_S)
  335. {
  336. s = false;
  337. }
  338. else if (id == KeyEvent.VK_UP)
  339. {
  340. up = false;
  341. }
  342. else if (id == KeyEvent.VK_DOWN)
  343. {
  344. down = false;
  345. }
  346. }
  347.  
  348. @Override
  349. public void keyTyped(KeyEvent e)
  350. {
  351.  
  352. }
  353. }
  354. -----------------------------------------------------------------------------------------------------------------------------
  355. import java.awt.Graphics;
  356. import java.awt.Graphics2D;
  357. import javax.swing.JPanel;
  358.  
  359. public class Renderer extends JPanel
  360. {
  361. private static final long serialVersionUID = 1L;
  362.  
  363. @Override
  364. protected void paintComponent(Graphics g)
  365. {
  366. super.paintComponent(g);
  367.  
  368. Pong.pong.render((Graphics2D) g);
  369. }
  370. }
  371. -----------------------------------------------------------------------------------------------------------------------------
  372. import java.awt.Color;
  373. import java.awt.Graphics;
  374.  
  375. public class Paddle
  376. {
  377. public int paddleNumber;
  378.  
  379. public int x, y;
  380. public int width = 50;
  381. public int height = 250;
  382. public int score;
  383.  
  384. public Paddle(Pong pong, int paddleNumber)
  385. {
  386. this.paddleNumber = paddleNumber;
  387.  
  388. if (paddleNumber == 1)
  389. {
  390. this.x = 0;
  391.  
  392. }
  393.  
  394. if (paddleNumber == 2)
  395. {
  396. this.x = pong.width - width;
  397. }
  398.  
  399. this.y = pong.height / 2 - this.height / 2;
  400. }
  401.  
  402. public void render(Graphics g)
  403. {
  404. g.setColor(Color.WHITE);
  405. g.fillRect(x, y, width, height);
  406.  
  407. }
  408.  
  409. public void move(boolean up)
  410. {
  411. int speed = 15;
  412.  
  413. if (up)
  414. {
  415. if (y - speed > 0)
  416. {
  417. y-= speed;
  418. }
  419. else
  420. {
  421. y = 0;
  422. }
  423. }
  424. else
  425. {
  426. if (y + height + speed < Pong.pong.height)
  427. {
  428. y += speed;
  429. }
  430. else
  431. {
  432. y = Pong.pong.height - height;
  433. }
  434. }
  435. }
  436.  
  437.  
  438. }
  439.  
  440. -----------------------------------------------------------------------------------------------------------------------------
  441. import java.awt.Color;
  442. import java.awt.Graphics;
  443. import java.util.Random;
  444.  
  445. public class Ball
  446. {
  447. public int x, y;
  448. public int width = 25;
  449. public int height = 25;
  450. public int motionX, motionY;
  451. public Random random;
  452. private Pong pong;
  453.  
  454. public int amountOfHits;
  455.  
  456. public Ball(Pong pong)
  457. {
  458. this.pong = pong;
  459. this.random = new Random();
  460.  
  461. spawn();
  462. }
  463.  
  464. public void update(Paddle paddle1, Paddle paddle2)
  465. {
  466. int speed = 5;
  467.  
  468. this.x += motionX * speed;
  469. this.y += motionY * speed;
  470.  
  471. if (this.y + height - motionY > pong.height || this.y + motionY < 0)
  472. {
  473. if (this.motionY < 0)
  474. {
  475. this.y = 0;
  476. this.motionY = random.nextInt(4);
  477.  
  478. if (motionY == 0)
  479. {
  480. motionY = 1;
  481. }
  482. }
  483. else
  484. {
  485. this.motionY = -random.nextInt(4);
  486. this.y = pong.height - height;
  487.  
  488. if (motionY == 0)
  489. {
  490. motionY = -1;
  491. }
  492. }
  493. }
  494.  
  495. if(checkCollision(paddle1) == 1)
  496. {
  497. this.motionX = 1 + (amountOfHits / 5);
  498. this.motionY = -2 + random.nextInt(4);
  499.  
  500. if (motionY == 0)
  501. {
  502. motionY = 1;
  503. }
  504.  
  505. amountOfHits++;
  506. }
  507. else if(checkCollision(paddle2) == 1)
  508. {
  509. this.motionX = -1 - (amountOfHits / 5);
  510. this.motionY = -2 + random.nextInt(4);
  511.  
  512. if (motionY == 0)
  513. {
  514. motionY = 1;
  515. }
  516.  
  517. amountOfHits++;
  518. }
  519.  
  520. if (checkCollision(paddle1) == 2)
  521. {
  522. paddle2.score++;
  523. spawn();
  524. }
  525. else if (checkCollision(paddle2) == 2)
  526. {
  527. paddle1.score++;
  528. spawn();
  529. }
  530. }
  531.  
  532. public void spawn()
  533. {
  534. this.amountOfHits = 0;
  535. this.x = pong.width / 2 - this.width / 2;
  536. this.y = pong.height / 2 - this.height / 2;
  537.  
  538. this.motionY = -2 + random.nextInt(4);
  539.  
  540. if (motionY == 0)
  541. {
  542. motionY = 1;
  543. }
  544.  
  545. if (random.nextBoolean())
  546. {
  547. motionX = 1;
  548. }
  549. else
  550. {
  551. motionX = -1;
  552. }
  553. }
  554.  
  555. public int checkCollision(Paddle paddle)
  556. {
  557. if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  558. {
  559. return 1; // bounce
  560. }
  561. else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
  562. {
  563. return 2; // score
  564. }
  565.  
  566. return 0; // nothing
  567. }
  568.  
  569. public void render(Graphics g)
  570. {
  571. g.setColor(Color.WHITE);
  572. g.fillOval(x, y, width, height);
  573. }
  574. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement