Commit 6b040c15 authored by 谌继荃's avatar 谌继荃

拼图

parent 9c63baab
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>拼图</title>
<script src="http://yun.duiba.com.cn/db_games/libs0924/fyge2018.minSpine.js" crossorigin="anonymous"></script> <script src="http://yun.duiba.com.cn/db_games/libs0924/fyge2018.minSpine.js" crossorigin="anonymous"></script>
<style> <style>
html, html,
...@@ -27,20 +27,6 @@ ...@@ -27,20 +27,6 @@
background-image: url("https://yun.duiba.com.cn/db_games/activity/game/1550472986/resource/assets/playscene/playscenebg.jpg"); */ background-image: url("https://yun.duiba.com.cn/db_games/activity/game/1550472986/resource/assets/playscene/playscenebg.jpg"); */
} }
</style> </style>
<script>
Array.prototype.shuffle = function () {
var array = this;
var m = array.length,
t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
</script>
</head> </head>
<body> <body>
......
export const addDragDemo = (stage: FYGE.Stage) => { import { equalTo, fill, shuffle, swap, getIndex } from "./util";
import { GAME_SIZE, GAME_SIZE2, PIC_URLS } from "./gameconfig";
export const addDragDemo = (stage: FYGE.Stage) => {
let startGameText = "开始游戏";
let startFlag = false;
let goneStep = 0;
let gameDuration = 60;
let timer = null;
const gameWrapper = new FYGE.Sprite(); const gameWrapper = new FYGE.Sprite();
let startGame = new FYGE.TextField();
let goneStepText = new FYGE.TextField();
let countDownText = new FYGE.TextField();
stage.addChild(gameWrapper); stage.addChild(gameWrapper);
const PIC_URLS = [ const onShuffle = () => {
'//yun.duiba.com.cn/aurora/assets/cd16134f2544202ed5676adbd5114286aec44347.png', shuffle(gamedata);
'//yun.duiba.com.cn/aurora/assets/c55dcd277542a6c3e983278ae5835d89848b9bd9.png', // 随机到正确答案后应该再随机一次
'//yun.duiba.com.cn/aurora/assets/b42c49baaf8754139d2e940fe97ba1cb289b11fa.png', if (equalTo(INIT_DATA, gamedata)) {
'//yun.duiba.com.cn/aurora/assets/4a4b850995da348ccd5fb520d5b9ef5e0fb8349f.png', shuffle(gamedata);
'//yun.duiba.com.cn/aurora/assets/5b3e3b52f406df1543e2eeca1ce11719a28c6401.png', }
'//yun.duiba.com.cn/aurora/assets/cae0529d1ff2eaa323dc0615d12a1f05e3a67c06.png', };
'//yun.duiba.com.cn/aurora/assets/5b91853f9d87de73e319d42ea9df139ddd89d537.png',
'//yun.duiba.com.cn/aurora/assets/a2893c5a0d03ca3252160de1584584e18abe2a4a.png', const reset = () => {
'//yun.duiba.com.cn/aurora/assets/c36b0851f61a812e6d745bc205c0551b6590d11d.png', goneStep = 0;
] gameDuration = 60;
clearInterval(timer);
const fill = (size) => { timer = null;
const list = []; stage.removeChildren(countDownText, goneStepText);
for (let i = 0; i < size; i++) { list.push(i) } startFlag = true;
return list; startGameText = "重新开始";
};
const onStartGame = () => {
// 清除点击事件
startGame.removeEventListener(FYGE.MouseEvent.CLICK, onStartGame, this);
console.log("startGameText", startGameText);
reset();
renderGoneStep();
countDown();
onShuffle(); //打乱图片,后续需要和INIT_DATA做对比
renderGame();
addStartGameBtn();
};
// 开始游戏按钮
const addStartGameBtn = () => {
startGame.text = startGameText;
startGame.fillColor = "#000000";
startGame.size = 50;
startGame.x = 250;
startGame.y = 700;
startGame.addEventListener(FYGE.MouseEvent.CLICK, onStartGame, this);
stage.addChild(startGame);
};
addStartGameBtn();
// 当前已走
const renderGoneStep = () => {
if (goneStep > 0) {
goneStepText.text = `当前已走${goneStep}步`;
goneStepText.fillColor = "#000000";
goneStepText.size = 50;
goneStepText.x = 225;
goneStepText.y = 800;
stage.addChild(goneStepText);
} }
};
renderGoneStep();
const countDown = () => {
timer = setInterval(async () => {
if (gameDuration === 0) {
clearInterval(timer);
timer = null;
startFlag = false;
alert("游戏结束");
stage.removeChild(countDownText);
} else {
gameDuration -= 1;
renderCountDown();
}
}, 1000);
};
// 倒计时
const renderCountDown = () => {
countDownText.text = `还剩${gameDuration}s`;
countDownText.fillColor = "#000000";
countDownText.size = 50;
countDownText.x = 275;
countDownText.y = 900;
stage.addChild(countDownText);
};
const INIT_DATA: any = fill(GAME_SIZE * GAME_SIZE2); //数据为图片id,从0开始(图片索引)
const GAME_SIZE = 3; let gamedata = JSON.parse(JSON.stringify(INIT_DATA)); //深拷贝
const INIT_DATA: any = fill(GAME_SIZE * GAME_SIZE);//数据为图片id,从0开始(图片索引)
let gamedata = JSON.parse(JSON.stringify(INIT_DATA));//深拷贝
gamedata.shuffle();//打乱图片,后续需要和INIT_DATA做对比
console.log( console.log(
'初始打乱的图片数据', "初始打乱的图片数据",
gamedata.map(i => (i + 1)) gamedata.map((i) => i + 1)
) );
function getIndex(row, col, maxCol) {
let index;
index = row * maxCol + col;
return index
}
//鼠标按下起始点 //鼠标按下起始点
let startPoint; let startPoint;
//图片起始位置 //图片起始位置
let currPicOriginPos; let currPicOriginPos;
// DisplayObject 显示对象类。
let currentPic: FYGE.DisplayObject; let currentPic: FYGE.DisplayObject;
const PIC_SIZE = 200; const PIC_SIZE = 200;
...@@ -55,11 +122,15 @@ export const addDragDemo = (stage: FYGE.Stage) => { ...@@ -55,11 +122,15 @@ export const addDragDemo = (stage: FYGE.Stage) => {
currentPic.x = currPicOriginPos.x + mouseOffsetX; currentPic.x = currPicOriginPos.x + mouseOffsetX;
currentPic.y = currPicOriginPos.y + mouseOffsetY; currentPic.y = currPicOriginPos.y + mouseOffsetY;
} };
const onMouseUp_pic = () => { const onMouseUp_pic = () => {
//鼠标抬起后应该移出舞台移动事件,否则会重复添加事件 //鼠标抬起后应该移出舞台移动事件,否则会重复添加事件
gameWrapper.stage.removeEventListener(FYGE.MouseEvent.MOUSE_MOVE, onStageMove, this); gameWrapper.stage.removeEventListener(
FYGE.MouseEvent.MOUSE_MOVE,
onStageMove,
this
);
const picCenterX = currentPic.x + PIC_SIZE / 2; const picCenterX = currentPic.x + PIC_SIZE / 2;
const picCenterY = currentPic.y + PIC_SIZE / 2; const picCenterY = currentPic.y + PIC_SIZE / 2;
...@@ -68,7 +139,7 @@ export const addDragDemo = (stage: FYGE.Stage) => { ...@@ -68,7 +139,7 @@ export const addDragDemo = (stage: FYGE.Stage) => {
const dropRow = Math.floor(picCenterY / PIC_SIZE); const dropRow = Math.floor(picCenterY / PIC_SIZE);
const dropIndex = getIndex(dropRow, dropCol, GAME_SIZE); const dropIndex = getIndex(dropRow, dropCol, GAME_SIZE);
console.log('drop index', dropIndex); console.log("drop index", dropIndex);
const dropId = gamedata[dropIndex]; const dropId = gamedata[dropIndex];
const dropPic = getPicDisplayById(dropId); const dropPic = getPicDisplayById(dropId);
...@@ -76,14 +147,19 @@ export const addDragDemo = (stage: FYGE.Stage) => { ...@@ -76,14 +147,19 @@ export const addDragDemo = (stage: FYGE.Stage) => {
const currentPicId = getPicId(currentPic); const currentPicId = getPicId(currentPic);
const currentPicIndex = gamedata.indexOf(currentPicId); const currentPicIndex = gamedata.indexOf(currentPicId);
console.log('currentPicIndex', currentPicIndex); console.log("currentPicIndex", currentPicIndex);
console.log("dropPic", dropPic);
console.log('上一个数据', gamedata.map(i => (i + 1)))
if (dropPic?.x >= 0) {
console.log(
"上一个数据",
gamedata.map((i) => i + 1)
);
swap(currentPicIndex, dropIndex, gamedata); swap(currentPicIndex, dropIndex, gamedata);
console.log(
console.log('交换后的数据', gamedata.map(i => (i + 1))) "交换后的数据",
gamedata.map((i) => i + 1)
);
currentPic.x = dropPic.x; currentPic.x = dropPic.x;
currentPic.y = dropPic.y; currentPic.y = dropPic.y;
...@@ -92,62 +168,72 @@ export const addDragDemo = (stage: FYGE.Stage) => { ...@@ -92,62 +168,72 @@ export const addDragDemo = (stage: FYGE.Stage) => {
if (equalTo(INIT_DATA, gamedata)) { if (equalTo(INIT_DATA, gamedata)) {
setTimeout(() => { setTimeout(() => {
alert('哈哈,赢了') alert("恭喜你拼图成功了!");
}, 500); clearInterval(timer);
timer = null;
stage.removeChildren(countDownText);
}, 300);
} else {
goneStep++;
renderGoneStep();
} }
} else {
FYGE.Tween.get(currentPic).to(
{ x: currPicOriginPos.x, y: currPicOriginPos.y },
200
);
// setTimeout(() => {
// alert("错误");
// currentPic.x = currPicOriginPos.x;
// currentPic.y = currPicOriginPos.y;
// }, 100);
} }
};
const onMouseDown_picItem = (event: FYGE.MouseEvent) => { const onMouseDown_picItem = (event: FYGE.MouseEvent) => {
// 没有开始游戏不让动
if (!startFlag) {
return;
}
currentPic = event.target; currentPic = event.target;
//图片鼠标弹起事件,事件触发一次即移除,否则会重复添加鼠标弹起事件 //图片鼠标弹起事件,事件触发一次即移除,否则会重复添加鼠标弹起事件
currentPic.once(FYGE.MouseEvent.MOUSE_UP, onMouseUp_pic, this); currentPic.once(FYGE.MouseEvent.MOUSE_UP, onMouseUp_pic, this);
//添加舞台移动事件,鼠标移动即触发 //添加舞台移动事件,鼠标移动即触发
//FYGE.MouseEvent.MOUSE_MOVE 会在鼠标移动过程中触发 //FYGE.MouseEvent.MOUSE_MOVE 会在鼠标移动过程中触发
gameWrapper.stage.addEventListener(FYGE.MouseEvent.MOUSE_MOVE, onStageMove, this); gameWrapper.stage.addEventListener(
FYGE.MouseEvent.MOUSE_MOVE,
onStageMove,
this
);
//event事件对象 //event事件对象
//event.stageX,event.stageY当前鼠标在舞台的位置 //event.stageX,event.stageY当前鼠标在舞台的位置
startPoint = { x: event.stageX, y: event.stageY };
//鼠标按下起始点
startPoint = { x: event.stageX, y: event.stageY };
//图片起始位置
currPicOriginPos = { x: currentPic.x, y: currentPic.y }; currPicOriginPos = { x: currentPic.x, y: currentPic.y };
gameWrapper.addChild(currentPic);//把当前图片移动到最上层 gameWrapper.addChild(currentPic); //把当前图片移动到最上层
} };
const PIC_DISPLAY_LIST = INIT_DATA.map((data) =>
FYGE.Sprite.fromUrl(PIC_URLS[data])
); //图片视图数据(列表)
const getPicDisplayById = (id) => PIC_DISPLAY_LIST[id]; //获取视图数据方法
const PIC_DISPLAY_LIST = INIT_DATA.map(data => FYGE.Sprite.fromUrl(PIC_URLS[data]));//图片视图数据(列表)
const getPicDisplayById = (id) => PIC_DISPLAY_LIST[id];//获取视图数据方法
const getPicId = (picDisplay) => { const getPicId = (picDisplay) => {
for (let i = 0; i < PIC_DISPLAY_LIST.length; i++) { for (let i = 0; i < PIC_DISPLAY_LIST.length; i++) {
const element = PIC_DISPLAY_LIST[i]; const element = PIC_DISPLAY_LIST[i];
if (element == picDisplay) if (element == picDisplay) return i;
return i
} }
return -1; return -1;
} //你会看到用索引的好处 }; //你会看到用索引的好处
const equalTo = (array1: any[], array2: any[]) => {
if (array1.length != array1.length) return false;
const len = array1.length || array1.length;
for (let i = 0; i < len; i++) {
const a = array1[i];
const b = array2[i];
if (a != b)
return false;
}
return true;
}//判断数组是否相等
//数组元素交换
const swap = (index1, index2, list) => {
const ele1 = list[index1];
const ele2 = list[index2];
list[index1] = ele2;
list[index2] = ele1;
return list;
}
//生成游戏 //生成游戏
const renderGame = () => {
gamedata.map((data, index) => { gamedata.map((data, index) => {
const picItem = gameWrapper.addChild(getPicDisplayById(data)); const picItem = gameWrapper.addChild(getPicDisplayById(data));
const col = index % GAME_SIZE; const col = index % GAME_SIZE;
...@@ -157,11 +243,14 @@ export const addDragDemo = (stage: FYGE.Stage) => { ...@@ -157,11 +243,14 @@ export const addDragDemo = (stage: FYGE.Stage) => {
picItem.y = row * (PIC_SIZE + GAP); picItem.y = row * (PIC_SIZE + GAP);
//增加鼠标按下事件 //增加鼠标按下事件
picItem.addEventListener(FYGE.MouseEvent.MOUSE_DOWN, onMouseDown_picItem, this); picItem.addEventListener(
FYGE.MouseEvent.MOUSE_DOWN,
onMouseDown_picItem,
this
);
return picItem; return picItem;
}); });
};
renderGame();
};
}
export const PIC_URLS = [
"//yun.duiba.com.cn/aurora/assets/cd16134f2544202ed5676adbd5114286aec44347.png",
"//yun.duiba.com.cn/aurora/assets/c55dcd277542a6c3e983278ae5835d89848b9bd9.png",
"//yun.duiba.com.cn/aurora/assets/b42c49baaf8754139d2e940fe97ba1cb289b11fa.png",
"//yun.duiba.com.cn/aurora/assets/4a4b850995da348ccd5fb520d5b9ef5e0fb8349f.png",
"//yun.duiba.com.cn/aurora/assets/5b3e3b52f406df1543e2eeca1ce11719a28c6401.png",
"//yun.duiba.com.cn/aurora/assets/cae0529d1ff2eaa323dc0615d12a1f05e3a67c06.png",
"//yun.duiba.com.cn/aurora/assets/5b91853f9d87de73e319d42ea9df139ddd89d537.png",
"//yun.duiba.com.cn/aurora/assets/a2893c5a0d03ca3252160de1584584e18abe2a4a.png",
"//yun.duiba.com.cn/aurora/assets/c36b0851f61a812e6d745bc205c0551b6590d11d.png",
"//yun.duiba.com.cn/aurora/assets/cd16134f2544202ed5676adbd5114286aec44347.png",
"//yun.duiba.com.cn/aurora/assets/c55dcd277542a6c3e983278ae5835d89848b9bd9.png",
"//yun.duiba.com.cn/aurora/assets/b42c49baaf8754139d2e940fe97ba1cb289b11fa.png",
];
export const GAME_SIZE = 3;
export const GAME_SIZE2 = 3;
\ No newline at end of file
// 填充数组
export const fill = (size) => {
const list = [];
for (let i = 0; i < size; i++) {
list.push(i);
}
return list;
};
//数组元素交换
export const swap = (index1, index2, list) => {
const ele1 = list[index1];
const ele2 = list[index2];
list[index1] = ele2;
list[index2] = ele1;
return list;
};
//判断数组是否相等
export const equalTo = (array1: any[], array2: any[]) => {
if (array1.length != array1.length) return false;
const len = array1.length || array1.length;
for (let i = 0; i < len; i++) {
const a = array1[i];
const b = array2[i];
if (a != b) return false;
}
return true;
};
export const getIndex = (row, col, maxCol) => {
let index;
index = row * maxCol + col;
return index;
};
export const shuffle = (array: any[]) => {
var m = array.length,
t,
i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment