<!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打飞机游戏特效代码的更多相关文章

  1. 18款js和jquery文字特效代码分享

    18款js和jquery文字特效代码分享 jQCloud标签云插件_热门城市文字标签云代码 js 3d标签云特效关键词文字球状标签云代码 原生JS鼠标悬停文字球状放大显示效果代码 原生js文字动画圆形 ...

  2. JS日历控件特效代码layDate

    https://www.js-css.cn/a/jscode/date/2015/0405/1461.html

  3. 原生js写的贪吃蛇网页版游戏特效

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <bo ...

  4. 一个标准的,兼容性很好的div仿框架的基础模型!

    <!DOCTYPE html> <html > <head> <meta http-equiv="Content-Type" conten ...

  5. 性能追击:万字长文30+图揭秘8大主流服务器程序线程模型 | Node.js,Apache,Nginx,Netty,Redis,Tomcat,MySQL,Zuul

    本文为<高性能网络编程游记>的第六篇"性能追击:万字长文30+图揭秘8大主流服务器程序线程模型". 最近拍的照片比较少,不知道配什么图好,于是自己画了一个,凑合着用,让 ...

  6. linux下bus、devices和platform的基础模型

    转自:http://blog.chinaunix.net/uid-20672257-id-3147337.html 一.kobject的定义:kobject是Linux2.6引入的设备管理机制,在内核 ...

  7. ThinkPHP 学习笔记 ( 三 ) 数据库操作之数据表模型和基础模型 ( Model )

    //TP 恶补ing... 一.定义数据表模型 1.模型映射 要测试数据库是否正常连接,最直接的办法就是在当前控制器中实例化数据表,然后使用 dump 函数输出,查看数据库的链接状态.代码: publ ...

  8. ThinkPHP 数据库操作之数据表模型和基础模型 ( Model )

    一.定义数据表模型 1.模型映射 要测试数据库是否正常连接,最直接的办法就是在当前控制器中实例化数据表,然后使用 dump 函数输出,查看数据库的链接状态.代码: public function te ...

  9. [您有新的未分配科技点]博弈论进阶:似乎不那么恐惧了…… (SJ定理,简单的基础模型)

    这次,我们来继续学习博弈论的知识.今天我们会学习更多的基础模型,以及SJ定理的应用. 首先,我们来看博弈论在DAG上的应用.首先来看一个小例子:在一个有向无环图中,有一个棋子从某一个点开始一直向它的出 ...

随机推荐

  1. .NET邮件发送详情

    ①百度下载个Free Smtp Server ②下载完成后,打开进去,点击左上角的options,弹出窗口,进行如下图选项即可(一般默认已正确选中),然后我们可以把这软件关了. (下次你重启电脑时,也 ...

  2. es6笔记4^_^function

    一.function默认参数 现在可以在定义函数的时候指定参数的默认值了,而不用像以前那样通过逻辑或操作符来达到目的了. es5 function sayHello(name){ //传统的指定默认参 ...

  3. vector的基础使用

    vector是一个容器,实现动态数组. 相似点:下标从0开始. 不同点:vector创建对象后,容器大小会随着元素的增多或减少而变化. 基础操作: 1.使用vector需要添加头文件,#include ...

  4. 小猫爬山:dfs

    题目描述: 翰翰和达达饲养了N只小猫,这天,小猫们要去爬山. 经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了(呜咕>_<). 翰翰和达达只好花钱让它们坐索道下山. ...

  5. C++ 学习笔记之——字符串和字符串流

    1. 字符数组 字符数组,也就是存放字符类型数据的数组,只不过字符数组的结尾必须是 '\0'.C++ 已经提供了一些字符串处理函数,这些函数被封装在头文件 和 <string.h> 中. ...

  6. [leetcode-753-Open the Lock]

    You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...

  7. HDU 4617 Weapon(三维几何)

    Problem Description Doctor D. are researching for a horrific weapon. The muzzle of the weapon is a c ...

  8. 软件工程 作业part3 读后感

    匆匆看完构建之法,觉得这种不认真看完书就去写随笔去评价这本书是对作者的不尊重,所以觉得应该提问题和写感悟. 我的一点拙见,提的问题在现在这个信息发达的时候感觉只要有时间都可以自己解决. 感觉软件工程这 ...

  9. Beta完结--感想及吐槽

    Beta冲刺结束啦!!! Beta冲刺结束啦!!! Beta冲刺结束啦!!! 这时候每个人的心情肯定都是非常激动的.随着Beta冲刺的结束,折磨了我们一整个学期的软工实践也差不多结束了.(实在是太不容 ...

  10. 1001 Duplicate Pair

    1.题目戳这里 2.代码: #include<stdio.h> #include<string.h> int main() { int n; while(scanf(" ...