打字游戏(使用Vue开发)

作者: 南昌大魔王 | 来源:发表于2018-03-14 16:51 被阅读61次

这是一个打字游戏。

规则如下

1、当输入第一个字母的时候,开始计时,当完全输入正确,计时停止。并且不能继续输入。
2、输入字母正确时显示为绿色,错误的显示为红色。
3、输入正确后,点击刷新按钮,可以换题目。

截图如下

开始游戏页面 输入正确页面

你可以学习到以下知识点

1、组件的全局使用。
2、模版的使用。
3、props。从父组件传递参数到子组件。
4、自定义事件。子组件通过emit与父组件进行通讯。

代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>打字游戏</title>
    <script src="https://cdn.bootcss.com/vue/2.5.13/vue.js"></script>
    <style type="text/css">
        .main {
            border: 1px solid #ccc;
            padding: 15px;
            font-size: 20px;
        }
        .green {
            color: green;
        }
        .red {
            color: red;
        }
    </style>
</head>
<body>

    <div id="app" class="main">
        <h1>{{ title }}</h1>
        <hr>
        <typing-game :time-status="currentStatus" @toggle-time-status="toggleStatus" ></typing-game>
        <time-record :time-status="currentStatus" ></time-record>
    </div>

    <div>
        还可以优化的地方:
        1、计数器刷新清零。实现单独计时。(完成)
        2、统计多次计时,算出总数。
        3、不要使用focus触发计时器,使用watch触发。(完成)
    </div>

    <!--模版区域-->
    <script type="text/x-template" id="typing-game-template">
        <div>
            <div>题目 
                <button @click="refresh" :disabled="buttonDisabled">刷新</button>
                <!--
                {{ timeStatus }}
                <button @click="toggleTime">切换</button>
                -->
            </div>
          <div>{{ question }}</div>

          <div><input type="text" v-model="input" :disabled="textDisabled" >  <!--@focus="toggleTime"-->    </div>
          <div v-html="answer"></div>
        </div>
    </script>

    <script type="text/x-template" id="time-record-template">
        <div>
            {{ time.toFixed(2) }}
        </div>
    </script>
    <!--模版区域-->

    <script type="text/javascript">
        let timeCounter; //计数器变量

        Vue.component('typing-game', {
            template: '#typing-game-template',
            props: ['timeStatus'],
            data () {
                return {
                    question: getRandomString() ,
                    input: '',
                    textDisabled: false,
                    buttonDisabled: false,
                    isTyping: true //是否正在输入
                }   
            },
            computed: {
                answer () {
                    let questionLen = this.question.length;
                    let questionAry = [];
                    let input = this.input;
                    let inputLen = input.length;
                    let inputAry = [];
                    let output = '';
                    let count = 0;
                    for (let i = 0; i < questionLen; i ++) {
                        questionAry.push(this.question[i])
                    }
                    
                    for (let i = 0; i < inputLen; i ++) {
                        inputAry.push(input[i])
                    }

                    inputAry = inputAry.map((value, index) => {
                        if (value === questionAry[index]) {
                            value = '<span class="green">' + value + '</span>';
                            count ++;
                        } else {
                            value = '<span class="red">' + value + '</span>';
                        }
                        return value;
                    })
                    if (questionLen === count && questionLen === inputLen ) {
                        output = inputAry.join('') + ' 完全正确';
                        this.textDisabled = true;
                        this.toggleTime();
                    } else {
                        output = inputAry.join('');
                    }
                    return output;
                }
            },
            methods: {
                refresh () {
                    this.question = getRandomString();
                    this.input = '';
                    this.textDisabled = false;
                },
                toggleTime () {
                    this.buttonDisabled = !this.buttonDisabled;
                    this.isTyping = !this.isTyping;
                    this.$emit('toggle-time-status');
                }
            },
            watch: {
                input: function (newValue, oldValue) {
                    if (this.isTyping) {
                        this.toggleTime();
                    } 
                }
            }
        })

        Vue.component('time-record', {
            template: '#time-record-template',
            //props: ['timeStatus', 'currentTime'],
            props: {
                timeStatus: String,
                currentTime: Number
            },
            data () {
                return {
                    time: 0.00,
                }
            },
            watch: {
                timeStatus (newValue, oldValue) {
                    console.log(111111);
                    if (newValue === 'start') {
                        this.time = 0.00;
                        timeCounter = setInterval(() => {
                            this.time += 0.01
                            //this.time = this.time.toFixed(2);
                        }, 10); 
                    } else {
                        clearInterval(timeCounter)
                    }
                }
            }
        })

        var app = new Vue({
            el: '#app',
            data: {
                title: 'Typing Game',
                currentStatus: 'end'
            },
            methods: {
                toggleStatus () {
                    this.currentStatus = this.currentStatus === 'start' ? 'end' : 'start';
                }
            }
        })

        function getRandomString() {
            let chars = 'abcdefghijklmnopqsrtuvwxyz';
            const max =  20; //less than 20
            let randomString = '';
            let randomNumber = Math.floor(Math.random() * max); 
            randomNumber = randomNumber === 0 ? max : randomNumber;
            for (let i = 0; i < randomNumber; i ++) {
                randomString += chars.charAt(Math.floor(Math.random() * 26));

            }
            return randomString;
        }
    </script>
</body>
</html>

相关文章

  • 打字游戏(使用Vue开发)

    这是一个打字游戏。 规则如下 1、当输入第一个字母的时候,开始计时,当完全输入正确,计时停止。并且不能继续输入。2...

  • 使用 Vue 开发单页应用全攻略

    使用 Vue 开发单页应用全攻略 网上面有很多关于如何使用 Vue 开发单页应用的教程,从 Vue-cli 的使用...

  • VUE+WebPack:开发一款太空版植物大战僵尸的前端页游

    从本节开始,我们探讨如何使用VUE和WebPack开发一款类似于植物大战僵尸的前端游戏,当游戏完成后,情况如下: ...

  • vue组件开发那些事

    使用vue开发感悟 刚开始开发vue的组件有些不太习惯,对vue templte的模板语法对比react渲染的内容...

  • 《Poptile》

    今天这款轻游戏是由开发《Mr Jump》的1Button SARL开发的《Poptile》。呼,手机打字真累! 这...

  • Github 上 基于 Vue 的 12 个开源且优秀的 UI

    越来越多的公司开始使用 Vue 开发各种项目, Vue 项目是组件化开发,在开发工程师使用各种各样的组件,例如...

  • plugin插件

    插件通常用来为 Vue 添加全局功能。 直接使用别人开发好的插件:Vue.use() 自己开发插件: Vue.js...

  • Vue-Router实现前端页面缓存

    一、使用情景 在使用Vue开发单页面应用时,我们通常会使用Vue-Router进行页面导航,Vue-Router在...

  • 五、Vue生态介绍

    一、Vue组件库Vue组件库是使用Vue框架开发的组件,一般包含着开发者可以直接使用的基础组件:表单、弹窗、表格等...

  • vue文档集合

    Vue2使用笔记3--父子组件的通信Vue 2.0开发实践(组件间通讯)Event Bus 在Vue中的使用vue...

网友评论

    本文标题:打字游戏(使用Vue开发)

    本文链接:https://www.haomeiwen.com/subject/avazfftx.html