Skip to content

Commit 5fef272

Browse files
authored
block by block added
1 parent 97259c1 commit 5fef272

5 files changed

Lines changed: 141 additions & 0 deletions

File tree

Block by Block/icon.png

4.83 KB
Loading

Block by Block/index.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Tower Builder Game</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
11+
<div id="gameArea"></div>
12+
<div id="score">Score: 0</div>
13+
14+
<script src="script.js"></script>
15+
16+
</body>
17+
</html>

Block by Block/manifest.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "Block by Block",
4+
"version": "1.0",
5+
"description": "Block by Block",
6+
"icons": {
7+
"48": "icon.png",
8+
"64": "icon.png",
9+
"128": "icon.png"
10+
},
11+
"permissions": [
12+
"storage"
13+
],
14+
"optional-permissions": [
15+
"tabs"
16+
],
17+
"action": {
18+
"default_popup": "index.html"
19+
},
20+
"web_accessible_resources": [
21+
{
22+
"resources": [
23+
"index.html"
24+
],
25+
"matches": [
26+
"<all_urls>"
27+
]
28+
}
29+
]
30+
}

Block by Block/script.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
let gameArea = document.getElementById('gameArea');
2+
let scoreDisplay = document.getElementById('score');
3+
let score = 0;
4+
let blockWidth = 60;
5+
let blockHeight = 20;
6+
let blockSpeed = 2;
7+
let blockInterval = null;
8+
let gameOver = false;
9+
10+
function startGame() {
11+
gameArea.innerHTML = '';
12+
score = 0;
13+
scoreDisplay.textContent = 'Score: ' + score;
14+
gameOver = false;
15+
addBlock();
16+
blockInterval = setInterval(moveBlock, 10);
17+
}
18+
19+
function addBlock() {
20+
let block = document.createElement('div');
21+
block.className = 'block';
22+
block.style.top = '0px';
23+
block.style.left = (gameArea.offsetWidth - blockWidth) / 2 + 'px';
24+
gameArea.appendChild(block);
25+
}
26+
27+
function moveBlock() {
28+
let blocks = document.getElementsByClassName('block');
29+
if (blocks.length > 0) {
30+
let block = blocks[blocks.length - 1];
31+
let top = parseInt(block.style.top);
32+
block.style.top = top + blockSpeed + 'px';
33+
34+
if (top + blockHeight >= gameArea.offsetHeight) {
35+
clearInterval(blockInterval);
36+
gameOver = true;
37+
alert('Game Over! Your score is: ' + score);
38+
}
39+
}
40+
}
41+
42+
function dropBlock() {
43+
if (gameOver) return;
44+
45+
let blocks = document.getElementsByClassName('block');
46+
if (blocks.length > 0) {
47+
let block = blocks[blocks.length - 1];
48+
let top = parseInt(block.style.top);
49+
50+
if (top + blockHeight < gameArea.offsetHeight) {
51+
let newBlock = block.cloneNode();
52+
newBlock.style.top = '0px';
53+
gameArea.appendChild(newBlock);
54+
score++;
55+
scoreDisplay.textContent = 'Score: ' + score;
56+
} else {
57+
gameOver = true;
58+
alert('Game Over! Your score is: ' + score);
59+
}
60+
}
61+
}
62+
63+
gameArea.addEventListener('click', dropBlock);
64+
window.onload = startGame;

Block by Block/style.css

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
display: flex;
4+
justify-content: center;
5+
align-items: center;
6+
height: 100vh;
7+
background-color: #f0f0f0;
8+
flex-direction: column;
9+
}
10+
11+
#gameArea {
12+
position: relative;
13+
width: 200px;
14+
height: 400px;
15+
border: 2px solid #333;
16+
background-color: #fff;
17+
}
18+
19+
.block {
20+
position: absolute;
21+
width: 60px;
22+
height: 20px;
23+
background-color: #007bff;
24+
border: 1px solid #0056b3;
25+
}
26+
27+
#score {
28+
margin-top: 20px;
29+
font-size: 24px;
30+
}

0 commit comments

Comments
 (0)