一款基础模型的JS打飞机游戏特效代码
<!DOCTYPE html>
<html lang="en">
<head>
<title>一款基础模型的JS打飞机游戏特效代码</title> <style type="text/css">
#show{
margin:auto;
width:300px;
height:500px;
background: black;
position: relative;
}
#fly{
width:10px;
height:10px;
position:absolute;
bottom:0;
left:140px;
background: #0044ff;
}
#sco{
z-index:1;
color:white;
}
.arrow{
width:6px;
height:6px;
background: #ddd;
margin-left: 2px;
margin-top:-10px;
}
.huiji{
width:10px;
height:10px;
background: #eee;
position:absolute;
top:0;
left:0;
}
</style>
</head>
<body>
<div id="show" style="width:300px;height:500px">
<div id="fly">
<div class="arrow" style="margin-top:-10px;">
</div>
</div>
</div>
</body>
</html>
<script>
function $(id){//获得id
return document.getElementById(id);
}
function keydown(e) {//判断键盘事件
var currKey=0,e=e||event;
currKey=e.keyCode||e.which||e.charCode;
return currKey;
}
function getClass(obj,attr){//getElementsByClass
var aArray=[];
var i=0;
var aAll = obj.getElementsByTagName('*');
for(i=0;i<aAll.length;i++){
if(aAll[i].className == attr){
aArray.push(aAll[i]);
}
}
return aArray;
}
function fly(){
this.begin=0;
this.arrowSpeed=5;
this.huijiSpeed=7;
this.showArea=$('show');
this.showWidth=this.showArea.style.width;
this.showHeight=this.showArea.style.height;
this.offsetleft='140px';
this.arrow=getClass(document,'arrow');
this.huiji=getClass(document,'huiji');
this.flying=$('fly');
this.scores=0;
}
fly.prototype={
constructor:fly,
version:1.0,
start:function(){
this.begin=1;
},
end:function(){
this.begin=0;
},
leftSet:function(width){
this.flying.style.left=width;
return this.flying.style.left;
},
process:function(){
var that=this;
if(that.begin==0){
return false;
}else if(that.begin==1){
that.leftSet(that.offsetleft);
document.addEventListener('keydown',function(){//判断边界
if(parseInt(that.leftSet())>0&&parseInt(that.leftSet())<(parseInt(that.showWidth)-10)){
if(keydown()==37){
that.leftSet(parseInt(that.flying.style.left)-5+'px');
}else if(keydown()==39){
that.leftSet(parseInt(that.flying.style.left)+5+'px');
}
}else if(parseInt(that.leftSet())==0){
if(keydown()==39){
that.leftSet(parseInt(that.flying.style.left)+5+'px');
}
}else if(parseInt(that.leftSet())==(parseInt(that.showWidth)-10)){
if(keydown()==37){
that.leftSet(parseInt(that.flying.style.left)-5+'px');
}
}
});
setInterval(function(){//子弹
for(var i=0;i<that.arrow.length;i++){
if(Math.abs(parseInt(that.arrow[i].style.marginTop))<parseInt(that.showHeight)-10){
that.arrow[i].style.marginTop=parseInt(that.arrow[i].style.marginTop)-10+'px';
}else if(Math.abs(parseInt(that.arrow[i].style.marginTop))==parseInt(that.showHeight)-10){
that.arrow[i].style.marginTop='-20px';
}
}
},that.arrowSpeed);
}
},
huijiInit:function(){
var that=this,
left=Math.random()*290,
textnode=document.createElement('div');
that.showArea.appendChild(textnode).setAttribute('class','huiji');
textnode.style.left=0;
textnode.style.top=0;
textnode.style.left=left+'px';
setInterval(function(){
if(Math.abs(parseInt(textnode.style.top))<parseInt(that.showHeight)+10){
textnode.style.top=parseInt(textnode.style.top)+1+'px';
for(var i=0;i<that.arrow.length;i++){
if(Math.abs(parseInt(that.arrow[i].style.marginTop))<=Math.abs(parseInt(textnode.style.top))&&Math.abs(parseInt(that.arrow[i].style.marginTop))>=Math.abs(parseInt(textnode.style.top))-20&&parseInt(that.leftSet())>left&&parseInt(that.leftSet())<left+20){
textnode.remove();
that.scores+=1;
}
}
}else if(Math.abs(parseInt(textnode.style.top))==parseInt(that.showHeight)+10){
textnode.remove();
}
},that.huijiSpeed);
},
score:function(){
var textnode=document.createElement('div');
if(!document.getElementById('sco')){
this.showArea.insertBefore(textnode).setAttribute('id','sco').innerHTML=this.scores;
}else{
document.getElementById('sco').innerHTML=this.scores;
}
},
init:function(){
var that=this;
this.start();
this.process();
setInterval(function(){that.huijiInit();that.score();},1000);
}
}
var fl=new fly();
fl.init()
</script>
一款基础模型的JS打飞机游戏特效代码的更多相关文章
- 18款js和jquery文字特效代码分享
18款js和jquery文字特效代码分享 jQCloud标签云插件_热门城市文字标签云代码 js 3d标签云特效关键词文字球状标签云代码 原生JS鼠标悬停文字球状放大显示效果代码 原生js文字动画圆形 ...
- JS日历控件特效代码layDate
https://www.js-css.cn/a/jscode/date/2015/0405/1461.html
- 原生js写的贪吃蛇网页版游戏特效
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <bo ...
- 一个标准的,兼容性很好的div仿框架的基础模型!
<!DOCTYPE html> <html > <head> <meta http-equiv="Content-Type" conten ...
- 性能追击:万字长文30+图揭秘8大主流服务器程序线程模型 | Node.js,Apache,Nginx,Netty,Redis,Tomcat,MySQL,Zuul
本文为<高性能网络编程游记>的第六篇"性能追击:万字长文30+图揭秘8大主流服务器程序线程模型". 最近拍的照片比较少,不知道配什么图好,于是自己画了一个,凑合着用,让 ...
- linux下bus、devices和platform的基础模型
转自:http://blog.chinaunix.net/uid-20672257-id-3147337.html 一.kobject的定义:kobject是Linux2.6引入的设备管理机制,在内核 ...
- ThinkPHP 学习笔记 ( 三 ) 数据库操作之数据表模型和基础模型 ( Model )
//TP 恶补ing... 一.定义数据表模型 1.模型映射 要测试数据库是否正常连接,最直接的办法就是在当前控制器中实例化数据表,然后使用 dump 函数输出,查看数据库的链接状态.代码: publ ...
- ThinkPHP 数据库操作之数据表模型和基础模型 ( Model )
一.定义数据表模型 1.模型映射 要测试数据库是否正常连接,最直接的办法就是在当前控制器中实例化数据表,然后使用 dump 函数输出,查看数据库的链接状态.代码: public function te ...
- [您有新的未分配科技点]博弈论进阶:似乎不那么恐惧了…… (SJ定理,简单的基础模型)
这次,我们来继续学习博弈论的知识.今天我们会学习更多的基础模型,以及SJ定理的应用. 首先,我们来看博弈论在DAG上的应用.首先来看一个小例子:在一个有向无环图中,有一个棋子从某一个点开始一直向它的出 ...
随机推荐
- 使用pycharm软件配置数据库可视化
必须品: pycharm软件,专业版最好自带就有,社区版就需要安装下插件. 专业版直接会在右边的编辑框浮动,直接点开就可以配置. 如图所示,点开就可以配置相应的数据库, 点开配置完毕就可以使用了. 还 ...
- 日历(Calendar)模块
#usr/bin/python3 #! -*-conding : utf-8 -*- #2018.3.14 """ 日历(Calendar)模块 此模块的函数都是日历相关 ...
- 概要梳理kafka知识点
主要是梳理一下kafka学习中的一些注意点,按照消息的流动方向进行梳理.详细的kafka介绍推荐看骑着龙的羊的系列博客,具体的某一块的知识点,可以参考我给出的一些参考文章. 1. kafka在系统中的 ...
- 关于wcf服务编译平台是x86, 运行平台是x64时,如何调试
关于调试CTDC项目中的的 wcf服务时注意事项: 因为wcf项目引用的的 x86的程序集,所以wcf生成的目标平台为x86.故在64系统上调试需要执行下面的脚本 具体操作步骤: 1. 必须使用201 ...
- Ext JS 6学习文档-第7章-图表
Ext JS 6学习文档-第7章-图表 使用图表 本章中将探索在 ExtJS 中使用不同类型的图表并使用一个名为费用分析的示例项目结束本章所学.以下是将要所学的内容: 图表类型 条形图 和 柱形图 图 ...
- error : Web 项目“RealEstate.Web”的 URL“http://localhost:20000”已配置为将 IIS 用作 Web 服务器,但是当前在 IIS Express W
error : Web 项目"RealEstate.Web"的 URL"http://localhost:20000"已配置为将 IIS 用作 Web 服务器 ...
- php mongodb扩展 其他扩展也类似
MongoDBPHP 扩展 本教程将向大家介绍如何在Linux.window.Mac平台上安装MongoDB扩展. Linux上安装 MongoDB PHP扩展 在终端上安装 你可以在linux中执行 ...
- oracle RAC集群启动和关闭
oracle 11G RAC集群启动和关闭:1.停止数据库$srvctl stop database –d oradb2.停止集群服务关闭集群需要root运行#/u01/11.2.0/grid/bin ...
- PHPCMSV9 黄页新闻、产品、商机均无法浏览具体信息,显示您没有访问该信息的权限!
原帖地址:http://bbs.phpcms.cn/forum.php?mod=viewthread&tid=294956&highlight=%C3%BB%D3%D0%B7%C3%C ...
- SQL SERVER技术内幕之5 表表达式
表表达式是一种命名的查询表达式,代表一个有效的关系表.可以像其他表一样,在数据处理语句中使用表表达式.SQL Server支持4种类型的表表达式:派生表(derived table).公用表表达式(C ...