vue 实现hangingman(简易

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>hangman</title>
	<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>

<div id="pa">
	<div id='word' v-modle:text="w" v-if="Wordshow">
		{{w}}
	</div>
	<button v-if="!word" @click="RandWord">start</button>

	<div id="letter-button" v-if="word">
			<button type="button" id="a" @click="ShowLetter('a')">a</button>
			<button type="button" id="b" @click="ShowLetter('b')">b</button>
			<button type="button" id="c" @click="ShowLetter('c')">c</button>
			<button type="button" id="d" @click="ShowLetter('d')">d</button>
			<button type="button" id="e" @click="ShowLetter('e')">e</button>
			<button type="button" id="f" @click="ShowLetter('f')">f</button>
			<!--......这里的id可以用模板代替,就不用那么多id了-->
	</div>


</div>

<script>

	String.prototype.replaceAt=function(index, character) {
    	return this.substr(0, index) + character + this.substr(index+character.length);
	}

	var p= new Vue({
		el:"#pa",
		data:{
			word:"",
			w:"",
			times:0,
			guesstime:0,
		},
		
		methods:{
			//先不设置随机单词
			RandWord : function(){
				this.word = "adaf";
				this.times = this.word.length;
				for(var i=0;i<this.times;i+=1){
					this.w+='-';
				}
				this.guesstime = 0;
			},
			Wordshow : function(){
				//console(this.word===this.w);
				if(this.word==this.w){
					this.w="you win:the word is "+this.word;
					this.word="";
					return;
				}
				if(this.times<=this.guesstime){
					this.w="you lose:the word is "+this.word;
					this.word="";
					return;
				}
				return;
			},

			ShowLetter : function(k){
				this.guesstime+=1;
				var p=document.getElementById(k);
				p.setAttribute('disabled',true);

				var i=0;
				while(i<this.times){
					if(this.word[i]==k){
						console.log(this.w[i]);
						this.w=this.w.replaceAt(i,k);
					}
					i+=1;
				}
				this.Wordshow();


			}

		}

	});

</script>




</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值