canvas实现超级玛丽的游戏代码

代码语言:html

所属分类:游戏

代码描述:canvas实现超级玛丽的游戏代码

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

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

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>超级玛丽游戏</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background: #5c94fc;
            font-family: 'Arial', sans-serif;
        }

        #gameContainer {
            position: relative;
        }

        #gameCanvas {
            border: 4px solid #000;
            display: block;
            background: #5c94fc;
        }

        #gameInfo {
            position: absolute;
            top: 10px;
            left: 10px;
            color: white;
            font-size: 20px;
            font-weight: bold;
            text-shadow: 2px 2px 0 #000;
            z-index: 10;
        }

        #gameOver {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(0, 0, 0, 0.9);
            color: white;
            padding: 40px;
            border-radius: 15px;
            text-align: center;
            display: none;
            z-index: 20;
        }

        #gameOver h2 {
            font-size: 48px;
            margin-bottom: 20px;
            color: #FFD700;
        }

        #gameOver p {
            font-size: 24px;
            margin: 10px 0;
        }

        #gameOver button {
            font-size: 20px;
            padding: 15px 40px;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            margin-top: 20px;
            transition: all 0.3s;
        }

        #gameOver button:hover {
            background: #45a049;
            transform: scale(1.1);
        }

        #instructions {
            position: absolute;
            bottom: 10px;
            left: 10px;
            color: white;
            font-size: 14px;
            text-shadow: 1px 1px 0 #000;
        }
    </style>
</head>
<body>
    <div id="gameContainer">
        <div id="gameInfo">
            <div>🏆 分数: <span id="score">0</span></div>
            <div>❤️ 生命: <span id="lives">3</span></div>
        </div>
        <div id="instructions">
            ⬅️➡️ 移动 | ⬆️/空格 跳跃 | 踩敌人消灭 | 收集金币
        </div>
        <canvas id="gameCanvas" width="800" height="600"></canvas>
        <div id="gameOver">
            <h2>游戏结束!</h2>
            <p>最终分数: <span id="finalScore">0</span></p>
            <button onclick="restartGame()">🎮 重新开始</button>
        </div>
    </div>

    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');

        // 游戏状态
        let gameRunning = true;
        let score = 0;
        let lives = 3;.........完整代码请登录后点击上方下载按钮下载查看

网友评论0