首页 技术 正文
技术 2022年11月9日
0 收藏 336 点赞 2,197 浏览 2542 个字
 package com.myproj.snake; public class Node {
private int i,j;
public Node(){}
public Node(int i, int j) {
super();
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + i;
result = prime * result + j;
return result;
}
@Override
public 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;
} }

Node.java

 package com.myproj.snake; import java.util.LinkedList; public class Snake {
private LinkedList<Node> nodes=new LinkedList<Node>();
public static final int UP=-5;
public static final int DOWN=5;
public static final int LEFT=-1;
public static final int RIGHT=1;
private int dir;//当前方向;
public Snake(){
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));
this.dir=RIGHT;
}
public boolean contains(int i,int j){
return nodes.contains(new Node(i,j));
}
public void move(){
nodes.removeLast();
Node head=nodes.getFirst();
int i=head.getI()+dir/5;
int j=head.getJ()+dir%5;
nodes.addFirst(new Node(i,j));
}
public void move(int dir){
if(this.dir+dir==0){
System.out.println("不能逆向行驶!");
System.out.println("游戏终止!");
System.exit(0);
}
this.dir=dir;
move();
}
}

Snake.java

 package com.myproj.snake; public class SnakePan {
private Snake snake=new Snake();
public Snake getSnake() {
return snake;
}
public void setSnake(Snake snake) {
this.snake = snake;
}
public void paint(){
for(int i=0;i<=15;i++){
for(int j=0;j<=40;j++){
if(i==0||i==15){
System.out.print("-");
}else if(j==0||j==40){
System.out.print("|");
}else if(snake.contains(i,j)){
System.out.print("■");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}

SnakePan

 package com.myproj.snake; import java.util.Scanner; public class SnakeTest {
public static void main(String[] args){
SnakePan snakePan=new SnakePan();
Snake snake=snakePan.getSnake();
snakePan.paint();
/*for(int i=0;i<10;i++){
snakePan.paint();
snake.move();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/ Scanner scanner=new Scanner(System.in);
System.out.println("请输入方向【上(U)、下(D)、左(L)、右(R)】:");
while(true){
String dir=scanner.nextLine();
if(dir.equalsIgnoreCase("U")){
snake.move(snake.UP);
}else if(dir.equalsIgnoreCase("D")){
snake.move(snake.DOWN);
}else if(dir.equalsIgnoreCase("L")){
snake.move(snake.LEFT);
}else if(dir.equalsIgnoreCase("R")){
snake.move(snake.RIGHT);
}else{
System.exit(0);
}
snakePan.paint();
}
}
}

SnakeTest.java

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898