Javascript----实现21点游戏

1、效果

洗牌:

要牌:

亮牌:

2、代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>21点游戏</title>
    <style type="text/css">
    .container {
        width: 1080px;
        height: 600px;
        border: 1px solid #cccccc;
        margin: 0 auto;
    }
    .header {
        width: 100%;
        height: 30px;
        line-height: 30px;
        text-align: right;
    }

    .header span {
        margin: 0 20px 0 0;
    }

    .nav {
        text-align: center;
    }

    .banker,
    .player {
        width: 100%;
        height: 200px;
        border: 1px solid red;
        position: relative;
        top: 0;
        left: 0;
    }

    .point {
        height: 40px;
        line-height: 40px;
        width: 100px;
        text-align: center;
        font-size: 20px;
    }

    .puke-list1 {
        position: absolute;
        top: 40px;
        left: 0;
        width: 72px;
        height: 107px;
        overflow: hidden;
        img {
            width: 72px;
            height: 107px;
        }
    }

    .puke-list2 {
        position: absolute;
        top: 40px;
        left: 0;
        width: 72px;
        height: 107px;
        overflow: hidden;
        box-shadow: 0 0 10px 1px gray;
        img {
            width: 72px;
            height: 107px;
        }
    }

    .option {}

    .winner-val {
        color: red;
        font-size: 30px;
    }
    </style>
</head>

<body>
    <div class="container">
        <div class="header">
            <span class=‘time‘ id=‘time‘></span>
        </div>
        <div class="nav">
            <h2>21点游戏</h2>
        </div>
        <div class="banker">
            <div class="banker-point point" id=‘banker-point‘>12</div>
            <div class="box" id=‘zhaungjia-box‘>
            </div>
        </div>
        <div class="player">
            <div class="player-point point" id=‘player-point‘>21</div>
            <div class="box" id=‘player-box‘>
            </div>
        </div>
        <div class=‘option‘>
            <fieldset>
                <legend>玩家</legend>
                <a href="#" class=‘player-add‘ id=‘player-add‘>要牌</a>
                <a href="#" class=‘player-show‘ id=‘player-show‘>亮牌</a>
                <a href="#" class=‘refresh‘ id=‘refresh‘>洗牌</a>
            </fieldset>
        </div>
        <div class="winner">
            <span>赢家:</span>
            <span class=‘winner-val‘ id=‘winner-val‘></span>
        </div>
    </div>
    <script type="text/javascript">
    var timeSpan = document.getElementById("time");
    var point1 = document.getElementById("banker-point");
    var point2 = document.getElementById("player-point");
    var box1 = document.getElementById("zhaungjia-box");
    var box2 = document.getElementById("player-box");
    var add = document.getElementById("player-add");
    var show = document.getElementById("player-show");
    var refresh = document.getElementById("refresh");
    var winnerVal = document.getElementById("winner-val");

    var left1 = 0;
    var left2 = 0;
    var left3 = 0;
    var bankerPoint = 0;
    var playerPoint = 0;
    var is_finish = false;
    var is_over = false;
    ////console.log(show);

    init();

    //初始化
    function init() {
        box1.innerHTML = "";
        box2.innerHTML = "";
        var pai1 = getPai();
        var n1 = pai1.number > 9 ? 10 : pai1.number;
        bankerPoint += n1;
        point1.innerText = "庄家:"+bankerPoint+"点";
        var obj = "<div class=‘puke-list1‘ style = ‘left: 0px‘>" +
            "<img src=‘static/imgs/21point/images/timg_" + pai1.url + ".png‘>" +
            "</div>";
        box1.innerHTML += obj;

        var pai2 = getPai();
        var n2 = pai2.number > 9 ? 10 : pai2.number;
        playerPoint += n2;
        point2.innerText = "玩家:"+playerPoint+"点";
        var obj = "<div class=‘puke-list1‘ style = ‘left: 0px‘>" +
            "<img src=‘static/imgs/21point/images/timg_" + pai2.url + ".png‘>" +
            "</div>";
        box2.innerHTML += obj;
        //console.log("洗牌:"+bankerPoint + "," + playerPoint);
    }

    //玩家要牌
    add.addEventListener("click", function() {
        if (!is_finish) {
            left2 += 40;
            var pai = getPai();
            var n = pai.number > 9 ? 10 : pai.number;
            playerPoint += n;
            point2.innerText = "玩家:"+playerPoint+"点";
            //console.log("玩家:"+bankerPoint + "," + playerPoint);
            var obj = "<div class=‘puke-list2‘ style = ‘left:" + left2 + "px‘>" +
                "<img src=‘static/imgs/21point/images/timg_" + pai.url + ".png‘>" +
                "</div>";
            box2.innerHTML += obj;
            if (playerPoint > 21) {
                winnerVal.innerText = "玩家已爆,庄家赢!";
                is_finish = true;
            }
        }
    })

     //亮牌
    show.addEventListener("click", function() {
        var clock = setInterval(function() {
            if (bankerPoint <= 21 && bankerPoint < playerPoint) {
                var pai = getPai();
                var n = pai.number > 9 ? 10 : pai.number;
                bankerPoint += n;
                point1.innerText = "庄家:"+bankerPoint+"点";
               //console.log("庄家:"+bankerPoint + "," + playerPoint);
                left1 += 40;
                var obj = "<div class=‘puke-list1‘ style = ‘left:" + left1 + "px‘>" +
                    "<img src=‘static/imgs/21point/images/timg_" + pai.url + ".png‘>" +
                    "</div>";
                box1.innerHTML += obj;

                if (bankerPoint > 21) {
                    winnerVal.innerHTML = "庄家已爆,玩家赢!";
                    clearInterval(clock);
                      is_over  = true;
                }

            } else {
                if (bankerPoint > playerPoint) {
                    winnerVal.innerText = "庄家点数大,庄家赢!";
                } else if (bankerPoint == playerPoint) {
                    winnerVal.innerText = "点数相同,平局!";

                } else {
                    winnerVal.innerText = "玩家点数大,玩家赢!";
                }
            }

        }, 1000)
    })

    //洗牌
    refresh.addEventListener("click", function() {
        bankerPoint = 0;
        playerPoint = 0;
        init();
        is_finish = false;
        left1 = 0;
        left2 = 0;
        winnerVal.innerText = "";
    })

    //获取随机牌
    function getPai() {
        var ran = parseInt(Math.random() * 51) + 1;
        //console.log("ran:" + ran);
        var imgUrl = "";
        var number;
        if (ran >= 1 && ran <= 52) {
            if (ran < 10) {
                imgUrl = "0" + ran;
            } else {
                imgUrl = ran;
            }

            if(ran>13){
               number = ran % 13;
               if(number == 0){
               	number = 13;
               }
            }
            else{
            	number = ran;
            }

        } else {
            return -1;
        }

        var pai = {
            "number": number,
            "url": imgUrl
        }
        return pai;
    }

    function getTime() {
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth();
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        var weekday = date.getDay();

        switch (weekday) {
            case "1":
                "星期一";
                break;
            case "2":
                "星期二";
                break;
            case "3":
                "星期三";
                break;
            case "4":
                "星期四";
                break;
            case "5":
                "星期五";
                break;
            case "6":
                "星期六";
                break;
            case "7":
                "星期日";
                break;
        }

        var s = year + "年" + month + "月" + day + "日 " + hour + ":" + minute + ":" + second;
        return s;
    }
    </script>
</body>

</html>

  3、总结:

游戏规则还不能灵活设定

时间: 2024-10-25 21:49:30

Javascript----实现21点游戏的相关文章

【分享】60行代码:Javascript 写的俄罗斯方块游戏

效果如下,可测试: javascript实现源码: <!doctype html> <html><head><title>俄罗斯方块</title> <meta name="Description" content="俄罗斯方块Javascript实现"> <meta name="Keywords" content="俄罗斯方块,Javascript,实现,短

利用javascript实现的小游戏-打地鼠

随着html5的兴起,那些公司对大型游戏的开发正在慢慢疏远,一.开发周期长:二.运营花费高:他们正找一些能够克服这些缺点的替代品.正好,html5的出现可以改变这些现状,在淘宝.京东等一些大型电商网站.QQ.微信等聊天软件都出现了html5的小游戏,这说明html5越来越受到大家的青睐.接下来我用javascript实现一个小型游戏---打地鼠. 打地鼠其实很好实现,主要思路有:先用表格做一个3*3的九宫格,然后放9张坑的图片-->设置一个小于等于9的随机数k,然后让k的坑的图片替换成地鼠的图片

60行代码:Javascript 写的俄罗斯方块游戏

先看效果图: 游戏结束图: javascript实现源码: <!doctype html> <html><head><title>俄罗斯方块</title> <meta name="Description" content="俄罗斯方块Javascript实现"> <meta name="Keywords" content="俄罗斯方块,Javascript,

用javascript实现一个2048游戏

早就想自己写一个2048游戏了,昨晚闲着没事,终于写了一个如下图,按方向键开始玩吧.如果觉得操作不方便,请直接打开链接玩吧:http://gujianbo.1kapp.com/2048/2048.html 附上源代码链接:https://github.com/gujianbo/js2048 个人博客地址:http://gujianbo.1kapp.com/ 新浪微博:http://weibo.com/gujianbobo 欢迎读者交流讨论并提出宝贵意见. 用javascript实现一个2048游

280行代码:Javascript 写的2048游戏

2048 原作者就是用Js写的,一直想尝试,但久久未动手. 昨天教学生学习JS代码.不妨就做个有趣的游戏好了.2048这么火,是一个不错的选择. 思路: 1. 数组 ,2维数组4x4 2. 移动算法,移动后有数字的对齐,无数字(我用的0,但不显示)补齐. 移动前 移动后(注意程序合并了第一行2个2,并产生了新的2) 移动算法分2步: 第一步骤:移动 第二步骤:合并 移动代码参考: [html] view plaincopy function left(t,i) { var j; var len 

21点游戏,命令行的,面像过程的写法。Ruby

21点游戏,命令行的,面像过程的写法. ruby 的写法以后再试试. # this is game 21 point # 操作命令符 空没有操作,n下一步,抽牌:w亮牌结束:l认输 $ope = '' $prompt = '>' $x1 = 0 $x2 = 0 $x3 = 0 $pc = 0 $u1 = 0 $u2 = 0 $u3 = 0 $over = '' def out_main(a = 0, b=0, c=0, pc = 0, us1=0, us2=0, us3=0) puts "

用python实现自动玩Npubits的21点游戏

用python实现自动玩Npubits的21点游戏 21点的主页面 https://--- (此处不提供链接,用户直接在网站首页点击21点后,地址栏的链接便是.) 需要知道的关键点 (写代码时用来抓取数据的关键点) 1. 等待开局 若之前的21点还没有结束(暂时没有对手上线),那么不能开局,需等待之前的结束.若需等待,主页面包含以下内容 <button type="submit" class="btn btn-default">刷新</button

应用zip压缩的javascript以及Egret H5游戏实战

代码地址如下:<br>http://www.demodashi.com/demo/11039.html 主要起因是策划对最快进入登录界面有硬性要求(3秒),那么最开始加载的文件越小越好.对H5的游戏程序进行压缩,可以大大缩小应用程序的加载时间. 本文主要有两个主题: 普通的javascript代码解压使用 实际Egret游戏项目的javascript代码解压使用 对实战感兴趣的朋友可以直接看后面的Egret部分,前面只是基础原理的入门版 一.js代码压缩效果 先看看压缩和没压缩之间的对比 可以

21点游戏

// 21点游戏 分为人机对战和人人对战 // 玩家每次抽一张牌 牌的点数为1-10点随机数 谁更接近21点谁就获胜 let readline = require("readline-sync"); // 清屏函数 let clear = () => process.stdout.write(process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H'); // 抽牌函数 接收一个参数 玩家对象 let takeC