这是JS版本的飞机大战,和C#版本的思路相同,就是语言上有差别,用来巩固知识。可以将代码直接引入到HTML中就可以看到效果

//编写背景对象
function Background(width,height,X,Y){
// 背景的宽度
this.width=width;
// 背景的高度
this.height=height;
// 背景的水平坐标
this.X=X;
// 背景的垂直坐标
this.Y=Y;
// 创建背景函数将背景存放在 draw属性上
this.draw=(function(X,Y,width,height){
var a=document.createElement("div");
a.style.cssText="position:fixed;top:"+Y+"px;left:"+X+"px;width:"
+width+"px;height:"+height+"px;background-color:aqua;";
return a;
})(this.X,this.Y,this.width,this.height);
// 创建背景函数的绘制方法
this.drawing=function(){
document.body.appendChild(this.draw);
}
}
//创建我方飞机对象
function MyPlane(width,height,X,Y){
// 背景的宽度
this.width=width;
// 背景的高度
this.height=height;
// 背景的水平坐标
this.X=X;
// 背景的垂直坐标
this.Y=Y;
// 创建我方飞机函数将背景存放在 draw属性上
this.draw=(function(X,Y,width,height){
var a=document.createElement("div");
a.style.cssText="position:fixed;top:"+Y+"px;left:"+X+"px;width:"
+width+"px;height:"+height+"px;background-color:aqua;";
return a;
})(this.X,this.Y,this.width,this.height);
// 创建我方飞机函数的绘制方法
this.drawing=function(){
document.body.appendChild(this.draw);
}
// 创建飞机移动函数,同时进行边缘判断
this.move=function(x,y){
if(x>=620&&x<=1030){
this.X = x;
}
if(y>=170&&y<=730){
this.Y = y;
}
this.draw.style.cssText="position:fixed;top:"
+(this.Y-this.height/2)+"px;left:"+(this.X-this.width/2)+
"px;width:"+width+"px;height:"+height+"px;background-color:red;";
}
}
//创建子弹对象
function MyZidan(width,height,X,Y,speed){
//子弹的宽度
this.width=width;
//子弹的高度
this.height=height;
//子弹的水平坐标
this.X=X;
//子弹的垂直坐标
this.Y=Y;
//子弹的速度
this.speed=speed;
//将子弹dom存放在dram上
this.draw=(function(X,Y,width,height){
var a=document.createElement("div");
a.style.cssText="position:fixed;top:"+Y+"px;left:"+X+"px;width:"
+width+"px;height:"+height+"px;background-color:aqua;";
return a;
})(this.X,this.Y,this.width,this.height);
//创建子弹函数的绘制方法
this.drawing=function(){
document.body.appendChild(this.draw);
}
//子弹移动函数
this.move=function(){
this.Y=this.Y-this.speed;
this.draw.style.cssText="position:fixed;top:"
+(this.Y-this.height/2)+"px;left:"+(this.X-this.width/2)+
"px;width:"+width+"px;height:"+height+"px;background-color:red;";
}
//子弹删除函数
this.remove=function(){
document.body.removeChild(this.draw);
}
}
//创建敌人飞机对象
function EnemyPlane(width,height,X,Y,speed){
//敌人飞机的宽度
this.width=width;
//敌人飞机的长度
this.height=height;
//敌人飞机的水平坐标
this.X=X;
//敌人飞机的垂直坐标
this.Y=Y;
//敌人飞机的速度
this.speed=speed;
//将敌人飞机dom存放在dram上
this.draw=(function(width,height,X,Y){
var a=document.createElement("div");
a.style.cssText="position:fixed;top:"+Y+"px;left:"+X+"px;width:"
+width+"px;height:"+height+"px;background-color:blue;";
return a;
})(this.width,this.height,this.X,this.Y);
//创建敌人飞机函数的绘制方法
this.drawing=function(){
document.body.appendChild(this.draw);
}
//敌人飞机移动函数
this.move=function(){
this.Y=this.Y+this.speed;
this.draw.style.cssText="position:fixed;top:"
+(this.Y-this.height/2)+"px;left:"+(this.X-this.width/2)+
"px;width:"+width+"px;height:"+height+"px;background-color:blue;";
}
//敌人飞机删除函数
this.remove=function(){
document.body.removeChild(this.draw);
}
}
//创建单例类
function SingleObjct(background,myPlane){
//存放实例化背景对象
this.background=background;
//存放实例化我方飞机对象
this.myPlane=myPlane;
//存放我方飞机子弹对象列表
this.myZidan=[];
//添加子弹对象
this.addZidan=function(){
this.myZidan.push(new MyZidan(10,10,this.myPlane.X,this.myPlane.Y-20,2));
}
//绘制子弹函数
this.drawMyZidan=function(){
for(var i=0;i<this.myZidan.length;i++){
this.myZidan[i].drawing();
}
}
//子弹移动函数
this.moveMyZidan=function(){
for(var i=0;i<this.myZidan.length;i++){
this.myZidan[i].move();
}
}
//判断子弹是否出界,出界则将对象删除
this.moveToBorderZidan=function(){
for(var i=0;i<this.myZidan.length;i++){
if(this.myZidan[i].Y<=150){
this.myZidan[i].remove();
this.myZidan.splice(i,1);
return;
}
}
}
//存放敌人飞机对象列表
this.ListEnemyPlane=[];
//添加敌人飞机对象方法
this.addEnemyPlany=function(X,Y){
var a=new EnemyPlane(20,20,X,Y,2);
a.drawing();
this.ListEnemyPlane.push(a);
}
//绘制敌人飞机函数
this.drawEnemyPlane=function(){
for(var i=0;i<this.ListEnemyPlane.length;i++){
this.ListEnemyPlane[i].drawing();
}
}
//移动敌人飞机函数
this.moveEnemyPlane=function(){
for(var i=0;i<this.ListEnemyPlane.length;i++){
this.ListEnemyPlane[i].move();
}
}
//判断敌人飞机是否出界,出界就将其移除
this.moveToBorderEnemyPlane=function(){
for(var i=0;i<this.ListEnemyPlane.length;i++){
if(this.ListEnemyPlane[i].Y>=740){
this.ListEnemyPlane[i].remove();
this.ListEnemyPlane.splice(i,1);
}
}
}
//判断子弹和敌人飞机是否向撞
this.PZJC=function(){
for(var i=0;i<this.myZidan.length;i++){
for(var j=0;j<this.ListEnemyPlane.length;j++){
if(rectangle(this.myZidan[i].width,this.myZidan[i].height,this.myZidan[i].X,this.myZidan[i].Y,
this.ListEnemyPlane[j].width,this.ListEnemyPlane[j].height,
this.ListEnemyPlane[j].X,this.ListEnemyPlane[j].Y)){
this.myZidan[i].remove();
this.myZidan.splice(i,1);
this.ListEnemyPlane[j].remove();
this.ListEnemyPlane.splice(j,1);
return;
}
}
}
}
}
//窗体加载
window.onload=function(){
var singleObjct=new SingleObjct(new Background(450,600,600,150),new MyPlane(40,40,100,100));
singleObjct.background.drawing();
singleObjct.myPlane.drawing();
//鼠标移动事件
window.onmousemove=function(event){
singleObjct.myPlane.move(event.clientX,event.clientY);
}
//鼠标单击事件
window.onclick=function(event){
singleObjct.addZidan();
singleObjct.drawMyZidan();
}
//间隔时间触发函数,10毫秒触发一次
var timer = setInterval(function(){
singleObjct.moveToBorderZidan();
singleObjct.moveMyZidan();
singleObjct.moveToBorderEnemyPlane();
singleObjct.moveEnemyPlane();
singleObjct.PZJC();
if(singleObjct.ListEnemyPlane.length<6){
singleObjct.addEnemyPlany(getRandomX(),getRandomY());
} },10)
}
//随机生成水平坐标
function getRandomX(){
return ~~(Math.random()*430+600)
}
//随机生成垂直坐标
function getRandomY(){
return ~~(Math.random()*200+110)
}
//判断矩形重合
function rectangle(width1,height1,X1,Y1,width2,height2,X2,Y2){
var rectangleX1=X1+width1/2;
var rectangleY1=Y1+height1/2;
var rectangleX2=X2+width2/2;
var rectangleY2=Y2+height2/2;
var X=Math.abs(rectangleX1-rectangleX2);
var Y=Math.abs(rectangleY1-rectangleY2);
if(2*X<width1+width2&&2*Y<height1+height2){
return true;
}
return false;
}

JS 实现飞机大战的更多相关文章

  1. js实例--飞机大战

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  2. JS+html实现简单的飞机大战

    摘要:通过原生的js+html实现简单的飞机大战小游戏效果,如图所示: 实现代码如下: 1.自己的飞机实现 飞机html: <!DOCTYPE html> <html lang=&q ...

  3. js 飞机大战

    完整文件及代码可以在网盘下载,下载链接为:https://pan.baidu.com/s/1hs7sBUs 密码: d83x 飞机大战css定义: <style> #container{ ...

  4. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...

  5. 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)

    微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...

  6. 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)

    微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  7. 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)

    微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  8. [知了堂学习笔记]_纯JS制作《飞机大战》游戏_第1讲(实现思路与游戏界面的实现)

    整体效果展示: 一.实现思路 如图,这是我完成该项目的一个逻辑图,也是一个功能模块完成的顺序图. 游戏界面的完成 英雄飞机对象实现,在实现发射子弹方法过程中,又引出了子弹对象并实现.在此时,英雄飞机能 ...

  9. 用Javascript模拟微信飞机大战游戏

    最近微信的飞机大战非常流行,下载量非常高. 利用JS进行模拟制作了一个简单的飞机大战[此源码有很多地方可以进行重构和优化] [此游戏中没有使用HTML5 任何浏览器都可以运行]. 效果图: 原理:利用 ...

随机推荐

  1. MeteoInfoLab脚本示例:读取文本文件绘制散度图

    MeteoInfoLab中读取文本文件数据的函数是asciiread,获取文本文件行.列数的函数是numasciirow和numasciicol,和NCL中函数名一致,但都是小写字母.本例中的示例数据 ...

  2. day12 Pyhton学习

    一.昨日内容回顾 1.函数名 函数名是一个变量名 可以作为集合类的元素 可以作为参数进行传递 def  func(fn): fn() 可以作为返回值返回 def outer(): def inner( ...

  3. 【转载】opencvVS2019配置方法

    环境: 系统:win10系统截至2020920版本 opencv版本:3.0.0版本 IDE:宇宙最强IDEA最新版本2019社区版 教程: 1.下载opencv安装包官网下载链接:https://o ...

  4. ubuntu vi编辑器上下左右为ABCD的解决办法

    这个ubuntu系统自带的vi版本太老导致的,所以解决办法就是安装新版的vi编辑器: 首先卸载旧版本的vi编辑器: $sudo apt-get remove vim-common 然后安装新版的vi: ...

  5. vue渐进式开发的理解和指令

    1.vue渐进式开发 vue是一个渐进式的框架,轻量,易于上手,为啥是渐进式那,我当时也很蒙,比如的官网是jquery写的,就可以通过script标签引入事先准备好的vue.min.js的压缩源代码或 ...

  6. [开源] .Net ORM FreeSql 1.10.0 稳步向行

    写在开头 FreeSql 是 .NET 开源生态下的 ORM 轮子,转眼快两年了,说真的开源不容易(只有经历过才明白).今天带点干货和湿货给大家,先说下湿货. 认识我的人,知道 CSRedisCore ...

  7. js实现无缝连接轮播图(七)实现左侧按钮的功能

    <!-- 这个animate.js 必须写到 index.js的上面引入 --><script src="js/animate.js"></scrip ...

  8. SELECT INTO与INSERT INTO SELECT用法

    SELECT INTO SELECT INTO 语句从一个表复制数据,然后把数据插入到另一个新表中: -- 创建 Websites 的备份,这种写法没走索引导致全表扫描 SELECT * INTO W ...

  9. Camera2使用surface支持

    surfaceview是运行在子线程,可以为相机提供不断的刷新 public class MainActivity extends AppCompatActivity { public void on ...

  10. MFiX-DEM中的串行碰撞搜索

    在计算颗粒碰撞的时候,需要进行neighbor颗粒的搜寻,只知道大概是基于网格与颗粒绑定的方式,但是具体的实现方式还是比较模糊.搜寻部分代码如下 (mfix-19.2.2): 可以直接观察到的是,这里 ...