1 packageSnake;2
3 importjava.awt.Color;4 importjava.awt.Dimension;5 importjava.awt.Font;6 importjava.awt.FontMetrics;7 importjava.awt.Graphics;8 importjava.awt.Image;9 importjava.awt.Toolkit;10 importjava.awt.event.ActionEvent;11 importjava.awt.event.ActionListener;12 importjava.awt.event.KeyAdapter;13 importjava.awt.event.KeyEvent;14 importjavax.swing.ImageIcon;15 importjavax.swing.JPanel;16 importjavax.swing.Timer;17
18 public class Board extends JPanel implementsActionListener {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 intdots;31 private intapple_x;32 private intapple_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 privateTimer timer;41 privateImage ball;42 privateImage apple;43 privateImage head;44
45 publicBoard() {46 initBoard();47 }48 private voidinitBoard() {49 addKeyListener(newTAdapter());50 setBackground(Color.black);51 setFocusable(true);52
53 setPreferredSize(newDimension(B_WIDTH, B_HEIGHT));54 loadImages();55 initGame();56 }57 private voidloadImages() {58 ImageIcon iid = new ImageIcon(getClass().getResource("/pic/dot.png"));59 ball =iid.getImage();60 ImageIcon iia = new ImageIcon(getClass().getResource("/pic/apple.png"));61 apple =iia.getImage();62 ImageIcon iih = new ImageIcon(getClass().getResource("/pic/head.png"));63 head =iih.getImage();64 }65 private voidinitGame() {66 dots = 3;67 for (int z = 0; z < dots; z++) {68 x[z] = 50 - z * 10;69 y[z] = 50;70 }71 locateApple();72 timer = new Timer(DELAY, this);73 timer.start();74 }75 @Override76 public voidpaintComponent(Graphics g) {77 super.paintComponent(g);78 doDrawing(g);79 }80 private voiddoDrawing(Graphics g) {81 if(inGame) {82 g.drawImage(apple, apple_x, apple_y, this);83 for (int z = 0; z < dots; z++) {84 if (z == 0) {85 g.drawImage(head, x[z], y[z], this);86 } else{87 g.drawImage(ball, x[z], y[z], this);88 }89 }90 //Toolkit.getDefaultToolkit().sync();
91 } else{92 gameOver(g);93 }94 }95 private voidgameOver(Graphics g) {96 String msg = "Game Over";97 Font small = new Font("Helvetica", Font.BOLD, 14);98 FontMetrics metr =getFontMetrics(small);99
100 g.setColor(Color.white);101 g.setFont(small);102 g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);103 }104 private voidcheckApple() {105 if ((x[0] == apple_x) && (y[0] ==apple_y)) {106 dots++;107 locateApple();108 }109 }110 private voidmove() {111 for (int z = dots; z > 0; z--) {112 x[z] = x[(z - 1)];113 y[z] = y[(z - 1)];114 }115 if(leftDirection) {116 x[0] -=DOT_SIZE;117 }118 if(rightDirection) {119 x[0] +=DOT_SIZE;120 }121 if(upDirection) {122 y[0] -=DOT_SIZE;123 }124 if(downDirection) {125 y[0] +=DOT_SIZE;126 }127 }128 private voidcheckCollision() {129 for (int z = dots; z > 0; z--) {130 if ((z > 4) && (x[0] == x[z]) && (y[0] ==y[z])) {131 inGame = false;132 }133 }134 if (y[0] >=B_HEIGHT) {135 inGame = false;136 }137 if (y[0] < 0) {138 inGame = false;139 }140 if (x[0] >=B_WIDTH) {141 inGame = false;142 }143 if (x[0] < 0) {144 inGame = false;145 }146 if (!inGame) {147 timer.stop();148 }149 }150 private voidlocateApple() {151 int r = (int) (Math.random() *RAND_POS);152 apple_x = ((r *DOT_SIZE));153
154 r = (int) (Math.random() *RAND_POS);155 apple_y = ((r *DOT_SIZE));156 }157 @Override158 public voidactionPerformed(ActionEvent e) {159 if(inGame) {160 checkApple();161 checkCollision();162 move();163 }164 repaint();165 }166 private class TAdapter extendsKeyAdapter {167 @Override168 public voidkeyPressed(KeyEvent e) {169 int key =e.getKeyCode();170 if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {171 leftDirection = true;172 upDirection = false;173 downDirection = false;174 }175 if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {176 rightDirection = true;177 upDirection = false;178 downDirection = false;179 }180 if ((key == KeyEvent.VK_UP) && (!downDirection)) {181 upDirection = true;182 rightDirection = false;183 leftDirection = false;184 }185 if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {186 downDirection = true;187 rightDirection = false;188 leftDirection = false;189 }190 }191 }192 }