Calculator Tools Calculator Tools
Create

Yellow Submarine Maze Game

Sep 26, 2023

About this app

Navigate your yellow submarine through the maze to reach the treasure!

Yellow Submarine Maze Game

Related apps

Put this on your site

Source

                    <html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Yellow Submarine Maze Game</title>
<meta name="description" content="Navigate your yellow submarine through the maze to reach the treasure!">
<meta name="keywords" content="game, maze, submarine, yellow submarine, interactive, fun">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script type="text/javascript">
try {
let maze = [
[1, 1, 1, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1]
];
let playerPosition = {x: 0, y: 0};
let treasurePosition = {x: 6, y: 0};

document.addEventListener("DOMContentLoaded", function() {
drawMaze();
document.addEventListener('keydown', keydownHandler);
});

function drawMaze() {
let container = document.getElementById('main-container');
container.innerHTML = '';
for(let y = 0; y < maze.length; y++) {
for(let x = 0; x < maze[y].length; x++) {
let cell = document.createElement('div');
cell.classList.add('cell');
if(maze[y][x] === 1) {
cell.classList.add('wall');
}
if(x === playerPosition.x && y === playerPosition.y) {
cell.innerHTML = '🚤';
}
if(x === treasurePosition.x && y === treasurePosition.y) {
cell.innerHTML = '💰';
}
container.appendChild(cell);
}
container.appendChild(document.createElement('br'));
}
}

function keydownHandler(e) {
let newPosition = {...playerPosition};
if(e.key === 'ArrowUp') newPosition.y--;
if(e.key === 'ArrowDown') newPosition.y++;
if(e.key === 'ArrowLeft') newPosition.x--;
if(e.key === 'ArrowRight') newPosition.x++;
if(maze[newPosition.y] && maze[newPosition.y][newPosition.x] !== 1) {
playerPosition = newPosition;
}
drawMaze();
if(playerPosition.x === treasurePosition.x && playerPosition.y === treasurePosition.y) {
alert('Congratulations, you found the treasure! 😄');
}
}
} catch (error) {
throw error;
}
</script>

<style>
body {
background: linear-gradient(to right, #e0c3fc, #8ec5fc);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: 'Pacifico', cursive;
}
#main-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
width: 280px;
height: 200px;
}
.cell {
width: 40px;
height: 40px;
border: 1px solid #fff;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-size: 20px;
}
.wall {
background-color: #000;
}
</style>

<link href="https://fonts.googleapis.com/css?family=Pacifico&display=swap" rel="stylesheet">
</head>
<body>
<div id="main-container">
</div>
</body>
</html>