canvas实现简单超级玛丽游戏代码

代码语言:html

所属分类:游戏

代码描述:canvas实现简单超级玛丽游戏代码。gemini3生成

代码标签: canvas 简单 超级 玛丽 游戏 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>HTML5 超级玛丽 clone</title>
    <style>
        body {
            margin: 0;
            background-color: #202020;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: 'Courier New', Courier, monospace;
            overflow: hidden;
        }
        #game-container {
            position: relative;
            box-shadow: 0 0 20px rgba(0,0,0,0.5);
            background-color: #5c94fc; /* 经典的天空蓝 */
        }
        canvas {
            display: block;
        }
        #ui-layer {
            position: absolute;
            top: 10px;
            left: 10px;
            color: white;
            font-weight: bold;
            font-size: 18px;
            text-shadow: 2px 2px 0 #000;
            width: 100%;
        }
        .controls-hint {
            position: absolute;
            bottom: 10px;
            width: 100%;
            text-align: center;
            color: white;
            font-size: 14px;
            text-shadow: 1px 1px 0 #000;
            pointer-events: none;
        }
        #overlay {
            position: absolute;
            top: 0; left: 0; width: 100%; height: 100%;
            background: rgba(0,0,0,0.8);
            display: none;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            color: white;
            z-index: 10;
        }
        button {
            padding: 10px 20px;
            font-size: 18px;
            cursor: pointer;
            background: #e52521;
            color: white;
            border: 2px solid white;
            margin-top: 20px;
            font-family: inherit;
        }
        button:hover { background: #ff4d4d; }
    </style>
</head>
<body>

<div id="game-container">
    <div id="ui-layer">
        <div>MARIO &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; COINS &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WORLD</div>
        <div><span id="score">000000</span> &nbsp;&nbsp;&nbsp;&nbsp; x <span id="coins">00</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1-1</div>
    </div>
    
    <canvas id="gameCanvas" width="760" height="480"></canvas>

    <div id="overlay">
        <h1 id="msg-title">GAME OVER</h1>
        <button onclick="resetGame()">TRY AGAIN</button>
    </div>
    
    <div class="controls-hint">← → 移动 | 空格/↑ 跳跃 (长按跳得高)</div>
</div>

<script>
/**
 * 游戏配置与常量
 */
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');

const TILE_SIZE = 32; // 格子大小
const GRAVITY = 0.5;
const FRICTION = 0.8;
const MAX_SPEED = 6;
const JUMP_FORCE = 11;

// 关卡地图设计 (简单的字符串数组表示)
// . = 空气, # = 地面/硬砖, B = 砖块(可碎), Q = 问号箱, P = 水管身, T = 水管口, G = 板栗仔, F = 旗杆顶, f = 旗杆杆, C = 金币
const levelDesign = [
    "..................................................................................................................................................................",
    ".................................................................完整代码请登录后点击上方下载按钮下载查看

网友评论0