本文主要是介绍Web开发基础:JavaScript飞机大战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
超详细的Java知识点路线图
飞机大战
图片链接:https://pan.baidu.com/s/1elfvlHClwwDnc8buUnySPw
提取码:mi5y
功能点:
1、飞机跟着鼠标移动
在onmousemove事件中获得鼠标的坐标,设置给飞机图片
2、发射子弹
子弹需要动态添加到网页中
动态创建标签对象
document.createElement(“标签名”);
动态添加到层中
层对象.appendChild(标签对象);
设置样式
3、敌机移动
4、击中敌机
代码
<!DOCTYPE html>
<html>
<head><title>飞机大战</title><style type="text/css">#bg{width: 300px;height: 500px;background: url(images/background_1.png);/*margin: 0px auto;*/}#plane{width: 40px;height: 50px;position: absolute;}</style>
</head>
<body><div id="bg" onmousemove="movePlane()"><img id="plane" src="images/plane.gif"></div><script type="text/javascript">//获得背景对象var bg = document.getElementById("bg");//获得飞机对象var plane = document.getElementById("plane");//鼠标的位置var x,y;//移动飞机function movePlane(){//获得鼠标的x和y坐标x = window.event.clientX;y = window.event.clientY;if(x >=0 && x <= bg.offsetWidth && y >= 0 && y <= bg.offsetHeight){//修改飞机图片的位置plane.style.left = (x - plane.offsetWidth / 2) + "px";plane.style.top = (y - plane.offsetHeight / 2) + "px";}}//子弹的数组var bullets = new Array();//敌人的数组var enemys = new Array();//子弹类,x,y代表子弹出现的初始位置function Bullet(x,y){var img = document.createElement("img");//创建img标签对象bg.appendChild(img); //添加图片对象到游戏背景上img.src = "images/bullet1.png"; //设置子弹图片img.style.position = "absolute"; //绝对定位img.style.width = "6px";img.style.height = "14px";img.style.left = x + "px"; //设置子弹图片初始坐标img.style.top = y + "px";this.width = 6; //子弹的尺寸this.height = 14;this.x = x; //子弹的坐标this.y = y;this.move = function(){ //子弹的移动this.y -= 10; //向上移动img.style.top = this.y + "px"; //修改子弹图片的位置}this.remove = function(){ //删除子弹bg.removeChild(img); //从背景中删除图片}}//敌人类,x,y代表子弹出现的初始位置function Enemy(x,y){var img = document.createElement("img");//创建img标签对象bg.appendChild(img); //添加图片对象到游戏背景上img.src = "images/enemy1_fly_1.png"; //设置子弹图片img.style.position = "absolute"; //绝对定位img.style.width = "34px";img.style.height = "24px";img.style.left = x + "px"; //设置子弹图片初始坐标img.style.top = y + "px";this.width = 34; //子弹的尺寸this.height = 24;this.x = x; //子弹的坐标this.y = y;this.isDead = false;this.move = function(){ //子弹的移动this.y += 10; //向上移动img.style.top = this.y + "px"; //修改子弹图片的位置}this.remove = function(){ //删除子弹bg.removeChild(img); //从背景中删除图片}this.dead = function(){ //显示爆炸图片img.src = "images/boom.png";this.isDead = true;}}//创建子弹对象function createBullet(){bullets.push(new Bullet(x,y - plane.offsetHeight / 2));}//创建敌人对象function createEnemy(){enemys.push(new Enemy(bg.offsetWidth * Math.random(),0));}//移动所有的子弹function moveBullets(){for(var i = 0;i < bullets.length;i++){bullets[i].move(); //移动子弹checkHit(bullets[i]); //判断子弹是否打中敌人if(bullets[i].y <= 0){ //子弹到顶部后,删除子弹bullets[i].remove();bullets.splice(i,1);}}}//移动所有的敌人function moveEnemys(){for(var i = 0;i < enemys.length;i++){enemys[i].move(); //移动子弹if(enemys[i].y >= bg.offsetHeight || enemys[i].isDead){ //敌人到底部或被子弹打中,就删除敌人enemys[i].remove();enemys.splice(i,1);}}}//判断子弹是否打中敌人function checkHit(bullet){//遍历所有敌人for(var i = 0;i < enemys.length;i++){//碰撞检查if(bullet.x > enemys[i].x && bullet.x + bullet.width < enemys[i].x + enemys[i].width &&bullet.y > enemys[i].y && bullet.y + bullet.height < enemys[i].y + enemys[i].height){enemys[i].dead();}}}//启动周期任务执行创建子弹setInterval("createBullet()",200);//移动子弹setInterval("moveBullets()",50);//创建敌人setInterval("createEnemy()",2000);//移动敌人setInterval("moveEnemys()",100);</script>
</body>
</html>
这篇关于Web开发基础:JavaScript飞机大战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!