今天做了一个小小的项目,案例
贪吃蛇游戏。
本实例分为
第一部分
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();
}
}
}
}