关于游戏开发:从零开发一个灰太狼游戏是什么样的体验

4次阅读

共计 4742 个字符,预计需要花费 12 分钟才能阅读完成。

先上一张效果图:

开发思路
开发一个游戏,首先你须要晓得游戏的规定。

这个游戏名为狂拍灰太狼。

规定:

游戏工夫 60 s
游戏角色为灰太狼、小灰灰
拼手速殴打灰太狼
殴打灰太狼 + 10 分,殴打小灰灰 – 10 分
开发技术

html
css
jq
实现思路

1. 利用 html + css 布局游戏界面
2. 导入 jq 库
3. 实现狂拍灰太狼游戏逻辑
外围逻辑

封装 60 s 进度条办法
封装解决灰太狼动画的办法
游戏按钮点击监听

HTML 代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title> 狂拍灰太狼 </title>
    <link rel="stylesheet" href="css/index.css">
    <script src="js/jquery-1.12.4.js"></script>
    <script src="js/index.js"></script>
</head>
<body>
<div class="container">
    <h1 class="score">0</h1>
    <div class="progress"></div>
    <button class="start"> 开始游戏 </button>
    <div class="rules"> 游戏规则 </div>
    <div class="rule">
        <p> 游戏规则:</p>
        <p>1. 游戏工夫:60s</p>
        <p>2. 拼手速, 殴打灰太狼 +10 分 </p>
        <p>3. 殴打小灰灰 -10 分 </p>
        <a href="#" class="close">[敞开]</a>
    </div>
    <div class="mask">
        <h1>GAME OVER</h1>
        <button class="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 () {
    // 1. 监听游戏规则的点击
    $(".rules").click(function () {$(".rule").stop().fadeIn(100);
    });

    // 2. 监听敞开按钮的点击
    $(".close").click(function () {$(".rule").stop().fadeOut(100);
    });

    // 3. 监听开始游戏按钮的点击
    $(".start").click(function () {$(this).stop().fadeOut(100);
        // 调用解决进度条的办法
        progressHandler();
        // 调用解决灰太狼动画的办法
        startWolfAnimation();});

    // 4. 监听从新开始按钮的点击
    $(".reStart").click(function () {$(".mask").stop().fadeOut(100);
        // 调用解决进度条的办法
        progressHandler();
        // 调用解决灰太狼动画的办法
        startWolfAnimation();});

    // 定义一个专门解决进度条的办法
    function progressHandler() {
        // 从新设置进度条的宽度
        $(".progress").css({width: 180});
        // 开启定时器解决进度条
        var timer = setInterval(function () {
            // 拿到进度条以后的宽度
            var progressWidth = $(".progress").width();
            // 缩小以后的宽度
            progressWidth -= 1;
            // 从新给进度条赋值宽度
            $(".progress").css({width: progressWidth});
            // 监听进度条是否走完
            if(progressWidth <= 0){
                // 敞开定时器
                clearInterval(timer);
                // 显示从新开始界面
                $(".mask").stop().fadeIn(100);
                // 进行灰太狼的动画
                stopWolfAnimation();}
        }, 100);
    }

    var wolfTimer;
    // 定义一个专门解决灰太狼动画的办法
    function startWolfAnimation() {
        // 1. 定义两个数组保留所有灰太狼和小灰灰的图片
        var wolf_1=['./images/h0.png','./images/h1.png','./images/h2.png','./images/h3.png','./images/h4.png','./images/h5.png','./images/h6.png','./images/h7.png','./images/h8.png','./images/h9.png'];
        var wolf_2=['./images/x0.png','./images/x1.png','./images/x2.png','./images/x3.png','./images/x4.png','./images/x5.png','./images/x6.png','./images/x7.png','./images/x8.png','./images/x9.png'];
        // 2. 定义一个数组保留所有可能呈现的地位
        var arrPos = [{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"}
        ];

        // 3. 创立一个图片
        var $wolfImage = $("<images src='' class='wolfImage'>");
        // 随机获取图片的地位
        var posIndex = Math.round(Math.random() * 8);
        // 4. 设置图片显示的地位
        $wolfImage.css({
           position: "absolute",
            left:arrPos[posIndex].left,
            top:arrPos[posIndex].top
        });
        // 随机获取数组类型
        var wolfType = Math.round(Math.random()) == 0 ? wolf_1 : wolf_2;
        // 5. 设置图片的内容
        window.wolfIndex = 0;
        window.wolfIndexEnd = 5;
        wolfTimer = setInterval(function () {if(wolfIndex > wolfIndexEnd){$wolfImage.remove();
                clearInterval(wolfTimer);
                startWolfAnimation();}
            $wolfImage.attr("src", wolfType[wolfIndex]);
            wolfIndex++;
        }, 300);

        // 6. 将图片增加到界面上
        $(".container").append($wolfImage);

        // 7. 调用解决游戏规则的办法
        gameRules($wolfImage);
    }

    function gameRules($wolfImage) {$wolfImage.one("click",function () {
            // 批改索引
            window.wolfIndex = 5;
            window.wolfIndexEnd = 9;

            // 拿到以后点击图片的地址
            var $src = $(this).attr("src");
            // 依据图片地址判断是否是灰太狼
            var flag = $src.indexOf("h") >= 0;
            // 依据点击的图片类型增减分数
            if(flag){
                // +10
                $(".score").text(parseInt($(".score").text()) + 10);
            }else{
                // -10
                $(".score").text(parseInt($(".score").text()) - 10);
            }
        });
    }
    function stopWolfAnimation() {$(".wolfImage").remove();
        clearInterval(wolfTimer);
    }
});

最终成果


根本制作过程不是很难,外围是了解其中的业务逻辑。

正文完
 0