原创javascript小游戏实现代码


Posted in Javascript onAugust 19, 2010

以下是代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>jjb</title> 
<style type="text/css"> 
* { margin:0px; padding:0px;} 
#content{ width:800px; height:500px; position:absolute; left:10px; top:10px; background:url(bg.gif) repeat-x left top;} 
#scbox{width:100px; background:#000000; font-weight:bold; color:#FFFFFF; font-size:12px; line-height:22px; padding-left:10px;} 
#start{ position:absolute; left:215px; _left:190px; top:140px;} 
#life { height:22px; background:#009000; position:absolute; left:110px; top:0px;} 
#bottom { width:800px; height:44px; position:absolute; left:0; top:456px; background:#333; z-index:9;} 
#startbox { height:150px; position:absolute; left:260px; top:100px; background:#000000; color:#fff; font-size:12px; padding:10px;} 
#jia {font-size:40px; font-family:Verdana, Arial, Helvetica, sans-serif; color:#0099CC; position:absolute; z-index:1001;} 
#jian {font-size:40px; font-family:Verdana, Arial, Helvetica, sans-serif; color:#FF0000; position:absolute; z-index:1001;} 
</style> 
<script language="javascript"> 
(function (){ 
(new Image()).src = "38.gif"; 
(new Image()).src = "48.gif"; 
(new Image()).src = "bike.gif"; 
(new Image()).src = "bike2.gif"; 
(new Image()).src = "bg.gif"; 
})(); 
//缓存加载图片 
function $(id) {return document.getElementById(id);} 
var boxBaseAttr = new Object; 
boxBaseAttr = { 
width : "26px", 
height : "40px", 
position: "absolute", 
top : "30px", 
background : "url(38.gif) no-repeat center top", 
zIndex : 10 
}; 
var boxBaseAttr2 = new Object; 
boxBaseAttr2 = { 
width : "26px", 
height : "40px", 
position: "absolute", 
top : "30px", 
background : "url(48.gif) no-repeat center top", 
zIndex : 10 
}; var ele = new Object; 
ele = { 
div : document.createElement("div"), 
span: function (){ 
var sp = document.createElement("span"); 
for(var s in boxBaseAttr) { 
sp.style[s] = boxBaseAttr[s]; 
} 
sp.setAttribute("type", "good"); 
return sp; 
}, 
badspan : function (){ 
var sp2 = document.createElement("span"); 
for(var s in boxBaseAttr2) { 
sp2.style[s] = boxBaseAttr2[s]; 
} 
sp2.setAttribute("type", "bad"); 
return sp2; 
} 
}; 
var sco = 0, lifeWidth = 690, lifes = true; 
var bar = { 
getScore : function () { 
sco += 10; 
$("sc").innerHTML = sco; 
}, 
loseScore : function (){ 
sco -= 10; 
$("sc").innerHTML = sco; 
}, 
life : function (){ 
var life = $("life"); 
var sl = setInterval(function(){ 
if(lifeWidth < 23) { 
lifes = false; 
clearInterval(sl); 
return; 
} 
else { 
lifeWidth -= 23; 
life.style.width = lifeWidth + "px"; 
} 
},1000); 
} 
}; 
var fire = { 
init : function (o, x, y) { 
o.style.display = "block"; 
o.style.left = x + "px"; 
o.style.top = y + "px"; 
var fs = setInterval(function(){ 
if (y >= 280) { 
y -= 10; 
o.style.top = y + "px"; 
} 
else{ 
clearInterval(fs); 
o.style.display = "none"; 
} 
},10); 
} 
}; 
/*-- game begin --*/ 
function game() { 
var content = $("content"), x = 0; 
var pigBaseAttr = new Object; 
pigBaseAttr = { 
background : "url(bike.gif) no-repeat center top", 
position : "absolute", 
top : content.clientHeight-44+"px", 
left : (content.clientWidth - 45)/2+"px", 
width : "45px", 
height : "44px", 
zIndex : 1000 
}; 
var pig = ele.div.cloneNode(true); 
for(var s in pigBaseAttr) { 
pig.style[s] = pigBaseAttr[s]; 
} 
content.appendChild(pig); 
/* -- 小猪移动功能begin--*/ 
content.onmousemove = function (event){ 
var e = event || window.event; 
if ( x > e.clientX) { 
//pig.style.width = "110px"; 
pig.style.background = "url(bike.gif) no-repeat left top"; 
if (e.clientX -45 < 0) { 
pig.style.left = "0px"; 
} else if(e.clientX + 45 > 800) { 
pig.style.left = "755px"; 
} 
else { 
pig.style.left = (e.clientX - 45)+"px"; 
} 
} 
else if (x < e.clientX) { 
//pig.style.width = "110px"; 
pig.style.background = "url(bike2.gif) no-repeat right top"; 
if (e.clientX -45 < 0) { 
pig.style.left = "0px"; 
} else if(e.clientX + 45 > 800) { 
pig.style.left = "755px"; 
} 
else { 
pig.style.left = (e.clientX - 45)+"px"; 
} 
} 
x = e.clientX; 
} 
/*-- 判断是否撞击,传入参数为box和content --*/ 
function meet(obox,parent) { 
var objX = obox.offsetLeft; 
var pigX = pig.offsetLeft; 
var boxType = obox.getAttribute("type"); 
if (objX + 26 >= pigX && objX <= pigX + 45) { 
if (boxType == "good") { 
bar.getScore(); 
fire.init($("jia"), pigX, pig.offsetTop-30); 
} 
else if(boxType == "bad") { 
bar.loseScore(); 
fire.init($("jian"), pigX, pig.offsetTop-30); 
} 
obox.style.left = "-2000px"; 
} 
} 
/* -- 创建随机位置的金币 begin--*/ 
function createbox() { 
var box = ele.span(); 
var b = 30; 
var boxLeft = Math.floor(Math.random()*760);//产生随机距离 
box.style.left = boxLeft + "px"; 
content.appendChild(box); 
//box进入游戏界面,开始移动,并开始执行函数和pig比对是否撞击 
var st = setInterval(function (){ 
if (b < 460){ 
box.style.top = b + 5 + "px"; 
b += 5; 
if (b >= 416) { 
meet(box, content); 
} 
} 
else { 
content.removeChild(box); 
clearInterval(st); 
b = 30; 
} 
},5); 
} 
function createbadbox() { 
var box = ele.badspan(); 
var b = 30; 
var boxLeft = Math.floor(Math.random()*760);//产生随机距离 
box.style.left = boxLeft + "px"; 
content.appendChild(box); 
//box进入游戏界面,开始移动,并开始执行函数和pig比对是否撞击 
var st = setInterval(function (){ 
if (b < 460){ 
box.style.top = b + 5 + "px"; 
b += 5; 
if (b > 416) { 
meet(box, content); 
} 
} 
else { 
content.removeChild(box); 
clearInterval(st); 
b = 30; 
} 
},5); 
} 
bar.life(); 
/*-- 开始执行循环添加box函数 --*/ 
var xt = setInterval(function(){ 
if (lifes == false) { 
clearInterval(xt); 
alert("时间到,您的得分是:"+sco); 
lifeWidth = 690; 
lifes = true; 
sco = 0; 
$("startbox").style.display = "block"; 
$("start").value = "再玩一次"; 
content.removeChild(pig); 
}else{ 
createbox(); 
createbadbox(); 
} 
},500); 
} 
window.onload = function () { 
$("start").onclick = function () { 
$("startbox").style.display = "none"; 
new game(); 
} 
} 
</script> 
</head> 
<body> 
<div id="content"> 
<div id="startbox"> 
<p>游戏规则:</p> 
<p>(1) 你只有30秒的时间,用鼠标控制小猪左右移动;</p> 
<p>(2) 小猪接到<img src="38.gif" />女孩,加10分;</p> 
<p>(3) 小猪接到<img src="48.gif" />女孩,减10分;</p> 
<input type="button" id="start" value="开始游戏" /> 
</div> 
<div id="scbox">分数:<span id="sc">0</span></div> 
<div id="life" style="width:690px;"></div> 
<div id="bottom"></div> 
<div id="jia" style="display:none;">+10</div> 
<div id="jian" style="display:none;">-10</div> 
</div> 
</body> 
</html>

演示代码:/js/js_game/index.htm
代码打包下载
Javascript 相关文章推荐
Javascript - HTML的request类
Jul 15 Javascript
原生js仿jq判断当前浏览器是否为ie,精确到ie6~8
Aug 30 Javascript
将数字转换成大写的人民币表达式的js函数
Sep 21 Javascript
ES6的新特性概览
Mar 10 Javascript
Centos7 中安装 Node.js v4.4.4
Nov 03 Javascript
微信小程序 video详解及简单实例
Jan 16 Javascript
简单实现js倒计时功能
Feb 13 Javascript
requirejs + vue 项目搭建详解
Jun 16 Javascript
node跨域请求方法小结
Aug 25 Javascript
vue-cli整合vuex的时候,修改actions和mutations,实现热部署的方法
Sep 19 Javascript
详解Vue.directive 自定义指令
Mar 27 Javascript
Layui tree 下拉菜单树的实例代码
Sep 21 Javascript
jQuery与ExtJS之选择实例分析
Aug 19 #Javascript
JQuery跨Iframe选择实现代码
Aug 19 #Javascript
JQuery中each()的使用方法说明
Aug 19 #Javascript
jquery获取input的value问题说明
Aug 19 #Javascript
JavaScript开发规范要求(规范化代码)
Aug 16 #Javascript
JavaScript学习历程和心得小结
Aug 16 #Javascript
javascript中的prototype属性使用说明(函数功能扩展)
Aug 16 #Javascript
You might like
php下用GD生成生成缩略图的两个选择和区别
2007/04/17 PHP
PHP面向对象分析设计的61条军规小结
2010/07/17 PHP
PHP逐行输出(ob_flush与flush的组合)
2012/02/04 PHP
Laravel5中contracts详解
2015/03/02 PHP
PHP的Yii框架的基本使用示例
2015/08/21 PHP
PHP获取当前URL路径的处理方法(适用于多条件筛选列表)
2017/02/10 PHP
Laravel网站打开速度优化的方法汇总
2017/07/16 PHP
php use和include区别总结
2019/10/13 PHP
兼容IE/Firefox/Opera/Safari的检测页面装载完毕的脚本Ext.onReady的实现
2009/07/14 Javascript
ASP.NET jQuery 实例10 动态修改hyperlink的URL值
2012/02/03 Javascript
JavaScript与DOM组合动态创建表格实例
2012/12/23 Javascript
jQuery focus和blur事件的应用详解
2014/01/26 Javascript
javascript 获取函数形参个数
2014/07/31 Javascript
浅析Node.js:DNS模块的使用
2016/11/23 Javascript
JQuery 动态生成Table表格实例代码
2016/12/02 Javascript
关于angularJs清除浏览器缓存的方法
2017/11/28 Javascript
Node.js Express安装与使用教程
2018/05/11 Javascript
从零开始实现Vue简单的Toast插件
2018/12/03 Javascript
js逆向解密之网络爬虫
2019/05/30 Javascript
Vue 组件的挂载与父子组件的传值实例
2020/09/02 Javascript
代码讲解Python对Windows服务进行监控
2018/02/11 Python
使用 Python 合并多个格式一致的 Excel 文件(推荐)
2019/12/09 Python
Pytorch对Himmelblau函数的优化详解
2020/02/29 Python
python进行OpenCV实战之画图(直线、矩形、圆形)
2020/08/27 Python
python 逆向爬虫正确调用 JAR 加密逻辑
2021/01/12 Python
利用Python过滤相似文本的简单方法示例
2021/02/03 Python
美国高端婴童品牌:Hanna Andersson
2016/10/30 全球购物
英国著名的小众美容品牌网站:Alyaka
2017/08/08 全球购物
Smallable意大利家庭概念店:设计师童装及家居装饰
2018/01/08 全球购物
PHP面试题集
2016/12/18 面试题
调研汇报材料范文
2014/08/17 职场文书
评奖评优个人先进事迹材料
2015/11/04 职场文书
只用Python就可以制作的简单词云
2021/06/07 Python
Python下opencv使用hough变换检测直线与圆
2021/06/18 Python
Js类的构建与继承案例详解
2021/09/15 Javascript
Java 异步任务计算FutureTask
2022/04/28 Java/Android