结合JavaScript和html写一个简单的猜拳游戏

本文介绍了一个简单的网页版石头剪刀布游戏实现方法。通过JavaScript进行逻辑处理,包括随机生成电脑选择、判断输赢并更新显示结果。玩家可以通过点击按钮进行游戏。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>石头剪刀布游戏</title>
</head>
<script type="text/javascript">
function Game(type){
var mth=parseInt(Math.random()*3+1);
comimg(type,mth);
var ss=document.getElementById("play");
switch (type){
case 1:
ss.src="img/石头.jpg";
break;
case 2:
ss.src=" img/剪刀.png"
break;
case 3:
ss.src=" img/布.jpg"
break;
}


if(type==mth){
document.getElementById("text").value="平局!!!!!!!"
}else if(type==1 && mth==2){
document.getElementById("text").value="你赢了!!!!"
}else if(type==1 && mth==3){
document.getElementById("text").value="你输了!!!!!1"
}else if(type==2 && mth==1){
document.getElementById("text").value="你输了!!!!!1"
}else if(type==2 && mth==3){
document.getElementById("text").value="你赢了!!!!1"
}else if(type==3 && mth==1){
document.getElementById("text").value="你赢了!!!!!!!1"
}else if(type==3 && mth==2){
document.getElementById("text").value="你输了!!!!!!"
}

}

function comimg (type,mth) {
var ss1=document.getElementById("playy");
switch (mth){
case 1:
ss1.src="img/石头.jpg";
break;
case 2:
ss1.src="img/剪刀.png";
break;
case 3:
ss1.src="img/布.jpg";
break;
}
}
</script>
<body>
<div style="width: 400px;height: 400px;display: inline;"><img src="img/石头.jpg" id="play" style="width: 350px;height: 350px;" ></div>
<div style="width: 400px;height: 400px;display: inline;"><img src="img/石头.jpg" id="playy" style="width: 350px;height: 350px;" ></div>

<input type="button" value="石头" onclick="Game(1)" />
<input type="button" value="剪刀" onclick="Game(2)" />
<input type="button" value="布" onclick="Game(3)" />
        <input type="text" id="text" style="width: 100px; height: 20px; font-family: '新宋体';" />
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值