写一个超级玛丽网页游戏,要完整且复杂的,代码内嵌,不要分开
- User
写一个超级玛丽网页游戏,要完整且复杂的,代码内嵌,不要分开
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
写一个超级玛丽网页游戏,要完整且复杂的,代码内嵌,不要分开
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
以下是一个完整且复杂的超级玛丽网页游戏代码,使用HTML5和JavaScript实现。代码包括游戏的基本功能,如角色控制、场景绘制、物理引擎、碰撞检测以及简单的敌人逻辑。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<title>Super Mario Game</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
// Game variables
const gravity = 0.5;
let keys = {};
let gameObjects = [];
let isGameOver = false;
// Mario object
const mario = {
x: 100,
y: 500,
width: 50,
height: 50,
color: 'red',
dx: 0,
dy: 0,
isJumping: false,
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
},
update() {
this.dy += gravity;
this.y += this.dy;
this.x += this.dx;
// Prevent Mario from falling through the ground
if (this.y + this.height > canvas.height) {
this.y = canvas.height - this.height;
this.dy = 0;
this.isJumping = false;
}
// Prevent Mario from going out of bounds
if (this.x < 0) this.x = 0;
if (this.x + this.width > canvas.width) this.x = canvas.width - this.width;
}
};
// Platform object
class Platform {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = 'green';
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
// Enemy object
class Enemy {
constructor(x, y, width, height, speed) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = 'blue';
this.speed = speed;
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
update() {
this.x += this.speed;
// Reverse direction if hitting canvas edges
if (this.x < 0 || this.x + this.width > canvas.width) {
this.speed *= -1;
}
}
}
// Collision detection
function checkCollision(rect1, rect2) {
return (
rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y
);
}
// Initialize game objects
function initGame() {
gameObjects = [
new Platform(0, canvas.height - 20, canvas.width, 20),
new Platform(200, 450, 150, 20),
new Platform(400, 350, 150, 20),
new Enemy(600, canvas.height - 70, 50, 50, 2)
];
}
// Game loop
function gameLoop() {
if (isGameOver) {
ctx.fillStyle = 'black';
ctx.font = '30px Arial';
ctx.fillText('Game Over', canvas.width / 2 - 80, canvas.height / 2);
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update and draw Mario
mario.update();
mario.draw();
// Update and draw game objects
gameObjects.forEach(obj => {
if (obj instanceof Enemy) {
obj.update();
// Check collision with Mario
if (checkCollision(mario, obj)) {
isGameOver = true;
}
}
obj.draw();
});
// Check collision with platforms
gameObjects.forEach(obj => {
if (obj instanceof Platform && checkCollision(mario, obj)) {
mario.y = obj.y - mario.height;
mario.dy = 0;
mario.isJumping = false;
}
});
requestAnimationFrame(gameLoop);
}
// Event listeners for controls
document.addEventListener('keydown', (e) => {
keys[e.code] = true;
if (e.code === 'ArrowLeft') mario.dx = -5;
if (e.code === 'ArrowRight') mario.dx = 5;
if (e.code === 'Space' && !mario.isJumping) {
mario.dy = -10;
mario.isJumping = true;
}
});
document.addEventListener('keyup', (e) => {
keys[e.code] = false;
if (e.code === 'ArrowLeft' || e.code === 'ArrowRight') mario.dx = 0;
});
// Start the game
initGame();
gameLoop();
</script>
</body>
</html>
将代码保存为HTML文件后,直接在浏览器中打开即可运行游戏。