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;
}
@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;
}
}
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));
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class snate extends JFrame implements KeyListener,Runnable { JLabel j; Canvas j1; public static final int canvasWidth = 200; public static final int canvasHeight = 300; public static final int nodeWidth = 10; public static final int nodeHeight = 10; //SnakeModel se=null; //222222 // boolean[][] matrix; LinkedList nodeArray = new LinkedList();//表 Node food;//节点 int maxX; int maxY; int direction = 2; boolean running = false; int timeInterval = 200; double speedChangeRate = 0.75; boolean paused = false; int score = 0; int countMove = 0; // UP and DOWN should be even // RIGHT and LEFT should be odd public static final int UP = 2; public static final int DOWN = 4; public static final int LEFT = 1; public static final int RIGHT = 3; snate() { super(); //setSize(500,400); Container c=getContentPane(); j=new JLabel("Score:"); c.add(j,BorderLayout.NORTH); j1=new Canvas(); j1.setSize(canvasWidth+1,canvasHeight+1); j1.addKeyListener(this); c.add(j1,BorderLayout.CENTER); JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout()); JLabel j2; j2 = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER); p1.add(j2, BorderLayout.NORTH); j2 = new JLabel("ENTER or R or S for start;", JLabel.CENTER); p1.add(j2, BorderLayout.CENTER); j2 = new JLabel("SPACE or P for pause",JLabel.CENTER); p1.add(j2, BorderLayout.SOUTH); c.add(p1,BorderLayout.SOUTH); addKeyListener(this); pack(); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); // begin(); // //2222222 // this.gs = gs; this.maxX = maxX; this.maxY = maxY; // initial matirx matrix = new boolean[maxX][]; for(int i=0; i<maxX; ++i){ matrix[i] = new boolean[maxY]; Arrays.fill(matrix[i],false); } // initial the snake int initArrayLength = maxX > 20 ? 10 : maxX/2; for(int i = 0; i < initArrayLength; ++i){ int x = maxX/2+i; int y = maxY/2; nodeArray.addLast(new Node(x, y)); matrix[x][y] = true; } food = createFood(); matrix[food.x][food.y] = true; } public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_UP) { //se.changeDirection(SnakeModel.UP); } if(e.getKeyCode()==KeyEvent.VK_DOWN) { //se.changeDirection(SnakeModel.DOWN); } if(e.getKeyCode()==KeyEvent.VK_LEFT) { //se.changeDirection(SnakeModel.LEFT); } if(e.getKeyCode()==KeyEvent.VK_RIGHT) { //se.changeDirection(SnakeModel.RIGHT); } if(e.getKeyCode()==KeyEvent.VK_R||e.getKeyCode()==KeyEvent.VK_S||e.getKeyCode()==KeyEvent.VK_ENTER) { } } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void repaint() { Graphics g = j1.getGraphics(); //背景 g.setColor(Color.red); g.fillRect(0,0,canvasWidth,canvasHeight); //蛇 //g.setColor(Color.BLUE); } public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(10,10,10,10); } // //222222 // public void changeDirection(int newDirection){ if (direction % 2 != newDirection % 2){ direction = newDirection; } } public boolean moveOn(){ Node n = (Node)nodeArray.getFirst(); int x = n.x; int y = n.y; switch(direction){ case UP: y--; break; case DOWN: y++; break; case LEFT: x--; break; case RIGHT: x++; break; } if ((0 <= x && x < maxX) && (0 <= y && y < maxY)){ if (matrix[x][y]){ if(x == food.x && y == food.y){ nodeArray.addFirst(food); int scoreGet = (10000 - 200 * countMove) / timeInterval; score += scoreGet > 0? scoreGet : 10; countMove = 0; food = createFood(); matrix[food.x][food.y] = true; return true; } else return false; } else{ nodeArray.addFirst(new Node(x,y)); matrix[x][y] = true; n = (Node)nodeArray.removeLast(); matrix[n.x][n.y] = false; countMove++; return true; } } return false; } public void run(){ running = true; while (running){ try{ Thread.sleep(timeInterval); } catch(Exception e){ break; } if(!paused){ if (moveOn()){ gs.repaint(); } else{ JOptionPane.showMessageDialog( null, "you failed", "Game Over", JOptionPane.INFORMATION_MESSAGE); break; } } } running = false; } private Node createFood(){ int x = 0; int y = 0; do{ Random r = new Random(); x = r.nextInt(maxX); y = r.nextInt(maxY); }while(matrix[x][y]); return new Node(x,y); } public void speedUp(){ timeInterval *= speedChangeRate; } public void speedDown(){ timeInterval /= speedChangeRate; } public void changePauseState(){ paused = !paused; } public String toString(){ String result = ""; for(int i=0; i<nodeArray.size(); ++i){ Node n = (Node)nodeArray.get(i); result += "[" + n.x + "," + n.y + "]"; } return result; } } class Node{ int x; int y; Node(int x, int y){ this.x = x; this.y = y; } } public static void main(String[] args) { //Graphics g=j1.getGraphics(); snate s=new snate(); //s.draw_something(g); //s.setVisible(true); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值