js实现出拳游戏

js实现出拳游戏

内容:玩家和电脑进行猜拳游戏,玩家进行点击剪刀石头布出拳,而电脑是随机出拳,只能进行10次游戏,谁赢一次叠加10分,最后输出结果
在这里插入图片描述

html代码:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>剪刀石头布</title>
    <style>
        .main {
            width: 500px;
            height: 300px;
            border: 2px solid black;
            display: flex;
            margin:auto;
            margin-top:10px;
        }

        .play {
            width: 200px;
            height: 100%;
        }

        .end {
            width: 100px;
            height: 100%;
            text-align: center;
            border-left: 2px solid black;
            border-right: 2px solid black;
        }

        img {
            width: 80px;
            height: 80px;
        }

        .wen {
            margin-bottom: 15px;
        }

        #cir {
            width: 90px;
            height: 90px;
            display: inline-block;
            border-radius: 50%;
            background-color: pink;
            margin-top: 75px;
            text-align: center;
            line-height: 90px;
            font-weight: bold;
        }

        #comList>img {
            margin-left: 50px;
            margin-top: 80px;
            position: absolute;
            display: none;
        }
        .move{
            margin-left: 80px;
        }
        #result{
            font-weight: bold;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="play">
            <div class="wen">
                玩家:<span>张三</span>分数:<span id="score">0</span>
            </div>
            <div id="playList">
                <img src="./images/cloth.png" alt="" data-value="0">
                <br>
                <img src="./images/clipper.png" alt="" data-value="1">
                <br>
                <img src="./images/stone.png" alt="" data-value="2">
            </div>

        </div>
        <div class="end">
            <p id="result">YOU WIN</p>
            <span id="cir">出拳</span>
        </div>
        <div class="comperter">
            <div class="wen">
                玩家:<span>电脑</span>分数:<span id="jscore">0</span>
            </div>
            <div id="comList">
                <img src="./images/cloth.png" alt="">
                <img src="./images/clipper.png" alt="">
                <img src="./images/stone.png" alt="">
            </div>

        </div>
    </div>
    <script src="./index3.js"></script>
    <!-- <script src="./index3-1.js"></script> -->

</body>

</html>
方法一:
//玩家分数
let score = document.getElementById("score");
//电脑分数
let jscore = document.getElementById("jscore");
//出拳
let cir = document.getElementById("cir");
//结果
let result = document.getElementById("result");
//玩家拳头
let playList = document.getElementById("playList");
//电脑拳头
let comList = document.querySelectorAll("#comList>img");

// 父类角色类
class Role {
    // 分数
    _score = 0;
    get score() {
        return this._score;
    }
    set score(value) {
        this._score = value;
    }
}
// 电脑类
class Computer extends Role {
    // 电脑出拳
    send() {
        //出拳点击
        let num = Math.floor(Math.random() * 3);
        if (num == 0) {
            comList[num].style.display = "block";
            comList[num + 1].style.display = "none";
            comList[num + 2].style.display = "none";
        } else if (num == 1) {
            comList[num].style.display = "block";
            comList[num - 1].style.display = "none";
            comList[num + 1].style.display = "none";
        } else if (num == 2) {
            comList[num].style.display = "block";
            comList[num - 1].style.display = "none";
            comList[num - 2].style.display = "none";
        }
        return num;
    }
}
// 玩家类
class Player extends Role {
    // 玩家出拳
    send() {
        //玩家出拳,返回一个我设定的自定义属性图片的value值
        let selected = document.querySelector(".move");
        if (selected) {
            return selected.dataset.value;
        }
        return -1;
    }
}

let play = new Player();
let com = new Computer();

//点击出拳
//判断游戏为10次
let count = 0;
cir.onclick = function () {
    count++;
    let comperValue = com.send();
    let playerValue = play.send();
    console.log(playerValue, comperValue);
    if (playerValue == -1) {
        alert("请选择要出的拳");
        return;
    }
    if(playerValue>comperValue){
        if(playerValue-comperValue==2){
            com.score +=10;
        }else{
            play.score +=10;
        }       
    }else if(playerValue <comperValue){
        if(comperValue-playerValue==2){
            play.score +=10;            
        }else{
            com.score +=10;            
        }
    }
    score.innerHTML = play.score;
    jscore.innerHTML = com.score;
    if(count==10){
        if(play.score>com.score){
            result.innerHTML = "YOU WIN";
        }else if (play.score<com.score){
            result.innerHTML = "YOU LOSE";
        }else{
            result.innerHTML = "LEVEL";
        }
        alert("GAME OVER!");
    }
}



playList.onclick = function (e) {
    //通过判断是否添加属性来控制属性为空还是添加
    let selected = document.querySelector(".move");
    if (selected) {
        selected.className = "";
    }
    e.target.className = "move";
}

方法二:
//玩家分数
let score = document.getElementById("score");
//电脑分数
let jscore = document.getElementById("jscore");
//出拳
let cir = document.getElementById("cir");
//结果
let result = document.getElementById("result");
//玩家拳头
let playList = document.querySelectorAll("#playList>img");
//电脑拳头
let comList = document.querySelectorAll("#comList>img");
//玩家赢了gplayer+10 电脑赢了gcomper+10
let gplayer = 0, gcomper = 0;


// 父类角色类
class Role {
    // 分数
    _score = 0;
}
// 电脑类
class Computer extends Role {
    // 电脑出拳
    send() {
        let num;
        num = Math.floor(Math.random() * 3);
        if (num == 0) {
            comList[num].style.display = "block";
            comList[num + 1].style.display = "none";
            comList[num + 2].style.display = "none";
        } else if (num == 1) {
            comList[num].style.display = "block";
            comList[num - 1].style.display = "none";
            comList[num + 1].style.display = "none";
        } else if (num == 2) {
            comList[num].style.display = "block";
            comList[num - 1].style.display = "none";
            comList[num - 2].style.display = "none";
        }
        //返回点击的出拳数
        return num;
    }
}
// 玩家类
class Player extends Role {

    // 玩家出拳
    send() {

    }

}



let play = new Player();
let com = new Computer();
let comval, sum;



for (let i = 0; i < playList.length; i++) {
    playList[i].onclick = function () {
        if (i == 0) {
            playList[i].style.marginLeft = "80px";
            playList[i + 1].style.marginLeft = "0px";
            playList[i + 2].style.marginLeft = "0px";
        }
        else if (i == 1) {
            playList[i].style.marginLeft = "80px";
            playList[i - 1].style.marginLeft = "0px";
            playList[i + 1].style.marginLeft = "0px";
        }
        else if (i == 2) {
            playList[i].style.marginLeft = "80px";
            playList[i - 1].style.marginLeft = "0px";
            playList[i - 2].style.marginLeft = "0px";
        }
        sum = i;
    }
}

let state = 0;
cir.onclick = function () {
    state++;
    comval = com.send();
    console.log("玩家:" + sum + ",电脑:" + comval);

    if (sum - comval == -2 || sum - comval == 2) {
        if (sum > comval) {
            gcomper += 10;
            jscore.innerHTML = gcomper;
        } else {
            gplayer += 10;
            score.innerHTML = gplayer;
        }
    } else if (sum > comval) {
        gplayer += 10;
        score.innerHTML = gplayer;
    } else if (comval > sum) {
        gcomper += 10;
        jscore.innerHTML = gcomper;
    }
    if (state == 10) {
        if (gplayer > gcomper) {
            result.innerHTML="YOU WIN";
            result.style.color = "green";
        } else if (gplayer == gcomper) {
            result.innerHTML="LEVEL";
            result.style.color = "yellow";
        } else {
            result.innerHTML="YOU LOSE";
            result.style.color = "red";
        }
        
        cir.onclick = null;
        alert("game over");
    }

}



// }

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值