• HTML实现猜数字游戏


    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>
            .wrap{ 
                width: 375px;
                height: 680px;
                margin: 20px auto;
                background-color: #bfa;
                text-align: center;
                padding-top: 20px;
                position: relative;
                
            }
            #submit{
                width: 120px;
                height: 60px;
                background-color: pink;
                margin: 20px auto;
                line-height: 60px;
                font-weight: bold;
                font-size: 22px;
                cursor: pointer;
                
            }
            #text{
                width: 300px;
                height: 375px;
                background-color: rgb(109, 121, 211);
                margin: 0 auto;
                padding-top: 2px;
            }
            #text p{
                line-height: 16px;
            }
            .win{
                color: #ff0;
            }
            #reset{
                width: 65px;
                height: 35px;
                text-align: center;
                line-height: 35px;
                font-size: 20px;
                background-color: #f00;
                position: absolute;
                cursor: pointer;
                top: 0;
                right: 0;
            }
            .qm{
                font-size: 10px;
                font-weight: lighter;
                color: pink;
            }
            .err{
                color: #f00;
            }
        style>
    head>
    <body>
        <div class="wrap">
            <h1>猜数字游戏h1>
            <p class="qm">高难度动脑游戏——前端专用(°ー°〃)   create by 小玉p>
            <p>请输入一个0-100的数字:p>
            <input type="text" id="num">
            <div id="submit">确定div>
            <div id="reset">resetdiv>
            <div id="text">div>
            
        div>
        <script>
            let i = 0;
            let value = Math.ceil(Math.random()*100);
            // console.log(value);
            let text = document.getElementById("num");
            let btn = document.getElementById("submit");
            let w = document.getElementById("text");
            const restart = document.getElementById("reset");
            var num;
            text.onblur=function(){
                num = Number(text.value);
            }
            btn.onclick=function(){
                i++;
                if(i<=10){
                    if(num < value){
                    w.innerHTML+=`

    您猜的数字 ${num} 小了哦。请重新输入!

    `
    }else if(num > value){ w.innerHTML+=`

    您猜的数字 ${num} 大了哦。请重新输入!

    `
    }else if(num == value){ w.innerHTML+=`

    你这个老6,仅用了${i}次就猜对了!${num}是正确的!!!

    `
    ; return; }else{ alert("请输入数字!") } }else{ w.innerHTML+=`

    游戏结束。自罚两杯!!!

    `
    return; } } restart.onclick = function(){ history.go(0); text.value = ""; }
    script> body> html>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
  • 相关阅读:
    [Git] 系列三随意修改提交记录以及一些技巧
    使用 SSL 实现 2 路身份验证的最简单方法 - 提供源代码的示例
    重复控制逆变器的仿真分析研究
    生成树欺骗攻击与防御策略
    【k8s】1、基础概念和架构及组件
    四大组件---ContentResolver
    “今天星期五“-SAP SE09/STMS 请求号传输中遇到的错误及解决方案
    chrome盗取用户身份
    Linux操作系统(一)系统初始化
    Java阿里云发送短信
  • 原文地址:https://blog.csdn.net/weixin_51445423/article/details/126122848