JavaScript实现气球打字的小游戏

 更新时间:2022年04月02日 17:19:48   作者:公众号_前端每日技巧  
这篇文章主要介绍了JavaScript实现气球打字的小游戏,下面文章的思路来源于打字游戏,具体实现过程,需要的朋友可以参考一下,希望对你的学习有所帮助

一、实现效果

JavaScript实现气球打字游戏_前端

1、定义球的类

气球类中我们需要对26个字符进行处理

this.arr = "abcdefghijklmnopqrstuvwxyz".split("");

生成一个随机字母

this.index = parseInt(Math.random() * this.arr.length);
// 定义随机字符
this.str = this.arr[this.index];

生成一个div标签并对图片进行处理

// 元素属性
this.dom = document.createElement("div");
// 图片属性
this.img = img;
// 图片的宽
this.width = this.img.width / 4;
// 图片的高
this.height = this.img.height / 3;
// 图片的背景定位X
this.positionX = parseInt(Math.random() * 4);
// 图片的背景定位Y
this.positionY = parseInt(Math.random() * 3);

关于样式的处理操作

// 设置样式
this.setStyle = function() {
// 设置元素定位
this.dom.style.position = "absolute";
this.dom.style.left = 0;
// 设置元素的内部文本
this.dom.innerHTML = this.str;
// 设置文本样式
this.dom.style.lineHeight = this.height * 2 / 3+ "px";
this.dom.style.textAlign = "center";
this.dom.style.fontSize = "20px";
this.dom.style.fontWeight = "bold";
this.dom.style.top = parseInt(Math.random() * (document.documentElement.clientHeight - this.height)) + "px";
// 设置元素的宽度和高度
this.dom.style.width = this.width + "px";
this.dom.style.height = this.height + "px";
// 设置元素背景图片
this.dom.style.backgroundImage = "url(" + this.img.src + ")";
// 设置元素的背景定位
this.dom.style.backgroundPositionX = -this.width * this.positionX + "px";
this.dom.style.backgroundPositionY = -this.height * this.positionY + "px";
}

定义一个上树的方法

// 上树方法
this.upTree = function() {
document.body.appendChild(this.dom);
}

我们需要检测气球是否到达浏览器边缘

// 检测气球是否到达边界
this.check = function() {
// 判断定位left值值是否到达别界
if (this.dom.offsetLeft >= document.documentElement.clientWidth - this.width) {
// 设置定位值
this.dom.style.left = document.documentElement.clientWidth - this.width + "px";
return true;
}
return false;
}

定义一个下树的方法

// 下树方法
this.boom = function() {
this.dom.parentNode.removeChild(this.dom);
}

定义一个移动的方法,其中的数字表示气球移动的速度

// 移动方法
this.move = function() {
this.dom.style.left = this.dom.offsetLeft + 5 + "px";
}

定义初始化的方法并执行

// 定义初始化方法
this.init = function() {
this.setStyle();
this.upTree();
}
// 执行init
this.init();

创建图片元素

// 创建图片元素
var img = document.createElement("img");
// 设置路径
img.src = "images/balloon.jpg";

气球每隔多少时间生成一个,我们需要设置定时器以及气球到达边界的处理,其中代码中的​​70​​表示每移动70次创建一个气球。

// 定义数组
var arr = [];
// 定义定时器
var timer = null;
// 定义一个信号量
var count = 0;
// 添加事件
img.onload = function() {
// 初始化气球对象
var balloon = new Balloon(img);
// 第一个气球也要放入数组中
arr.push(balloon);
// 赋值定时器
timer = setInterval(function() {
// 信号量++
count++;
// 判断信号量
if (count % 70 === 0) {
// 气球每移动70次, 创建一个气球
arr.push(new Balloon(img));
}
// 循环数组
for (var i = 0; i < arr.length; i++) {
// 调用move方法
arr[i].move();
// 调用check方法
var result = arr[i].check();
// 判断是否到达别界
if (result) {
// 说明气球到达边界了
// 将气球从数组中移除
arr.splice(i, 1);
// 防止数组塌陷
i--;
// 清除并接触边界进行弹窗
// clearInterval(this.timer)
// alert('游戏结束')
}
}
}, 20)

最后就是我们在气球未触到边缘时,通过键盘清除打出对应的字母

// 给document绑定键盘事件
document.onkeydown = function(e) {
// 获取用户按下的字符
var key = e.key;
// 拿着这个key与数组中每一个气球对象的str属性值作比对,如果比对上了,就让气球从数组中移除并且从dom中移除

for (var i = 0; i < arr.length; i++) {
// 判断
if (key.toLowerCase() === arr[i].str.toLowerCase()) {
// 调用boom方法
arr[i].boom();
// 移除当前项
arr.splice(i, 1);
break;
}
}
}

二、源码仓库和效果

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script type="text/javascript">
	// 定义气球类
	function Balloon(img) {
		// 定义携带的字符
		this.arr = "abcdefghijklmnopqrstuvwxyz".split("");
		// 定义索引
		this.index = parseInt(Math.random() * this.arr.length);
		// 定义随机字符
		this.str = this.arr[this.index];
		// 元素属性
		this.dom = document.createElement("div");
		// 图片属性
		this.img = img;
		// 图片的宽
		this.width = this.img.width / 4;
		// 图片的高
		this.height = this.img.height / 3;
		// 图片的背景定位X
		this.positionX = parseInt(Math.random() * 4);
		// 图片的背景定位Y
		this.positionY = parseInt(Math.random() * 3);
		// 设置样式
		this.setStyle = function() {
			// 设置元素定位
			this.dom.style.position = "absolute";
			this.dom.style.left = 0;
			// 设置元素的内部文本
			this.dom.innerHTML = this.str;
			// 设置文本样式
			this.dom.style.lineHeight = this.height * 2 / 3+ "px";
			this.dom.style.textAlign = "center";
			this.dom.style.fontSize = "20px";
			this.dom.style.fontWeight = "bold";
			this.dom.style.top = parseInt(Math.random() * (document.documentElement.clientHeight - this.height)) + "px";
			// 设置元素的宽度和高度
			this.dom.style.width = this.width + "px";
			this.dom.style.height = this.height + "px";
			// 设置元素背景图片
			this.dom.style.backgroundImage = "url(" + this.img.src + ")";
			// 设置元素的背景定位
			this.dom.style.backgroundPositionX = -this.width * this.positionX + "px";
			this.dom.style.backgroundPositionY = -this.height * this.positionY + "px";
		}
		// 上树方法
		this.upTree = function() {
			document.body.appendChild(this.dom);
		}
		// 检测气球是否到达边界
		this.check = function() {
			// 判断定位left值值是否到达别界
			if (this.dom.offsetLeft >= document.documentElement.clientWidth - this.width) {
				// 设置定位值
				this.dom.style.left = document.documentElement.clientWidth - this.width + "px";
				return true;
			}
			return false;
		}
		// 下树方法
		this.boom = function() {
			this.dom.parentNode.removeChild(this.dom);
		}
		// 移动方法
		this.move = function() {
			this.dom.style.left = this.dom.offsetLeft + 5 + "px";
		}
		// 定义初始化方法
		this.init = function() {
			this.setStyle();
			this.upTree();
		}
		// 执行init
		this.init();
	}

	// 创建图片元素
	var img = document.createElement("img");
	// 设置路径
	img.src = "images/balloon.jpg";

	// 定义数组
	var arr = [];
	// 定义定时器
	var timer = null;
	// 定义一个信号量
	var count = 0;
	// 添加事件
	img.onload = function() {
		// 初始化气球对象
		var balloon = new Balloon(img);
		// 第一个气球也要放入数组中
		arr.push(balloon);
		// 赋值定时器
		timer = setInterval(function() {
			// 信号量++
			count++;
			// 判断信号量
			if (count % 70 === 0) {
				// 气球每移动70次, 创建一个气球
				arr.push(new Balloon(img));
			}
			// 循环数组
			for (var i = 0; i < arr.length; i++) {
				// 调用move方法
				arr[i].move();
				// 调用check方法
				var result = arr[i].check();
				// 判断是否到达别界
				if (result) {
					// 说明气球到达边界了
					// 将气球从数组中移除
					arr.splice(i, 1);
					// 防止数组塌陷
					i--;
          // 清除并接触边界进行弹窗
          // clearInterval(this.timer)
          // alert('游戏结束')
				}
			}
		}, 20)
	}


	// 给document绑定键盘事件
	document.onkeydown = function(e) {
		// 获取用户按下的字符
		var key = e.key;
		// 拿着这个key与数组中每一个气球对象的str属性值作比对,如果比对上了,就让气球从数组中移除并且从dom中移除
		
		for (var i = 0; i < arr.length; i++) {
			// 判断
			if (key.toLowerCase() === arr[i].str.toLowerCase()) {
				// 调用boom方法
				arr[i].boom();
				// 移除当前项
				arr.splice(i, 1);
				break;
			}
		}
	}
	</script>
</body>
</html>

效果:

JavaScript实现气球打字游戏_前端

到此这篇关于JavaScript实现气球打字的小游戏的文章就介绍到这了,更多相关JavaScript气球打字游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JS实现六位字符密码输入器功能

    JS实现六位字符密码输入器功能

    这篇文章主要介绍了JS实现六位字符密码输入器功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-08-08
  • JavaScript实现星座查询功能 附详细代码

    JavaScript实现星座查询功能 附详细代码

    最近小编在做一个项目,其中涉及到一个模块关于星座查询功能,即在文本框中输入一个生日值,点击按钮可以得到对应的星座,怎么实现这个需求呢?下面小编通过示例代码给大家介绍下,需要的朋友参考下吧
    2021-11-11
  • uniapp实现日期时间选择器

    uniapp实现日期时间选择器

    这篇文章主要为大家详细介绍了uniapp实现日期时间选择器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • JavaScript中layer关闭指定弹出窗口方法总结

    JavaScript中layer关闭指定弹出窗口方法总结

    这篇文章主要给大家介绍了关于JavaScript中layer关闭指定弹出窗口方法的相关资料,layer是layui的一个弹出层组件,但是可以作为独立组件使用,需要的朋友可以参考下
    2023-10-10
  • js实现点击图片改变页面背景图的方法

    js实现点击图片改变页面背景图的方法

    这篇文章主要介绍了js实现点击图片改变页面背景图的方法,实例分析了javascript操作css与图片的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • 原生JS利用transform实现banner的无限滚动示例代码

    原生JS利用transform实现banner的无限滚动示例代码

    这篇文章主要介绍了原生JS利用transform实现banner的无限滚动示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • JavaScript如何优化逻辑判断代码详解

    JavaScript如何优化逻辑判断代码详解

    我们在编写 JS 代码时,经常会遇到逻辑判断复杂的情况,这篇文章主要给大家介绍了关于JavaScript如何优化逻辑判断代码的相关资料,需要的朋友可以参考下
    2021-06-06
  • 鼠标经过tr时,改变tr当前背景颜色

    鼠标经过tr时,改变tr当前背景颜色

    本篇文章主要介绍了鼠标经过tr时,改变tr当前背景颜色的示例代码,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • 解析JavaScript中 querySelector 与 getElementById 方法的区别

    解析JavaScript中 querySelector 与 getElementById 方法的区别

    这篇文章主要介绍了JavaScript中 querySelector 与 getElementById 方法的区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-10-10
  • 微信小程序实现跟随菜单效果和循环嵌套加载数据

    微信小程序实现跟随菜单效果和循环嵌套加载数据

    这篇文章主要为大家详细介绍了微信小程序实现跟随菜单效果和循环嵌套加载数据,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11

最新评论