can you write me javascript skoban game, using moji font as graphisc
- User
can you write me javascript skoban game, using moji font as graphisc
- Assistant
can you write me javascript skoban game, using moji font as graphisc
Certainly! Below is a simple Sokoban game implemented in JavaScript using emojis for graphics. You can run this code by copying it into an HTML file and opening it in a web browser.
<!DOCTYPE html>
<html>
<head>
<title>Sokoban Emoji Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
#game {
display: inline-block;
font-size: 24px;
line-height: 24px;
margin-top: 20px;
}
.row {
height: 24px;
}
button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
}
#moves {
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Sokoban Game with Emojis</h1>
<div id="game"></div>
<div id="moves">Moves: 0</div>
<button onclick="resetGame()">Reset Game</button>
<script>
const WALL = '⬛️';
const PLAYER = '😀';
const BOX = '📦';
const GOAL = '❌';
const BOX_ON_GOAL = '✅';
const FLOOR = '⬜️';
let level = [
['⬛️', '⬛️', '⬛️', '⬛️', '⬛️', '⬛️'],
['⬛️', '⬜️', '⬜️', '⬜️', '⬜️', '⬛️'],
['⬛️', '⬜️', '📦', '⬜️', '⬜️', '⬛️'],
['⬛️', '⬜️', '⬜️', '📦', '⬜️', '⬛️'],
['⬛️', '❌', '⬜️', '⬜️', '😀', '⬛️'],
['⬛️', '⬛️', '⬛️', '⬛️', '⬛️', '⬛️'],
];
let playerPosition = { x: 4, y: 4 };
let moves = 0;
function drawGame() {
let gameDiv = document.getElementById('game');
gameDiv.innerHTML = '';
for (let y = 0; y < level.length; y++) {
let rowDiv = document.createElement('div');
rowDiv.className = 'row';
for (let x = 0; x < level[y].length; x++) {
let cell = level[y][x];
rowDiv.innerHTML += cell;
}
gameDiv.appendChild(rowDiv);
}
document.getElementById('moves').textContent = 'Moves: ' + moves;
}
function resetGame() {
level = [
['⬛️', '⬛️', '⬛️', '⬛️', '⬛️', '⬛️'],
['⬛️', '⬜️', '⬜️', '⬜️', '⬜️', '⬛️'],
['⬛️', '⬜️', '📦', '⬜️', '⬜️', '⬛️'],
['⬛️', '⬜️', '⬜️', '📦', '⬜️', '⬛️'],
['⬛️', '❌', '⬜️', '⬜️', '😀', '⬛️'],
['⬛️', '⬛️', '⬛️', '⬛️', '⬛️', '⬛️'],
];
playerPosition = { x: 4, y: 4 };
moves = 0;
drawGame();
}
function isWin() {
for (let y = 0; y < level.length; y++) {
for (let x = 0; x < level[y].length; x++) {
if (level[y][x] === '📦' && (y !== 4 || x !== 1)) {
return false;
}
}
}
return true;
}
function move(dx, dy) {
let newX = playerPosition.x + dx;
let newY = playerPosition.y + dy;
let nextCell = level[newY][newX];
if (nextCell === WALL) return;
if (nextCell === FLOOR || nextCell === GOAL) {
// Move player
level[playerPosition.y][playerPosition.x] = FLOOR;
playerPosition.x = newX;
playerPosition.y = newY;
level[newY][newX] = PLAYER;
moves++;
} else if (nextCell === BOX || nextCell === BOX_ON_GOAL) {
// Try to push the box
let boxNewX = newX + dx;
let boxNewY = newY + dy;
let nextNextCell = level[boxNewY][boxNewX];
if (nextNextCell === FLOOR || nextNextCell === GOAL) {
// Move box
level[boxNewY][boxNewX] = nextNextCell === GOAL ? BOX_ON_GOAL : BOX;
level[newY][newX] = PLAYER;
level[playerPosition.y][playerPosition.x] = FLOOR;
playerPosition.x = newX;
playerPosition.y = newY;
moves++;
}
}
drawGame();
if (isWin()) {
alert('Congratulations! You solved the puzzle in ' + moves + ' moves.');
resetGame();
}
}
document.addEventListener('keydown', function(event) {
if (event.key === 'ArrowUp') {
move(0, -1);
} else if (event.key === 'ArrowDown') {
move(0, 1);
} else if (event.key === 'ArrowLeft') {
move(-1, 0);
} else if (event.key === 'ArrowRight') {
move(1, 0);
}
});
drawGame();
</script>
</body>
</html>
Copy the Code: Copy the entire code above and paste it into a new file with the .html extension, for example, sokoban.html.
Open in Browser: Open the HTML file with a modern web browser like Chrome, Firefox, or Edge.
Game Controls:
😀).📦) onto the goal spots (❌).✅.Reset Game: Click the "Reset Game" button to restart the game at any time.
Moves Count: Keep an eye on the "Moves" counter to see how many moves you've made.
⬛️): Cannot be moved or passed through.⬜️): Empty space where the player can move.😀): The character you control.📦): Can be pushed by the player.❌): The spot where boxes need to be moved.✅): Indicates a box is correctly placed on a goal.level) that holds the game map.drawGame function renders the current state of the game on the web page.move function handles player movement and box pushing logic.isWin function checks if all boxes are on goal spots.If you'd like to customize the game:
level array to create new puzzles.font-size to make the game larger or smaller.Note: This is a simple implementation intended for educational purposes. For a more advanced game, consider adding multiple levels, sound effects, and a better graphics system.
Feel free to ask if you have any questions or need further assistance.
Not yet included in a published release.