如何用js写一个简单的迷宫和打地鼠游戏

最近在学JavaScript,就用学的知识写了两个充满童年回忆的小游戏--迷宫和打地鼠,现在让我们一起来看看怎么实现吧~

1.简单迷宫

这是一个不能再简单的迷宫了,游戏规则:从迷宫中走到终点就赢了;碰墙就输;从地图外走到终点算作弊。S为起点,E为终点。


在这里插入图片描述

完整HTML代码

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <title>The Amazing Mouse Maze</title>
    <meta charset="utf-8">
    <script src="maze.js" type="text/javascript"></script>
    <link rel="stylesheet" type="text/css" href="maze.css">
</head>

<body>
    <h1>The Amazing Mouse Maze</h1>
    <p>
        Move your mouse over the "S" to begin.
    </p>
    <div id="result"></div>
    <div id="wholeMaze">
        <div id="firstRow" class="wall"></div>



        <div id="middle">
            <div id="one" class="wall"></div>
            <div id="two" class="wall"></div>
            <div id="three" class="wall"></div>
            <div id="start">S</div>
            <div id="end">E</div>

        </div>

        <div id="lastRow" class="wall"></div>

    </div>

    <p class="info">The object of this game is to guide the mouse cursor through the start area and get to the end
        area.Be
        sure to avoid the walls.
    </p><br /><br />
    <div id="block"></div>
    <p class="info">Good luck!</p>
</body>

</html>

css代码

h1,p{
    text-align: center;
}

#wholeMaze{
    position: relative;
    top: 12px;
    height: 300px;
    width: 500px;
    margin: auto;
}
#result{
    text-align: center;
    font-size: 20px;
    height: 20px;
}
#firstRow{
    width: 100%;
    height: 50px;
    margin: 0;
}
.wall{
    border: 1px solid black ;    
    float: left;
    background-color: #EEEEEE;
}


#middle{
    margin: 0;
    width: 100%;
    height: 198px;
}
#one{
    position: relative;
    top: -1px;
    width: 148px;
    height: 148px;
    margin-right: 0;
    border-top: 1px solid #EEEEEE ;   
}
#two{
    position: relative;
    left: 50px;
    width: 100px;
    height: 145px;
    top: 41px;
    margin: 0;
    border-bottom: 1px solid #EEEEEE ;   
    z-index:2;/*用于覆盖*/
}
#three{
    position: relative;
    left: 100px;
    top: -1px;
    width: 148px;
    height: 148px;
    border: 0 1px 1px 1px;
    margin: 0;
    border-top: 1px solid #EEEEEE ;   
}
#start{
    line-height: 30px;
    text-align: center;
    position: relative;
    float: left;
    font-weight: bold;
    font-size: 20px;
    height: 30px;
    width: 30px;
    left: -402px;
    top: 152px;
    border: 1px solid black;

    background-color:#83FF82;

}
#end{
    line-height: 30px;
    text-align: center;
    position: relative;
    float: left;
    font-weight: bold;
    font-size: 20px;
    height: 30px;
    width: 30px;
    left: 36px;
    top: 152px;
    background-color: #8784FF;
    border: solid black 1px;
}
#lastRow{
    position: relative;
    top: 37px;
    width: 100%;
    height: 48px;
    margin: 0;
    z-index: 1;
}

.info{
    position: relative;
    top: 20px;
    text-align: left;
    width: 600px;
    margin: auto;
}
#block{
    width: 100px;
    height: 25px;
    margin: auto;
    background-color: #EEEEEE;
    border: 1px solid black;
}

js代码

var isStart = false;
var isInMap = false;
window.onload = function () {
    var wall = document.getElementsByClassName("wall");
    /*start */
    document.getElementById("start").addEventListener("mouseover", function () {
        document.getElementById("result").textContent = "";
        isStart = true;
        isInMap = true;
        for (var i = 0; i < wall.length; i++) { wall[i].style.backgroundColor = "#EEEEEE"; }

    })

    /*out of the map */
    document.getElementById("wholeMaze").addEventListener("mouseleave", function () {
        isInMap = false;
    });
    /*wall */
    for (var i = 0; i < wall.length; i++)
        wall[i].addEventListener("mouseover", function (event) {
            if (isStart) {
                event.target.style.backgroundColor = "#FF0000";
                document.getElementById("result").textContent = "You hit the wall, lost the game!";
                isStart = false;
            }
        });
    for (var i = 0; i < wall.length; i++)
        wall[i].addEventListener("mouseleave", function (event) {
            event.target.style.backgroundColor = "#EEEEEE";
        });
    /*end */
    document.getElementById("end").addEventListener("mouseover", function () {
        if (isStart) {
            if (isInMap) { document.getElementById("result").textContent = "Congratulation! You win the game!"; }
            else {
                document.getElementById("result").textContent = "Oh, my friend, please don't cheat!";
                isStart = false;
            }
        }
        isStart = false;
    });
}

js部分的关键在于给元素添加Listener,相当于附加个触发器,一旦事件发生如“mouseleave”(鼠标离开对应元素区域)就调用对应的函数。另外注意面向对象编程。

2.打地鼠

这个游戏比较复杂,也比较有意思,加上了计时和计分功能,还可以控制游戏的开始和结束并显示游戏状态。游戏结束时弹出结果。
游戏规则:打中地鼠加一分,打错扣一分,不打分不变,玩家可以挑战30秒内能打中多少只地鼠。(拼手速的时刻到了~)

在这里插入图片描述

由于代码有点长,这里小编就不附上了。详见:https://gitee.com/Hometown2020/mole

3.游戏链接

看了代码,接下来就是体验游戏了,这里小编附上自己的游戏链接,有兴趣的朋友可以体验一下~

  1. 迷宫
  2. 打地鼠

PS:如果你对开发小游戏感兴趣又不想打码的话,推荐一个对新手很友好的开发平台construct3,这里小编写了一个简单的植物大战僵尸塔防版,有兴趣的了解一下~
如何在construct3上开发游戏&游戏展示

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,888评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,677评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,386评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,726评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,729评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,337评论 1 310
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,902评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,807评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,349评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,439评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,567评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,242评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,933评论 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,420评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,531评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,995评论 3 377
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,585评论 2 359