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