背景红黄蓝 也可以条随机其他颜色
body {
width: 100%;
padding: 0;
margin: 0
}
.letter {
position: absolute;
width: 30px;
height: 30px;
text-align: center;
padding: 5px;
}
#start {
width: 100px;
text-align: center;
background-color: black;
color: #ffffff;
padding: 15px 15px;
margin: 0 auto
}
#start:hover {
cursor: pointer
}
#score {
font-size: 46px;
text-align: right;
top: 30px;
right: 50px;
display: none
}
$(function () {
var score = 0;// 分数
$("#start").click(function () {
$("#score").show();//显示分数
$(this).fadeOut("slow");//按钮隐藏
getLetter();//调用随机生成的字母
})
//生成一个随机的字母在A-Z之间
function getLetter() {
var color = randomcolor();
//A-Z 对应编码 65-90
var code = Math.floor(Math.random() * 26) + 65;
var ch = String.fromCharCode(code);
var swith = screen.width - 300;
var sheight = screen.height - 300;
//随机出现的位置
var top = Math.floor(Math.random() * sheight);
var left = Math.floor(Math.random() * swith);
//拼接好元素向页面追加
$("body").append('' + ch + '');
setTimeout(getLetter, 1000);//每隔一秒生成一个字母
}
//随机一个背景颜色
function randomcolor() {
var color = '';
// var arr = ['a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
var arr=['red','blue','yellow'];
// for (var i = 0; i < 3; i++) {
// color += arr[Math.floor(Math.random() * 15)];
// }
color = arr[Math.floor(Math.random() * 3)];
return color;
}
//键盘按下事件
$(document).keydown(function () {
var keycode = event.keyCode;//获取到我们按下了键盘的哪个按键,返回的是ascii码
var height = screen.height;
$('.letter' + keycode).animate({ "top": height + "px" }, 1000, function () {
score += 20;//分数累加
$("#score").html(score);//把分数显示在页面上的元素中
$(this).remove();// 把隐藏元素移除掉
})
})
})
源代码在这 我只是稍作修改
h5网页版字母消除游戏制作,html+jquery_字母游戏 h5-CSDN博客
code.jquery.com/jquery-3.7.1.js
相关链接
发表评论