CODE 2D Java game


SUBMITTED BY: rabah75

DATE: July 24, 2016, 1:40 p.m.

UPDATED: July 24, 2016, 1:45 p.m.

FORMAT: Java

SIZE: 7.3 kB

HITS: 12092

  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.Font;
  4. import java.awt.FontMetrics;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.Toolkit;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyAdapter;
  11. import java.awt.event.KeyEvent;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JPanel;
  14. import javax.swing.Timer;
  15. public class Board extends JPanel implements ActionListener {
  16. private final int B_WIDTH = 300;
  17. private final int B_HEIGHT = 300;
  18. private final int DOT_SIZE = 10;
  19. private final int ALL_DOTS = 900;
  20. private final int RAND_POS = 29;
  21. private final int DELAY = 140;
  22. private final int x[] = new int[ALL_DOTS];
  23. private final int y[] = new int[ALL_DOTS];
  24. private int dots;
  25. private int apple_x;
  26. private int apple_y;
  27. private boolean leftDirection = false;
  28. private boolean rightDirection = true;
  29. private boolean upDirection = false;
  30. private boolean downDirection = false;
  31. private boolean inGame = true;
  32. private Timer timer;
  33. private Image ball;
  34. private Image apple;
  35. private Image head;
  36. public Board() {
  37. addKeyListener(new TAdapter());
  38. setBackground(Color.black);
  39. setFocusable(true);
  40. setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
  41. loadImages();
  42. initGame();
  43. }
  44. private void loadImages() {
  45. ImageIcon iid = new ImageIcon("dot.png");
  46. ball = iid.getImage();
  47. ImageIcon iia = new ImageIcon("apple.png");
  48. apple = iia.getImage();
  49. ImageIcon iih = new ImageIcon("head.png");
  50. head = iih.getImage();
  51. }
  52. private void initGame() {
  53. dots = 3;
  54. for (int z = 0; z < dots; z++) {
  55. x[z] = 50 - z * 10;
  56. y[z] = 50;
  57. }
  58. locateApple();
  59. timer = new Timer(DELAY, this);
  60. timer.start();
  61. }
  62. @Override
  63. public void paintComponent(Graphics g) {
  64. super.paintComponent(g);
  65. doDrawing(g);
  66. }
  67. private void doDrawing(Graphics g) {
  68. if (inGame) {
  69. g.drawImage(apple, apple_x, apple_y, this);
  70. for (int z = 0; z < dots; z++) {
  71. if (z == 0) {
  72. g.drawImage(head, x[z], y[z], this);
  73. } else {
  74. g.drawImage(ball, x[z], y[z], this);
  75. }
  76. }
  77. Toolkit.getDefaultToolkit().sync();
  78. } else {
  79. gameOver(g);
  80. }
  81. }
  82. private void gameOver(Graphics g) {
  83. String msg = "Game Over";
  84. Font small = new Font("Helvetica", Font.BOLD, 14);
  85. FontMetrics metr = getFontMetrics(small);
  86. g.setColor(Color.white);
  87. g.setFont(small);
  88. g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
  89. }
  90. private void checkApple() {
  91. if ((x[0] == apple_x) && (y[0] == apple_y)) {
  92. dots++;
  93. locateApple();
  94. }
  95. }
  96. private void move() {
  97. for (int z = dots; z > 0; z--) {
  98. x[z] = x[(z - 1)];
  99. y[z] = y[(z - 1)];
  100. }
  101. if (leftDirection) {
  102. x[0] -= DOT_SIZE;
  103. }
  104. if (rightDirection) {
  105. x[0] += DOT_SIZE;
  106. }
  107. if (upDirection) {
  108. y[0] -= DOT_SIZE;
  109. }
  110. if (downDirection) {
  111. y[0] += DOT_SIZE;
  112. }
  113. }
  114. private void checkCollision() {
  115. for (int z = dots; z > 0; z--) {
  116. if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
  117. inGame = false;
  118. }
  119. }
  120. if (y[0] >= B_HEIGHT) {
  121. inGame = false;
  122. }
  123. if (y[0] < 0) {
  124. inGame = false;
  125. }
  126. if (x[0] >= B_WIDTH) {
  127. inGame = false;
  128. }
  129. if (x[0] < 0) {
  130. inGame = false;
  131. }
  132. if(!inGame) {
  133. timer.stop();
  134. }
  135. }
  136. private void locateApple() {
  137. int r = (int) (Math.random() * RAND_POS);
  138. apple_x = ((r * DOT_SIZE));
  139. r = (int) (Math.random() * RAND_POS);
  140. apple_y = ((r * DOT_SIZE));
  141. }
  142. @Override
  143. public void actionPerformed(ActionEvent e) {
  144. if (inGame) {
  145. checkApple();
  146. checkCollision();
  147. move();
  148. }
  149. repaint();
  150. }
  151. private class TAdapter extends KeyAdapter {
  152. @Override
  153. public void keyPressed(KeyEvent e) {
  154. int key = e.getKeyCode();
  155. if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
  156. leftDirection = true;
  157. upDirection = false;
  158. downDirection = false;
  159. }
  160. if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
  161. rightDirection = true;
  162. upDirection = false;
  163. downDirection = false;
  164. }
  165. if ((key == KeyEvent.VK_UP) && (!downDirection)) {
  166. upDirection = true;
  167. rightDirection = false;
  168. leftDirection = false;
  169. }
  170. if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
  171. downDirection = true;
  172. rightDirection = false;
  173. leftDirection = false;
  174. }
  175. }
  176. }
  177. }
  178. First we will define the constants used in our game.
  179. private final int B_WIDTH = 300;
  180. private final int B_HEIGHT = 300;
  181. private final int DOT_SIZE = 10;
  182. private final int ALL_DOTS = 900;
  183. private final int RAND_POS = 29;
  184. private final int DELAY = 140;
  185. The B_WIDTH and B_HEIGHT constants determine the size of the board. The DOT_SIZE is the size of the apple and the dot of the snake. The ALL_DOTS constant defines the maximum number of possible dots on the board (900 = (300*300)/(10*10)). The RAND_POS constant is used to calculate a random position for an apple. The DELAY constant determines the speed of the game.
  186. private final int x[] = new int[ALL_DOTS];
  187. private final int y[] = new int[ALL_DOTS];
  188. These two arrays store the x and y coordinates of all joints of a snake.
  189. private void loadImages() {
  190. ImageIcon iid = new ImageIcon("dot.png");
  191. ball = iid.getImage();
  192. ImageIcon iia = new ImageIcon("apple.png");
  193. apple = iia.getImage();
  194. ImageIcon iih = new ImageIcon("head.png");
  195. head = iih.getImage();
  196. }
  197. In the loadImages() method we get the images for the game. The ImageIcon class is used for displaying PNG images.
  198. private void initGame() {
  199. dots = 3;
  200. for (int z = 0; z < dots; z++) {
  201. x[z] = 50 - z * 10;
  202. y[z] = 50;
  203. }
  204. locateApple();
  205. timer = new Timer(DELAY, this);
  206. timer.start();
  207. }
  208. In the initGame() method we create the snake, randomly locate an apple on the board, and start the timer.
  209. private void checkApple() {
  210. if ((x[0] == apple_x) && (y[0] == apple_y)) {
  211. dots++;
  212. locateApple();
  213. }
  214. }

comments powered by Disqus