Bomb HTML
Bomb HTML
Bomb HTML
DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
html, body {
height: 100%;
margin: 0;
body {
background: black;
display: flex;
align-items: center;
justify-content: center;
canvas {
background: forestgreen;
</style>
</head>
<body>
<script>
softWallCtx.fillStyle = 'black';
softWallCtx.fillStyle = '#a9a9a9';
// create a new canvas and draw the soft wall image. then we can use this
wallCtx.fillStyle = 'black';
wallCtx.fillStyle = 'white';
wallCtx.fillRect(0, 0, grid - 2, grid - 2);
wallCtx.fillStyle = '#a9a9a9';
const types = {
wall: '▉',
softWall: 1,
bomb: 2
};
// keep track of what is in every cell of the game using a 2d array. the
// template is used to note where walls are and where soft walls cannot spawn.
// 'x' represents a cell that cannot have a soft wall (player start zone)
const template = [
['▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉'],
['▉','x','x', , , , , , , , , ,'x','x','▉'],
['▉','x', , , , , , , , , , , ,'x','▉'],
['▉', , , , , , , , , , , , , ,'▉'],
['▉', , , , , , , , , , , , , ,'▉'],
['▉','x', , , , , , , , , , , ,'x','▉'],
['▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉','▉']
];
function generateLevel() {
cells = [];
cells[row] = [];
cells[row][col] = types.softWall;
cells[row][col] = types.wall;
function blowUpBomb(bomb) {
if (!bomb.alive) return;
bomb.alive = false;
// remove bomb from grid
cells[bomb.row][bomb.col] = null;
const dirs = [{
// up
row: -1,
col: 0
}, {
// down
row: 1,
col: 0
}, {
// left
row: 0,
col: -1
}, {
// right
row: 0,
col: 1
}];
dirs.forEach((dir) => {
return;
}
cells[row][col] = null;
return (
);
});
blowUpBomb(nextBomb);
if (cell) {
return;
});
this.row = row;
this.col = col;
this.radius = grid * 0.4;
this.alive = true;
this.type = types.bomb;
this.timer = 3000;
this.update = function(dt) {
this.timer -= dt;
if (this.timer <= 0) {
return blowUpBomb(this);
// change the size of the bomb every half second. we can determine the size
// by dividing by 500 (half a second) and taking the ceiling of the result.
// then we can check if the result is even or odd and change the size
if (interval % 2 === 0) {
else {
};
// draw bomb
context.fillStyle = 'black';
context.beginPath();
context.fill();
// draw bomb fuse moving up and down with the bomb size
context.strokeStyle = 'white';
context.lineWidth = 5;
context.beginPath();
context.arc(
);
context.stroke();
};
this.row = row;
this.col = col;
this.dir = dir;
this.alive = true;
// show explosion for 0.3 seconds
this.timer = 300;
this.update = function(dt) {
this.timer -= dt;
if (this.timer <=0) {
this.alive = false;
};
this.render = function() {
if (center || horizontal) {
}
if (center || vertical) {
if (center || horizontal) {
if (center || vertical) {
};
const player = {
row: 1,
col: 1,
numBombs: 1,
bombSize: 3,
render() {
context.save();
context.fillStyle = 'white';
context.beginPath();
context.fill();
}
// game loop
let last;
let dt;
function loop(timestamp) {
requestAnimationFrame(loop);
context.clearRect(0,0,canvas.width,canvas.height);
if (!last) {
last = timestamp;
dt = timestamp - last;
last = timestamp;
switch(cells[row][col]) {
case types.wall:
break;
case types.softWall:
break;
}
// update and render all entities
entities.forEach((entity) => {
entity.update(dt);
entity.render();
});
player.render();
document.addEventListener('keydown', function(e) {
col--;
// up arrow key
row--;
col++;
row++;
else if (
entities.filter((entity) => {
){
// place bomb
entities.push(bomb);
cells[row][col] = types.bomb;
if (!cells[row][col]) {
player.row = row;
player.col = col;
});
generateLevel();
requestAnimationFrame(loop);
</script>
</body>
</html>