300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > java编写 超简易贪吃蛇

java编写 超简易贪吃蛇

时间:2022-04-19 20:21:54

相关推荐

java编写 超简易贪吃蛇

. 这是本人早些在别的地方发过的帖子了,加上本靓仔有点懒(很懒),备注只给了一些关键的地方(应该够了吧。。。。)

. 最后,如果大家觉的还可以,还请大家给点赞给我,你们的鼓励也就是我坚持的动力

/*** @see 8 is UP,* @see 2 is DOWN,* @see 4 is LEFT,* @see 6 is RIGHT;* @author 一只蓝色的水杯子* @docends Don it go to negative direction ,* and if ask you doing The last step,you no don it want do it,* you can go do Enter*/public class SnaleTest {@SuppressWarnings({"static-access", "resource" })public static void main(String[] args) {NodePanel nodes = new NodePanel();Snakes snakes = (Snakes)nodes.getSnakes();/*for(int sum = 0;sum< 10;sum ++) {nodes.prints();snakes.step();try {Thread.sleep(1000);//暂停一秒} catch (InterruptedException e) {e.printStackTrace();}}*/Scanner imports = new Scanner(System.in);snakes.eat();nodes.prints();for(;;) {System.out.println("Please Import:");String dir = imports.nextLine();if(dir.equalsIgnoreCase("8")) {snakes.step(snakes.UP);}else if(dir.equalsIgnoreCase("2")) {snakes.step(snakes.DOWN);}else if(dir.equalsIgnoreCase("4")) {snakes.step(snakes.LEFT);}else if(dir.equalsIgnoreCase("6")) {snakes.step(snakes.RIGHT);}else if(dir.equalsIgnoreCase("no")) {System.out.println("GameOver!");System.exit(0);}else {snakes.step();}snakes.yes();nodes.prints();if(snakes.yeah()) snakes.eat();}}}/*** @see 8 is UP,* @see 2 is DOWN,* @see 4 is LEFT,* @see 6 is RIGHT;* @author 一只蓝色的水杯子* @docends Don it go to negative direction ,* and if ask you doing The last step,you no don it want do it,* you can go do Enter*/public class SnaleTest {@SuppressWarnings({"static-access", "resource" })public static void main(String[] args) {NodePanel nodes = new NodePanel();Snakes snakes = (Snakes)nodes.getSnakes();/*for(int sum = 0;sum< 10;sum ++) {nodes.prints();snakes.step();try {Thread.sleep(1000);//暂停一秒} catch (InterruptedException e) {e.printStackTrace();}}*/Scanner imports = new Scanner(System.in);snakes.eat();nodes.prints();for(;;) {System.out.println("Please Import:");String dir = imports.nextLine();if(dir.equalsIgnoreCase("8")) {snakes.step(snakes.UP);}else if(dir.equalsIgnoreCase("2")) {snakes.step(snakes.DOWN);}else if(dir.equalsIgnoreCase("4")) {snakes.step(snakes.LEFT);}else if(dir.equalsIgnoreCase("6")) {snakes.step(snakes.RIGHT);}else if(dir.equalsIgnoreCase("no")) {System.out.println("GameOver!");System.exit(0);}else {snakes.step();}snakes.yes();nodes.prints();if(snakes.yeah()) snakes.eat();}}}class Node {private int i,j;public Node(int i,int j) {super();this.i = i;this.j = j;}public Node() {}public void setJ(int j) {this.j = j;}public void setI(int i) {this.i = i;}public int getJ() {return j;}public int getI() {return i;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + i;result = prime * result + j;return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Node other = (Node) obj;if (i != other.i)return false;if (j != other.j)return false;return true;}}class NodePanel {private Snakes snakes = new Snakes();public Snakes getSnakes() {return snakes;}public void prints() {int j,i;//,sum = 5,num = 5;for(i = 0;i< 10;i ++) {for(j = 0;j < 32;j ++) {if(snakes.contaninss(i, j)) {System.out.print("#");}else if(j == 0||j == 31) {System.out.print("|");}else if(i == 0 || i == 9) {System.out.print("+");}else if(i == snakes.score && j == snakes.count){System.out.print("0");}else {System.out.print(" ");}}System.out.println();}}}class Snakes {LinkedList<Node> nodes= new LinkedList<Node>();Random random = new Random();public static final int UP = -10;public static final int DOWN = 10;public static final int LEFT = -1;public static final int RIGHT = 1;private int dir;//存储当前方向public int sum;public int num;public int score;public int count;public void step() {//去尾巴nodes.removeLast();//加头Node head = nodes.getFirst();Node last = nodes.getLast();int i = head.getI() + dir / 10;int j = head.getJ() + dir % 10;nodes.addFirst(new Node(i,j));sum = i;num = j;if(yeah()) nodes.add(new Node(last.getI(),last.getJ()));}public void step(int dir) {if(this.dir + dir == 0) throw new RuntimeException("Don it go to negative direction !");this.dir = dir;step();}public Snakes() {nodes.add(new Node(1,5));nodes.add(new Node(1,4));nodes.add(new Node(1,3));nodes.add(new Node(1,2));nodes.add(new Node(1,1));//this.dir = RIGHT;}public void eat() {score = random.nextInt(8)+1;count = random.nextInt(30)+1;}public boolean yeah() {boolean answer = false;if(sum == score && num == count)answer = true;return answer;}public void yes() {if(sum == 0 || sum == 9 || num == 0 || num == 31)throw new RuntimeException("Game over,In the dust!");}//判断该位置是否有对象public boolean contaninss(int i,int j) {return nodes.contains(new Node(i,j));}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。