Javascript: 迷宫与打地鼠

本文分享了使用JavaScript实现的两个小游戏——迷宫和打地鼠。迷宫游戏要求玩家从起点S到达终点E而不碰墙,而打地鼠游戏则要求玩家点击随机出现的地鼠得分。每个游戏都有详细的UI、功能需求和相应的HTML、CSS及JavaScript代码展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

github地址:小威威的github

1.迷宫

项目要求:

需求规格(50分)
UI(10分):UI与上图完全一致;或者,自由发挥,设计出更加漂亮、合理的UI。
正常赢(10分):移动鼠标,从S开始,到E结束,中间不碰墙,赢得游戏,显示“You Win”
碰墙输(10分):从S开始后,到E结束之前,碰墙,墙变红,输,显示“You Lose”
重置结果(10分):离开迷宫,墙恢复正常;从S开始时,隐藏结果显示
发现作弊(10分):如果用户未经过S,就指到E,又或者指向S之后,从迷宫外绕路指向E,显示”Don’t cheat, you should start form the ‘S’ and move to the ‘E’ inside the maze!”

效果图如下:
Maze

HTML代码如下:

<!DOCTYPE html>
<html>
    <head>
        <title>The Amazing Mouse Maze</title>
        <link rel="stylesheet" type="text/css" href="CSS/maze.css">
        <script type="text/javascript" src="Js/maze.js"></script>
    </head>
    <body>
        <h1>The Amazing Mouse Maze</h1>
        <p>Move your mouse over the "S" to begin.</p>
        <p id="result"><br></p>
        <div id="maze">
            <div id="wall_1" class="wall"></div>
            <div id="wall_2" class="wall"></div>
            <div id="wall_3" class="wall"></div>
            <div id="wall_4" class="wall"></div>
            <div id="wall_5" class="wall"></div>
            <div id="path_1" class="path">
                <div id="start">
                    <p>S</p>
                </div>
            </div>
            <div id="path_2" class="path"></div>
            <div id="path_3" class="path"></div>
            <div id="path_4" class="path"></div>
            <div id="path_5" class="path">
                <div id="end">
                    <p>E</p>
                </div>
                <!--用于测试作弊的结构-->
                <div id="test"></div>
            </div>
        </div>
        <p>The object of this game is to guide the mouse cursor through the start area<br>
           and get to the end area. Be sure to avoid the walls:
        </p>
        <div id="wall_example"></div>
        <br>
        <p>Good luck!</p>
    </body>
</html>

CSS代码如下:

html, body {
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    text-align: center;
    font-family: "Andale Mono", Arial, sans-serif;
}

.wall, .path {
    display: inline-block;
    position: absolute;
}

body>p:nth-of-type(-n+2) {
    font-size: 22px;
}

.wall, #wall_example {
    background-color: rgb(238, 238, 238);
}

#wall_example, #maze {
    position: relative;
    margin-left: auto;
    margin-right: auto;
}

#wall_example {
    border: solid 1px black;
    width: 100px;
    height: 25px;
}

#maze {
    width: 500px;
    height: 300px;
}

#wall_1, #wall_2, #wall_3, #wall_4, #wall_5 {
    border-left: solid 1px black;
    border-right: solid 1px black;
}

#wall_2, #wall_3, #wall_5, #path_1, #path_5 {
    border-bottom: solid 1px black;
}

#wall_1, #wall_4, #path_3 {
    border-top: solid 1px black;
}

#wall_1, #wall_2, #wall_5, #path_1 {
    left: 0px;
}

#wall_1, #wall_5 {
    width: 100%;
}

#wall_2, #wall_3, #path_3 {
    top: 52px;
}

#wall_1 {
    top:0px;
    height: 52px;
}

#wall_2 {
    width: 150px;
    height: 150px;
}

#wall_3 {
    width: 153px;
    height: 150px;
    right: -2px;
}

#wall_4 {
    width: 98px;
    height: 150px;
    left: 200px;
    top: 103px;
}

#wall_5 {
    height: 46px;
    bottom: 0px;
}

#path_1 {
    width: 200px;
    height: 50px;
    top:203px;
}

#path_2 {
    width: 50px;
    height: 100px;
    top: 103px;
    left: 150px;
}

#path_3 {
    width: 196px;
    height: 51px;
    left: 150px;
}

#path_4 {
    width: 50px;
    height: 100px;
    top: 103px;
    right: 150px;
}

#path_5 {
    width: 200px;
    height: 50px;
    top: 203px;
    right: 0px;
}

.path, .wall, #start, #end {
    cursor: pointer;
}

#start, #end {
    position: absolute;
    border: solid 1px black;
    width: 43px;
    height: 40px;
    font-weight: bold;
    font-size: 35px;
    top: 4px;
}

#start {
    left: 0px;
    background-color: rgb(132, 255, 124);
    opacity: 0.7;
}

#start:hover {
    opacity: 1;
}

#end {
    right: -2px;
    background-color: rgb(134, 131, 254);
    opacity: 0.7;
}

#end:hover {
    opacity: 1;
}

#start p, #end p {
    margin-top: 0px;
}

.error {
    background-color: red;
}

#test {
    position: absolute;;
    width: 20px;
    height: 50px;
    bottom: 0px;
    right: -20px;
}

JavaScript代码如下:

/*
项目名称:The Amazing Mouse Maze
时间:Oct 14
*/

var S_flag = 0; // 标识是否经过起点
var Path_flag = [0, 0, 0, 0, 0];  // 标识是否经过各路段避免作弊
var E_flag = 0; // 标识是否经过终点
var Lose_flag = 0; // 标识是否碰到墙壁
var test_cheat = 0; // 标识是否经过测试结构

function addListener() { // 添加事件处理器
    var _wall = document.getElementsByClassName("wall");
    var _path = document.getElementsByClassName("path");
    for (var i = 0; i < 5; i++) {
        _wall[i].addEventListener('mouseover', error_func);  // 鼠标碰到墙触发的事件
        _wall[i].addEventListener('mouseout', my_reset);  // 鼠标离开墙触发的事件
        _path[i].addEventListener('mouseout', record); // 鼠标经过路径触发的事件
    }
    var _start = document.getElementById("start");
    var _end = document.getElementById("end");
    _start.addEventListener('mouseover', start_); // 鼠标经过起点触发的事件
    _end.addEventListener('mouseover', end_); // 鼠标经过终点触发的事件
    var _test = document.getElementById("test");
    _test.addEventListener('mouseover', test_); // 鼠标经过测试结构触发的事件
}


function test_(event) { // 如果经过测试结构,赋值为1
    if (S_flag == 1) {
        test_cheat = 1;
    }
}

function error_func(event) { // 如果鼠标触碰到墙壁
    if (S_flag == 1 && E_flag == 0) { // 在已经开始且未结束的情况下变色
        if (Lose_flag != 1) { // 同时还得在没有触碰到其他墙壁的情况下变色
            event.target.className += " error";  // 给出发事件的元素增加一个类,应用对应的css
        }
        document.getElementById("result").textContent = "You Lose!" // 修改输出的值
        Lose_flag = 1; // 失败的标识赋值为1,方便后续检测
        S_flag = 0; // 将开头标识赋值为0,方便初始化
    }
}

function my_reset(event) { // 鼠标离开墙壁后的重置
    event.target.className = "wall"; // 恢复原来的class值
}

function record(event) { // 鼠标经过跑道时作记号以便检测是否作弊
    if (event.target.id == "path_1") {
        Path_flag[0] = 1;
    } else if (event.target.id == "path_2") {
        Path_flag[1] = 1;
    } else if (event.target.id == "path_3") {
        Path_flag[2] = 1;
    } else if (event.target.id == "path_4") {
        Path_flag[3] = 1;
    } else if (event.target.id == "path_5") {
        Path_flag[4] = 1;
    }
}

function start_(event) { // 鼠标经过起点
    if (S_flag == 0) { // 如果起点标识为0,进行相关变量初始化
        document.getElementById("result").textContent = "Have A Try!";
        E_flag = 0;
        Lose_flag = 0;
        test_cheat = 0;
        for (var i = 0; i < 5; i++) {
            Path_flag[i] = 0;
        }
    }
    S_flag = 1; // 经过起点,起点标识赋值为1
}

function JudgeCheat() { // 检测是否经过整条path
    if (Path_flag[0] == 1 && Path_flag[1] == 1 && Path_flag[2] == 1
        && Path_flag[3] == 1 && Path_flag[4] == 1) {
        return true;
    } else {
        return false;
    }
}

function end_(event) { // 鼠标经过终点
    E_flag = 1; // 结束的标识赋值为1
    if (Lose_flag != 1) { // 在不失败的情况下
        if (JudgeCheat()) { // 通过检测作弊的第一关
            if (test_cheat == 0) {  // 通过检测作弊的第二关
                document.getElementById("result").textContent = "You Win!";
            } else { // 没有通过检测,即为作弊
                document.getElementById("result").textContent
                = "Don't Cheat, you should start from the 'S' and move to the 'E' inside the maze!";        
            }
        } else { // 没有通过检测,即为作弊
            document.getElementById("result").textContent
            = "Don't Cheat, you should start from the 'S' and move to the 'E' inside the maze!";
        }
        S_flag = 0;
    }
}

window.onload = function() { // 页面加载完毕后执行添加事件处理器函数。
    addListener();
}

2.打地鼠

项目要求:

需求规格(30分)
UI(10分):UI与上图完全一致;或者,自由发挥,设计出更加漂亮、合理的UI。
打地鼠(10分):能够随机出现地鼠,鼠标能够击中(点击正确,地鼠消失,出现新地鼠;点击错误,地鼠不消失)
正确计分(10分):正确计算分数并显示,包括正确结束游戏

效果图如下:
打地鼠

HTML代码如下:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Whac-a-mole</title>
        <link rel="stylesheet" type="text/css" href="CSS/mole.css">
        <script type="text/javascript" src="Js/mole.js"></script>
    </head>
    <body>
        <h1>Whac-a-mole</h1>
        <p>Test your skill.how many boxes(moles) can you check(hit) in 30 seconds?</p>
        <p>
            <button id="start_stop"> Start Game | Stop Game </button>  &nbsp &nbsp &nbsp
            Time: <input type="text" name="time" disabled="disabled" id="time">
        </p>
        <p>
            <input type="text" name="result" disabled="disable" id="result"> &nbsp &nbsp &nbsp
            Score: <input type="text" name="socre" disabled="disable" id="score">
        </p>
        <br>
        <div id="container"></div>
        <div id="instruction">
        <p>Instructions:</p>
            <ol>
                <li>Click on the radio buttons(moles) as they are selected randomly by the computer.</li>
                <li>1 point per hit, minus 1 point per miss.</li>
            </ol>
        </div>
    </body>
</html>

CSS代码如下:

html, body {
    width: 100%;
    height: 100%;
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    text-align: center;
    font-family: Courier, "Andale Mono", Arial, sans-serif;
}

#container {
    border-top: solid 2px rgb(183,179,179);
    border-bottom: solid 2px rgb(183,179,179);
    width: 300px;
    height: 150px;
    margin-left: auto;
    margin-right: auto;
    padding-top: 10px;
}

#instruction {
    width:600px;
    margin-left: auto;
    margin-right: auto;
    text-align: left;
}

#instruction p {
    font-weight: bold;
}

p:first-of-type {
    font-size: 20px;
}

#time, #score {
    width: 30px;
}

.hole {
    margin: 5px 8px 5px 8px;
}

JavaScript代码如下:

/* 

    类型:Javascripe
    项目名称:mole

*/

var count = 0;  // 分数
var status = 0; 

window.onload = function() {
    Create_button();
    document.getElementById("start_stop").onclick = _control;
    button = document.getElementsByClassName("hole");
}

/* 计时器 */
_clock = 31;
function clock() {
    _clock = _clock-1;
    document.getElementById("time").value = _clock;
    time_value = setTimeout(clock, 1000);
    if (_clock == 0) {
        clearInterval(time_value);
        document.getElementById("result").value = "Gameover";
        alert("Game Over.\n Your score is: " + document.getElementById("score").value);
        status = 0;
        button[current].checked = false;
    }
}

/* Start Game | Stop Game 按钮的处理器 */
function _control() {
    if (status == 0) {  // 初始化
        status = 1;
        count = 0;
        _clock = 31;
        Random_occur();
        document.getElementById("score").value = count;
        document.getElementById("time").value = _clock;
        document.getElementById("result").value = "Playing";
        clock();
    } else {
        alert("Game Over.\n Your score is: " + document.getElementById("score").value);
        document.getElementById("result").value = "Gameover";
        clearInterval(time_value);
        button[current].checked = false;
        _clock = 0;
        status = 0;
    }
}

/* 产生随机数的函数 */
function Random_occur() {
    if (_clock != 0) { // 避免游戏结束后还能继续操作
        current = Math.round(Math.random()*60-1);  // 使范围处于0~59,减一是为了避免出现60的情况
        button[current].checked = true;
    }
}

/* 按钮处理器 */
function button_react(event) {
    if (_clock != 0) {
        if (before == true) { // before 是点击前的状态
            count++;
            this.checked = false;  // 取消点击状态
            Random_occur();
        } else {  // 非目标按钮
            count--;
            this.checked = false;
            button[current].checked = true; // 由于单选框的特性,点击其他按钮时原来的按钮也会消失,所以要给原来的按钮重新checked
        }
        document.getElementById("score").value = count;
    } else { // 避免接触后继续对按钮操作
        this.checked = false;
    }
}

/* mousedown 事件处理器 */
function before_button_react(event) {
    before = this.checked;
}

/* 产生按钮函数 */
function Create_button() {
    var _container = document.getElementById("container");
    for (var i = 0; i < 60; i++) {
        var new_button = document.createElement("input");
        new_button.setAttribute("type", "radio");
        new_button.setAttribute("name", "mouse");
        new_button.className = "hole";
        new_button.addEventListener('click', button_react);
        new_button.addEventListener('mousedown', before_button_react);
        _container.appendChild(new_button);
    }
}

以上内容本人观点,欢迎大家提出批评和指导,我们一起探讨!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值