简单的贪吃蛇游戏

本文介绍了一款简单的贪吃蛇游戏实现过程,包括游戏的基本结构和核心代码。通过定义节点类Node来表示蛇身的位置,并使用LinkedList进行节点管理,实现了蛇的移动功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天做了一个小小的项目,案例

贪吃蛇游戏。

本实例分为

第一部分

public class Node {
	private int i;
	private int j;

	public Node() {

	}

	public Node(int i, int j) {
		this.i = i;
		this.j = j;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	public int getJ() {
		return j;
	}

	public void setJ(int j) {
		this.j = j;
	}

	public boolean equals(Object obj) {
		if(obj==null){
			return false;
		}
		if(obj==this){
			return true;
		}
		if(obj instanceof Node){
			Node node=(Node)obj;
			return this.i==node.i&&this.j==node.j;
		}
		return false;
	}

	public int hashCode() {
		return 10*this.i+this.j;
	}

	public String toString() {
		return "["+this.i+","+"]";
	}
         
}


第二部分

import java.util.LinkedList;

import com.tarena.day_project_1.Node;

public class Worm {
    private LinkedList<Node> nodes=new LinkedList<Node>();
    private int dir;
    public static final int  UP=-10;
    public  static final int DOWN=10;
    public static final int LEFT=-1;
    public  static final int RIGHT=1;
    public Worm(){
    	this.dir=UP;
    	nodes.add(new Node(3,9));
    	nodes.add(new Node(4,9));
    	nodes.add(new Node(5,9));
    	nodes.add(new Node(5,10));
    	nodes.add(new Node(5,11));
    	nodes.add(new Node(6,11));
    	nodes.add(new Node(7,11));
    	
    }
    public void step(){
    	  //1) 新的头节点坐标:i = i+dir/10  j = j+dir%10 
        //Node head = nodes.getFirst();//取得原来nodes的第1个节点
        Node head =nodes.getFirst();
        int i=head.getI()+dir/10;
        int j=head.getJ()+dir%10;
        		Node newHead=new Node(i,j);
        nodes.addFirst(newHead);
        nodes.removeLast();
        //int i = head.getI()+dir/10;
        //int j = head.getJ()+dir%10;
        //Node newHead = new Node(i,j);
    //把新的头节点,放nodes(LinkedList)第1个位置
       // nodes.addFirst(newHead);
      //2) 尾节点去掉: 
        //nodes.removeLast();   
      }
    //   step(dir),换个方向移动一步
    	public void step(int dir){
    		if(dir+this.dir==0){
    			throw new RuntimeException("不能反向移动");
    		}
    		this.dir=dir;
    		this.step();
    	}
		/* //toString:显示当前这条小蛇各个点的坐标,所有点都在nodes里面
           //LinkedList(nodes):sun重写toString 
		 */
		@Override
		public String toString() {
			return this.nodes.toString();
		}
		public boolean contains(int i ,int j){
			return this.nodes.contains(new Node(i,j));
		}
    	
    
}


第三部分

public class WormPanel {
	private Worm worm;
	private int rows = 10;
	private int clos = 32;

	public WormPanel() {
		worm = new Worm();
	}

	public Worm getWorm() {
		return this.worm;
	}

	public void Print() {
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < clos; j++) {
              if(i==0||i==rows-1){
            	  System.out.print("-");
            	 
              } 
              else if(j==0||j==clos-1){
        		  System.out.print("|");
        	  }
              else if(worm.contains(i, j)){
            	  System.out.print("#");
              }
              else{
            	  System.out.print(" ");
              }
			}
			System.out.println();
		}
	}

}


测试用例

import java.util.Scanner;

public class TestDemo {

	public static void main(String[] args) {
		WormPanel wormPanel = new WormPanel();
		Worm worm = wormPanel.getWorm();
		Scanner scanner = new Scanner(System.in);
		while (true) {
			wormPanel.Print();
			System.out.println(worm);
			String dir = scanner.nextLine();
			if ("u".equalsIgnoreCase(dir)) {
				worm.step(worm.UP);
			}
			else if("D".equalsIgnoreCase(dir)){
				worm.step(worm.DOWN);
			}
			else if("L".equalsIgnoreCase(dir)){
				worm.step(worm.LEFT);
			}
			else if("R".equalsIgnoreCase(dir)){
				worm.step(worm.RIGHT);
			}
			else if("q".equalsIgnoreCase(dir)){
				System.out.println("不玩了");
				break;
			}
			else {
				worm.step();
			}
		}

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值