canvas实现超级玛丽游戏代码
代码语言:html
所属分类:游戏
代码描述:canvas实现超级玛丽游戏代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="utf-8"/><meta content="width=device-width,initial-scale=1,user-scalable=no" name="viewport"/>
<title>小狐狸与火焰橡果</title>
<style>html,body{margin:0;height:100%;background:#0b0f14;overflow:hidden}canvas{display:block;position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);width:min(100vw,calc(100vh*16/9));height:min(100vh,calc(100vw*9/16));touch-action:none}</style>
</head><body><canvas height="720" id="game" width="1280"></canvas>
<script>
const canvas = document.getElementById('game'), ctx = canvas.getContext('2d');
const W = 1280, H = 720, TILE = 48, ROWS = 15, DT = 1 / 60, LEVEL_TIME = 300;
const HERO_W = 30, HERO_SMALL = 42, HERO_BIG = 84;
const PHYS = { walk: 260, run: 430, acc: 1500, airAcc: 1100, fric: 1700, skid: 3000,
jump: 920, grav: 2300, cut: 2.6, maxFall: 960, coyote: 0.1, buffer: 0.12 };
const T = { EMPTY: 0, GROUND: 1, BRICK: 2, Q: 3, USED: 4, STONE: 5, STUMP: 6, HIDDEN: 7 };
const clamp = (v, a, b) => v < a ? a : v > b ? b : v;
const lerp = (a, b, t) => a + (b - a) * t;
const rnd = (a, b) => a + Math.random() * (b - a);
const sign = v => v > 0 ? 1 : v < 0 ? -1 : 0;
function sfx(name) { if (typeof playSfx === 'function') playSfx(name); } // 音效 — 第 4 部分
// ---------- 2. 关卡地图 ----------
// . 空地 # 泥土 X 石块 B 砖块 ? 金币块 M 道具块(橡果/火焰花) T 十连金币块 S 星星块
// L 隐藏生命块 P 树桩 o 金币 b 甲虫 s 蜗牛 h 刺猬 f 食人花(在树桩上方)
// - 横向平台 ↔ ^ 纵向平台 ↕ C 存档点 F 旗杆 D 洞穴入口 E 洞穴出口
// I 洞内出生点 O 洞内出口树桩。各区域行按底部对齐(共 15 行)。
const ZONE_A = [ // 阳光草地
'......................?................................',
'.......................................................',
'.......................................................',
'.......................................................',
'............?.......BMB?B.......................L......',
'.......ooo..........................PP......PP.........',
'............................PP......PP......PP.........',
'..................b.........PP...b..PP..b.b.PP.........',
'###################################################..##',
'###################################################..##'];
const ZONE_B = [ // 树桩森林
'........................BBSBB..........................',
'.......................................................',
'.......................................................',
'..................................o....................',
'....BTB..............D...?M?.....o.o..........E........',
'.............f.......PP...............f.......PP.......',
'.............PP......PP...............PP......PP.......',
'.........s...PP..b.b.PP.......h.......PP..s.b.PP..C....',
'#################################...###################',
'#################################...###################'];
const ZONE_C = [ // 平台悬崖
'...........ooo...............................ooo.......',
'.......X.............?M?............B?BB...............',
'......XX.-.............................................',
'.....XXX...................................-...........',
'....XXXX............h....s...^......b.b................',
'########.........###########......########.........####',
'########.........###########......########.........####'];
const ZONE_D = [ // 黄昏山丘(终点)
'....................?....................XX............',
'........................................XXX............',
'.......................................XXXX............',
'......................................XXXXX............',
'...?B?.........f...BMB...............XXXXXX............',
'...............PP...................XXXXXXX............',
'...............PP..................XXXXXXXX............',
'......s........PP.h.........b.b.s.XXXXXXXXX....F.......',
'##########...#########....#############################',
'##########...#########....#############################'];
const CAVE = [ // 奖励洞穴
'############################',
'#.I........................#',
'#..........................#',
'#..........................#',
'#..........................#',
'#..........................#',
'#..........................#',
'#..........................#',
'#.......oooooooooo.........#',
'#.......oooooooooo.....O...#',
'#......BBBBBBBBBBBB....PP..#',
'#......................PP..#',
'#.......oooooooooo.....PP..#',
'############################',
'############################'];
const ZONES = [ZONE_A, ZONE_B, ZONE_C, ZONE_D];
const ZONE_W = 55, MAIN_W = ZONE_W * 4, CAVE_X = MAIN_W + 4, CAVE_W = 28, LW = CAVE_X + CAVE_W;
// ---------- 3. 世界:数组与游戏状态 ----------
const tiles = new Uint8Array(LW * ROWS);
const blocks = new Map(); // 方块内容:键 tx+ty*LW -> {item:'coin'|'power'|'multi'|'star'|'life', n}
const enemies = [], items = [], fireballs = [], particles = [], platforms = [], floatTexts = [];
let checkpoint = { x: 0, y: 0, active: false };
let flag = { x: 0, topY: 0, baseY: 0, houseX: 0 };
let warp = { enter: null, exit: null, caveIn: null, caveOut: null };
const area = { name: 'main', x0: 0, x1: MAIN_W * TILE };
const cam = { x: 0, y: 0, look: 0 };
const game = { mode: 'menu', modeT: 0, frame: 0, score: 0, coins: 0, lives: 3, time: LEVEL_TIME,
checkpoint: false, muted: false, demo: false, hiscore: 0, shake: 0, fade: 1, timeBonus: 0, flagBonus: 0 };
try { game.hiscore = +localStorage.getItem('foxAcornHi') || 0; } catch (e) { /* 无本地存储 */ }
function saveHiscore() {
if (game.score <= game.hiscore) return false;
game.hiscore = game.score;
try { localStorage.setItem('foxAcornHi', String(game.hiscore)); } catch (e) { /* 无本地存储 */ }
return true;
}
const hero = { power: 0 }; // power:0 小形态,1 大形态,2 火焰形态
// ---------- 4. 图块与地图解析 ----------
const bkey = (tx, ty) => tx + ty * LW;
function tileAt(tx, ty) {
if (ty < 0 || ty >= ROWS) return T.EMPTY;
if (tx < 0 || tx >= LW) return T.STONE;
return tiles[ty * LW + tx];
}
function setTile(tx, ty, t) { if (tx >= 0 && tx < LW && ty >= 0 && ty < ROWS) tiles[ty * LW + tx] = t; }
function solidAt(tx, ty) { const t = tileAt(tx, ty); return t !== T.EMPTY && t !== T.HIDDEN; }
function zoneOf(x) { return x >= CAVE_X * TILE ? 4 : clamp(Math.floor(x / (ZONE_W * TILE)), 0, 3); }
function makeEnemy(type, cx, by) { // cx — X 中心,by — 底边
const S = { beetle: [40, 36, 60], snail: [42, 38, 45], hedgehog: [44, 36, 50], flower: [40, 60, 0] }[type];
return { type, x: cx - S[0] / 2, y: by - S[1], w: S[0], h: S[1], speed: S[2], vx: 0, vy: 0, dir: -1,
state: type === 'flower' ? 'hide' : 'walk', t: Math.random() * 3, baseY: by, rise: 0,
alive: true, active: false, onGround: false, wall: 0, head: null };
}
function makePlatform(tx, ty, axis) {
return { x: tx * TILE, y: ty * TILE + 6, w: TILE * 3, h: 20, x0: tx * TILE, y0: ty * TILE + 6, axis,
range: axis === 'h' ? TILE * 5 : TILE * 4, t: 0, dx: 0, dy: 0 }; // 'h':向右移动 range,'v':向上移动 range
}
function parseCell(ch, tx, ty) {
const px = tx * TILE, py = ty * TILE, k = bkey(tx, ty);
switch (ch) {
case '#': setTile(tx, ty, T.GROUND); break;
case 'X': setTile(tx, ty, T.STONE); break;
case 'P': setTile(tx, ty, T.STUMP); break;
case 'B': setTile(tx, ty, T.BRICK); break;
case '?': setTile(tx, ty, T.Q); blocks.set(k, { item: 'coin' }); break;
case 'M': setTile(tx, ty, T.Q); blocks.set(k, { item: 'power' }); break;
case 'T': setTile(tx, ty, T.BRICK); blocks.set(k, { item: 'multi', n: 10 }); break;
case 'S': setTile(tx, ty, T.BRICK); blocks.set(k, { item: 'star' }); break;
case 'L': setTile(tx, ty, T.HIDDEN); blocks.set(k, { item: 'life' }); break;
case 'o': items.push({ type: 'coin', x: px + 12, y: py + 8, w: 24, h: 32, vx: 0, vy: 0, fixed: true, t: tx * 0.3 }); break;
case 'b': enemies.push(makeEnemy('beetle', px + 24, py + TILE)); break;
case 's': enemies.push(makeEnemy('snail', px + 24, py + TILE)); break;
case 'h': enemies.push(makeEnemy('hedgehog', px + 24, py + TILE)); break;
case 'f': enemies.push(makeEnemy('flower', px + TILE, py + TILE)); break; // 藏在下方的树桩里
case .........完整代码请登录后点击上方下载按钮下载查看















网友评论0