//Snake.java
1 package snake; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.io.BufferedReader; 7 import java.io.BufferedWriter; 8 import java.io.FileNotFoundException; 9 import java.io.FileReader; 10 import java.io.FileWriter; 11 import java.io.IOException; 12 import java.util.Iterator; 13 import java.util.LinkedList; 14 import java.util.Random; 15 class go implements Runnable { 16 Snake s; 17 Random random; 18 int foodX = 15, foodY = 15; 19 int N; 20 go(Snake snake) { 21 s = snake; 22 random = new Random(); 23 N=s.N; 24 } 25 Point getFood() { 26 Point p = new Point(); 27 Iterator<Point> it = s.list.iterator(); 28 Point temp = it.next(); 29 boolean isFoodinBody = true; 30 while (isFoodinBody) { 31 foodX = Math.abs(random.nextInt()) % N; 32 foodY = Math.abs(random.nextInt()) % N; 33 isFoodinBody = false; 34 while (it.hasNext()) { 35 temp = (Point) it.next(); 36 if (foodX == temp.x && temp.y == foodY) { 37 isFoodinBody = true; 38 break; 39 } 40 } 41 } 42 return p; 43 } 44 public void run() { 45 while (true) { 46 Point first = s.list.getFirst(); 47 int x = first.x, y = first.y; 48 int gotfood = 0; 49 if (foodX == x && foodY == y) { 50 if (!s.isDead) { 51 s.SCORE += 100; 52 s.scoreField.setText("SCORE: "+new Integer(s.SCORE).toString()); 53 s.list.add(s.list.getLast()); 54 } 55 gotfood = 1; 56 foodX = Math.abs(random.nextInt()) % N; 57 foodY = Math.abs(random.nextInt()) % N; 58 s.bs[foodX][foodY].setBackground(Color.PINK); 59 60 } 61 Iterator<Point> it = s.list.iterator(); 62 Point temp = it.next(); 63 while (it.hasNext()) { 64 temp = (Point) it.next(); 65 if (temp.x == x && temp.y == y) { 66 67 68 s.gameOver(); 69 70 } 71 if (gotfood == 1 && foodX == temp.x && temp.y == foodY) { 72 Point food = getFood(); 73 foodX = food.x; 74 foodY = food.y; 75 s.bs[foodX][foodY].setBackground(Color.PINK); 76 gotfood=0; 77 } 78 } 79 switch (s.dir) { 80 case 2 : 81 y =( (y - 1)+N )% N; 82 /*if (y == -1) 83 y = 29;*/ 84 break; 85 case 0 : 86 y = (y + 1) % N; 87 break; 88 case 3 : 89 x = ((x - 1)+N) % N; 90 /*if (x == -1) 91 x = 29;*/ 92 break; 93 case 1 : 94 x = (x + 1) % N; 95 break; 96 } 97 Point head = new Point(x, y); 98 s.bs[x][y].setBackground(Color.LIGHT_GRAY); 99 100 s.list.addFirst(head); 101 Point lastP = s.list.removeLast(); 102 s.bs[lastP.x][lastP.y].setBackground(Color.GRAY); 103 try { 104 Thread.sleep(100); 105 } catch (InterruptedException e) { 106 e.printStackTrace(); 107 } 108 109 /* 110 * for (int i = 0; i <= list.size() - 1; i++) { System.out.print( 111 * "(" + list.get(i).x + "," + list.get(i).y + ") "); 112 * 113 * } 114 */ 115 // System.out.println(); 116 117 } 118 } 119 } 120 public class Snake extends JFrame { 121 private static final long serialVersionUID = -6494984874207734357L; 122 int N = 30; 123 int dir = 0;// 0 右 1下 2左 3上 124 int SCORE = 0; 125 int a=0,b=0,c=0; 126 boolean isDead = false; 127 JButton[][] bs = new JButton[N][N]; 128 JPanel panel,scorePanel; 129 JTextField scoreField,bestScoreField; 130 LinkedList<Point> list; 131 Snake() { 132 super("Snake"); 133 Container container = getContentPane(); 134 container.setLayout(new BorderLayout()); 135 scorePanel = new JPanel(); 136 scorePanel .setLayout(new BorderLayout()); 137 scoreField = new JTextField(20); 138 bestScoreField= new JTextField(50); 139 bestScoreField.setText(getBestScores()); 140 bestScoreField.setEditable(false); 141 scoreField.setText("SCORE: "+new Integer(SCORE).toString()); 142 scoreField.setEditable(false); 143 scorePanel.add(scoreField,BorderLayout.WEST); 144 scorePanel.add(bestScoreField,BorderLayout.EAST); 145 container.add(scorePanel, BorderLayout.NORTH); 146 panel = new JPanel(); 147 panel.setLayout(new GridLayout(N, N)); 148 149 for (int i = 0; i <= N - 1; i++) { 150 for (int j = 0; j <= N - 1; j++) { 151 bs[i][j] = new JButton(); 152 bs[i][j].setBackground(Color.GRAY); 153 panel.add(bs[i][j]); 154 } 155 } 156 157 container.add(panel, BorderLayout.CENTER); 158 list = new LinkedList<Point>(); 159 160 list.add(new Point(15, 15)); 161 list.add(new Point(15, 14)); 162 list.add(new Point(15, 13)); 163 list.add(new Point(15, 12)); 164 list.add(new Point(15, 11)); 165 for (int i = 0; i <= list.size() - 1; i++) { 166 bs[list.get(i).x][list.get(i).y].setBackground(Color.LIGHT_GRAY); 167 } 168 /*for (int i = 0; i <= list.size() - 1; i++) { 169 System.out.print("(" + list.get(i).x + "," + list.get(i).y + ") "); 170 171 }*/ 172 /*System.out.println();*/ 173 174 this.addKeyListener(new KeyAdapter() { 175 public void keyPressed(KeyEvent e) { 176 int c = e.getKeyCode(); 177 //System.out.print(c); 178 int dir1 = dir; 179 switch (c) { 180 case KeyEvent.VK_LEFT : 181 dir1 = 2; 182 break; 183 case KeyEvent.VK_RIGHT : 184 dir1 = 0; 185 break; 186 case KeyEvent.VK_UP : 187 dir1 = 3; 188 break; 189 case KeyEvent.VK_DOWN : 190 dir1 = 1; 191 break; 192 default : 193 System.out.print("hey"); 194 break; 195 } 196 if (!(dir1 - dir == -2 | dir1 - dir == 2)) 197 dir = dir1; 198 } 199 }); 200 this.setFocusable(true); 201 setLocation(200, 10); 202 setSize(800, 700); 203 setVisible(true); 204 } 205 String getBestScores() { 206 207 BufferedReader reader=null; 208 try { 209 reader=new BufferedReader(new FileReader("score.txt")); 210 } catch (FileNotFoundException e) { 211 e.printStackTrace(); 212 } 213 214 try { 215 a=Integer.parseInt(reader.readLine()); 216 b=Integer.parseInt(reader.readLine()); 217 c=Integer.parseInt(reader.readLine()); 218 } catch (NumberFormatException e) { 219 e.printStackTrace(); 220 } catch (IOException e) { 221 e.printStackTrace(); 222 } 223 String bestS=new String("Best Score: "+new Integer(a).toString()+" "+new Integer(b).toString()+" "+new Integer(c).toString()); 224 return bestS; 225 } 226 int gameOver(){ 227 isDead = true; 228 JOptionPane.showMessageDialog(null, "你死了~~~.", "GAME OVER", 229 JOptionPane.PLAIN_MESSAGE); 230 if(SCORE>a){c=b;b=a;a=SCORE;save();System.exit(0);} 231 if(SCORE>b){c=b;b=SCORE;save();System.exit(0);} 232 if(SCORE>c){c=SCORE;save();System.exit(0);} 233 System.exit(0); 234 return 0; 235 } 236 int save(){ 237 238 BufferedWriter writer=null; 239 try { 240 writer=new BufferedWriter(new FileWriter("score.txt")); 241 } catch (IOException e) { 242 e.printStackTrace(); 243 } 244 String d=new Integer(a).toString(),e=new Integer(b).toString(),f=new Integer(c).toString(); 245 try { 246 writer.write(d); 247 writer.newLine(); 248 writer.write(e); 249 writer.newLine(); 250 writer.write(f); 251 writer.flush(); 252 writer.close(); 253 } catch (NumberFormatException ev) { 254 ev.printStackTrace(); 255 } catch (IOException ev) { 256 ev.printStackTrace(); 257 } 258 return 0; 259 } 260 public static void main(String[] args) { 261 Snake snake = new Snake(); 262 snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 263 Thread thread = new Thread(new go(snake)); 264 thread.start(); 265 } 266 267 }