这是贪吃蛇的代码,虽然还没有全部写完,呵呵不过现在只学到这里。
蛇身节点代码:
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;
}
}
}
}