如何做一个贪吃蛇的小游戏

1. 首先创建贪吃蛇的一节的类

package tanchishe;
//one depart cell
public class cell {
  private int x ;// 单元格的属性属性是私有的
  private int y;
 public cell() {
    
 }
     public cell (int x ,int y) {
         this.x=x;
         this.y=y;
     }
     

     public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public String toString() {
         return "["+x+","+y+"]";
     }
     }
   


  2.创建一个类,让蛇走动和可以吃掉食物

package tanchishe;

import java.util.Arrays;

/*贪吃蛇*/
public class worm {
    public static final int DEFAULT_LENGTH=12;
    private cell[] cells;//创建一个数组,表示一个蛇上有n个单元格
    
    public static final int UP=1;
    public static final int DOWN=-1;
    public static final int LEFT=2;
    public static final int RIGHT=-2;
    //蛇当前运行的方向
    private int currentDirection;
    
    public worm() {
        cells =new cell [DEFAULT_LENGTH];
        for(int i=0;i<cells.length;i++) {
            cells[i]=new cell(i,0);
        }
        currentDirection=DOWN;
        
    }
    
    
    public boolean contains(int x ,int y) {
        for(int i=0;i<cells.length;i++) {
            cell node =cells[i];
            if(node.getX()==x && node.getY()==y) {
                return true;
            }
        }
        return false;
    }
    /*计算currentDirection与direction的和
     * 如果是0就表示方向反了,就结束方法返回,不进行任何操作
     * 2.curentDirection改变当前的方向,作为下次运行的方向
     * 3.判断当前节点的坐标与实物对象的坐标是否一致,如果一致就说明吃到食物
     * 4.如果吃到食物,就把cells进行扩容,将cells的每个数组向后移动
     * 将新头节点插入头的位置cells[0]=newhead
     * 6.返回是否吃到食物
     * */
    public boolean creep(int direction,cell food) {
        if(currentDirection +direction==0) {
            return false;//反向了,不进行任何操作
        }
        currentDirection=direction;
        cell head=createHead(direction);
        boolean eat =false;
        if(head.getX()==food.getX()&&head.getY()==food.getY()) {
            eat=true;
        }if(eat) {
            cells=Arrays.copyOf(cells, cells.length+1);
        }
        for (int i=cells.length-1;i>=1;i--) {
            cells[i]=cells[i-1];
        }
        cells[0]=head;
        return eat;
    }
    public boolean hit() {
        return hit (currentDirection);
    }
    
    public boolean hit (int direction) {
        System.out.println("方向:(2)"+direction);//2
        cell head =createHead(direction);
        System.out.println(head);
        if(head.getX()<0||head.getX()>=wormstage.COLS||head.getY()<0||head.getY()>=wormstage.ROWS) {
            return true;
        }
        for(int i=0;i<cells.length-1;i++) {
            cell node=cells[i];
            if(node.getX()==head.getX()&&node.getY()==head.getY()) {
                return true;
            }
        }
        return false;
    }
    
    /*重载一份*/
    public boolean creep(cell food) {
        return creep(currentDirection,food);
    }
    
    public void creep() {
        for(int i=cells.length-1;i>=1;i--) {
            cells[i]=cells[i-1];
            
        }
        cells[0]=createHead(currentDirection);
    }
    private cell createHead(int direction) {
        switch(direction) {
        case DOWN:
            int x=cells[0].getX();
            int y=cells[0].getY()+1;
              return new cell(x,y);
        case UP:
            int xu=cells[0].getX();
            int yu=cells[0].getY()-1;
            return new cell(xu,yu);
        case RIGHT:
            int xr=cells[0].getX()+1;
            int yr=cells[0].getY();
            return new cell(xr,yr);
        case LEFT:
            int xl=cells[0].getX()-1;
            int yl=cells[0].getY();
            return new cell(xl,yl);
        
            
    }
    return null;
    
    }
    public cell[] getCells() {
        return cells;
    }
    
    public String toString () {
        return Arrays.toString(cells);//显示蛇
    }
}

3.最后,写一个平台的类,让蛇在平台上走

package tanchishe;

import java.util.Arrays;

/*贪吃蛇*/
public class worm {
    public static final int DEFAULT_LENGTH=12;
    private cell[] cells;//创建一个数组,表示一个蛇上有n个单元格
    
    public static final int UP=1;
    public static final int DOWN=-1;
    public static final int LEFT=2;
    public static final int RIGHT=-2;
    //蛇当前运行的方向
    private int currentDirection;
    
    public worm() {
        cells =new cell [DEFAULT_LENGTH];
        for(int i=0;i<cells.length;i++) {
            cells[i]=new cell(i,0);
        }
        currentDirection=DOWN;
        
    }
    
    
    public boolean contains(int x ,int y) {
        for(int i=0;i<cells.length;i++) {
            cell node =cells[i];
            if(node.getX()==x && node.getY()==y) {
                return true;
            }
        }
        return false;
    }
    /*计算currentDirection与direction的和
     * 如果是0就表示方向反了,就结束方法返回,不进行任何操作
     * 2.curentDirection改变当前的方向,作为下次运行的方向
     * 3.判断当前节点的坐标与实物对象的坐标是否一致,如果一致就说明吃到食物
     * 4.如果吃到食物,就把cells进行扩容,将cells的每个数组向后移动
     * 将新头节点插入头的位置cells[0]=newhead
     * 6.返回是否吃到食物
     * */
    public boolean creep(int direction,cell food) {
        if(currentDirection +direction==0) {
            return false;//反向了,不进行任何操作
        }
        currentDirection=direction;
        cell head=createHead(direction);
        boolean eat =false;
        if(head.getX()==food.getX()&&head.getY()==food.getY()) {
            eat=true;
        }if(eat) {
            cells=Arrays.copyOf(cells, cells.length+1);
        }
        for (int i=cells.length-1;i>=1;i--) {
            cells[i]=cells[i-1];
        }
        cells[0]=head;
        return eat;
    }
    public boolean hit() {
        return hit (currentDirection);
    }
    
    public boolean hit (int direction) {
        System.out.println("方向:(2)"+direction);//2
        cell head =createHead(direction);
        System.out.println(head);
        if(head.getX()<0||head.getX()>=wormstage.COLS||head.getY()<0||head.getY()>=wormstage.ROWS) {
            return true;
        }
        for(int i=0;i<cells.length-1;i++) {
            cell node=cells[i];
            if(node.getX()==head.getX()&&node.getY()==head.getY()) {
                return true;
            }
        }
        return false;
    }
    
    /*重载一份*/
    public boolean creep(cell food) {
        return creep(currentDirection,food);
    }
    
    public void creep() {
        for(int i=cells.length-1;i>=1;i--) {
            cells[i]=cells[i-1];
            
        }
        cells[0]=createHead(currentDirection);
    }
    private cell createHead(int direction) {
        switch(direction) {
        case DOWN:
            int x=cells[0].getX();
            int y=cells[0].getY()+1;
              return new cell(x,y);
        case UP:
            int xu=cells[0].getX();
            int yu=cells[0].getY()-1;
            return new cell(xu,yu);
        case RIGHT:
            int xr=cells[0].getX()+1;
            int yr=cells[0].getY();
            return new cell(xr,yr);
        case LEFT:
            int xl=cells[0].getX()-1;
            int yl=cells[0].getY();
            return new cell(xl,yl);
        
            
    }
    return null;
    
    }
    public cell[] getCells() {
        return cells;
    }
    
    public String toString () {
        return Arrays.toString(cells);//显示蛇
    }
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值