package cd.itcast.snake;import java.awt.BorderLayout;import java.awt.Point;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.util.LinkedList;import java.util.Random;import javax.swing.JButton;import javax.swing.JFrame;import cn.itcast.util.FrameUtil;public class SnakeGame { //宽度(列数)x public static final int WIDTH = 50; //高度(行数)y public static final int HEIGHT = 10; //地图 private char[][] background = new char[HEIGHT][WIDTH]; //初始化地图 private void initBackground() { for(int rows = 0 ; rows < background.length ; rows++){ for(int cols = 0 ; cols < background[rows].length ; cols++){ if (rows==0||rows==(background.length-1)) { background[rows][cols]='*'; }else { background[rows][cols]=' '; } } } } //显示地图的 private void showBackground() { for(int rows = 0 ; rows < background.length ; rows++){ for(int cols = 0 ; cols < background[rows].length ; cols++){ System.out.print(background[rows][cols]); } System.out.println(); } } //使用集合保存蛇所有信息 LinkedListsnake = new LinkedList (); //初始化蛇 public void initsnake() { int x = WIDTH/2; int y = HEIGHT/2; snake.addFirst(new Point(x-1,y)); snake.addFirst(new Point(x,y)); snake.addFirst(new Point(x+1,y)); } //显示蛇--》蛇的节点信息,反馈到地图上 public void showSnake() { //画蛇身,因为取出了蛇身,所以从1开始 for(int i=1; i