您的位置:首页 > 编程语言 > Java开发

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

2018-01-20 11:39 411 查看
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]=cre
8f15
ateHead(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);//显示蛇

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA