cmd版贪吃蛇_贪吃蛇老版本无敌版

cmd版贪吃蛇_贪吃蛇老版本无敌版importjava.awt.*;importjava.util.LinkedList;importjava.util.Scanner;/***@authoraachen0*@date2018/3/2713:56*IDE:IntelliJIDEA*/publicclassSnakeGame{staticfinalintWIDTH=…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

import java.awt.*; import java.util.LinkedList; import java.util.Scanner; /**  * @author aachen0  * @date 2018/3/27 13:56  * IDE:IntelliJ IDEA  */ public class SnakeGame {  static final int WIDTH = 40, HEIGHT = 10;  static char[][] map = new char[HEIGHT][WIDTH];  public static void main(String[] args) {  SnakeGame snakeGame = new SnakeGame();  snakeGame.initBackground();//初始化背景,放只虫子  SnakeLine snakeLine = new SnakeLine();  snakeLine.initSnake();//初始化一条蛇  snakeGame.putSnakeInMap(snakeLine);  snakeGame.show();//显示一下  //键盘移动蛇进行游戏  Scanner scanner = new Scanner(System.in);  int move;  while (true) {  System.out.println("输入AWSD控制蛇的移动,Q退出游戏");  String choice = scanner.next();  switch (choice) {  case "a":  case "A":  move = 2;  break;  case "s":  case "S":  move = 1;  break;  case "w":  case "W":  move = 3;  break;  case "d":  case "D":  move = 0;  break;  case "q":  case "Q":  int points=snakeLine.snakePoints.size();  snakeGame.putGameOverInMap(points);  default:  System.out.println("输入有误,请重试");  continue;  }  if (snakeLine.move(move) == -1) {  snakeGame.putGameOverInMap(snakeLine.snakePoints.size());  snakeGame.show();  break;  }  snakeGame.putSnakeInMap(snakeLine);  snakeGame.show();  }  }  //用字符画背景  private void initBackground() {  for (int i = 0; i < HEIGHT; i++) { 
   //外围控制行  for (int j = 0; j < WIDTH; j++) { 
   //内循环控制各行的第几个  this.map[i][j] = (j == 0 || (j == WIDTH - 1) || i == 0 || (i == HEIGHT - 1)) ? '*' : ' ';  }  }  }  //显示背景  public void show() {  int height = map.length;  int width = map[0].length;  for (int i = 0; i < height; i++) {  for (int j = 0; j < width; j++) {  System.out.print(map[i][j]);  }  System.out.println();  }  }  //把加到地图  void putSnakeInMap(SnakeLine snakeLine) {  Point p;  this.initBackground();  map[SnakeLine.food.y][SnakeLine.food.x] = SnakeLine.worm;  for (int i = 0; i < snakeLine.snakePoints.size(); i++) {  p = snakeLine.snakePoints.get(i);  if (p.y > 0 && p.y < HEIGHT - 1 && p.x > 0 && p.x < WIDTH - 1) {  map[p.y][p.x] = (i == 0) ? snakeLine.head : snakeLine.body;  } else {  putGameOverInMap(snakeLine.snakePoints.size());  }  }  }  void putGameOverInMap(int points) {  char[] gameOver = ("GameOver Score:"+(points-3)).toCharArray();  for (int i = 0; i < gameOver.length; i++) {  map[HEIGHT / 2 - 1][i + (WIDTH - gameOver.length) / 2] = gameOver[i];  }  show();  System.exit(1);  } } class SnakeLine {  static final int RIGHT = 0, DOWN = 1, LEFT = 2, UP = 3;  static final char head = 'O', body = 'o', worm = '~';//头和身体表示  static Point food = new Point((int) (Math.random() * (SnakeGame.WIDTH - 2)) + 1, (int) (Math.random() * (SnakeGame.HEIGHT - 2)) + 1);  private void newFood() {  food = new Point((int) (Math.random() * (SnakeGame.WIDTH - 2)) + 1, (int) (Math.random() * (SnakeGame
            .HEIGHT - 2)) + 1);  }  LinkedList<Point> snakePoints = new LinkedList<>();//蛇的身体内容  void initSnake() {  Point head = new Point(SnakeGame.WIDTH / 2, SnakeGame.HEIGHT / 2);  snakePoints.addFirst(head);// snakePoints.addLast(new Point(head.x - 1, head.y));  snakePoints.addLast(new Point(head.x - 2, head.y));  }  int move(int orient) {  Point p = snakePoints.getFirst();  Point np = null;  switch (orient) {  case SnakeLine.RIGHT:  np = new Point(p.x + 1, p.y);  break;  case SnakeLine.LEFT:  np = new Point(p.x - 1, p.y);  break;  case SnakeLine.DOWN:  np = new Point(p.x, p.y + 1);  break;  case SnakeLine.UP:  np = new Point(p.x, p.y - 1);  break;  }  if (snakePoints.contains(np)) {  return -1;//咬到自己了  }  snakePoints.addFirst(np);  if (np.equals(food)) { 
   //吃到食物了  newFood();  return 2;  }  snakePoints.removeLast();  return 1;  } }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/197011.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号