一个打蜜蜂的小游戏demo的代码

2023-10-31 09:41

本文主要是介绍一个打蜜蜂的小游戏demo的代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

图片: fj.png  mf1.png  mf2.png  mf3.png

前沿介绍:

   this.map.offsetWidth  代表地图本身的宽度
this.oUl.offsetWidth  代表蜜蜂ul的宽度
this.oUl.style.left   代表小蜜蜂距离左边的位置
this.oA.style.left = (this.map.offsetWidth - this.oA.offsetWidth)/2 + 'px';  //自己飞机距离左边的位置
this.oA.style.top = this.map.offsetHeight - this.oA.offsetHeight + 'px';     //自己飞机距离上边的位置
var oLi = document.createElement('li');  创建一个li节点信息  并保存在 oLi变量中(代表每一个小蜜蜂)
var oB = document.createElement('div');  创建一个div节点信息,代表子弹的变量
oB.style.left = this.oA.offsetLeft + this.oA.offsetWidth/2 + 'px'; 子弹左边的距离
oB.style.top = this.oA.offsetTop + 'px';  子弹的高度
this.oA = document.createElement('div'); 创建一个div节点信息,代表自己战机的div
var e = evt || window.event; 判断是ie浏览器还是其他浏览器
//如果是空格将发射子弹
if(e.keyCode==32){
This.createBullet();
}

//转换蜜蜂的布局,将浮动转换成定位
击中蜜蜂之后,会删除最后那一个,因为蜜蜂(li标签)是浮动布局,删除某一个之后后面的元素会补充上来,所以我们需要将浮动布局转换成定位布局
for (var i = 0; i < this.oLi.length; i++) {
this.oLi[i].style.left = this.oLi[i].offsetLeft + 'px';
this.oLi[i].style.top = this.oLi[i].offsetTop + 'px';
}
for (var i = 0; i < this.oLi.length; i++) {
this.oLi[i].style.position = 'absolute'
}
//掉下一个蜜蜂
// X代表横坐标移动的距离
// Y代表纵坐标移动的高度
// X轴的速度 = X轴移动的距离/总共移动的距离)*合成的速度
// Y轴的速度 = Y轴移动的距离/总共移动的距离)*合成的速度

var nowLi = this.oLi[Math.round(Math.random()*this.oLi.length)];//随机出现一个蜜蜂。
window.location.reload(); //刷新页面

以下是代码**********************************************************************************打蜜蜂********************************************************************************

<html>

<head>
<meta charset="utf-8">
<title>打蜜蜂demo</title>
<style type="text/css">
body,div,ul,li{
margin: 0;
padding: 0;
}
li{
list-style: none;
}
#map{
width: 800px;
height: 600px;
background: #000;
margin: 20px auto 0px;
position: relative;
}
#btn{
position: absolute;
left: 350px;
top: 285px;
color: #fff;
border: 1px solid #fff;
padding: 10px;
cursor: pointer;
font-size: 20px;
}
.score{
color: white;
}
.enemy{

position: absolute;
}
.enemy1{
float: left;
width: 40px;
height: 28px;
background: url(images/mf1.png) no-repeat;


}
.enemy2{
float: left;
width: 40px;
height: 28px;
background: url(images/mf2.png) no-repeat;


}
.enemy3{
float: left;
width: 40px;
height: 28px;
background: url(images/mf3.png) no-repeat;


}
.air{
width: 46px;
height: 60px;
background: url(images/fj.png) no-repeat;
position: absolute;


}
.bullet{width:1px;height: 10px;background: #fff;position: absolute;


}
</style>
</head>
<body>
<div id="map">
<div id="btn">开始游戏</div>
</div>




<script type="text/javascript">
var btn =document.getElementById('btn');
btn.onclick = function(){
btn.style.display="none";
//start游戏
Game.init();  //初始化游戏内部结构
}


//用面向对象来实现功能
//采用面向对象来实现该游戏
var Game = {
//属性:描述物体的一些特征
enemy:{
e1:{style:'enemy1',blood:1,score:1,speed:5},
e2:{style:'enemy2',blood:2,score:2,speed:7},
e3:{style:'enemy3',blood:3,score:3,speed:10}
},
air:{
style:'air',
bulletStyle:'bullet'
},
gk:[
{
eMap:[
'e2','e2','e2','e2','e2','e2','e2','e2','e2','e2',
'e2','e2','e2','e2','e2','e2','e2','e2','e2','e2',
'e2','e2','e2','e2','e2','e2','e2','e2','e2','e2',
'e1','e1','e1','e1','e1','e1','e1','e1','e1','e1',
'e1','e1','e1','e1','e1','e1','e1','e1','e1','e1',
'e1','e1','e1','e1','e1','e1','e1','e1','e1','e1',
],
colNum:10, //每一列的数量
speedX:5,
speedY:10,
times:2000
},
{
eMap:[
'e2','e2','e2','e2','e2','e2','e2','e2','e2','e2',
'e2','e2','e2','e2','e2','e2','e2','e2','e2','e2',
'e2','e2','e2','e2','e2','e2','e2','e2','e2','e2',
'e3','e3','e3','e3','e3','e3','e3','e3','e3','e3',
'e3','e3','e3','e3','e3','e3','e3','e3','e3','e3',
'e3','e3','e3','e3','e3','e3','e3','e3','e3','e3',
],
colNum:12,
speedX:10,
speedY:10,
times:1000
},
],
//初始化的操作
init:function(){
//获取地图的节点信息
this.map = document.getElementById('map');
//绘制积分
this.createScore();
//绘制蜜蜂
this.createEnemy(0);//默认创建第一关的蜜蜂
//绘制战机
this.createAir();
},
//创建积分
createScore:function(){
var score = document.createElement('div');
score.className = 'score';//设置class样式
score.innerHTML = '积分:<span>0</span>分';
this.oScore = score.getElementsByTagName('span')[0];
this.map.appendChild(score);
},
//创建敌人蜜蜂
createEnemy:function(g){
//创建ul的节点信息
this.oUl = document.createElement('ul');
//给他一个name属性(好将来设置样式)
this.oUl.className = 'enemy';
this.oUl.style.width = this.gk[g].colNum * 40 + 'px';
//地图追加ul为子元素
this.map.appendChild(this.oUl);
this.oUl.style.left = (this.map.offsetWidth - this.oUl.offsetWidth)/2+'px';
//开始创建li标签,每个li标签代表一只小蜜蜂
for(var i=0;i<this.gk[g].eMap.length;i++){
var oLi = document.createElement('li');
oLi.className =  this.enemy[this.gk[g].eMap[i]].style;//e1   e1  e1  e2.
oLi.blood =  this.enemy[this.gk[g].eMap[i]].blood;//e1   e1  e1  e2....
oLi.score =  this.enemy[this.gk[g].eMap[i]].score;//e1   e1  e1  e2....
oLi.speed =  this.enemy[this.gk[g].eMap[i]].speed;//e1   e1  e1  e2....
this.oUl.appendChild(oLi);
}


this.oLi = this.oUl.getElementsByTagName('li');
//转换蜜蜂的布局,将浮动转换成定位
for (var i = 0; i < this.oLi.length; i++) {
this.oLi[i].style.left = this.oLi[i].offsetLeft + 'px';
this.oLi[i].style.top = this.oLi[i].offsetTop + 'px';
}
for (var i = 0; i < this.oLi.length; i++) {
this.oLi[i].style.position = 'absolute'
}
//蜜蜂创建完就开始移动
this.runEnemy(this.gk[g]);
},
//让蜜蜂移动
runEnemy:function(gk){
var This = this;
var L = 0;
var R = this.map.offsetWidth - this.oUl.offsetWidth -5;
window.setInterval(function(){
if(This.oUl.offsetLeft<=0){
gk.speedX *= -1;
This.oUl.style.top = This.oUl.offsetTop + gk.speedY +'px';
}else if(This.oUl.offsetLeft>R){
gk.speedX *= -1;
This.oUl.style.top = This.oUl.offsetTop + gk.speedY +'px';
}
//定时器里面的this代表的当前对象,指的就是window对象
This.oUl.style.left = This.oUl.offsetLeft + gk.speedX + 'px';
//document.title = This.oUl.offsetLeft;
}, 100)
//每隔30毫秒让它出现一个蜜蜂来攻击我们
setInterval(function(){
This.oneMove();//每隔2秒就派一个蜜蜂来攻击
}, gk.times)
},
//掉下一个蜜蜂
// X代表横坐标移动的距离
// Y代表纵坐标移动的高度
// X轴的速度 = X轴移动的距离/总共移动的距离)*合成的速度
// Y轴的速度 = Y轴移动的距离/总共移动的距离)*合成的速度
oneMove:function(){
var nowLi = this.oLi[Math.round(Math.random()*this.oLi.length)];
var This = this;
clearInterval(nowLi.timer)
nowLi.timer = setInterval(function(){
var X = This.oA.offsetLeft - nowLi.offsetLeft - nowLi.parentNode.offsetLeft;
var Y = This.oA.offsetTop - nowLi.offsetTop - nowLi.parentNode.offsetTop;
var C = Math.sqrt(X*X + Y*Y);
var HSpeed = nowLi.speed;
var xspeed = HSpeed * (X/C);
var yspeed = HSpeed * (Y/C);
nowLi.style.left = nowLi.offsetLeft + xspeed +'px';
nowLi.style.top = nowLi.offsetTop + yspeed +'px';
if(X<=0 && Y<=0){
alert(' 你太菜了...游戏结束');
window.location.reload();//刷新页面
}
}, 50)
},
//创建自己的战机
createAir:function(){
this.oA = document.createElement('div');
this.oA.className = this.air.style;
this.map.appendChild(this.oA);
this.oA.style.left = (this.map.offsetWidth - this.oA.offsetWidth)/2 + 'px';
this.oA.style.top = this.map.offsetHeight - this.oA.offsetHeight + 'px';
//创建战机之后就绑定事件
this.bindEvent();
},
bindEvent:function(){
var This = this;
var timer = null;
document.onkeydown = function(evt){
//移动战机
var e = evt || window.event;
//alert(e.keyCode)
if(e.keyCode==37){
if(!timer){ //timer不存在的时候,则创建定时器
timer = setInterval(function(){
This.oA.style.left = This.oA.offsetLeft - 10 + 'px';
}, 30) //向左
}
}else if(e.keyCode==39){
if(!timer){
timer = setInterval(function(){
This.oA.style.left = This.oA.offsetLeft + 10 + 'px';
}, 30) //向右
}
}
}
document.onkeyup = function(evt){
//发射子弹
var e = evt || window.event;
//alert(e.keyCode)
clearInterval(timer);
timer = null;
if(e.keyCode==32){
This.createBullet();
}
}
},
createBullet:function(){
var oB = document.createElement('div');
oB.className = this.air.bulletStyle;
this.map.appendChild(oB);
oB.style.left = this.oA.offsetLeft + this.oA.offsetWidth/2 + 'px';
oB.style.top = this.oA.offsetTop + 'px';
//开始移动
this.runBullet(oB);//让刚刚创建的这颗子弹移动
},
runBullet:function(oB){
var This = this;
clearInterval(oB.timer);
oB.timer = setInterval(function(){
if(oB.offsetop<-10){
clearInterval(oB.timer);
This.map.removeChild(oB);
}else{
oB.style.top = oB.offsetTop - 10 +'px';
}
//判断这颗子弹是否击中蜜蜂
for(var i=0;i<This.oLi.length;i++){
var BL = oB.offsetLeft;
var LIL = This.oLi[i].offsetLeft + This.oUl.offsetLeft;
var LIR = This.oLi[i].offsetLeft+This.oLi[i].offsetWidth+This.oUl.offsetLeft;
var BT = oB.offsetTop;
var LIT = This.oLi[i].offsetTop + This.oUl.offsetTop;
var LITT = This.oLi[i].offsetTop+This.oLi[i].offsetHeight+This.oUl.offsetTop;
if(
   BL >  LIL && 
   BL< LIR &&  
   BT > LIT && 
   BT< LITT){
//alert('hello')
//删除子弹
This.map.removeChild(oB);
clearInterval(oB.timer);
//删除蜜蜂
if(This.oLi[i].blood==1){
//删除蜜蜂之前获得该蜜蜂的积分
This.oScore.innerHTML = parseInt(This.oScore.innerHTML) + This.oLi[i].score;
This.oUl.removeChild(This.oLi[i]);
}else{
This.oLi[i].blood--;
}
}
}
}, 30)
}
}


</script>


</body>
</html>

这篇关于一个打蜜蜂的小游戏demo的代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/gujinapenggu5/article/details/50464004
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/314451

相关文章

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元

Python实现一键PDF转Word(附完整代码及详细步骤)

《Python实现一键PDF转Word(附完整代码及详细步骤)》pdf2docx是一个基于Python的第三方库,专门用于将PDF文件转换为可编辑的Word文档,下面我们就来看看如何通过pdf2doc... 目录引言:为什么需要PDF转Word一、pdf2docx介绍1. pdf2docx 是什么2. by

Spring Security介绍及配置实现代码

《SpringSecurity介绍及配置实现代码》SpringSecurity是一个功能强大的Java安全框架,它提供了全面的安全认证(Authentication)和授权(Authorizatio... 目录简介Spring Security配置配置实现代码简介Spring Security是一个功能强

通过cmd获取网卡速率的代码

《通过cmd获取网卡速率的代码》今天从群里看到通过bat获取网卡速率两段代码,感觉还不错,学习bat的朋友可以参考一下... 1、本机有线网卡支持的最高速度:%v%@echo off & setlocal enabledelayedexpansionecho 代码开始echo 65001编码获取: >

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

IDEA实现回退提交的git代码(四种常见场景)

《IDEA实现回退提交的git代码(四种常见场景)》:本文主要介绍IDEA实现回退提交的git代码(四种常见场景),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.已提交commit,还未push到远端(Undo Commit)2.已提交commit并push到

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.