<!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. Linux命令应用大词典-第33章 X Window

    33.1 xhost:X服务器的访问控制程序 33.2 xinit:X Window系统初始化 33.3 Xlsclients:在显示器中列出正在运行的客户端应用程序 33.4 xlsfonts:显示 ...

  2. mysql新手入门随笔4

    40.子查询:出现在其他SQL语句里的SELECT语句 例如:SELECT sname,mark FROM student WHERE mark = (SELECT max(mark) FROM st ...

  3. 【C++模版之旅】项目中一次活用C++模板(traits)的经历 -新注解

    问题与需求: 请读者先看这篇文章,[C++模版之旅]项目中一次活用C++模板(traits)的经历. 对于此篇文章提出的问题,我给出一个新的思路. talking is cheap,show me t ...

  4. Java开发工程师(Web方向) - 04.Spring框架 - 第2章.IoC容器

    第2章.IoC容器 IoC容器概述 abstract: 介绍IoC和bean的用处和使用 IoC容器处于整个Spring框架中比较核心的位置:Core Container: Beans, Core, ...

  5. mybatis interceptor 处理查询参数及查询结果

    拦截器:拦截update,query方法,处理查询参数及返回结果. /** * Created by windwant on 2017/1/12. */ @Intercepts({ @Signatur ...

  6. 关于@media不生效的问题和meta总结

    1:之前做的是两套页面.现在改成响应式布局.发现加上 @media only screen and (max-width: 500px) {    .gridmenu {        width:1 ...

  7. Training Models

    In this page, I am going to talk about the 'hello world' model that is linear regression and train i ...

  8. 基于Hadoop2.5.0的集群搭建

    http://download.csdn.net/download/yameing/8011891 一. 规划 1.  准备安装包 JDK:http://download.oracle.com/otn ...

  9. java DTO 转 POJO

    如果这两个类的要转化的属性其属性名不一样的话,那只能用get和set方法赋值 如果你的两个类要转化的属性名都一样,那可以用org.springframework.beans.BeanUtils这个类来 ...

  10. 算法模板の数学&数论

    1.求逆元 int inv(int a) { ) ; return (MOD - MOD / a) * inv(MOD % a); } 2.线性筛法 bool isPrime[MAXN]; int l ...