前端!使用javascript實現(xiàn)超級瑪麗小游戲

原生JS實現(xiàn)超級瑪麗經典小游戲

前言

小時候沒有玩過《超級瑪麗》這個游戲,我真不敢想像你的童年是怎么過的。曾經因為它,不知道挨了老媽多少頓打,有時連飯都顧不上吃。記得上初中那年,老媽終于同意給我買了個小霸王,但是因為當時是走讀,一個星期才能回一次家。而當我第二個星期回家的時候,小弟弟用委屈的眼神看著我說:哥,你的學習機不知道為什么接電視機上沒反應了。。接電視機上沒反應了。。。沒反應了!當時我那個恨??!

通過按鍵,來控制圖片的位置

  • 按S鍵開始
  • 方向鍵:移動
  • S鍵:跳躍/進入
  • A鍵:快跑/射擊

運行截圖

項目截圖

源碼

HTML部分

<!doctype html>
<html>
<head>
<meta charset="utf-8">

<link rel=stylesheet type="text/css" href="VNkyVaVxUV.css">
<script type="text/javascript" src="jquery.js"></script>

</head>
<body>
<div class=spacing></div>
<div class=main>
<canvas id=canvas width=640 height=480>
</canvas>
<div class=spacing></div>
</div>
<script src="wNGu2CtEMx.js"></script>
<script src="QAuIByrkL.js"></script>
<script>$(document).ready(function() { new Enjine.Application().Initialize(new Mario.LoadingState(), 320, 240) });</script>
</div>

</body>
</html>

CSS部分

/*
 * - Crafted by Rob Kleffner -
 */
*
{
    margin: 0; 
    padding: 0;
} 
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td
{  
    border: 0 none;  
    font-family: inherit;  
    font-size: 100%;  
    font-style: inherit;  
    font-weight: inherit;   
    vertical-align: baseline;  
}
input
{
    outline: none;
}

body
{
    background: #fff;
    line-height: 14px;
    font-size: 12px;
    font-family: Arial, Verdana, Helvetica, sans-serif;
    margin: 0px;
    cursor: default;
}

html, body
{
    height: 100%;
    text-align: center;
}
a
{
    text-decoration: none;
}

.left
{
    float: left;
}

.right
{
    float: right;
}
.clear
{
    clear: both;
    height: 0;
    visibility: hidden;
    display: block;
}
body
{
    font-family: 'SilkscreenNormal', Arial, sans-serif;
    padding-top: 10px;
    background-color: #fafafa;
}
h1, h2
{
    color: #B2DFDA;
    letter-spacing: 0;
}
h1
{
    font: 60px 'SilkscreenNormal', Arial, sans-serif;
}
h2
{
    font: 30px 'SilkscreenNormal', Arial, sans-serif;
}

h4
{
    font: 20px 'SilkscreenNormal', Arial, sans-serif;
    color: #F44;
}
p
{
    color: #F44;
    font-size: 16px;
    font-family: 'SilkscreenExpandedNormal', Arial, sans-serif;
    line-height: 22px;
    margin-bottom: 20px;
}
a
{
    font-family: 'SilkscreenExpandedBold', Arial, sans-serif;
}
a:hover
{
    color: #F44;
}
canvas
{
    background-color: #fff;
    border: 1px solid #aaa;
    padding: 5px;   
}
.spacing
{
    width: 100%;
    height: 5px;
}
.main
{
    width: 640px;
    margin: auto;
}
.lbUser
{
    background-color: #eee;
    border: 1px solid #666;
    margin-bottom: 5px;
    padding: 10px;
    height: 50px;
}
.lbUser img
{
    padding-right: 20px;
}
.lbUser p
{
    line-height: 50px;
}
.activeUser
{
    background-color: #ccc;
    border: 1px solid #666;
}

總結

雖然游戲很簡單,實現(xiàn)方式也很簡單,但是在處理一些異常操作時還是需要用心處理的。寫這個小游戲的過程中加深了自己對javascript的理解。有需要完整源碼的在評論區(qū)留言即可。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容