javascript

javascript模式

2019-12-03  本文已影响0人  洛音轩

模式一

中介者模式

demo实现展示

分配玩家数及对应的key
结果展示

代码实现

<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/style.css" rel="stylesheet">
    <style>
        * {
            padding: 0px;
            margin: 0px;
        }
        
        .wrapper {
            width: 80%;
            margin: 100px auto;
            border: 2px solid #000;
            text-align: center;
        }
        
        .wrapper .startSection button {
            width: 60px;
            height: 40px;
            color: #f40;
            background-color: #000;
        }
        
        .wrapper .timeSection span {
            color: #0ff;
        }
        
        .wrapper .playerSection p {
            width: 60%;
            margin: 10px auto;
            height: 25px;
            line-height: 25px;
            border-bottom: 1px solid blue;
            background-color: yellow;
            color: #f00;
        }
        .wrapper .playerSection p #score {
            color: #0f0;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <div class="startSection">
            <button id="btn">start</button>
        </div>
        <div class="timeSection">
            <span id="time">倒计时3秒</span>
        </div>
        <div class="playerSection" id="playerSection">
        </div>
    </div>
    <script type="text/javascript" src="game-707.js">
    </script>

</body>
</html>
'use strict'
// 玩家构造函数 
// 有分数 有名字 有唯一的键盘key  还可以自己玩  玩完之后 -> mediator
function Player () {
    this.points = 0;
    this.name = '';
    this.key = '';
}
Player.prototype.play = function () {
    this.points += 1;
    mediator.playerPlay(this);
}

// 得分板对象
var scoreBoard = {
    element: document.getElementById('playerSection'),
    renderPage: function (allPlayers) {
        //显示所有玩家信息
        var innerHTMLStr = '';
        var str = '';
        for (var key in allPlayers) {
            if (key === 'length') continue;
            innerHTMLStr += '<p><span id="user">' + allPlayers[key].name + ':   </span><span id="score' + allPlayers[key].key + '">' + allPlayers[key].points + '</span></p>'; 
        }
        this.element.innerHTML = innerHTMLStr;
    },
    upPlayerScore: function (player) {
        var oSpanScore = document.getElementById('score' + player.key);
        oSpanScore.innerText = player.points;
    }
}

// mediator 中介者对象
var mediator = {
    allPlayers: {
        length: 0,
    },
    init: function () {
        var playNum = parseInt( window.prompt('请输入玩家个数') ) || 2;
        this.managePlayer(playNum);
        scoreBoard.renderPage(this.allPlayers);
        this.bindEvent();
    },
    bindEvent: function () {
        var startBtn = document.getElementById('btn');
        startBtn.onclick = this.gameStart.bind(this);
    },
    gameStart: function () {
        var oSpanTime = document.getElementById('time');
        var time = 3;
        var outerTimer = null;
        var innerTimer = null;
        var stopTime = 10;
        var _self = this;
        outerTimer = setInterval(function () {
            oSpanTime.innerText = '倒计时' + (time - 1) + '秒';
            if(--time == 0) {
                oSpanTime.innerText = 'Go!!!';
                window.onkeypress = _self.keyPress.bind(_self);
                clearInterval(outerTimer);
                innerTimer = setInterval(function () {
                    oSpanTime.innerText = stopTime - 1;
                    if (--stopTime == 0) {
                        clearInterval(innerTimer);
                        window.onkeypress = null;
                        oSpanTime.innerText = 'over';
                    }
                }, 1000);
            }
        }, 1000)
    },
    playerPlay: function (player) {
        scoreBoard.upPlayerScore(player);
    },
    managePlayer: function (num) {
        while(num--) {
            this.addPlayer();
        }
    },
    addPlayer: function () {
        var newPlayer = new Player();
        newPlayer.name = window.prompt('请设置玩家' + (this.allPlayers.length + 1) + '姓名');
        var onlyKey = window.prompt('请为玩家' + (this.allPlayers.length + 1) + '分配唯一的按键');
        while(this.allPlayers[onlyKey]) {
            // 提示你重新分配按键
            onlyKey = window.prompt('按键已经分配过,请为玩家重新分配唯一的按键');

        }
        newPlayer.key = onlyKey;
        this.allPlayers[onlyKey] = newPlayer;
        this.allPlayers.length++;
    },
    keyPress: function (e) {
        var event = e || window.event;
        var downKey = String.fromCharCode(event.charCode); //a
        this.allPlayers[downKey].play();
    }
}
mediator.init();
上一篇 下一篇

猜你喜欢

热点阅读