js 考记忆力得小游戏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
window.onload = function (){
var oStar = document.getElementById("start");
var oSpan = oStar.getElementsByTagName("span")[0]; var oContent = document.getElementById("content");
var oRect = document.getElementById("rect");
var timer = null;
var oTime = document.getElementById("time"); var aImg = document.getElementsByTagName("img"); var arrImg = ["http://img3.douban.com/view/photo/thumb/public/p1994200192.jpg",
"http://img5.douban.com/view/photo/thumb/public/p1994200528.jpg",
"http://img3.douban.com/view/photo/thumb/public/p1994200963.jpg",
"http://img3.douban.com/view/photo/thumb/public/p1994201400.jpg",
"http://img3.douban.com/view/photo/thumb/public/p1994201804.jpg",
"http://img5.douban.com/view/photo/thumb/public/p1994201968.jpg",
"http://img3.douban.com/view/photo/thumb/public/p1994202154.jpg",
"http://img5.douban.com/view/photo/thumb/public/p1994202276.jpg",
"http://img3.douban.com/view/photo/thumb/public/p1994202342.jpg"]; var oScore = document.getElementById("score");
var oFail = document.getElementById("fail");
var oSuccess = document.getElementById("success");
var level = [{"num":"3","timeGame":"10"},
{"num":"4","timeGame":"10"},
{"num":"6","timeGame":"15"},
{"num":"8","timeGame":"20"}]; oSpan.onclick = function (){ var rating = 0;
starGame( level[rating].num,level[rating].timeGame,rating ); } // 游戏程序 参数分别代表 块数 游戏时间 等级
function starGame( num,timeGame,rating){ oStar.style.display = "none";
oFail.style.display = "none";
oSuccess.style.display = "none";
oContent.style.display = "block";
oContent.setAttribute("off","0"); oTime.style.width = "440px";
oScore.innerHTML = 0;
oRect.innerHTML = ""; fillContent(num);
setTimeout(function (){
for(var i = 0; i < aImg.length; i++){
aImg[i].src = "http://img3.douban.com/view/photo/thumb/public/p2191686093.jpg";
}
getTime( oTime,"width", -Math.floor( parseInt( getStyle(oTime,"width") )/timeGame ), timeGame, num*20, rating );
},3000);
} //填充块
function fillContent(size){
var arr = Mix(size,1,2);
//console.log(arr); for(var i = 0; i < arr.length; i++){
oRect.innerHTML += "<img style = 'top:"+ 110*Math.floor(i/4) +"px; left:"+ 110*Math.floor(i%4) +"px' select = 'N' index = " + i + " address = " + arrImg[arr[i]] +" src = "+ arrImg[arr[i]] +" num = "+ arr[i] + ">";
}
} function Mix( max,min,fre){
var arr = [];
for(var i = min; i <= max; i++ ){
arr.push(i);
}
var newarr = [getRandom(arr.length,1)];
for(var i = 0; newarr.length < arr.length*fre; i++ ){ var num = getRandom(arr.length,1); if( toCon(num,fre-1 ) ){
newarr.push(num);
}
} function toCon( num,fre ){
var iNow = 0;
for(var i = 0; i < newarr.length; i++ ){
if( num == newarr[i] && iNow != fre ){
++iNow;
}else if( num == newarr[i] && iNow == fre ){
return false;
}
}
return true;
} return newarr;
} function getTime( obj,attr,speed,time,totle,rating ){
clearInterval(obj.timer); selectRect(totle,rating);
obj.timer = setInterval( function (){ time--;
if( time != 0 ){
obj.style[attr] = parseInt( getStyle(obj,attr) ) + speed + "px";
if( parseInt(oContent.getAttribute("off")) == 1 ){
clearInterval(obj.timer);
}
} if( time == 0 ){
obj.style[attr] = "0px";
clearInterval(obj.timer);
for(var i = 0; i < aImg.length; i++ ){
aImg[i].src = aImg[i].getAttribute("address");
} //console.log( oContent.getAttribute("off") );
if( parseInt(oContent.getAttribute("off"))!= 1 ){ oContent.style.display = "none";
oFail.style.display = "block"; var oP = oFail.getElementsByTagName("p")[0]; oP.onclick = function (){ oFail.style.display = "none";
oStar.style.display = "block"; oSpan.onclick = function (){
starGame(level[0].num,level[0].timeGame,0);
}
}
} }
},1000)
} function selectRect(totle,rating){
var selectNum = 0;
var selectVal = [];
var selectIndex = [];
for(var i = 0; i < aImg.length; i++ ){
aImg[i].style.cursor = "pointer";
aImg[i].onclick = function(){
selectNum++;
this.style.border = "1px red solid";
this.src = this.getAttribute("address");
this.setAttribute("select","Y");
selectVal.push( parseInt( this.getAttribute("num")) );
selectIndex.push( parseInt( this.getAttribute("index")) ); //console.log(selectVal );
if( selectNum == 2 ){
selectNum = 0; if( selectVal.length == 2 ){
if( selectVal[0] == selectVal[1] && selectIndex[0]!= selectIndex[1] ){ toSelectSuccess(totle,rating);
oScore.innerHTML = parseInt( oScore.innerHTML ) + 20;
} else {
toSelectError();
}
}
selectVal = [];
selectIndex = [];
}
}
} function toSelectError(){
setTimeout (function(){
var selArrE = []; for(var i = 0; i < aImg.length; i++ ){
if( aImg[i].getAttribute("select") == "Y"){
selArrE.push( parseInt(aImg[i].getAttribute("index")) );
}
} for(var i = 0; i < aImg.length; i++ ){
for(var j = 0; j < selArrE.length; j++ ){
if( aImg[i].getAttribute("index") == selArrE[j] ){
aImg[i].src = aImg[i].getAttribute("address");
aImg[i].src = "http://img3.douban.com/view/photo/thumb/public/p2191686093.jpg";
aImg[i].style.border = "1px white solid";
aImg[i].setAttribute("select","N");
}
}
}
selArrE.length = 0;
},300);
} function toSelectSuccess( totle,rating){
setTimeout (function(){
var selArrS = [];
for(var i = 0; i < aImg.length; i++ ){
if( aImg[i].getAttribute("select") == "Y"){
selArrS.push( aImg[i].getAttribute("index") );
}
} //console.log(totle); for(var i = 0; i < aImg.length; i++ ){
for(var j = 0; j < selArrS.length; j++ ){
if( aImg[i].getAttribute("index") == selArrS[j] ){
aImg[i].src = aImg[i].getAttribute("address");
aImg[i].style.display = "none";
aImg[i].setAttribute("select","N");
}
}
}
selArrS.length = 0; if( parseInt( oScore.innerHTML ) == parseInt( totle ) ){ oContent.setAttribute("off","1");
oContent.style.display = "none";
oSuccess.style.display = "block"; var oP = oSuccess.getElementsByTagName("p")[0];
oP.onclick = function(){
rating++; if( rating == 4 ){
oStar.style.display = "block";
oFail.style.display = "none";
oSuccess.style.display = "none";
oContent.style.display = "none";
rating = 0; oSpan.onclick = function (){ starGame( level[0].num,level[0].timeGame,0 ); } } starGame( level[rating].num,level[rating].timeGame,rating); } } //console.log( selArr[0],selArr[1] );
var selArr = [];
//console.log( selArr[0],selArr[1] );
//var selArr = [];
},200);
} } function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
} else{
return getComputedStyle(obj)[attr];
}
} function getRandom( max, min){
return Math.floor( Math.random()*( max - min + 1) + min );
}
}
</script>
<style>
*{
padding: 0px;
margin: 0px;
}
#start{
background: url(images/background.jpg) no-repeat;
margin: 50px auto;
position: relative;
width: 800px;
height: 500px;
overflow: hidden;
display: block;
} #start span{
background: pink;
color: white;
position: absolute;
top: 400px;
left: 700px;
cursor: pointer;
padding: 15px;
} #content{
background: #cccccc;
margin: 50px auto;
position: relative;
width: 440px;
height: 440px;
display: none;
}
#content p{
position: absolute;
right: 5px;
top: -20px;
} #content #rect{
width: 440px;
height: 440px;
position: relative;;
} #content img{
border: 1px white solid;
width: 98px;
height: 98px;
margin: 5px;
position: absolute;
} #time{
width: 440px;
background: red;
height: 30px;
position: absolute;
bottom: -30px;
} #fail{
background: black;
margin: 50px auto;
width: 440px;
height: 440px;
display: none;
} #fail p {
margin: 50px auto;
font-size: 120px;
color:white;
cursor: pointer;
} #success{
background: red;
margin: 50px auto;
width: 440px;
height: 440px;
display: none;
} #success p {
margin: 50px auto;
font-size: 120px;
color:white;
cursor: pointer;
}
</style>
</head>
<body>
<div id = "start">
<span>start</span>
</div> <div id = "content" off = "0">
<p>Score: <span id = "score">0</span>point</p>
<div id = "time"></div>
<div id="rect"></div> </div> <div id = "fail">
<p>Failure!</p>
</div> <div id = "success">
<p>Success</p>
</div>
</body>
</html>
js 考记忆力得小游戏的更多相关文章
- 原生JS实现的h5小游戏-植物大战僵尸
代码地址如下:http://www.demodashi.com/demo/12755.html 项目介绍 本项目是利用原生js实现的h5小游戏-植物大战僵尸,主要结合了一下自己对于h5小游戏的理解,结 ...
- js实现表格配对小游戏
js实现表格配对小游戏 一.总结 一句话总结: 二.js实现表格配对 1.配对游戏案例说明 实例描述: 当用户点击两个相同的图案或字符后配对成功,全部配对成功后游戏获胜 案例008采用了大家常见的小游 ...
- JS实现植物大战僵尸小游戏,代码记录及效果展示
前几天看到了一个很有趣的demo,用js制作植物大战僵尸小游戏,本着学习的心态,对照着做了一下,发现这里面的一些代码设计的确很精妙,这里分享下源码和效果,如果有需要,可以看下. 效果如下: 下载地址
- 原生js写的flybird小游戏
游戏地址:http://zangzhihong.jusukeji.com/flybird/index.html html部分 <!DOCTYPE html> <!-- This ...
- css3+jquery+js做的翻翻乐小游戏
主要是为了练习一下css3的3D翻转功能,就做了这么个小游戏,做的比较粗糙,但是效果看的见. 主要用到的css3代码如下: html结构: <div class="container& ...
- 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
回顾 上一节我们搭建了游戏的骨架,添加了四个游戏场景,分别是加载.开始.游戏.结束.那么这一节我们来介绍加载这个场景,顺带丰富一下各个场景的基本内容. Phaser.Loader Phaser框架自带 ...
- 原生JS制作贪吃蛇小游戏
感情都在代码里,来,干了!... <!doctype html> <html> <head> <meta http-equiv="Content-T ...
- js实现点气球小游戏
二话不说直接贴代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- js 面向对象 打气球小游戏
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- MySql数据库 - 2.启动与关闭
MySql服务的启动: 右键计算机 - 管理 - 服务和应用程序 - 服务 - 右键 MySQL80 可以启动/停止MySQL(也可以点击属性 改变启动类型:手动/自动). 自动启动状态下,每次打开计 ...
- Linux常用命令与基本概念
复制 文件的复制 cp 源文件 目标文件 文件夹的复制 cp -r 源文件夹 目标文件夹 删除 删除文件 rm 文件名 删除文件夹 rm -rf 文件夹 查看文件类型 file 文件名 压缩与解压 z ...
- jquery 实践操作:div 动态嵌套(追加) div
此片记录在指定 div 中动态添加 div html(): append(): 在被选元素的结尾(但仍在元素内部)插入指定的内容. 语法: $(selector).append(content); / ...
- Python操作MySQL[转]
本篇对于Python操作MySQL主要使用两种方式: 1.原生模块pymsql. 2.ORM框架SQLAchemy. pymsql pymsql是Python中操作MySQL的模块,其使用方法和MyS ...
- node 监听接口
var http = require('http'); var mysql = require('mysql'); var connection = mysql.createConnection({ ...
- android中提示&对话框----Notification
Notification(状态栏通知) 一.Notification用于状态栏显示通知的控件,在不同的设备上面Notification是不一样的 二.Notification的基本布局 元素组成: I ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---27
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- Scala不使用null 而使用Option,None,Some的好处
刚接触Scala时就很奇怪, 为什么Java已经有null了,却偏偏还要弄出个None 后来依然我行我素在Scala里使用null, 结果就是经常被NullPointerException折磨得阴魂不 ...
- iptables之centos6版本详解
1 Linux防火墙概述 Linux防火墙实际指的是Linux下的Netfilter/Iptables.Netfilter/Iptables是2.4.x/2.6.x版本Linux内核集成的IP信息包过 ...
- 机器学习3_EM算法与混合高斯模型
①EM算法: http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006936.html 李航 <统计学习方法>9.1节 ②混合高斯模 ...