预备知识点:
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: 20px;
font-family: "Lucida Grande", Verdana,Arial, Helvetica, sans-serif;
}
h1, th {
font-family: Georgia, "Times New Roman",Times, serif;
}
h1 {
font-size: 28px;
}
table {
border-collapse: collapse;
}
th, td {
padding: 10px;
border: 2px #666 solid;
text-align: center;
width: 20%;
}
#free, .pickedBG {
background-color: #f66;<!--控制Free的键-->
}
.winningBG {
background-image:url(images/redFlash.gif);
}

js的片段

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

静态的展示结果

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

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

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

window.onload = initAll;
function initAll() {if (document.getElementById) {
for (var i=0; i<24; 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() * 75) + 1;
document.getElementById(currSquare).innerHTML = newNum;
}

**消除重复的数字:更新数组
将数组的内容改为存储当前值是一种非常强大的技术**

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

还允许用户单击页面底部的链接来重新运行脚本,这样就可以完全在
浏览器中生成Bingo 卡片,而不需要从服务器重新加载页面。这向用户提供了快速的响应,而且不会产生服务器负载。
让用户有能力自己运行脚本:

window.onload = initAll;
var usedNums = new Array(76);
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=0; i<24; i++) {
setSquare(i);
}
}
function setSquare(thisSquare) {var currSquare = "square" + thisSquare;
var colPlace = new Array(0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4);
var colBasis = colPlace thisSquare] * 15;
var newNum;
do {
newNum = colBasis + getNewNum() + 1;
}
while (usedNums[newNum]);
usedNums[newNum] = true;
document.getElementById(currSquare).innerHTML = newNum;
}
function getNewNum() {return Math.floor(Math.random() * 15);
}
function anotherCard() {for (var i=1; i<usedNums.length; i++) {
usedNums[i]=false;
}
newCard();
return false;
}

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

window.onload = initAll;
var usedNums = new Array(76);
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=0; i<24; i++) {
setSquare(i);
}
}
function setSquare(thisSquare) {var currSquare = "square" + thisSquare;
var colPlace = new Array(0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4);
var colBasis = colPlace [thisSquare] * 15;
var newNum;
do {
newNum = colBasis + getNewNum() + 1;
}
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() * 15);
}
function anotherCard() {for (var i=1; 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(76);
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=0; i<24; i++) {
setSquare(i);
}
}
function setSquare(thisSquare) {var currSquare = "square" + thisSquare;
var colPlace = new Array(0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4,4);
var colBasis = colPlace [thisSquare] * 15;
var newNum;
do {newNum = colBasis + getNewNum() + 1;
}
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() * 15);
}
function anotherCard() {for (var i=1; i<usedNums.length; i++) {usedNums[i] = false;1
}
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 = -1;
var setSquares = 0;
var winners = new Array(31,992,15360,507904,541729,557328,1083458,2162820,4329736,8519745,8659472,16252928);
for (var i=0; i<24; i++) {
var currSquare = "square" + i;
if (document.getElementById (currSquare).className != "") {
document.getElementById (currSquare).className = "pickedBG";
setSquares = setSquares | Math.pow(2,i);
}
}
for (var i=0; i<winners.length; i++) {
if ((winners[i] & setSquares) == winners[i]) {
winningOption = i;
}
}
if (winningOption > -1) {
for (var i=0; i<24; i++) {
if (winners[winningOption] & Math.pow(2,i)) {
currSquare = "square" + i;
document.getElementById (currSquare).className = "winningBG";
}
}
}
}

Javascript构建Bingo卡片游戏相关推荐

  1. 利用Javascript制作宾果(BINGO)游戏

    Bingo游戏简介 Bingo卡片是5×5的正方形,五个列上标着B-I-N-G-O,美国的Bingo游戏格子中的数字通常在70以内随机抽取,英国和澳大利亚的是在90以内.正中间通常是一个空的格子,常常 ...

  2. javascript做游戏_我用JavaScript构建了一个角色扮演游戏。 你也可以 这是如何做。...

    javascript做游戏 by Robert Skalko 罗伯特·斯科尔科(Robert Skalko) 我用JavaScript构建了一个角色扮演游戏. 你也可以 这是如何做. (I built ...

  3. HTML5游戏开发进阶指南(亚马逊5星畅销书,教你用HTML5和JavaScript构建游戏!)

    HTML5游戏开发进阶指南(亚马逊5星畅销书,教你用HTML5和JavaScript构建游戏!) [印]香卡(Shankar,A.R.) 著 谢光磊 译 ISBN 978-7-121-21226-0 ...

  4. bootstrap项目实例_Vue.js 项目实践——创建记忆卡片游戏

    作者:Jiawei Pan 转发链接:https://mp.weixin.qq.com/s/VXPD2p7q2S3yR9I7lzAkfw 前言 如果你刚开始学习 Vue,想巩固基础知识,那么你可以试试 ...

  5. Javascript开发的HTML5游戏的知识产权保护

    Javascript开发的HTML5游戏的知识产权保护,其实这里面保护了两个部分,一个是及时注册相关商标和专利,一个是程序的核心算法,核心数据和用户数据的保护. 本文主要讨论第二个方面,在当前这个时间 ...

  6. 给一个div innerhtml 后 没有内容显示的问题_实战:仅用18行JavaScript构建一个倒数计时器...

    有时候,你会需要构建一个JavaScript倒计时时钟.你可能会有一个活动.一个销售.一个促销或一个游戏.你可以用原生的JavaScript构建一个时钟,而不是去找一个插件.尽管有很多很棒的时钟插件, ...

  7. 视频教程-JavaScript打飞机小游戏视频教程-JavaScript

    JavaScript打飞机小游戏视频教程 拥有6年web前端和后端开发经验,4年授课经验,还曾在百度专业培训过网络营销课程,曾就职于联想集团和当当网,不仅有丰富的项目实战经验还有营销经验,综合实力较强 ...

  8. 如何使用 JavaScript 构建计算器应用程序

    这个史诗般的教程通过描述如何使用该语言开发一个简单的计算器应用程序,为 JavaScript 新手提供了一个可靠的练习. 面向初学者的 javascript 项目(8 部分系列) 1 构建你的第一个 ...

  9. 画布式编程_构建HTML5画布游戏如何帮助我学习编程

    画布式编程 by Surbhi Oberoi 由Surbhi Oberoi 构建HTML5画布游戏如何帮助我学习编程 (How building HTML5 canvas games helped m ...

  10. JavaScript实现2048小游戏,我终于赢了一把

    JavaScript实现2048小游戏 作者简介 作者名: 简介:CSDN博客专家,从事软件开发多年,精通Java.JavaScript,博主也是从零开始一步步把学习成长.深知学习和积累的重要性,喜欢 ...

最新文章

  1. [转]matlab GUI 新手入门——最基本的几个概念
  2. triplet loss 在深度学习中主要应用在什么地方?有什么明显的优势?
  3. bim 模型web页面展示_BIM+装配式建筑工程师2020年必须拿下的技能证书
  4. blp模型 上读下写_Golang 并发模型系列:1. 轻松入门流水线模型
  5. CSS3选择非第一个子元素
  6. win10计算机本地无法连接,win10无法连接到这个网络怎么办_win10无法连接到这个网络如何解决...
  7. html如何设置鼠标选中状态,怎么用CSS 设置 当鼠标移动到菜单时,该按钮变色,鼠标点击后,页面停留在鼠标滑过时的状态!!很急!...
  8. 曝光:一位来自微软公司的粉丝 写给我的信
  9. java书籍台湾翻译_《现代专业Javasctript 技术》一书中英文目录,翻译记录下来方便学习用...
  10. codeforce C. Okabe and Boxes
  11. cmake 检查文件更新_2020年6月:Visual Studio对Linux开发平台的更新
  12. 安装Oracle 11g 出现交换空间不够
  13. 测试一段C代码的执行时间(windows系统和ubuntu系统)
  14. js页面跳转 和 js打开新窗口 方法 【转】
  15. 从JimmyNews有感于互联网的传播能力
  16. 聊聊reactive streams的schedulers 1
  17. 在Ubuntu系统下安装WPS(21.3.2)
  18. [2022年大学生创新创业训练计划项目立项申报]
  19. 秀米图文排版转html,秀米微信编辑器图文排版H5秀图文教程
  20. matlab亚式 期权定价,MATLAB在幂型几何亚式期权定价中的应用

热门文章

  1. Selenium启动项参数设置
  2. C语言用随机函数做猜拳游戏,c语言猜拳游戏
  3. 天下一品茗介绍:小户赛茶叶的特点是什么
  4. CentOS下MySQL安装失败,报socket '/tmp/mysql.sock错误解决方法
  5. 为什么说采购一定要成为专家
  6. 2021-06-16 srm平台电子化采购的优势
  7. 神经网络打印模型参数及参数名字和数量
  8. 骂人不带脏字的80后
  9. DeepinXP Lite 完美精简版 |5.2|5.3|5.4|5.5|5.6|5.7|5.8|5.9|5.10||6.1New| 迅雷下载
  10. 课时1:Vitis HLS的工作机制——Vitis HLS教程