1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
| import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList;
import javax.swing.JFrame; import javax.swing.JPanel;
public class SnakePanel extends JPanel implements KeyListener, Runnable{ private static final long serialVersionUID = 1L; public static Image background; public static Image cellImage; public static Image foolImage; ArrayList<Cell> snakeList = new ArrayList<Cell>(); private Cell food = new Cell(); public char direction = Direction.RIGHT; public boolean isLive = true; public int sleep = 100; public static final int MIN_X = 50 + 2; public static final int MAX_X = MIN_X + 10 * 35 - 10; public static final int MIN_Y = 50 - 2; public static final int MAX_Y = MIN_Y + 10 * 35 - 10;
public SnakePanel() { background = Toolkit.getDefaultToolkit().createImage("bg.png"); cellImage = Toolkit.getDefaultToolkit().createImage("cell.png"); foolImage = Toolkit.getDefaultToolkit().createImage("food.png"); int x = MIN_X; int y = MIN_Y; for (int i = 0; i < 10; i++) { Cell cell = new Cell(x, y); snakeList.add(cell); x += 10; } getFood(); } public void getFood() { boolean flag; int x, y; do{ flag = false; x = (int)(Math.random() * 35) * 10 + 52; y = (int)(Math.random() * 35) * 10 + 48; for (Cell cell : snakeList) { if (cell.getX() == x && cell.getY() == y) { flag = true; break; } } }while(flag); food.setX(x); food.setY(y); } @Override public void paint(Graphics g) { super.paint(g); g.drawImage(background, 0, 0, this); for (Cell cell : snakeList) { g.drawImage(cellImage, cell.x , cell.y, this); } g.drawImage(foolImage, food.x , food.y, this); if (!isLive){ int x = (MIN_X + MAX_X) / 2; int y = (MIN_Y + MAX_Y) / 2; g.setColor(Color.red); g.drawString("Game over !!!", x, y); } }
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("小破蛇"); frame.setSize(470, 480); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SnakePanel sp = new SnakePanel(); frame.add(sp); frame.addKeyListener(sp); new Thread(sp).start(); } public class Direction { public static final char UP = 'w'; public static final char DOWN = 's'; public static final char LEFT = 'a'; public static final char RIGHT = 'd'; } public class Cell { private int x; private int y; public Cell() { } public Cell(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } }
@Override public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_W || e.getKeyCode() == KeyEvent.VK_UP) { if(direction != Direction.DOWN)direction = Direction.UP; } else if(e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_LEFT) { if(direction != Direction.RIGHT)direction = Direction.LEFT; } else if(e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_DOWN) { if(direction != Direction.UP)direction = Direction.DOWN; } else if(e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_RIGHT) { if(direction != Direction.LEFT)direction = Direction.RIGHT; } }
@Override public void keyReleased(KeyEvent e) { }
@Override public void keyTyped(KeyEvent e) { }
@Override public void run() { while(true) { try { Thread.sleep(sleep); } catch (InterruptedException e) { e.printStackTrace(); } int x = snakeList.get(snakeList.size() - 1).x; int y = snakeList.get(snakeList.size() - 1).y; switch(direction) { case Direction.DOWN: y += 10; break; case Direction.LEFT: x -= 10; break; case Direction.RIGHT: x += 10; break; case Direction.UP: y -= 10; break; } if (x < MIN_X || x > MAX_X || y < MIN_Y || y > MAX_Y) { isLive = false; break; } boolean flag = false; for(Cell cell : snakeList) { if (cell.getX() == x && cell.getY() == y) { flag = true; break; } } if (flag) { isLive = false; break; } if (x == food.x && y == food.y) { Cell cell = new Cell(x, y); snakeList.add(cell); getFood(); } else { Cell cell = new Cell(x, y); snakeList.add(cell); snakeList.remove(0); } this.repaint(); } this.repaint(); } }
|