// 随机抽取N张牌
create = function(n){
carddata = [];
leveldata = [];
// 创建所有牌
for(var i=1; i<=total; i++){
carddata.push(i);
}
// 抽取牌
for(var i=0; i<n; i++){
var curcard = carddata.splice(Math.random()*carddata.length, 1).pop();
leveldata.push({'cardno':curcard,'turn':0}, {'cardno':curcard,'turn':0});
}
// 生成随机顺序游戏牌
leveldata = shuffle(leveldata);
}
// 生成牌
show = function(){
var cardhtml = '';
for(var i=0; i<leveldata.length; i++){
cardhtml += '<div>';
cardhtml += '<div>';
cardhtml += '<div><img src="images/card' + leveldata[i]['cardno'] + '.jpg"></div>';
cardhtml += '<div><img src="https://www.linuxidc.com/images/cardbg.jpg"></div>';
cardhtml += '</div>';
cardhtml += '</div>';
}
setHtml('gameplane', cardhtml);
}
// 全部翻转
turnall = function(){
for(var i=0; i<leveldata.length; i++){
turn_animate(i);
}
}
// 翻转动画
turn_animate = function(key){
var obj = $_tag('div', 'card' + key);
var cardfont, cardback;
if(getClass(obj[0]).indexOf('out')!=-1){
cardfont = obj[0];
cardback = obj[1];
}else{
cardfont = obj[1];
cardback = obj[0];
}
setClass(cardback, 'list flip out');
var et = setTimeout(function(){
setClass(cardfont, 'list flip in');
}, 225);
}
// 设置点击事件
set_event = function(){
var o = $_tag('div', 'gameplane');
for(var i=0,count=o.length; i<count; i++){
if(getClass(o[i])=='card viewport-flip'){
o[i].onclick = function(){
turn(this.id);
}
}
}
}
// 计时开始
process = function(){
is_lock = 0;
var curtime = gametime[level];
setHtml('livetime', curtime);
var et = setInterval(function(){
if(matchnum==cardnum){
clearInterval(et);
return ;
}
curtime--;
setHtml('livetime', curtime);
if(curtime==0){
clearInterval(et);
is_over = 1;
message('fail', start);
}
}, 1000);
}
// 游戏讯息动画
message = function(type, callback){
is_lock = 1;
var message = $('message');
var processed = 0;
var opacity = 0;
var soundtime = {
'start': 1500,
'success': 4000,
'fail': 6000,
'clear': 4000
};