[360前端星计划]总部学习笔记(6/6)javascript
21点又名黑杰克(英文:Blackjack) ,起源于法国,已流传到世界各地。21点,是一种使用扑克牌玩的赌博游戏。亦是惟一一种在赌埸中能够在几率中打败庄家的一种赌博游戏。html
HTML: 标准的html结构,meta标签等 思考布局方式,合理的结构 避免使用行内样式 事件尽可能采用事件绑定 页面样式尽可能处理的精致一些(优先级以功能为主,这些次之) 本次做品,时间和质量的比重,质量的权重高,因此要优先提高质量 CSS: 1. 页面单独引用xxxx.css 2. 功能样式能够分类(就算不必分页、也能够按照功能写在一块儿),如: 2.1 公共样式:包括字体、h1~h6 p div等会用到的一些样式 2.2 布局样式:就是布局相关的css写在一块儿,主要处理布局、结构 2.3 功能样式:各个子功能块样式,如:桌面、操做图标,基本思想是按功能分块 2.4 编码相关:尽量少使用id;最好使用class,且其命名有功能性 (用于绑定事件) 样式性(用户写样式),命名语义话(能表达出你这个样式是干什么用的, 尽可能避免写flash一、flash2...)(已经改为sendCardTo) 动画相关样式能够单独写在一个文件里,进行引用 手机上不居中可能也是这个缘由,另外手机上能够设置meta的viewport. 按钮问题:能够给其父元素一个position:relative; 而后操做元素总体使用position:absolute;bottom:xx;进行定位 若是手机上要求访问效果和PC相同,能够考虑样式作两套(根据时间状况看吧) Js: 格式得当。 尽可能简化代码。 简单封装一下dom操做:获取dom、addClass、removeClass、事件绑定等。 合理的注释,每一个方法都得有注释。 函数命名按照功能命名。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <link rel="stylesheet" href=""> </head> <body> <header></header> <section> <div></div> …… …… …… …… <div></div> </section> <footer></footer> <script></script> </body> </html>
/*绘制透明的绿色桌面以及木质桌面边缘*/ @keyframes sendCardTo1 { 0% { right:-450px;top: 30px; transform: rotate(240deg); } 100% { right: 0; transform: rotate(0); } } @keyframes sendCardTo2 { 0% { right:-220px; top:30px; transform: rotate(240deg); } 100% { right: 0; top:0; transform: rotate(0); } } @-moz-keyframes sendCardTo1 /* Firefox */ {sendCardTo1 0% { right:-450px;top: 30px; transform: rotate(240deg); } 100% { right: 0; transform: rotate(0); } } @-moz-keyframes sendCardTo2 /* Firefox */ { 0% { right:-220px; top:30px; transform: rotate(240deg); } 100% { right: 0; top:0; transform: rotate(0); } } @-webkit-keyframes sendCardTo1 /* Safari 和 Chrome */ { 0% { right:-450px;top: 30px; transform: rotate(240deg); } 100% { right: 0; transform: rotate(0); } } @-webkit-keyframes sendCardTo2 /* Safari 和 Chrome */ { 0% { right:-220px; top:30px; transform: rotate(240deg); } 100% { right: 0; top:0; transform: rotate(0); } } @-o-keyframes sendCardTo1 /* Opera */ { 0% { right:-450px;top: 30px; transform: rotate(240deg); } 100% { right: 0; transform: rotate(0); } } @-o-keyframes sendCardTo2 /* Opera */ { 0% { right:-220px; top:30px; transform: rotate(240deg); } 100% { right: 0; top:0; transform: rotate(0); } }
.circle { background: rgba(7,121,5,0.7); border-radius: 50%; position: absolute; top: -460px; left: 145px; width: 900px; height: 900px; border: 30px solid rgba(85,72,4,0.9); } /*发牌区动画的出发点以及牌*/ .send { position: absolute; top: 531px; right: 50px; height: 85px; width: 60px; -ms-transform: rotate(35deg); /* IE 9 */ -webkit-transform: rotate(35deg); /* Safari and Chrome */ -o-transform: rotate(35deg); /* Opera */ -moz-transform: rotate(35deg); /* Firefox */ transform: rotate(35deg); background-color: white; z-index: 2; border-top: 3px solid white; border-right: 2px solid white; }
//此为核心代码,重复部分已省去,详情见源代码。 var num = 1; if (count[1].count == 0) { num = 2; //AI for (var i = 0; i < num; ++i) { setTimeout(function() {realSend(1)}, i * 100); setTimeout(function() { var n = document.getElementById('p_1') .getElementsByClassName('send-card1'); if (n.length > 0) n[0].classList.remove('send-card1'); }, i * 100 + 800); } }
//此为核心代码,重复部分已省去,详情见源代码。 var begain = 0;//防止没开始游戏直接点击要牌和亮牌的按钮 function showCard() { if (begain == 0) {} else { var hidden = document.getElementById('p_1') .getElementsByClassName('card-hidden')[0]; if (typeof(hidden) != "undefined") hidden.result(); if (over) return; count[1].sum = getMax(1); count[2].sum = getMax(2); … … … … if(){alert("… …"); }else{alert("… …"); }; }
if (id == 1 && count[1].count == 1) { /* 庄家第二章暗牌 经过属性k, v来实现明牌和计算 */ newNode.className = "send-card" + id + " card-hidden"; newNode.setAttribute('k', pos); newNode.result = function() { this.className = "card"; this.style.backgroundPosition = this.getAttribute('k'); } } else { newNode.className = "send-card" + id + " card"; newNode.style.backgroundPosition = pos; } if (count[id].count == 1) newNode.setAttribute('v', card.value); node.appendChild(newNode); ++count[id].count; /* 对A单独处理 */ if (card.value != 1) { if (card.value > 10) count[id].sum += 10; else count[id].sum += card.value; } else ++count[id].A; var ai = count[1]; var pl = count[2]; if (pl.A == 1 && pl.count == 2 && pl.sum == 10) //若是一开始两张牌为1和10则为黑杰克。 blackjack = 1; if (blackjack == 1 && ai.A != 1 && ai.count != 2 && ai.sum != 10) scale = 2; //若是2号玩家拥有黑杰克而庄家没有则筹码翻倍。 /* 先断定是否已经结束 不然调用机器人函数 */ if (count[id].sum > 21) lose(id); else if (count[id].sum + count[id].A > 21) lose(id); else if (id != 1 && count[2].count > 2) AI();
function getPorkerPos(tmp) { // console.log(tmp.value); return porkerPos[tmp.suit.toString() + tmp.value.toString()]; }
根据不一样状况,编写不一样的获胜提示,以二号玩家为操做玩家的角度提示。前端
function AI() { if (noneed) return; if (count[1].sum + count[1].A < 16 || getMax(1) < 16) { realSend(1); setTimeout(function() { var n = document.getElementById('p_1') .getElementsByClassName('send-card1'); if (n.length > 0) n[0].classList.remove('send-card1'); }, 800); } else noneed = 1; }
// 给[btnName]按钮 添加fn()功能并发牌功能 function addFn(btnName,fn) { var btnName = document.getElementById("btnName"); btnName.onclick = function() { if (begain == 0) {} else { fn(); } } }
感谢北京奇虎360能够给我此次展现本身的机会,不管结果如何。
本游戏仅为奇虎360公司前端星计划编写,任何人未征得本人赞成 以前请勿使用本代码做商业用途。