微信小程序黑白块游戏



代码如下:

//play.js
// play
var app = getApp()
Page({
data: {
typeName: '计时模式',
score: 0,
time: 60,
shouldStop: false,
blockData:[]
},
onReady: function(){
var array = [];
// 先生成一个10个长度的数组
for(var i = 0; i < 10; i++){
// 生成一个随机位数为1的数组
var orderArray = [0,0,0,0];
var randomNum = Math.floor(Math.random() * 4);
orderArray[randomNum] = 1;
array.push({id: i, block: orderArray});
}
this.setData({
blockData: array.reverse()
});
},
handleClick: function(events){
var id = events.currentTarget.id;
var line = id.split("-")[1];
var column = id.split("-")[2];
var isBlack = id.split("-")[3];
var blockData = this.data.blockData.reverse();
var score = this.data.score;
var orderArray = [0,0,0,0];
// 判断是否是第一行
if(line != blockData[0].id){
this.handleWrong(0, score);
return;
}
// 判断是否正确
if(isBlack != 1){
this.handleWrong(1, score);
return;
} // 正确下一个
// 分数++
// 最后一个小块的id为分数+10
score++;
orderArray[Math.floor(Math.random() * 4)] = 1;
blockData.push({id: score+10, block: orderArray});
blockData.shift();
this.setData({
silding: true,
score: score,
blockData: blockData.reverse()
});
},
handleWrong: function( type , score){
const titleArr = ["请点击第一个白块!游戏结束", "别点白块!游戏结束", "时间到"];
var _this = this;
wx.showToast({
title: titleArr[type],
icon: 'cancel',
duration: 2000,
complete: function(){
// 将此分数存入全局变量
app.globalData.currentScore = score;
// 停止计数器
_this.setData({
shouldStop: true
});
// 若此分数比最高分数还高 将其存入本地
if(score > app.globalData.timeScore){
app.globalData.timeScore = score;
wx.setStorageSync('timeScore',score);
}
var timer = setTimeout(function(){
wx.redirectTo({
url: '../end/end?type=time&score=' + score
})
clearTimeout(timer);
}, 2000);
}
})
},
timeInterval: function(){
var that = this;
var timer = setInterval(function(){
// 判断是否小于0
var nowTime = that.data.time; if(that.data.shouldStop){
clearInterval(timer);
} if(nowTime > 1){
that.setData({
time: nowTime-1
});
return;
} that.setData({
time: nowTime-1
});
that.handleWrong(2, that.data.score);
clearInterval(timer);
}, 1000);
},
onLoad: function(){
var that = this;
wx.setNavigationBarTitle({
title: that.data.typeName
});
this.timeInterval();
}
})

急速模式

//play.js
// // play
var app = getApp()
Page({
data: {
typeName: '急速模式',
score: 0,
blockData:[],
scrollHeight: 0,
canRun: false
},
onLoad: function(){
var that = this;
// 设置title
wx.setNavigationBarTitle({
title: that.data.typeName
});
},
onReady: function(){
var array = [];
// 先生成一个10个长度的数组
for(var i = 0; i < 10; i++){
array.push(this.getNewLine(i));
}
this.setData({
blockData: array.reverse()
});
},
handleClickWhite: function(events){
// 点击白块一定会报错 差别在于报错文案
// 判断是否是点击的第一行
// 被点击的id
var id = events.currentTarget.id;
// 被点击的行
var line = id.split("-")[1];
// 数据
var blockData = this.data.blockData.concat().reverse();
// 当前分数
var score = this.data.score; // 判断是否是第一行
if(line != this.getClickableBlockLine(blockData)){
this.handleWrong("请点击第一个黑块!游戏结束", score);
} else {
// 点击的第一行白块
this.handleWrong("别点白块!游戏结束", score);
}
},
handleClickBlack: function(events){
// 黑块是应该点击的块
// 判断是否是点击的第一行
// 被点击的id
var id = events.currentTarget.id;
// 被点击的行
var line = id.split("-")[1];
// 数据
var blockData = this.data.blockData.concat().reverse();
// 当前分数
var score = this.data.score;
// 可点击的第一行
var clickableLine = this.getClickableBlockLine(blockData); // 判断是否是第一行
if(line == clickableLine){
// 点击了第一行黑块
// 判断是否是是第一次
if(score == 0){
// 启动滑动程序
this.run();
}
score++; // 将黑块变灰块
this.getBlockBlackToGray(line, blockData);
// 分数++
this.setData({
score: score,
blockData: blockData.concat().reverse()
}); } else {
// 点击的不是第一行白块
this.handleWrong("请点击第一个黑块!游戏结束", score);
}
},
handleClickGray: function(events){
// 灰块是指黑块点击之后的块
// 其在显示是白块 并且同样不可点
var score = this.data.score;
this.handleWrong("别点白块!游戏结束", score);
},
run: function(){
// 滑动方法
var that = this;
var speed = 10; this.setData({
canRun: true
}); var timer = setInterval(function(){
// 当前滑动距离
if(!that.data.canRun){
clearInterval(timer);
return;
} var currentScrollHeight = that.data.scrollHeight;
// 当前分数
var score = that.data.score;
// 滑块数据
var blockData = that.data.blockData.concat().reverse(); if(Math.abs(currentScrollHeight) == 150){
// 滑到临界点
// 判断是否过期
// 判断条件是 第一个滑块的状态是否为已点击
if(that.checkFirstLineBlockClicked(blockData[0].block)){
// 没过期
// 继续 去除旧节点 插入新节点 scrolllHeight归0
var newId = blockData[blockData.length - 1].id + 1;
blockData.push(that.getNewLine(newId));
blockData.shift();
that.setData({
scrollHeight: 0,
blockData: blockData.concat().reverse()
});
return;
} // 过期
// 报错
that.handleWrong("请点击白块!游戏结束", score);
return;
} currentScrollHeight = currentScrollHeight - speed;
that.setData({
scrollHeight: currentScrollHeight
});
}, 20);
},
checkFirstLineBlockClicked: function(blockDataLine){ for(var i = 0; i < blockDataLine.length; i++){
if(blockDataLine[i] == 2){
return true;
}
} return false;
},
getBlockBlackToGray: function(line, blockData){
for(var i = 0; i < blockData.length; i++){
if(blockData[i].id == line){
var currentArray = blockData[i].block;
for(var j = 0; j < currentArray.length; j++){
if(currentArray[j] == 1){
currentArray[j] = 2;
return;
}
}
}
}
},
getClickableBlockLine: function(blockData){
var line = 0;
for(var i = 0; i < blockData.length; i++){
var block = blockData[i].block;
for(var j = 0; j < block.length; j++){
// 行内四个元素 有1即可
if(block[j] == 1){
return blockData[i].id;
}
}
}
return line;
},
getNewLine: function(i){
// 生成一个标准的数据
var orderArray = [0,0,0,0];
// 生成一个随机数
var randomNum = Math.floor(Math.random() * 4);
// 赋值给对应的obj
orderArray[randomNum] = 1;
return {id: i, block: orderArray};
},
handleWrong: function(text, score){ this.setData({
canRun: false
}); wx.showToast({
title: text,
icon: 'cancel',
duration: 2000,
complete: function(){
// 将此分数存入全局变量
app.globalData.currentScore = score;
// 若此分数比最高分数还高 将其存入本地
if(score > app.globalData.speedScore){
app.globalData.speedScore = score;
wx.setStorageSync('speedScore',score);
}
var timer = setTimeout(function(){
wx.redirectTo({
url: '../end/end?type=speed&score=' + score
})
clearTimeout(timer);
}, 2000);
}
})
}
})



无尽模式

//play.js
// play
var app = getApp()
Page({
data: {
typeName: '无尽模式',
silding: false,
score: 0,
blockData:[]
},
onReady: function(){
var array = [];
// 先生成一个10个长度的数组
for(var i = 0; i < 10; i++){
// 生成一个随机位数为1的数组
var orderArray = [0,0,0,0];
var randomNum = Math.floor(Math.random() * 4);
orderArray[randomNum] = 1;
array.push({id: i, block: orderArray});
}
this.setData({
blockData: array.reverse()
});
},
handleClick: function(events){
var id = events.currentTarget.id;
var line = id.split("-")[1];
var column = id.split("-")[2];
var isBlack = id.split("-")[3];
var blockData = this.data.blockData.reverse();
var score = this.data.score;
var orderArray = [0,0,0,0];
// 判断是否是第一行
if(line != blockData[0].id){
this.handleWrong(0, score);
return;
}
// 判断是否正确
if(isBlack != 1){
this.handleWrong(1, score);
return;
} // 正确下一个
// 分数++
// 最后一个小块的id为分数+10
score++;
orderArray[Math.floor(Math.random() * 4)] = 1;
blockData.push({id: score+10, block: orderArray});
blockData.shift();
this.setData({
silding: true,
score: score,
blockData: blockData.reverse()
});
},
handleWrong: function( type , score){
const titleArr = ["请点击第一个白块!游戏结束", "别点白块!游戏结束"];
wx.showToast({
title: titleArr[type],
icon: 'cancel',
duration: 2000,
complete: function(){
// 将此分数存入全局变量
app.globalData.currentScore = score;
// 若此分数比最高分数还高 将其存入本地
if(score > app.globalData.endlessScore){
app.globalData.endlessScore = score;
wx.setStorageSync('endlessScore',score);
}
var timer = setTimeout(function(){
wx.redirectTo({
url: '../end/end?type=endless&score=' + score
})
clearTimeout(timer);
}, 2000);
}
})
},
onLoad: function(){
var that = this;
wx.setNavigationBarTitle({
title: that.data.typeName
});
}
})
//结束
var app = getApp()
Page({
data: {
currentScore: 0,
gameType: "",
heighestScore: 0,
backUrl: ""
},
onLoad: function(options){
var score = options.score;
var gameType = options.type;
var text = {endless: "无尽模式", time: "计时模式", speed: "极速模式"};
// 从全局变量中获取分数
this.setData({
currentScore: app.globalData.currentScore,
gameType: text[gameType],
heighestScore: app.globalData[gameType + "Score"],
backUrl: '../'+gameType+'/play'
});
}
})

本文感谢无私开源的程序员点击获取项目

Black-White-Blocks的更多相关文章

  1. Uva 1103 Ancient Messages

    大致思路是DFS: 1. 每个图案所包含的白色连通块数量不一: Ankh : 1 ;  Wedjat : 3  ; Djed : 5   ;   Scarab : 4 ; Was : 0  ;  Ak ...

  2. EOS.IO Technical White Paper v2

    [EOS.IO Technical White Paper v2] Abstract: The EOS.IO software introduces a new blockchain architec ...

  3. Understanding Flash: Blocks, Pages and Program / Erases

    https://flashdba.com/2014/06/20/understanding-flash-blocks-pages-and-program-erases/ In the last pos ...

  4. Ethereum White Paper

    https://github.com/ethereum/wiki/wiki/White-Paper White Paper EditNew Page James Ray edited this pag ...

  5. VSX(翻译)Moving Code Blocks Among Code Regions using VS 2010 Extensions

    Moving Code Blocks Among Code Regions using VS 2010 Extensions (翻译)使用VS 2010 扩展性将代码块移至Region区域中 Down ...

  6. [Codeforces #608 div2]1272B Blocks

    Description There are nnn blocks arranged in a row and numbered from left to right, starting from on ...

  7. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM

    刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...

  8. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  9. 开发该选择Blocks还是Delegates

    前文:网络上找了很多关于delegation和block的使用场景,发现没有很满意的解释,后来无意中在stablekernel找到了这篇文章,文中作者不仅仅是给出了解决方案,更值得我们深思的是作者独特 ...

  10. poj 1390 Blocks

    poj 1390 Blocks 题意 一排带有颜色的砖块,每一个可以消除相同颜色的砖块,,每一次可以到块数k的平方分数.问怎么消能使分数最大.. 题解 此题在徐源盛<对一类动态规划问题的研究&g ...

随机推荐

  1. IntelliJ IDEA 如何在同一个窗口创建多个项目

    一.IntelliJ IDEA与Eclipse的区别   二.在同一个窗口创建多个项目 1.打开IntelliJ IDEA,点击Create New Project 2.Java Enterprise ...

  2. 多对多关联懒加载导致failed to lazily initialize a collection of role: 实体类, could not initialize proxy - no Session 追加配置fetch = FetchType.EAGER解决

    一篇文章需要关联很多个标签,所以他们呈一对多(多对多)的关系 org.springframework.web.util.NestedServletException: Request processi ...

  3. SpringCloud微服务实战三:Hystix的基本概念

    1.说到隔离.熔断.降级,最出名的就是 Netflix 开源的 Hystrix 组件,Hystix官方对它描述为:Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止级联故障,在复 ...

  4. 2019-8-31-C#-程序集数量对软件启动性能的影响

    title author date CreateTime categories C# 程序集数量对软件启动性能的影响 lindexi 2019-08-31 16:55:58 +0800 2018-10 ...

  5. HDFS数据读写过程

  6. mac下更改MySQL的默认编码

    mysql默认的编码是latin1,它不支持中文,所以我们一般需要修改他的默认编码格式. 打开终端1. 进入root权限sudo -i 2. cp /usr/local/mysql/support-f ...

  7. 未加星标 Linux磁盘下查看I/O磁盘的性能

    iostat查看linux硬盘IO性能 rrqm/s:每秒进行merge的读操作数目.即delta(rmerge)/s wrqm/s:每秒进行merge的写操作数目.即delta(wmerge)/s ...

  8. python 中的split()函数和os.path.split()函数

    Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...

  9. 基于MaxCompute的媒体大数据开放平台建设

    摘要:随着自媒体的发展,传统媒体面临着巨大的压力和挑战,新华智云运用大数据和人工智能技术,致力于为媒体行业赋能.通过媒体大数据开放平台,将媒体行业全网数据汇总起来,借助平台数据处理能力和算法能力,将有 ...

  10. node.js install and cecium apply

    ubuntu 18.04 install node.js https://blog.csdn.net/m0_43404744/article/details/94364508