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

comments powered by Disqus