Make a super mario game that works on mobile, make it using one file that consists of html,css, and JavaScript all together. The character can be 2 red blocks and should have ability to jump and move, add infinite generating levels
Assistant
Certainly! Below is a simple platformer game that works on mobile devices. The game is contained within a single HTML file that includes HTML, CSS, and JavaScript. The player controls a character represented by two red blocks that can move and jump. The game features infinitely generating platforms for continuous play.
You can copy the code below into a file with an .html extension and open it in a mobile browser to play.
html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Infinite Platformer Game</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #87CEEB; /* Sky blue background */
}
#gameCanvas {
display: block;
background-color: #87CEEB;
}
/* On-screen controls for mobile */
.control-btn {
position: fixed;
bottom: 20px;
width: 60px;
height: 60px;
background-color: rgba(0,0,0,0.5);
border-radius: 50%;
color: white;
font-size: 30px;
text-align: center;
line-height: 60px;
user-select: none;
}
#leftBtn { left: 20px; }
#rightBtn { left: 100px; }
#jumpBtn { right: 20px; }
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<!-- On-screen controls -->
<div id="leftBtn" class="control-btn">◀</div>
<div id="rightBtn" class="control-btn">▶</div>
<div id="jumpBtn" class="control-btn">▲</div>
<script>
// Canvas setup
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
resizeCanvas();
// Game variables
let player = {
x: 50,
y: canvas.height - 150,
width: 20,
height: 40,
vx: 0,
vy: 0,
speed: 2,
jumping: false,
color: 'red'
};
let keys = {};
let platforms = [];
let platformSpawnTimer = 0;
let gravity = 0.2;
let maxPlatformGap = 200;
// Event listeners for keyboard controls
document.addEventListener('keydown', (e) => { keys[e.keyCode] = true; });
document.addEventListener('keyup', (e) => { keys[e.keyCode] = false; });
// Event listeners for on-screen controls
document.getElementById('leftBtn').addEventListener('touchstart', () => { keys['left'] = true; });
document.getElementById('leftBtn').addEventListener('touchend', () => { keys['left'] = false; });
document.getElementById('rightBtn').addEventListener('touchstart', () => { keys['right'] = true; });
document.getElementById('rightBtn').addEventListener('touchend', () => { keys['right'] = false; });
document.getElementById('jumpBtn').addEventListener('touchstart', () => { keys['up'] = true; });
document.getElementById('jumpBtn').addEventListener('touchend', () => { keys['up'] = false; });
// Resize canvas to fit the window
window.addEventListener('resize', resizeCanvas);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
// Platform class
class Platform {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = 'green';
}
update() {
this.x -= player.speed; // Platforms move left to simulate player movement
}
draw() {
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
// Initialize platforms
function initPlatforms() {
platforms = [];
// Create initial ground platform
platforms.push(new Platform(0, canvas.height - 100, canvas.width, 100));
}
initPlatforms();
// Game loop
function gameLoop() {
requestAnimationFrame(gameLoop);
update();
render();
}
function update() {
// Player movement
if (keys[37] || keys['left']) { // Left arrow key
player.vx = -player.speed;
} else if (keys[39] || keys['right']) { // Right arrow key
player.vx = player.speed;
} else {
player.vx = 0;
}
if ((keys[38] || keys['up'] || keys[32]) && !player.jumping) { // Up arrow or spacebar
player.vy = -5;
player.jumping = true;
}
player.vy += gravity;
player.x += player.vx;
player.y += player.vy;
// Collision detection with platforms
platforms.forEach(platform => {
if (player.x < platform.x + platform.width &&
player.x + player.width > platform.x &&
player.y < platform.y + platform.height &&
player.y + player.height > platform.y) {
// Collision detected, reset position
player.y = platform.y - player.height;
player.vy = 0;
player.jumping = false;
}
});
// Remove platforms that are off-screen
platforms = platforms.filter(platform => platform.x + platform.width > 0);
// Spawn new platforms
platformSpawnTimer--;
if (platformSpawnTimer <= 0) {
let gap = Math.random() * (maxPlatformGap - 100) + 100;
let platformWidth = Math.random() * 100 + 50;
platforms.push(new Platform(canvas.width, canvas.height - 100, platformWidth, 20));
platformSpawnTimer = gap;
}
// Update platforms
platforms.forEach(platform => platform.update());
// Prevent player from falling off the screen
if (player.y > canvas.height) {
// Reset player position
player.x = 50;
player.y = canvas.height - 150;
player.vx = 0;
player.vy = 0;
player.jumping = false;
initPlatforms();
}
}
function render() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw player
ctx.fillStyle = player.color;
// Draw two red blocks as the character
ctx.fillRect(player.x, player.y, player.width, player.height / 2);
ctx.fillRect(player.x, player.y + player.height / 2, player.width, player.height / 2);
// Draw platforms
platforms.forEach(platform => platform.draw());
}
// Start the game loop
gameLoop();
</script>
</body>
</html>
Instructions:
Copy the Code: Copy the entire code above into a text editor.
Save the File: Save the file with an .html extension, for example, infinite_platformer.html.
Open in Browser: Open the file using a mobile browser or desktop browser.
Controls:
Mobile: Use the on-screen buttons to move left, right, and jump.
Desktop: Use the arrow keys for movement (Left, Right, Up to jump).
Features:
Character: The player character is represented by two red blocks stacked vertically.
Movement: The character can move left or right and jump.
Infinite Platforms: Platforms are generated infinitely, scrolling from right to left.
Gravity and Collision: The game includes basic gravity and collision detection with platforms.
Responsive Design: The game canvas resizes to fit the window, making it suitable for various screen sizes.
Feel free to modify and enhance the game by adding new features like scoring, obstacles, or different platform types!