天天看点

Javascript构建Bingo卡片游戏

预备知识点:

1、

“< table >”表示表格,

“< tr >”表示表格的开始一行,

“< th>”表示表格中列的标题单元格,

“< td>”表示表格中的每个单元格

2、常用的字符含义

”&nbsp“表示空格

“&amp”; &

“&lt”; <

“&gt”; >

“&quot”; ”

“&qpos”; ‘

一、静态bingo区

程序代码区:

Html片段:

<!DOCTYPE html>
<html>
<head>
<title>Make Your Own Bingo Card</title>
<link rel="stylesheet" href="script01.css">
<script src="script01.js"></script>
</head>
<body>
<h1>Create A Bingo Card</h1>
<table>
<tr>
<th>B</th>
<th>I</th>
<th>N</th>
<th>G</th>
<th>O</th>
</tr>
<tr>
<td id="square0">&nbsp;</td> <!--&nbsp表示空格-->
<td id="square5">&nbsp;</td>
<td id="square10">&nbsp;</td>
<td id="square14">&nbsp;</td>
<td id="square19">&nbsp;</td>
</tr>
<tr>
<td id="square1">&nbsp;</td>
<td id="square6">&nbsp;</td>
<td id="square11">&nbsp;</td>
<td id="square15">&nbsp;</td>
<td id="square20">&nbsp;</td>
</tr>
<tr>
<td id="square2">&nbsp;</td>
<td id="square7">&nbsp;</td>
<td id="free">Free</td>
<td id="square16">&nbsp;</td>
<td id="square21">&nbsp;</td>
</tr>
<tr>
<td id="square3">&nbsp;</td>
<td id="square8">&nbsp;</td>
<td id="square12">&nbsp;</td>
<td id="square17">&nbsp;</td>
<td id="square22">&nbsp;</td>
</tr>
<tr>
<td id="square4">&nbsp;</td>
<td id="square9">&nbsp;</td>
<td id="square13">&nbsp;</td>
<td id="square18">&nbsp;</td>
<td id="square23">&nbsp;</td>
</tr>
</table>
<p><a href="script01.html" id="reload">Click here</a> to create a new card</p>
</body>
</html>
           

css片段:

body {
background-color: white;
color: black;
font-size: px;
font-family: "Lucida Grande", Verdana,Arial, Helvetica, sans-serif;
}
h1, th {
font-family: Georgia, "Times New Roman",Times, serif;
}
h1 {
font-size: px;
}
table {
border-collapse: collapse;
}
th, td {
padding: px;
border: px #666 solid;
text-align: center;
width: %;
}
#free, .pickedBG {
background-color: #f66;<!--控制Free的键-->
}
.winningBG {
background-image:url(images/redFlash.gif);
}
           

js的片段

window.onload = initAll;
//窗口的显示加载,调用initAll()函数,事件处理程序调用函数
function initAll() {
for (var i=; i<; i++) {
var newNum = Math.floor(Math.random() * ) + ;
//JavaScript 命令Math.random()生成0~1 的一个随机数;floor运算会获得结果的整数部,最后获得1到最大值+1的结果
document.getElementById("square" + i).innerHTML = newNum;
}
}
           

静态的展示结果

Javascript构建Bingo卡片游戏

修改js的代码:使用值传递的方式:

window.onload = initAll;
function initAll() {
for (var i=; i<; i++) {
setSquare(i);
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var newNum = Math.floor(Math.random() * ) + ;
document.getElementById(currSquare). innerHTML = newNum;
}
           

探测对象:对象探测拒绝这种老式浏览器(Mac 的Netscape 4)并显示这个错误消息。

window.onload = initAll;
function initAll() {
if (document.getElementById) {
for (var i=; i<; i++) {
setSquare(i);
}
}
else {
alert("Sorry, your browser doesn't support this script");
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var newNum = Math.floor (Math.random() * ) + ;
document.getElementById(currSquare).innerHTML = newNum;
}
           

**消除重复的数字:更新数组

将数组的内容改为存储当前值是一种非常强大的技术**

window.onload = initAll;
var usedNums = new Array();
function initAll() {
if (document.getElementById) {
for (var i=; i<; i++) {
setSquare(i);
}
}
else {
alert("Sorry, your browser doesn't support this script");
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var colPlace = new Array(,,,,,,,,,,,,,,,,,,,,,,,);
var colBasis = colPlace [thisSquare] * ;
var newNum = colBasis + getNewNum() + ;
if (!usedNums[newNum]) {
usedNums[newNum] = true;
document.getElementById(currSquare).innerHTML = newNum;
}
}
function getNewNum() {
return Math.floor(Math.random() * );
}
           

还允许用户单击页面底部的链接来重新运行脚本,这样就可以完全在

浏览器中生成Bingo 卡片,而不需要从服务器重新加载页面。这向用户提供了快速的响应,而且不会产生服务器负载。

让用户有能力自己运行脚本:

window.onload = initAll;
var usedNums = new Array();
function initAll() {
if (document.getElementById) {
document.getElementById("reload").onclick = anotherCard;
newCard();
}
else {
alert("Sorry, your browser doesn't support this script");
}
}
function newCard() {
for (var i=; i<; i++) {
setSquare(i);
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var colPlace = new Array(,,,,,,,,,,,,,,,,,,,,,,,);
var colBasis = colPlace thisSquare] * ;
var newNum;
do {
newNum = colBasis + getNewNum() + ;
}
while (usedNums[newNum]);
usedNums[newNum] = true;
document.getElementById(currSquare).innerHTML = newNum;
}
function getNewNum() {
return Math.floor(Math.random() * );
}
function anotherCard() {
for (var i=; i<usedNums.length; i++) {
usedNums[i]=false;
}
newCard();
return false;
}
           

通过JavaScript 添加一个类,使代码可以利用CSS 的功能

window.onload = initAll;
var usedNums = new Array();
function initAll() {
if (document.getElementById) {
document.getElementById("reload").onclick = anotherCard;
newCard();
}
else {
alert("Sorry, your browser doesn't support this script");
}
}
function newCard() {
for (var i=; i<; i++) {
setSquare(i);
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var colPlace = new Array(,,,,,,,,,,,,,,,,,,,,,,,);
var colBasis = colPlace [thisSquare] * ;
var newNum;
do {
newNum = colBasis + getNewNum() + ;
}
while (usedNums[newNum]);
usedNums[newNum] = true;
document.getElementById(currSquare).innerHTML = newNum;
document.getElementById(currSquare).className = "";
document.getElementById(currSquare).onmousedown = toggleColor;
}
function getNewNum() {
return Math.floor(Math.random() * );
}
function anotherCard() {
for (var i=; i<usedNums.length; i++) {
usedNums[i] = false;
}
newCard();
return false;
}
function toggleColor(evt) {
if (evt) {
var thisSquare = evt.target;
}
else {
var thisSquare = window.event.srcElement;
}
if (thisSquare.className == "") {
thisSquare.className = "pickedBG";
}
else {
thisSquare.className = "";
}
}
           

这个脚本使用复杂的数学计算判断获胜组合

window.onload = initAll;
var usedNums = new Array();
function initAll() {
if (document.getElementById) {
document.getElementById("reload").onclick = anotherCard;
newCard();
}
else {
alert("Sorry, your browser doesn't support this script");
}
}
function newCard() {
for (var i=; i<; i++) {
setSquare(i);
}
}
function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var colPlace = new Array(,,,,,,,,,,,,,,,,,,,,,,,);
var colBasis = colPlace [thisSquare] * ;
var newNum;
do {newNum = colBasis + getNewNum() + ;
}
while (usedNums[newNum]);
usedNums[newNum] = true;
document.getElementById(currSquare).innerHTML = newNum;
document.getElementById(currSquare).className = "";
document.getElementById(currSquare).onmousedown = toggleColor;
}
function getNewNum() {
return Math.floor(Math.random() * );
}
function anotherCard() {
for (var i=; i<usedNums.length; i++) {
    usedNums[i] = false;
}
newCard();
return false;
}
function toggleColor(evt) {
if (evt) {
var thisSquare = evt.target;
}
else {
var thisSquare = window.event.srcElement;
}
if (thisSquare.className == "") {
thisSquare.className = "pickedBG";
}
else {
thisSquare.className = "";
}
checkWin();
}
function checkWin() {
var winningOption = -;
var setSquares = ;
var winners = new Array(,,,,,,,,,,,);
for (var i=; i<; i++) {
var currSquare = "square" + i;
if (document.getElementById (currSquare).className != "") {
document.getElementById (currSquare).className = "pickedBG";
setSquares = setSquares | Math.pow(,i);
}
}
for (var i=; i<winners.length; i++) {
if ((winners[i] & setSquares) == winners[i]) {
winningOption = i;
}
}
if (winningOption > -) {
for (var i=; i<; i++) {
if (winners[winningOption] & Math.pow(,i)) {
currSquare = "square" + i;
document.getElementById (currSquare).className = "winningBG";
}
}
}
}
           

继续阅读