首页 > 其他 > 详细

vue 实现一个简单的石头剪刀布小游戏

时间:2021-08-24 20:21:01      阅读:10      评论:0      收藏:0      [点我收藏+]

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>es2015</title>
</head>
<body>
<div id="counter">
<button v-for="(item,index) in buttonList" @click="choose1(index)">{{item}}</button>
<br/>
<button @click="begin(this)">确定</button>

<div v-for="item in resultList" :key="item.id">
<p v-if="item.id">{{item.value}}</p>
</div>
</div>
</body>
</html>
<script src="https://unpkg.com/vue@next"></script>
<script>

let array = [‘剪刀‘,‘石头‘,‘布‘]
let array_so = {0:1,1:2,2:0}

// 获取1-10随机数
let random = () => {
return Math.floor( Math.random() * 10 )
}

let Ai = () => {
let random1 = random()
if(random1 <= 3)
{
return 0
}
if(random1 > 3 && random1 < 7)
{
return 1
}
if(random1 >= 7)
{
return 2
}
}

const Counter = {
data(){
return {
counter:0, //总次数
choose:0, //用户选择
random:0,//Ai选择
aiCount:0, //Ai次数
userCount:0, // 用户次数
resultList:[], // 结果集
buttonList:array
}
},
methods:{
begin(event){
let that = this
that.random = Ai()
that.counter += 1
if(that.choose === that.random)
{
data = {‘id‘:that.counter,‘value‘:`AI【${that.buttonList[that.random]}】user【${that.buttonList[that.choose]}】平局`}
}else if(that.choose == array_so[that.random])
{
data = {‘id‘:that.counter,‘value‘:`AI【${that.buttonList[that.random]}】user【${that.buttonList[that.choose]}】您赢了`}
}else{
data = {‘id‘:that.counter,‘value‘:`AI【${that.buttonList[that.random]}】user【${that.buttonList[that.choose]}】Ai赢了`}
}
that.resultList.push(data)
console.log(that.resultList)
},
choose1:function(e)
{
this.choose = e
}
}
}
Vue.createApp(Counter).mount("#counter")
</script>
<style>
button{
margin-left: 20px;
margin-top: 20px;
}
</style>

vue 实现一个简单的石头剪刀布小游戏

原文:https://www.cnblogs.com/zhangxi1/p/15181620.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!