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();
}
}
//使用集合保存蛇所有信息
LinkedList<Point> snake = new LinkedList<Point>();
//初始化蛇
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<snake.size();i++){
Point body = snake.get(i);
background[body.y][body.x] = '#';
}
//画蛇头,注意和二维数组对应
Point head = snake.getFirst();
background[head.y][head.x] = '$';
}
//蛇移动:向上移动
public static final int UP_DIRECTION = 1;
public static final int DOWN_DIRECTION = -1;
public static final int LEFT_DIRECTION = 2;
public static final int RIGHT_DIRECTION = -2;
//默认方向
int currentDecration=-2;
//蛇移动的方法
public void move() {
Point head = snake.getFirst();
//根据当前方向移动
switch (currentDecration) {
case UP_DIRECTION:
snake.addFirst(new Point(head.x, head.y-1));
break;
case DOWN_DIRECTION:
snake.addFirst(new Point(head.x, head.y+1));
break;
case LEFT_DIRECTION:
//穿越左边
if(head.x==0){
snake.addFirst(new Point(WIDTH-1, head.y));
}else {
snake.addFirst(new Point(head.x-1, head.y));
}
break;
case RIGHT_DIRECTION:
if(head.x==WIDTH-1){
snake.addFirst(new Point(0, head.y));
}else {
snake.addFirst(new Point(head.x+1, head.y));
}
break;
default:
break;
}
//如果吃到了,就要创建新的食物;没吃到才删除
if (snakeEat()) {
createFood();
}else {
snake.removeLast();
}
}
//改变方向的方法
public void changeDirection(int newDirection) {
if(newDirection + currentDecration!=0){
this.currentDecration = newDirection;
}
}
//食物
Point food;
//创建食物
public void createFood() {
Random random = new Random();
while (true) {
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
if(background[y][x]!='*'){
food = new Point(x,y);
break;
}
}
}
//显示食物
public void showFood() {
background[food.y][food.x] = '&';
}
//吃食物:在移动的时候调用,因为每走一步,都可能吃到;当吃到的时候,蛇尾不删除
public boolean snakeEat() {
Point head = snake.getFirst();
if(head.equals(food)){
return true;
}
return false;
}
//刷新
public void refrash() {
//清空全部信息
initBackground();
//把食物的最新信息反馈到地图上
showFood();
//把蛇的最新信息反馈到地图上
showSnake();
//显示当前地图的信息
showBackground();
}
//游戏是否结束
static boolean isGameOver = false;
public void isGameOver() {
Point head = snake.getFirst();
//撞墙死
if (background[head.y][head.x]=='*') {
isGameOver = true;
}
//咬死自己
for(int i=1; i<snake.size(); i++){
Point body = snake.get(i);
if(head.equals(body)){
isGameOver = true;
}
}
}
public static void main(String[] args) throws InterruptedException {
final SnakeGame snake = new SnakeGame();
//初始化地图
snake.initBackground();
//初始化蛇
snake.initsnake();
//显示蛇
snake.showSnake();
//初始化食物
snake.createFood();
//显示食物
snake.showFood();
snake.showBackground();
JFrame frame = new JFrame("方向盘");
frame.add(new JButton("↑"),BorderLayout.NORTH);
frame.add(new JButton("↓"),BorderLayout.SOUTH);
frame.add(new JButton("←"),BorderLayout.WEST);
frame.add(new JButton("→"),BorderLayout.EAST);
JButton button = new JButton("点我控制方向");
frame.add(button);
FrameUtil.initFrame(frame, 300, 300);
//添加事件监听器
button.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
snake.changeDirection(UP_DIRECTION);
break;
case KeyEvent.VK_DOWN:
snake.changeDirection(DOWN_DIRECTION);
break;
case KeyEvent.VK_LEFT:
snake.changeDirection(LEFT_DIRECTION);
break;
case KeyEvent.VK_RIGHT:
snake.changeDirection(RIGHT_DIRECTION);
break;
default:
break;
}
snake.move();
snake.isGameOver();
if(isGameOver){
System.err.println("游戏结束!!!");
System.exit(0);
}
snake.refrash();
}
});
}
}