egret 简单的一笔画算法,在wing中可以直接跑(以后玩这类游戏就有个作弊器了)
/**
* 在Main中创建游戏场景
* Create a game scene
*/
private createGameScene() {
MtwGame.Instance.init(this.stage);
}
//===========================场景类============================
class MtwGame {
public constructor() {
}
private static _instance: MtwGame;
public static get Instance(): MtwGame {
if (this._instance == null || this._instance == undefined) {
this._instance = new MtwGame();
}
return this._instance;
}
public stage: egret.Stage;
// X ↓; Y →
public mapTitle: Array<Array<number>>;
//障碍
public mapObstacle: Array<Array<number>> = [
[1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3],
[2, 5], [3, 5], [4, 5], [5, 5], [6, 5],
];
public path: Array<Array<number>>;
public width: number = 20;
public strokeWid: number = 3;
public color1: number = 0x00000;//线和阻挡的颜色
public color2: number = 0xe60b0b;//路径的颜色
// public color3: number = 0x00000;
public showTxt2: eui.Label;
pathRect = [];//用于存储路径方块,便于管理
map: MapClass;
point = {};//用于存储阻挡点方块,便于管理
//初始化舞台
public init(stage: egret.Stage): void {
this.stage = stage;
this.initMap(8, 8);//输入地图的长宽(格子个数)
this.initmenu();
this.initBlock();
}
/**点击添加或删除阻挡点*/
protected addObstacle(evt: egret.TouchEvent): void {
let x = Math.floor(Math.floor(evt.localY) / (this.strokeWid + this.width));
let y = Math.floor(Math.floor(evt.localX) / (this.strokeWid + this.width));
let name = x + "_" + y;
if (this.point[name]) {
this.stage.removeChild(this.point[name]);
delete this.point[name];
this.map.update(x, y, false);
} else {
let xx = this.strokeWid * (y + 1) + this.width * y;
let yy = this.strokeWid * (x + 1) + this.width * x;
this.setRect(xx, yy, this.color1, 0.8, name);
this.map.update(x, y, true);
}
}
/**初始化菜单*/
protected initmenu(): void {
let shp: egret.Shape = new egret.Shape();
shp.graphics.beginFill(0xffffff, 1);
shp.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
shp.graphics.endFill();
this.stage.addChild(shp);
let shp1: eui.Rect = new eui.Rect();
shp1.fillAlpha = 0.2;
shp1.width = shp1.height = this.stage.stageWidth;
this.stage.addChild(shp1);
shp1.addEventListener(egret.TouchEvent.TOUCH_END, this.addObstacle, this);
this.showTxt2 = new eui.Label();
this.stage.addChild(this.showTxt2);
this.showTxt2.text = "重新生成路径";
this.showTxt2.size = 32;
this.showTxt2.textAlign = "center";
this.showTxt2.fontFamily = "SimHei";
this.showTxt2.textColor = 0xf3c382;
this.showTxt2.strokeColor = 0x0;
this.showTxt2.stroke = 1;
this.showTxt2.width = Math.floor(this.stage.stageWidth / 3 * 2);
this.showTxt2.x = this.stage.stageWidth - this.showTxt2.width >> 1;
this.showTxt2.y = 680;
this.showTxt2.addEventListener(egret.TouchEvent.TOUCH_END, this.touchHandler, this);
}
/**重新寻路*/
protected touchHandler(evt: egret.TouchEvent): void {
if (this.tt > 0)
clearInterval(this.tt);
this.findPath();
}
/**开始寻路*/
protected findPath(): void {
let returns = this.map.getPath();//输入起点和终点(2,2),(5,5)
this.path = returns;
//显示路径
this.initPath();
}
/**渲染地图*/
protected initBlock(): void {
//横
for (let i = 0; i < this.mapTitle.length + 1; i++) {
let y = i * (this.width + this.strokeWid);
this.setLine(0, y, this.stage.stageWidth, y);
}
//竖
for (let i = 0; i < this.mapTitle[0].length + 1; i++) {
let x = i * (this.width + this.strokeWid);
let height = this.width * this.mapTitle[0].length + this.strokeWid * (this.mapTitle[0].length + 1);
this.setLine(x, 0, x, height);
}
//阻挡点
for (let i = 0; i < this.mapTitle.length; i++) {
for (let j = 0; j < this.mapTitle[i].length; j++) {
if (this.mapTitle[i][j] == 1) {
this.setRect(this.strokeWid * (j + 1) + this.width * j, this.strokeWid * (i + 1) + this.width * i, this.color1, 0.8, i + "_" + j);
}
}
}
}
tt;
/**
* 将路径显示在地图上
*/
protected initPath(): void {
if (this.pathRect.length > 0) {
for (let i = 0; i < this.pathRect.length; i++) {
this.stage.removeChild(this.pathRect[i]);
}
this.pathRect = [];
}
if (this.path && this.path.length > 0) {
let i = 0;
this.tt = setInterval(() => {
if (!this.path[i]) {
clearInterval(this.tt);
return;
}
let x = this.strokeWid * (this.path[i][1] + 1) + this.width * this.path[i][1];
let y = this.strokeWid * (this.path[i][0] + 1) + this.width * this.path[i][0];
this.setRect(x, y, this.color2, 0.3, this.path[i][0] + "_" + this.path[i][1], true);
i++;
}, 50);
}
}
/**
* 初始化地图
* */
protected initMap(w: number, h: number): void {
if (w < 2 || h < 2) console.error("地图格子长宽数不对");
this.mapTitle = [];
for (let i = 0; i < h; i++) {
this.mapTitle.push([]);
for (let j = 0; j < w; j++) {
if (i == 0 || j == 0 || i == h - 1 || j == w - 1) {
this.mapTitle[i][j] = 1;
} else {
this.mapTitle[i][j] = 0;
}
}
}
this.width = (this.stage.stageWidth - (this.mapTitle[0].length + 1) * this.strokeWid) / this.mapTitle[0].length;
this.map = new MapClass(this.mapTitle);
this.initObstacle();
for (let i in this.mapObstacle) {
this.map.update(this.mapObstacle[i][0], this.mapObstacle[i][1], true)
}
}
/**
* 将障碍显示在地图上
* */
protected initObstacle(): void {
for (let i = 0; i < this.mapObstacle.length; i++) {
if (this.mapObstacle[i][0] < this.mapTitle.length - 1 && this.mapObstacle[i][0] > 0
&& this.mapObstacle[i][1] < this.mapTitle[0].length - 1 && this.mapObstacle[i][1] > 0)
this.mapTitle[this.mapObstacle[i][0]][this.mapObstacle[i][1]] = 1;
}
}
//划线
public setLine(x: number, y: number, x1: number, y1: number): egret.Shape {
var shp: egret.Shape = new egret.Shape();
shp.touchEnabled = false;
shp.graphics.lineStyle(this.strokeWid, this.color1);
shp.graphics.moveTo(x, y);
shp.graphics.lineTo(x1, y1);
shp.graphics.endFill();
this.stage.addChild(shp);
return shp;
}
//划块
protected setRect(x: number, y: number, col: number, alpha: number = 1, name: string, bool: boolean = false): egret.Shape {
var shp: egret.Shape = new egret.Shape();
shp.touchEnabled = false;
shp.graphics.beginFill(col, alpha);
shp.graphics.drawRect(x, y, this.width, this.width);
shp.graphics.endFill();
this.stage.addChild(shp);
if (bool) {
this.pathRect.push(shp);//记录路径
} else {
this.point[name] = shp;//记录阻挡点
}
return shp;
}
}
//================================================寻路的类(核心代码)===============================================
class MapClass {
public closeArr: { [pos: string]: number };//已经用过的格子
public openArr: { [pos: string]: Array<number> };//可以走的格子
public Arr: { [pos: string]: number };//所有的格子
public pathArr: Array<Array<number>>;//当前的只能连1路奇点
openObj;//记录所有用过的格子的信息,用于最后找到终点后的寻路
public constructor(mapArr: Array<Array<number>>) {
this.Arr = {};
for (let i = 0; i < mapArr.length; i++) {
for (let j = 0; j < mapArr.length; j++) {
this.Arr[i + "_" + j] = mapArr[i][j];
}
}
}
count: number;//迭代次数
allCount: number;
/**
* 获得最短路径;
* x1,y1是起点;x2,y2是终点
*/
public getPath(): any {
this.closeArr = {};
this.openArr = {};
this.count = 0;
this.pathArr = [];
for (let key in this.Arr) {
if (this.Arr[key] == 0)
this.openArr[key] = [Number(key.split("_")[0]), Number(key.split("_")[1])]
}
this.allCount = Object.keys(this.openArr).length;
if(this.returnOneStrockes())
return this.pathArr;
else{
console.log("不能一笔画完");
}
}
public jiArr: Array<Array<number>>;//当前的只能连1路奇点
public ouArr: Array<Array<number>>;//记录偶点
/**
* 判断this.openArr里面的奇点的情况
* 返回能否一步走完
*/
public returnOneStrockes(): boolean {
if (Object.keys(this.openArr).length == 1) {
for(let key in this.openArr){
this.pathArr.push([this.openArr[key][0],this.openArr[key][1]]);
}
return true;
}
this.count++;
if (this.count > this.allCount)//每个格子都走一遍时,表示不能一笔画
return false
this.jiArr = [];
this.ouArr = [];
for (let key in this.openArr) {
let four = this.getfour(this.openArr[key][0], this.openArr[key][1]);
if (four.length % 2 == 1) {//记录奇点
if (four.length == 1) {
this.jiArr.push([this.openArr[key][0], this.openArr[key][1]])
}
}
if (four.length % 2 == 0) {//记录偶点
this.ouArr.push([this.openArr[key][0], this.openArr[key][1]])
}
if (four.length == 0 && Object.keys(this.openArr).length > 1) //1.还有多个点时,就存在无路的点
return false;
if (this.jiArr.length > 2)//2.一路的奇点最多存在两个
return false
}
if (this.jiArr.length % 2 != 0 && this.jiArr.length != 1)//3.偶数个奇点时//奇数个奇点时,必须存在一个一路的奇点
return false;
if (Object.keys(this.openArr).length > 0 && this.jiArr.length > 0) {//存在奇点
let xy: Array<number> = [this.jiArr[0][0], this.jiArr[0][1]];
if (this.pathArr.length != 0) {//刚开始寻路时,以第一个奇点为起点
let lastP: Array<number> = [this.pathArr[this.pathArr.length - 1][0], this.pathArr[this.pathArr.length - 1][1]];
let four = this.getfour(lastP[0], lastP[1]);
if (four.length == 1) {
xy = four[0];
} else {
let ouXy: Array<number> = [];
let jiStr: Array<string> = this.toStringArr(this.jiArr);
let ouStr: Array<string> = this.toStringArr(this.ouArr);
for (let i in four) {
let fours:string = four[i].join("#");
if (jiStr.indexOf(fours) > -1) {
xy = [four[i][0], four[i][1]];
}
if (ouStr.indexOf(fours) > -1 && ouXy == []) {
ouXy = [four[i][0], four[i][1]];
}
}
if (!xy) xy = ouXy;
}
}
this.pathArr.push([xy[0], xy[1]]);
delete this.openArr[xy[0] + "_" + xy[1]];
this.closeArr[xy[0] + "_" + xy[1]] = 1;
return this.returnOneStrockes();
} else {//全为偶点
let xy: Array<number> = [this.ouArr[0][0], this.ouArr[0][1]];
if (this.pathArr.length != 0) {//刚开始寻路时,以第一个偶点为起点
let lastP: Array<number> = [this.pathArr[this.pathArr.length - 1][0], this.pathArr[this.pathArr.length - 1][1]];
xy = this.getfour(lastP[0], lastP[1])[0];
}
this.pathArr.push([xy[0], xy[1]]);
delete this.openArr[xy[0] + "_" + xy[1]];
this.closeArr[xy[0] + "_" + xy[1]] = 1;
return this.returnOneStrockes();
}
}
public toStringArr(arr:Array<Array<number>>):Array<string>{
let strArr:Array<string> = [];
for(let i in arr){
strArr.push(arr[i].join("#"))
}
return strArr;
}
//获得四个方向的格子[x,y,]
public getfour(x1: number, y1: number): Array<Array<number>> {
let siGe: Array<Array<number>> = [];
let str1: string = x1 + 1 + "_" + y1;
let str2: string = x1 - 1 + "_" + y1;
let str3: string = x1 + "_" + (y1 + 1);
let str4: string = x1 + "_" + (y1 - 1);
if (this.Arr[str1] == 0 && !this.closeArr[str1])
siGe.push([x1 + 1, y1]);
if (this.Arr[str2] == 0 && !this.closeArr[str2])
siGe.push([x1 - 1, y1]);
if (this.Arr[str3] == 0 && !this.closeArr[str3])
siGe.push([x1, y1 + 1]);
if (this.Arr[str4] == 0 && !this.closeArr[str4])
siGe.push([x1, y1 - 1]);
return siGe;
}
/**更新地图的阻挡物 */
public update(x1: number, y1: number, bool: boolean): void {
this.Arr[x1 + "_" + y1] = bool ? 1 : 0;
}
public dispose(): void {
this.closeArr = null;
this.openArr = null;
this.Arr = null;
this.openObj = null;
}
}

egret 简单的一笔画算法,在wing中可以直接跑(以后玩这类游戏就有个作弊器了)的更多相关文章
- egret 简单的四方向的a星寻路,在wing中可以直接跑
/** * main类中加载场景 * 创建游戏场景 * Create a game scene */ private createGameScene() { MtwGame.Instance.init ...
- 简单易学的机器学习算法——EM算法
简单易学的机器学习算法——EM算法 一.机器学习中的参数估计问题 在前面的博文中,如“简单易学的机器学习算法——Logistic回归”中,采用了极大似然函数对其模型中的参数进行估计,简单来讲即对于一系 ...
- 简单的理解deflate算法
简单的理解deflate算法 最近做压缩算法. 用到了deflate压缩算法, 找了很多资料, 这篇文章算是讲的比较易懂的, 这篇文章不长,但却浅显易懂, 基本上涵盖了我想要知道的所有要点. 翻译 ...
- 简单易学的机器学习算法—SVD奇异值分解
简单易学的机器学习算法-SVD奇异值分解 一.SVD奇异值分解的定义 假设M是一个的矩阵,如果存在一个分解: 其中的酉矩阵,的半正定对角矩阵,的共轭转置矩阵,且为的酉矩阵.这样的分解称为M的奇 ...
- 简单易学的机器学习算法—基于密度的聚类算法DBSCAN
简单易学的机器学习算法-基于密度的聚类算法DBSCAN 一.基于密度的聚类算法的概述 我想了解下基于密度的聚类算法,熟悉下基于密度的聚类算法与基于距离的聚类算法,如K-Means算法之间的区别. ...
- 算法课上机实验(一个简单的GUI排序算法比较程序)
(在家里的电脑上Linux Deepin截的图,屏幕大一点的话,deepin用着还挺不错的说) 这个应该是大二的算法课程上机实验时做的一个小程序,也是我的第一个GUI小程序,实现什么的都记不清了,只记 ...
- KMP算法 --- 在文本中寻找目标字符串
KMP算法 --- 在文本中寻找目标字符串 很多时候,为了在大文本中寻找到自己需要的内容,往往需要搜索关键字.这其中就牵涉到字符串匹配的算法,通过接受文本和关键词参数来返回关键词在文本出现的位置.一般 ...
- 【Oracle】-【LRU和DBWR】-LRU算法与DBWR中的应用
Oracle体系结构中经常看到LRU算法,Least Recently Used,也有叫“最近最少使用页面置换算法”,简单讲,Oracle会将内存中最近不用的数据库移出内存以腾出空间来加载另外的数据. ...
- 利用“海底捞算法”在MongoDB中优雅地存储一棵树
目前常见的树形结构数据库存储方案有以下四种,但是在处理无限深度.海量数据的树结构时,都存在一些问题: 1)Adjacency List(邻接表):每个节点仅记录父节点主键.优点是简单,缺点是访问子树需 ...
随机推荐
- 移动web开发中input等输入框问题
移动端web开发时,input等输入框在安卓和iso中都有问题,分别有:1.iso不能点击其他区域使得输入框失去焦点2.iso输入框失去焦点后,键盘产生的空白部分不消失3.安卓端输入框得到焦点后,输入 ...
- 2019金融科技风往哪儿吹?蚂蚁金服联合20余家金融机构预测新年热点:5G、区块链上榜
2019年,金融科技的风向标在哪里?哪些板块成新宠,哪些科技成潮流? 1月4日,蚂蚁金服ATEC城市峰会在上海举行.大会上,蚂蚁金服与20余家金融机构一起预测了2019年金融科技的发展. “未来金融会 ...
- HDU 5299 Circles Game
HDU 5299 思路: 圆扫描线+树上删边博弈 圆扫描线有以下四种情况,用set维护扫描线与圆的交点,重载小于号 代码: #pragma GCC optimize(2) #pragma GCC op ...
- selenium 模拟拖动滚动条下拉
senium做自动化测试的过程中,有的页面需要下拉滚动条才能全部加载完成,否则加载不出来就定位不到想要的元素. 参考链接:http://www.cnblogs.com/landhu/p/5761794 ...
- windows 定时备份linux 上oracle 数据库
1.bat 脚本 exp root/root@10.0.0.0:1521/feiye grants=y owner=root file='E:\code\environment\mysqlBackDa ...
- textarea跟随内容自动伸缩高度实现方案
监听input事件,然后将textarea的style.height设置为最低高度(19px),进而获取到元素的scrollHeight,然后将scroolHeight设置为style.height
- SWUST OJ(1028)
特定字符序列的判断 #include <iostream> #include <cstdlib> #include <stack> #include <str ...
- MemSQL Start[c]UP 2.0 - Round 1E. Three strings
题意:给3个字符串,问从1到min(l1,l2,l3)的长度的子串,找到从该位置长度为l,三个子串相同的三元组的个数 题解:把3个子串用分隔符串起来.然后分开统计每个节点在三个串中出现次数.最后乘起来 ...
- 启用hyper-v后无法卸载vmware
机器上启用了hyper-v,想卸载vmware,报错了: Error: This product may not be installed on a computer that has Microso ...
- 将VMware虚拟机系统镜像导入到ESXi vSphere
原因: 公司有一个VMware虚拟机的交叉编译镜像,但主机性能不行,因此需要将镜像导入ESXi vSphere 过程: 1.将WMware虚拟机克隆; 2.将虚拟机的多个磁盘文件合并成一个;(否则vS ...