A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© edithe 中级黑马   /  2015-6-5 18:34  /  612 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package cn.itcast.snake;

  2. import java.awt.Point;
  3. import java.util.LinkedList;
  4. import java.util.Random;
  5. /*

  6. 食物其实就是一个坐标,
  7. */



  8. import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MoveAction;

  9. public class SnakeGame {
  10.        
  11.         //宽
  12.         public static final int WIDTH = 40;
  13.        
  14.         //高
  15.         public static final int HEIGHT = 10;
  16.        
  17.        
  18.         //地图
  19.         char[][] background = new char[HEIGHT][WIDTH];
  20.        
  21.        
  22.         //蛇
  23.         private LinkedList<Point> snakes = new LinkedList<Point>();
  24.        
  25.         //食物
  26.         private Point food;
  27.        
  28.        
  29.         //四个常量代表了四个方向
  30.         public static final int DIRECTION_UP = 1;  //上
  31.        
  32.         public static final int DIRECTION_DOWN = -1;  //下
  33.        
  34.         public static final int DIRECTION_LEFT = 2;  //左
  35.        
  36.         public static final int DIRECTION_RIGHT = -2; // 右
  37.        
  38.        
  39.        
  40.         //当前的方向
  41.         private int currentDirecion = 2;
  42.        
  43.        
  44.         //蛇移动应该是根据当前的方向进行移动的。
  45.         public void move(){
  46.                 //获取原始头的位置
  47.                 Point head = snakes.getFirst();
  48.                 switch (currentDirecion) {
  49.                         case DIRECTION_UP:
  50.                                         snakes.addFirst(new Point(head.x,head.y-1));
  51.                                 break;
  52.                         case DIRECTION_DOWN:
  53.                                 snakes.addFirst(new Point(head.x,head.y+1));                       
  54.                                 break;
  55.                         case DIRECTION_LEFT:
  56.                                 snakes.addFirst(new Point(head.x-1,head.y));
  57.                                 break;
  58.                         case DIRECTION_RIGHT:
  59.                                 snakes.addFirst(new Point(head.x+1,head.y));
  60.                                 break;
  61.                 }
  62.                 //删除蛇尾
  63.                 snakes.removeLast();
  64.         }
  65.        
  66.        
  67.          //改变蛇方向的方法
  68.         public void changeDirection(int newDirection){
  69.                 if(newDirection+currentDirecion!=0){
  70.                         //不是反方向
  71.                          this.currentDirecion = newDirection;
  72.                        
  73.                 }
  74.                
  75.         }
  76.        
  77.        


  78.        
  79.        
  80.         //生成食物
  81.         public void createFood(){
  82.                 Random random = new Random();
  83.                 while(true){
  84.                         int x = random.nextInt(WIDTH); //
  85.                         int y = random.nextInt(HEIGHT);
  86.                         if(background[y][x]!='*'){
  87.                                 food = new Point(x, y);
  88.                                 break;
  89.                         }
  90.                 }
  91.         }
  92.        
  93.        
  94.         //显示食物--- 把食物的坐标反馈到地图上,在地图上画上相应的字符
  95.         public void showFood(){
  96.                 background[food.y][food.x] = '@';
  97.                
  98.         }
  99.        
  100.         //初始化蛇节点
  101.         public void initSnake(){
  102.                 int x = WIDTH/2;
  103.                 int y = HEIGHT/2;
  104.                 snakes.addFirst(new Point(x-1, y));
  105.                 snakes.addFirst(new Point(x, y));
  106.                 snakes.addFirst(new Point(x+1, y));
  107.         }
  108.        
  109.        
  110.         //显示蛇(把蛇的坐标反馈到地图上,在地图上对应的位置画上对应的符号而已)
  111.         public void showSnake(){
  112.                 //画蛇头
  113.                 Point head  = snakes.getFirst();
  114.                 background[head.y][head.x] = ';
  115.                
  116.                 //画蛇身
  117.                 for(int i =1 ; i< snakes.size() ; i++){
  118.                         Point body = snakes.get(i);
  119.                         background[body.y][body.x] = '#';
  120.                 }       
  121.         }
  122.        
  123.        
  124.         //初始化地图
  125.         public void initBackground(){
  126.                 for(int rows = 0 ; rows<HEIGHT ; rows++){
  127.                         for(int cols = 0 ; cols<WIDTH ; cols++){
  128.                                 //第一行、最后一行、 第一列、最后一列
  129.                                 if(rows==0||rows==(HEIGHT-1)||cols==0||cols==(WIDTH-1)){
  130.                                         background[rows][cols] = '*';
  131.                                 }else{
  132.                                         background[rows][cols] = ' ';
  133.                                 }
  134.                         }
  135.                 }
  136.         }
  137.        
  138.        
  139.         //显示地图的方法
  140.         public void showBackground(){
  141.                 for(int rows = 0 ; rows<HEIGHT ; rows++){
  142.                         for(int cols = 0 ; cols<WIDTH ; cols++){
  143.                                 System.out.print(background[rows][cols]);
  144.                         }
  145.                         System.out.println();
  146.                 }
  147.                
  148.         }
  149.        
  150.        
  151.         //刷新游戏的最新状态
  152.         public void refresh(){
  153.                  //把游戏 之前的状态先清空
  154.                  initBackground();
  155.                  //把蛇最 新的状态显示
  156.                  showSnake();
  157.                  //显示食物
  158.                  showFood();
  159.                  //显示地图
  160.                  showBackground();
  161.                
  162.         }
  163.        
  164.        
  165.        
  166.          public static void main(String[] args) throws Exception {
  167.                  SnakeGame game = new SnakeGame();
  168.                  game.initBackground();
  169.                  //初始化蛇
  170.                  game.initSnake();
  171.                  //把蛇的节点反馈到地图上,在对应的位置上画上对应的符号
  172.                  game.showSnake();
  173.                  ;
  174.                  //初始化食物
  175.                  game.createFood();
  176.                  //显示食物
  177.                  game.showFood();
  178.                  
  179.                  //把地图打印出来
  180.                  game.showBackground();
  181.                  
  182.                  //让蛇向上移动三步
  183.                  for(int i= 0 ;i< 3; i++){                 
  184.                          game.changeDirection(DIRECTION_UP);
  185.                          game.move();
  186.                          game.refresh();
  187.                          Thread.sleep(1000);
  188.                  }
  189.                  
  190.                  
  191.                  //让蛇向下移动三步
  192.                  for(int i= 0 ;i< 3; i++){                 
  193.                          game.changeDirection(DIRECTION_DOWN);
  194.                          game.move();
  195.                          game.refresh();
  196.                          Thread.sleep(1000);
  197.                  }
  198.                  
  199.                
  200.         }

  201. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
lwj123 + 2

查看全部评分

2 个回复

倒序浏览
为什么不能移动
。。。。
回复 使用道具 举报
thoris 发表于 2015-6-5 20:03
为什么不能移动
。。。。

因为我的主方法中是测试的,你可以自己改下嘛,反正方法都有了,就是调用下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马