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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 油油油菜花 中级黑马   /  2014-10-12 18:59  /  1230 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

看完马士兵的坦克大战之后写出来的,有些粗糙,但是基本功能算是实现了……
整个程序使用GUI实现,只要谈谈其中的算法吧:
    蛇不停移动的算法: 游戏开始的时候,蛇默认长度是5,每一节是装在一个集合里面的,移动的时候,蛇尾先移动到倒数第二节的位置,倒数第2节移动到倒数第三节的位置…… 直到第二节移动到蛇头的位置,最后一步就根据键盘的输入移动蛇头了,这样就完成了一次移动;
    如果向上移动,代码如下:
               if(up){
                        for(j=i-1;j>0;j--){
                                points.get(j).x = points.get(j-1).x;
                                points.get(j).y = points.get(j-1).y;
                        }
                        points.get(0).y -= 20;
                        j = i-1;
                }

    蛇的不停移动用一个不停循环的线程来实现:
               public class MThread implements Runnable{
                public void run(){
                        while(aLive){
                                move();
                                try{
                                        Thread.sleep(time);
                                }catch(InterruptedException e){
                                        e.printStackTrace();
                                }
                        }
                }
        }

     程序中一共有三个类:Snake(蛇类) 、Food(食物类)、 GreedSnake(游戏主程序)

    Snake代码如下:

  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;

  4. class Snake{
  5.         Point po = null;
  6.         int size = 0;
  7.         GreedSnake gs;
  8.         private int time = 150;

  9.         ArrayList<Point> points = new ArrayList<Point>();
  10.         Point head;
  11.         
  12.         private int step = 0;
  13.         private int scoreAdd = 0;
  14.         private boolean up = false,down = false,left = false,right = false;
  15.         private boolean aLive = true;
  16.         private Rectangle rect = null;

  17.         enum Direction {U,D,L,R};
  18.         Direction dir = Direction.U;

  19.         public Snake(Point po,int size,GreedSnake gs){
  20.                 this.po = po;
  21.                 this.size = size;
  22.                 this.gs = gs;
  23.                 for(int i=0;i<size;i++){
  24.                         addPoint(po);
  25.                         po.x-=20;
  26.                         po = new Point(po.x,po.y);
  27.                 }
  28.                 new Thread(new MThread()).start();
  29.         }

  30.         public boolean isAlive(){
  31.                 return this.aLive;
  32.         }
  33.                
  34.         public void Live(boolean aLive){
  35.                 this.aLive = aLive;
  36.         }

  37.         public void addPoint(Point p){
  38.                 points.add(p);               
  39.         }
  40.         
  41.         public void speedUp(){
  42.                 time-=20;
  43.         }

  44.         public void directionReset(){
  45.                 up = false; down = false; left = false; right = false;
  46.         }
  47.         //设置蛇在移动时不能反方向移动
  48.         public void keyPressed(KeyEvent e){
  49.                 if(up||down){
  50.                         switch(e.getKeyCode()){

  51.                         case KeyEvent.VK_UP :
  52.                         break;
  53.                         case KeyEvent.VK_DOWN :
  54.                         break;
  55.                         case KeyEvent.VK_LEFT :
  56.                                 directionReset();
  57.                                 left = true;
  58.                         break;
  59.                         case KeyEvent.VK_RIGHT :
  60.                                 directionReset();
  61.                                 right = true;
  62.                         break;
  63.                         }
  64.                 }
  65.                 if(left||right){
  66.                         switch(e.getKeyCode()){

  67.                         case KeyEvent.VK_UP :
  68.                                 directionReset();
  69.                                 up = true;
  70.                         break;
  71.                         case KeyEvent.VK_DOWN :
  72.                                 directionReset();
  73.                                 down = true;
  74.                         break;
  75.                         case KeyEvent.VK_LEFT :
  76.                         break;
  77.                         case KeyEvent.VK_RIGHT :
  78.                         break;
  79.                         }
  80.                 }
  81.                 if(!up&&!down&&!left&&!right){
  82.                         switch(e.getKeyCode()){

  83.                         case KeyEvent.VK_UP :
  84.                                 directionReset();
  85.                                 up = true;
  86.                         break;
  87.                         case KeyEvent.VK_DOWN :
  88.                                 directionReset();
  89.                                 down = true;
  90.                         break;
  91.                         case KeyEvent.VK_LEFT :
  92.                         break;
  93.                         case KeyEvent.VK_RIGHT :
  94.                                 directionReset();
  95.                                 right = true;
  96.                         break;
  97.                         }
  98.                 }
  99.         }
  100.         
  101.         //蛇的移动算法
  102.         public void move(){
  103.                
  104.                 int i = points.size();
  105.                 int j;
  106.                
  107.                 if(up){
  108.                         for(j=i-1;j>0;j--){
  109.                                 points.get(j).x = points.get(j-1).x;
  110.                                 points.get(j).y = points.get(j-1).y;
  111.                         }
  112.                         points.get(0).y -= 20;
  113.                         j = i-1;
  114.                 }
  115.         
  116.                 else if(down){
  117.                                        
  118.                         for(j=i-1;j>0;j--){
  119.                                 points.get(j).x = points.get(j-1).x;
  120.                                 points.get(j).y = points.get(j-1).y;
  121.                         }
  122.                         points.get(0).y += 20;
  123.                         j = i-1;
  124.                 }        
  125.                 else if(left){
  126.                                 
  127.                         for(j=i-1;j>0;j--){
  128.                                 points.get(j).x = points.get(j-1).x;
  129.                                 points.get(j).y = points.get(j-1).y;
  130.                         }
  131.                         points.get(0).x -= 20;
  132.                         j = i-1;
  133.                 }
  134.                 else if(right){
  135.                                        
  136.                         for(j=i-1;j>0;j--){
  137.                                 points.get(j).x = points.get(j-1).x;
  138.                                 points.get(j).y = points.get(j-1).y;
  139.                         }
  140.                         points.get(0).x += 20;
  141.                         j = i-1;
  142.                 }
  143.                
  144.                 head = points.get(0);
  145.                 rect = new Rectangle(head.x,head.y,20,20);
  146.                 if(head.x<20||head.x>780|head.y<30||head.y>580){
  147.                         aLive = false;
  148.                 }
  149.                 for(int t=1;t<points.size();t++){
  150.                         if(head.x == points.get(t).x && head.y == points.get(t).y){
  151.                                 aLive = false;
  152.                         }
  153.                 }

  154.         }
  155.         //蛇吃到食物时的处理
  156.         public void eat(){
  157.                 for(int i=0;i<gs.foods.size();i++){
  158.                         if(rect.contains(gs.foods.get(i).x, gs.foods.get(i).y) &&gs.foods.get(i).isAlive()){
  159.                                 gs.foods.get(i).setAlive(false);
  160.                                 gs.foods.remove(i);
  161.                                 grow();
  162.                         }
  163.                 }
  164.         }

  165.         public void grow(){
  166.                 Point lastPoint = points.get(points.size()-1);
  167.                 Point newEnd = new Point(lastPoint.x,lastPoint.y);
  168.                 addPoint(newEnd);
  169.                 if(step == 5) {
  170.                         speedUp();
  171.                         scoreAdd+=3;
  172.                         step = 0;
  173.                 }
  174.                 step++;
  175.         }

  176.         public void paint(Graphics g){
  177.                 Color c = g.getColor();
  178.                 g.setColor(Color.GREEN);
  179.                 g.drawString(" head area:"+head.x+","+head.y,30,50);
  180.                 g.drawString(" scores:"+(points.size()-5)*(5+scoreAdd),30,70);
  181.                 Iterator<Point> i = points.iterator();
  182.                 while(i.hasNext()){
  183.                         Point p = i.next();
  184.                         g.setColor(Color.GREEN);
  185.                         g.fillRect(p.x,p.y,20,20);
  186.                         g.setColor(Color.YELLOW);
  187.                         g.fillOval(p.x,p.y,20,20);
  188.                 }
  189.                 g.setColor(c);
  190.                 eat();
  191.         }

  192.         //创建一个线程让蛇不停地移动
  193.         public class MThread implements Runnable{
  194.                 public void run(){
  195.                         while(aLive){
  196.                                 move();
  197.                                 try{
  198.                                         Thread.sleep(time);
  199.                                 }catch(InterruptedException e){
  200.                                         e.printStackTrace();
  201.                                 }
  202.                         }
  203.                 }
  204.         }
  205.         
  206. }
复制代码


GreedSnake 代码:
额 …………总算完了  告诉我怎么贴代码  不会啊!!  上传附件吧  诶。。。

        

GreedSnake0.3.zip

12.24 KB, 下载次数: 99

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1 赞一个!

查看全部评分

1 个回复

倒序浏览
太厉害了,大神啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马