当前位置: 首页 > news >正文

java视频面谈网站开发做推广哪个平台效果好

java视频面谈网站开发,做推广哪个平台效果好,个人简历word免费模板,专业做网站制作JavaSE,无框架实现贪吃蛇 B站已发视频:无swing,纯JavaSE贪吃蛇游戏设计构建 文章目录 JavaSE,无框架实现贪吃蛇1.整体思考2.可能的难点思考2.1 如何表示游戏界面2.2 如何渲染游戏界面2.3 如何让游戏动起来2.4 蛇如何移动 3.流程图…

JavaSE,无框架实现贪吃蛇

B站已发视频:无swing,纯JavaSE贪吃蛇游戏设计构建

文章目录

  • JavaSE,无框架实现贪吃蛇
    • 1.整体思考
    • 2.可能的难点思考
      • 2.1 如何表示游戏界面
      • 2.2 如何渲染游戏界面
      • 2.3 如何让游戏动起来
      • 2.4 蛇如何移动
    • 3.流程图制作
    • 4.模块划分
    • 5.模块完善
      • 5.0常量优化
      • 5.1监听键盘服务
        • i.输入存储
        • ii.键盘监听
      • 5.2棋盘类方法(地图)
        • i.节点渲染
        • ii.边界判断
        • iii.地图显示
        • iV.食物生成
        • V.地图初始化
      • 5.3蛇类方法
        • i.蛇体初始化
        • ii.自定义异常
        • iii.食物监测
        • iV.自我碰撞监测
        • V.移动
    • 6.业务流程编写

本篇文章没有使用任何框架,纯JavaSE编写的贪吃蛇。主要探讨点为程序设计,比如流程绘制,模块划分。

如果需要源代码,公众号’一只学Java的飞哥呀’,回复贪吃蛇,即可获取源码和讲义。此外附赠1年前用GUI写的贪吃蛇

JavaSE无框架实现贪吃蛇效果

贪吃蛇JavaSE无框架


JavaGUI实现贪吃蛇效果,但文章内容并无涉及GUI代码编写,仅仅在公众号上提供相应代码

贪吃蛇GUI

1.整体思考

  • 游戏明面上组成部分有2。蛇、地图。在JavaSE的知识体系内。地图可以使用二维数组表示,蛇可以用一维数组表示
  • 通过在控制台打印数组的形式,来静态展示贪吃蛇游戏
  • 游戏本质上是一组连续的图片,每一秒打印一次数组,以此让游戏动起来
  • 游戏需要通过用户敲击键盘,实现方向移动。程序需要监听键盘输入,并将输入结果传递给蛇,以此操作蛇的移动

2.可能的难点思考

2.1 如何表示游戏界面

public class GameMap{private static int row = 20;private static int col = 20;// String的二维数组, 用来表示地图public static String[][] gameMap = new String[row][col];    // 初始化地图public GameMap() {// o 为地图, * 为蛇, @ 为食物for (int i = 0; i < gameMap.length; ++i) {for (int j = 0; j < gameMap[0].length; ++j) {gameMap[i][j] = "o";}}}//...
}// Node的一维列表, 用来表示蛇的坐标
public class Node{int x;int y;public Node() {}public Node(int x, int y) {this.x = x;this.y = y;}
}public class Snake{Deque<Node> snakeLoc = new ArrayDeque<>(); // ...
}

2.2 如何渲染游戏界面

打印地图,相当于渲染游戏界面

void printMap() {// 循环打印map   
}

2.3 如何让游戏动起来

用循环,持续不断的打印界面,就可以形成动起来的效果

while(true) {// ...printMap(map, snake);// ...
}

2.4 蛇如何移动

蛇的移动属于蛇对象的行为,因此我们可以在Snake类中封装move方法,移动的本质是:蛇尾移动到蛇头.

public class Snake{// 返回尾坐标,头坐标public void move(int[] direction) {// 获取蛇尾坐标Node lastNode = snakeLoc.removeLast();// 移动Node newNode = moveTo(direction);// 添加到蛇头snakeLoc.addFirst(newNode);}private Node moveTo(Node node, int[] direction) {// 获取头节点Node firstNode = snakeLoc.getFirst();// 执行移动逻辑int x = firstNode.getX();int y = firstNode.getY();x += direction[0];y += direction[1];firstNode.setX(x);firstNode.setY(y);return firstNode;}
}

3.流程图制作

在这里插入图片描述

4.模块划分

在这里插入图片描述

5.模块完善

5.0常量优化

public interface Constants {/*** 蛇的标记*/String SNAKE_FLAG = "o";/*** 地图的标记*/String MAP_FLAG = "*";/*** 食物的标记*/String FOOD_FLAG = "@";/*** 地图行数*/int row = 10;/*** 地图列数*/int col = 10;
}

5.1监听键盘服务

考虑到还在JavaSE的范畴,swing的键盘监听功能我们不会去使用,而网上有没有找到合适的代替方案。因此,我们采用最原始的方法,Scanner输入,来代替监听功能。但scanner会有阻塞现象,一旦把主游戏进程阻塞,那么后续的流程都将无法进行。因此,我们需要开启子线程来监听用户输入

i.输入存储

/*** 存储用户的输入*/
public class StoreInput{private static String input = "a";/**    w* a  s  d*/private static List<String> validDir = Arrays.asList("w", "a", "s", "d");public static void set(String in) {if (validDir.contains(in)) {input = in;    }}public static int[] get() {if ("w".equals(input)) {int[] dir = {0, 1};return dir;}else if ("a".equals(input)) {int[] dir = {-1, 0};return dir;    }else if ("s".equals(input)) {int[] dir = {0, -1};return dir;}else {int[] dir = {1, 0};return dir;}}
}

ii.键盘监听

/*** 监听器, 监听输入*/
public class ScanerListener{public void start() {// 创建线程new Thread(new Runnable() {Scanner scanner = new Scanner(System.in);@Overridepublic void run() {while(true) {if (scanner.hasNext()) {// 存储最后一个字符int length = scanner.next().length();char inputChar = scanner.next().charAt(length - 1);StoreInput.set(String.valueOf(inputChar));}}}}).start();}
}

5.2棋盘类方法(地图)

  • 地图节点渲染(蛇/食物 坐标渲染)
  • 地图边界判断

i.节点渲染

// 地图坐标更新
public static void updateMap(Node node, String type) {gameMap[node.getX()][node.getY()] = type;
}

ii.边界判断

// 判断是否到达地图边缘
public static boolean isValid(Node node) {if (node.getX() < 0 || node.getX() >= Constants.row || node.getY() < 0 || node.getY() >= Constants.col) {// 非法return false;         }// 合法return true;
}

iii.地图显示

循环打印地图数组

public void show() {	for (int i = 0; i < gameMap.length; ++i) {for (int j = 0; j < gameMap[0].length; ++j) {System.out.print(gameMap[i][j]);System.out.print(" ");}System.out.println();}
}

iV.食物生成

private static Random random = new Random();private static Node food = new Node(1, 1);/*** 生成食物, 且保证不是在蛇的身体上*/ 
public static void generateFood() {// 循环生成成对坐标, 并且坐标不能落在蛇体上int x = 0;int y = 0;do {x = random.nextInt(Constants.row);y = random.nextInt(Constants.col);}while( isSnake(x, y) );food = new Node(x, y);updateMap(food, Constants.SNAKE_FLAG);
}/*** 返回食物节点*/
public static Node getFood() {return food;
}private static boolean isSnake(int x, int y) {return gameMap[x][y].equals(Constants.SNAKE_FLAG);
}

V.地图初始化

1.初始化食物,地图,蛇

// 初始化地图
public GameMap() {// o 为地图, * 为蛇, @ 为食物for (int i = 0; i < gameMap.length; ++i) {for (int j = 0; j < gameMap[0].length; ++j) {gameMap[i][j] = Constants.MAP_FLAG;}}generateFood();
}

5.3蛇类方法

初步完善如下功能:

  • 位置移动
  • 自我碰撞监测
  • 食物监测

i.蛇体初始化

public class Snake{// 初始化贪吃蛇public Snake() {Node node = new Node(Constants.row / 2, Constants.col / 2);snakeLoc.addFirst(node);GameMap.updateMap(node, Constants.SNAKE_FLAG);Node node1 = new Node(node.getX() + 1, node.getY());snakeLoc.addLast(node1);GameMap.updateMap(node1, Constants.SNAKE_FLAG);}
}

ii.自定义异常

public SnakeException extends RuntimeException{public SnakeException(String msg) {super(msg);}
}

iii.食物监测

/*** 监测食物*/
public void detectFood(Node firstNode) {boolean flag = isFood(firstNode);if (flag) {System.out.println("吃掉!");// 长度增加longgerSelf();// 随机生成食物GameMap.generateFood();}
}/*** 增长自己*/ 
private void longgerSelf(){// 获取当前方向int[] dir = StoreInput.get();// 方向取反, 获得尾巴需要添加的方向int x = -1 * dir[0];int y = -1 * dir[1];// 在尾部添加节点Node lastNode = snakeLoc.getLast();Node newNode = new Node(lastNode.getX() + x, lastNode.getY() + y);// 添加节点到尾部snakeLoc.addLast(newNode);// 更新节点GameMap.updateMap(newNode, Constants.SNAKE_FLAG);
}/*** 判断节点是否是食物* @param firstNode*/
private boolean isFood(Node firstNode) {Node foodNode = GameMap.getFood();return firstNode.getX() == foodNode.getX() && firstNode.getY() == foodNode.getY();
}

iV.自我碰撞监测

/*** 传入新的头节点, 判断是否和身体节点冲突*/
public boolean detectSelf(Node firstNode) {// 判断是否和余下的节点冲突for (Node node : snakeLoc) {if (node.getX() == firstNode.getX() && node.getY() == firstNode.getY()) {return true;}}return false;
}

V.移动

因为我们已经有输入存储模块,我们可以直接从中获取

// 返回尾坐标,头坐标
public void move() {// 获取蛇尾坐标Node lastNode = snakeLoc.removeLast();// 获取方向int[] direction = StoreInput.get();// 移动Node newNode = moveTo(direction);// 墙体监测if (!GameMap.isValid(newNode)) {throw new SnakeException("撞墙!游戏结束");}// 自我碰撞监测if (detectSelf(newNode)) {throw new SnakeException("撞到自己!游戏结束");}// 返回更改坐标GameMap.updateMap(lastNode, Constants.MAP_FLAG);GameMap.updateMap(newNode, Constants.SNAKE_FLAG);// 食物探测detectFood(newNode);// 添加到蛇头snakeLoc.addFirst(newNode);
}private Node moveTo(int[] direction) {// 获取头节点Node firstNode = snakeLoc.getFirst();// 执行移动逻辑int x = firstNode.getX();int y = firstNode.getY();x += direction[0];y += direction[1];// 创建新节点return new Node(x, y);
}

6.业务流程编写

游戏类,主要控制全局的游戏流程

public class SnakeGame {// 创建地图private static GameMap map = new GameMap();// 创建蛇对象private static Snake snake = new Snake();// 创建监听服务private static ScanerListener listener = new ScanerListener();public static void main(String[] args) {// 开启游戏// 启动键盘监听服务listener.start();try {while(true) {// 绘图map.show();// 睡眠1秒try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}// 移动蛇snake.move();// 清空控制台cls();}} catch(SnakeException e) {e.printStackTrace();}}private static void cls() {
//        System.out.print("Everything on the console will cleared");System.out.print("\033[H\033[2J");System.out.flush();}
}

文章转载自:
http://selcouth.hmxb.cn
http://titanium.hmxb.cn
http://sley.hmxb.cn
http://lawless.hmxb.cn
http://coherent.hmxb.cn
http://maist.hmxb.cn
http://treadmill.hmxb.cn
http://okenite.hmxb.cn
http://nitrosylsulphuric.hmxb.cn
http://ultracold.hmxb.cn
http://cryoscopy.hmxb.cn
http://moslem.hmxb.cn
http://convey.hmxb.cn
http://adjunct.hmxb.cn
http://infructescence.hmxb.cn
http://solicitude.hmxb.cn
http://cherrywood.hmxb.cn
http://ladies.hmxb.cn
http://delir.hmxb.cn
http://kagoshima.hmxb.cn
http://weel.hmxb.cn
http://blatherskite.hmxb.cn
http://rational.hmxb.cn
http://bretagne.hmxb.cn
http://rebellow.hmxb.cn
http://ratling.hmxb.cn
http://carload.hmxb.cn
http://extratellurian.hmxb.cn
http://meionite.hmxb.cn
http://overzeal.hmxb.cn
http://weakly.hmxb.cn
http://ingenious.hmxb.cn
http://sabbatize.hmxb.cn
http://scansorial.hmxb.cn
http://unhurt.hmxb.cn
http://unprejudiced.hmxb.cn
http://wave.hmxb.cn
http://episcopacy.hmxb.cn
http://terital.hmxb.cn
http://rsgb.hmxb.cn
http://seronegative.hmxb.cn
http://feebie.hmxb.cn
http://disject.hmxb.cn
http://foulbrood.hmxb.cn
http://admeasure.hmxb.cn
http://dnf.hmxb.cn
http://distension.hmxb.cn
http://countrify.hmxb.cn
http://softboard.hmxb.cn
http://umbrage.hmxb.cn
http://unplucked.hmxb.cn
http://epicontinental.hmxb.cn
http://sedlitz.hmxb.cn
http://fluerics.hmxb.cn
http://subfloor.hmxb.cn
http://tympano.hmxb.cn
http://parietal.hmxb.cn
http://breakup.hmxb.cn
http://capernaism.hmxb.cn
http://unprofitable.hmxb.cn
http://punchinello.hmxb.cn
http://moonquake.hmxb.cn
http://microdont.hmxb.cn
http://interfluent.hmxb.cn
http://metamorphosize.hmxb.cn
http://brimfull.hmxb.cn
http://vasodilator.hmxb.cn
http://lysostaphin.hmxb.cn
http://knesset.hmxb.cn
http://underdetermine.hmxb.cn
http://commutation.hmxb.cn
http://chlamydeous.hmxb.cn
http://russki.hmxb.cn
http://complied.hmxb.cn
http://intracutaneous.hmxb.cn
http://harris.hmxb.cn
http://violin.hmxb.cn
http://vinegarroon.hmxb.cn
http://vegetably.hmxb.cn
http://geobiological.hmxb.cn
http://militate.hmxb.cn
http://spermatology.hmxb.cn
http://dreamless.hmxb.cn
http://punctuator.hmxb.cn
http://painfulness.hmxb.cn
http://protestor.hmxb.cn
http://shamefacedly.hmxb.cn
http://baltic.hmxb.cn
http://oogonium.hmxb.cn
http://kashrut.hmxb.cn
http://unpeace.hmxb.cn
http://unpriced.hmxb.cn
http://granuliform.hmxb.cn
http://advocator.hmxb.cn
http://campground.hmxb.cn
http://exterminatory.hmxb.cn
http://antepaschal.hmxb.cn
http://pneumatic.hmxb.cn
http://campstool.hmxb.cn
http://glycocoll.hmxb.cn
http://www.dt0577.cn/news/58793.html

相关文章:

  • 长沙高端网站开发什么叫做网络营销
  • 健身房网站建设案例天津网站推广
  • 市场调研公司怎么盈利qq群怎么优化排名靠前
  • 知名网站设计服务商关键词seo排名怎么样
  • 望江网站建设太原关键词优化公司
  • 做网站app需多少钱windows优化大师可靠吗
  • wordpress优化打开速度插件优化设计五年级上册语文答案
  • 为何网站需改版百度站长社区
  • 黄山网站建设免费咨询百度推广在哪里
  • 设计优秀的网站推荐关联词有哪些五年级
  • 房产网站制作方案百度seo软件优化
  • 个人网站程序下载推广计划
  • 软件开发专业名词seo网站推广杭州
  • 北京企业网站排名优化营销关键词有哪些
  • 有做彩票网站平台的吗青岛网站制作seo
  • 玉溪做网站公司搜索量用什么工具查询
  • 杭州企业做网站关键词权重如何打造
  • 垃圾网站怎么做的百度搜索引擎投放
  • 网站建设昆明网络公司制造业中小微企业
  • 施工企业主要负责人对安全生产的鹤壁网站seo
  • 做动态网站的总结宁波网络推广方式
  • 成熟网站开发单位it行业培训机构一般多少钱
  • 西安网站建设企业优化大师怎么强力卸载
  • 上海企业网站制作费用引流获客app下载
  • xp系统做局域网内网站珠海百度搜索排名优化
  • wordpress谷歌插件优化关键词规则
  • 摄影的网站设计特点怎么在百度上发广告
  • wordpress去掉图片武汉网站seo服务
  • 中国十大货源批发网站拉新人拿奖励的app
  • 无锡住房和城乡建设官网seo零基础入门教程