极客江南: 一个对开发技术特别执着的程序员,对移动开发有着独到的见解和深入的研究,有着多年的ios、android、html5开发经验,对NativeApp、HybridApp、WebApp开发有着独到的见解和深入的研究, 除此之外还精通 Javascript、AngularJS、 NodeJS 、Ajax、jQuery、Cordova、React Native等多种Web前端技术及Java、php等服务端技术。
先上一张效果图:
开发一个游戏,首先你需要知道游戏的规则。
这个游戏名为狂拍灰太狼。
规则:
- 游戏时间 60 s
- 游戏角色为灰太狼、小灰灰
- 拼手速殴打灰太狼
- 殴打灰太狼 + 10 分,殴打小灰灰 - 10 分
开发技术
实现思路
- 1.利用 html + css 布局游戏界面
- 2.导入 jq 库
- 3.实现狂拍灰太狼游戏逻辑
核心逻辑
- 封装 60 s 进度条方法
- 封装处理灰太狼动画的方法
- 游戏按钮点击监听
HTML 代码
<html lang&#61;"en">
<head>
<meta charset&#61;"UTF-8">
<title>狂拍灰太狼title>
<link rel&#61;"stylesheet" href&#61;"css/index.css">
<script src&#61;"js/jquery-1.12.4.js">script>
<script src&#61;"js/index.js">script>
head>
<body>
<div class&#61;"container">
<h1 class&#61;"score">0h1>
<div class&#61;"progress">div>
<button class&#61;"start">开始游戏button>
<div class&#61;"rules">游戏规则div>
<div class&#61;"rule">
<p>游戏规则:p>
<p>1.游戏时间:60sp>
<p>2.拼手速,殴打灰太狼&#43;10分p>
<p>3.殴打小灰灰-10分p>
<a href&#61;"#" class&#61;"close">[关闭]a>
div>
<div class&#61;"mask">
<h1>GAME OVERh1>
<button class&#61;"reStart">重新开始button>
div>
div>
body>
html>
css 代码
*{
margin: 0;
padding: 0;
}
.container{
width: 320px;
height: 480px;
background: url("../images/game_bg.jpg") no-repeat 0 0;
margin: 50px auto;
position: relative;
}
.container>h1{
color: white;
margin-left: 60px;
}
.container>.progress{
width: 180px;
height: 16px;
background: url("../images/progress.png") no-repeat 0 0;
position: absolute;
top: 66px;
left: 63px;
}
.container>.start{
width: 150px;
line-height: 35px;
text-align: center;
color: white;
background: linear-gradient(#E55C3D,#C50000);
border-radius: 20px;
border: none;
font-size: 20px;
position: absolute;
top: 320px;
left: 50%;
margin-left: -75px;
}
.container>.rules{
width: 100%;
height: 20px;
background: #ccc;
position: absolute;
left: 0;
bottom: 0;
text-align: center;
}
.container>.rule{
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
position: absolute;
left: 0;
top: 0;
padding-top: 100px;
box-sizing: border-box;
text-align: center;
display: none;
}
.rule>p{
line-height: 50px;
color: white;
}
.rule>a{
color: red;
}
.container>.mask{
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
position: absolute;
left: 0;
top: 0;
padding-top: 200px;
box-sizing: border-box;
text-align: center;
display: none;
}
.mask>h1{
color: #ff4500;
text-shadow: 3px 3px 0 #fff;
font-size: 40px;
}
.mask>button{
width: 150px;
line-height: 35px;
text-align: center;
color: white;
background: linear-gradient(#74ACCF,#007DDC);
border-radius: 20px;
border: none;
font-size: 20px;
position: absolute;
top: 320px;
left: 50%;
margin-left: -75px;
}
jq 代码
$(function () {
$(".rules").click(function () {
$(".rule").stop().fadeIn(100);
});
$(".close").click(function () {
$(".rule").stop().fadeOut(100);
});
$(".start").click(function () {
$(this).stop().fadeOut(100);
progressHandler();
startWolfAnimation();
});
$(".reStart").click(function () {
$(".mask").stop().fadeOut(100);
progressHandler();
startWolfAnimation();
});
function progressHandler() {
$(".progress").css({
width: 180
});
var timer &#61; setInterval(function () {
var progressWidth &#61; $(".progress").width();
progressWidth -&#61; 1;
$(".progress").css({
width: progressWidth
});
if(progressWidth <&#61; 0){
clearInterval(timer);
$(".mask").stop().fadeIn(100);
stopWolfAnimation();
}
}, 100);
}
var wolfTimer;
function startWolfAnimation() {
var wolf_1&#61;[&#39;./images/h0.png&#39;,&#39;./images/h1.png&#39;,&#39;./images/h2.png&#39;,&#39;./images/h3.png&#39;,&#39;./images/h4.png&#39;,&#39;./images/h5.png&#39;,&#39;./images/h6.png&#39;,&#39;./images/h7.png&#39;,&#39;./images/h8.png&#39;,&#39;./images/h9.png&#39;];
var wolf_2&#61;[&#39;./images/x0.png&#39;,&#39;./images/x1.png&#39;,&#39;./images/x2.png&#39;,&#39;./images/x3.png&#39;,&#39;./images/x4.png&#39;,&#39;./images/x5.png&#39;,&#39;./images/x6.png&#39;,&#39;./images/x7.png&#39;,&#39;./images/x8.png&#39;,&#39;./images/x9.png&#39;];
var arrPos &#61; [
{left:"100px",top:"115px"},
{left:"20px",top:"160px"},
{left:"190px",top:"142px"},
{left:"105px",top:"193px"},
{left:"19px",top:"221px"},
{left:"202px",top:"212px"},
{left:"120px",top:"275px"},
{left:"30px",top:"295px"},
{left:"209px",top:"297px"}
];
var $wolfImage &#61; $("");
var posIndex &#61; Math.round(Math.random() * 8);
$wolfImage.css({
position: "absolute",
left:arrPos[posIndex].left,
top:arrPos[posIndex].top
});
var wolfType &#61; Math.round(Math.random()) &#61;&#61; 0 ? wolf_1 : wolf_2;
window.wolfIndex &#61; 0;
window.wolfIndexEnd &#61; 5;
wolfTimer &#61; setInterval(function () {
if(wolfIndex > wolfIndexEnd){
$wolfImage.remove();
clearInterval(wolfTimer);
startWolfAnimation();
}
$wolfImage.attr("src", wolfType[wolfIndex]);
wolfIndex&#43;&#43;;
}, 300);
$(".container").append($wolfImage);
gameRules($wolfImage);
}
function gameRules($wolfImage) {
$wolfImage.one("click",function () {
window.wolfIndex &#61; 5;
window.wolfIndexEnd &#61; 9;
var $src &#61; $(this).attr("src");
var flag &#61; $src.indexOf("h") >&#61; 0;
if(flag){
$(".score").text(parseInt($(".score").text()) &#43; 10);
}else{
$(".score").text(parseInt($(".score").text()) - 10);
}
});
}
function stopWolfAnimation() {
$(".wolfImage").remove();
clearInterval(wolfTimer);
}
});
最终效果
基本制作过程不是很难&#xff0c;核心是理解其中的业务逻辑。
大家记得收藏前&#xff0c;先点个赞哦&#xff01;好的文章值得被更多人看到。
推荐阅读&#xff1a;
13万字C语言保姆级教程2021版
10万字Go语言保姆级教程
2 万字 Jquery 入门教程
3 万字 html &#43;css 入门教程
最后&#xff0c;再多一句&#xff0c;粉丝顺口溜&#xff0c;关注江哥不迷路&#xff0c;带你编程上高速。