贪吃蛇 部分代码

这是贪吃蛇的代码,虽然还没有全部写完,呵呵不过现在只学到这里。

 

蛇身节点代码:

package day13;
/*
 * 蛇的节点
 */
public class Node {
 int i;// 行

 int j; // 列

 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 (this == obj)
   return true;
  if (obj == null)
   return false;
  if (obj instanceof Node) {
   Node o = (Node) obj;
   return this.i == o.i && this.j == o.j;
  }
  return false;
 }

 public int hashCode() {
  return i * 1000 + j;
 }

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

 

 

 

 

 

 

蛇的类

package day13;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * 实现“贪吃蛇”游戏Worm类相关方法1. step() 2. step(int direction)
 *
 *
 */
public class Worm {
 /** 为头 0 */
 private LinkedList<Node> body = new LinkedList<Node>();

 private int direction;

 /*
  * 贪吃蛇屏幕 00 01 02 03 04 05 06 07 08 09 10 j 01 # # # 02 03 04 05 i
  */

 public static final int UP = -10;// -10 i-1, j+0

 public static final int RIGHT = 1;// 01 i+0, j+1

 public static final int LEFT = -1;// -01 i+0, j-1

 public static final int DOWN = 10;// 10 i+1, j+0

 public Worm() {

 }

 public Worm(Node... nodes) {
  for (int i = 0; i < nodes.length; i++) {
   Node n = nodes[i];
   body.add(n);
  }
  

 }

 public Worm(int direction, Node... nodes) {
  this.direction = direction;
  this.body.addAll(Arrays.asList(nodes));
 }

 public static int getDOWN() {
  return DOWN;
 }

 public static int getLEFT() {
  return LEFT;
 }

 public static int getRIGHT() {
  return RIGHT;
 }

 public static int getUP() {
  return UP;
 }

 public void setBody(LinkedList<Node> body) {
  this.body = body;
 }

 public Worm(int direction, List<Node> body) {
  this.direction = direction;
  this.body.addAll(body);
 }

 // 继续走
 public void step() {
  Node head = (Node) body.get(0);
  int i = head.getI() + direction / 10;
  int j = head.getJ() + direction % 10;
  body.add(0, new Node(i, j));
  if(eatSomething(j, j)){
   return ;
  }
  body.remove(body.size() - 1);
 }

 private boolean eatSomething(int i ,int j) {
  
  return false;
 }

 // 向指定方向走
 public void step(int direction) {
  if (this.direction + direction == 0) {
   throw new IllegalArgumentException("不能掉头走");
  }
  this.direction = direction;
  step();
 }

 public boolean contains(int i, int j) {
//  for (int n = 0; n < body.size(); n++) {
//   Node node = (Node) body.get(n);
//   if (node.getI() == i && node.getJ() == j) {
//    return true;
//   }
//  }
//  return false;
  for(Iterator ite =body.iterator();ite.hasNext();){
   Node n = (Node)ite.next();
   if(n.getI()==i&&n.getJ()==j){
    return true;
   }
  }
  
  
  
  return false;
 }

 public void print() {
  System.out.println(body);
 }

 public void setDirection(int direction) {
  this.direction = direction;
 }

 public int getDirection() {
  return direction;
 }

 public LinkedList<Node> getBody() {
  return body;
 }
}

 

 

蛇面板代码:

 

 

 

package day13;
/*
 * 面板
 */
public class WormPane {
 private Worm worm;
 private int rows=20;
 private int cols=20;
 public WormPane(Worm worm) {
  this.worm = worm;
 }
/*
 * 上边下边输出“-”
 * 左右边框输出“|”
 * 蛇身子用$表示
 */
 public void print() {//以字符的形式输出面板和蛇的形象
  for (int i = 0; i < rows; i++)
  {
   for (int j = 0; j < cols; j++)
   {
    if (i==0||i==rows-1)
    {
     System.out.print("-");
    }else if(j==0||j==cols-1)
    {
     System.out.print("|");
    }else if (worm.contains(i, j))
    {
     System.out.print("$");
    }else{
     System.out.print(" ");
    }
   }
   System.out.println();
  }
 }

}

 

蛇面板代码:

package day13;

import java.util.Scanner;

public class WormDemo {

 /**
  * 控制蛇行走:
  *   回车继续,
  *   W up
  *  s down
  *   a left
  *   d right
  *   q quit
  */
 public static void main(String[] args) {
  Node[] nodes = {
    new Node(1, 1),
    new Node(1, 2),
    new Node(1, 3),
    new Node(1, 4),
    new Node(1, 5),
    new Node(1, 6) };
  Worm worm = new Worm(Worm.LEFT, nodes);
  WormPane pane = new WormPane(worm);
  Scanner sca = new Scanner(System.in);
  while (true) {
   pane.print();
   String cmd = sca.nextLine();
   if ("".equalsIgnoreCase(cmd)) {
    worm.step();
   } else if ("w".equalsIgnoreCase(cmd)) {
    worm.step(Worm.UP);
   } else if ("".equalsIgnoreCase(cmd)) {
    worm.step();
   } else if ("s".equalsIgnoreCase(cmd)) {
    worm.step(Worm.DOWN);
   } else if ("a".equalsIgnoreCase(cmd)) {
    worm.step(Worm.LEFT);
   } else if ("d".equalsIgnoreCase(cmd)) {
    worm.step(Worm.RIGHT);
   } else if ("q".equalsIgnoreCase(cmd)) {
    break;
   }
  }

 }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值