Spaces:
Running
Running
? wtf starting block is not centered and no blocks to drop to see
Browse files- components/tower-game.js +42 -12
components/tower-game.js
CHANGED
|
@@ -5,8 +5,9 @@ class TowerGame extends HTMLElement {
|
|
| 5 |
<style>
|
| 6 |
.game-container {
|
| 7 |
width: 100%;
|
| 8 |
-
height:
|
| 9 |
-
|
|
|
|
| 10 |
position: relative;
|
| 11 |
overflow: hidden;
|
| 12 |
border-radius: 8px;
|
|
@@ -40,20 +41,25 @@ class TowerGame extends HTMLElement {
|
|
| 40 |
}
|
| 41 |
|
| 42 |
initGame() {
|
| 43 |
-
|
| 44 |
-
this.
|
| 45 |
-
this.
|
|
|
|
| 46 |
this.blockHeight = 20;
|
| 47 |
this.speed = 2;
|
| 48 |
this.direction = 1;
|
| 49 |
this.tower = [];
|
| 50 |
this.score = 0;
|
| 51 |
this.gameOver = false;
|
|
|
|
| 52 |
|
| 53 |
-
// Create base block
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
this.gameLoop();
|
| 58 |
|
| 59 |
// Set up controls
|
|
@@ -91,23 +97,47 @@ class TowerGame extends HTMLElement {
|
|
| 91 |
|
| 92 |
requestAnimationFrame(() => this.gameLoop());
|
| 93 |
}
|
| 94 |
-
|
| 95 |
placeBlock() {
|
| 96 |
if (!this.activeBlock || this.gameOver) return;
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
// Add active block to tower
|
| 99 |
this.activeBlock.element.classList.remove('active-block');
|
| 100 |
this.tower.push(this.activeBlock);
|
| 101 |
this.score++;
|
| 102 |
this.updateScore();
|
| 103 |
|
| 104 |
-
// Create new active block
|
|
|
|
| 105 |
const newY = this.tower[this.tower.length-1].y - this.blockHeight;
|
|
|
|
| 106 |
this.createBlock(0, newY);
|
| 107 |
|
| 108 |
// Increase speed slightly
|
| 109 |
this.speed = Math.min(5, this.speed + 0.1);
|
| 110 |
-
|
| 111 |
|
| 112 |
updateScore() {
|
| 113 |
this.shadowRoot.querySelector('.score-display').textContent = `Height: ${this.score}`;
|
|
|
|
| 5 |
<style>
|
| 6 |
.game-container {
|
| 7 |
width: 100%;
|
| 8 |
+
height: 60vh;
|
| 9 |
+
min-height: 400px;
|
| 10 |
+
background: #f0f0f0;
|
| 11 |
position: relative;
|
| 12 |
overflow: hidden;
|
| 13 |
border-radius: 8px;
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
initGame() {
|
| 44 |
+
const container = this.shadowRoot.querySelector('.game-container');
|
| 45 |
+
this.gameWidth = container.clientWidth;
|
| 46 |
+
this.gameHeight = container.clientHeight;
|
| 47 |
+
this.blockWidth = Math.min(60, this.gameWidth * 0.2);
|
| 48 |
this.blockHeight = 20;
|
| 49 |
this.speed = 2;
|
| 50 |
this.direction = 1;
|
| 51 |
this.tower = [];
|
| 52 |
this.score = 0;
|
| 53 |
this.gameOver = false;
|
| 54 |
+
this.activeBlock = null;
|
| 55 |
|
| 56 |
+
// Create base block centered
|
| 57 |
+
const baseX = (this.gameWidth - this.blockWidth) / 2;
|
| 58 |
+
this.createBlock(baseX, this.gameHeight - this.blockHeight, true);
|
| 59 |
+
|
| 60 |
+
// Create first moving block
|
| 61 |
+
this.createBlock(0, this.gameHeight - this.blockHeight * 2);
|
| 62 |
+
// Start game loop
|
| 63 |
this.gameLoop();
|
| 64 |
|
| 65 |
// Set up controls
|
|
|
|
| 97 |
|
| 98 |
requestAnimationFrame(() => this.gameLoop());
|
| 99 |
}
|
|
|
|
| 100 |
placeBlock() {
|
| 101 |
if (!this.activeBlock || this.gameOver) return;
|
| 102 |
|
| 103 |
+
// Calculate overlap with previous block
|
| 104 |
+
const prevBlock = this.tower[this.tower.length - 1];
|
| 105 |
+
const overlapLeft = Math.max(0, prevBlock.x - this.activeBlock.x);
|
| 106 |
+
const overlapRight = Math.max(0, (this.activeBlock.x + this.blockWidth) - (prevBlock.x + this.blockWidth));
|
| 107 |
+
const overlapTotal = overlapLeft + overlapRight;
|
| 108 |
+
|
| 109 |
+
// Check if block is completely off
|
| 110 |
+
if (overlapTotal >= this.blockWidth) {
|
| 111 |
+
this.gameOver = true;
|
| 112 |
+
this.shadowRoot.querySelector('.score-display').textContent += ` - Game Over!`;
|
| 113 |
+
return;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// Trim the block to fit
|
| 117 |
+
if (overlapTotal > 0) {
|
| 118 |
+
this.blockWidth -= overlapTotal;
|
| 119 |
+
this.activeBlock.element.style.width = `${this.blockWidth}px`;
|
| 120 |
+
if (overlapLeft > 0) {
|
| 121 |
+
this.activeBlock.x += overlapLeft;
|
| 122 |
+
this.activeBlock.element.style.left = `${this.activeBlock.x}px`;
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
// Add active block to tower
|
| 127 |
this.activeBlock.element.classList.remove('active-block');
|
| 128 |
this.tower.push(this.activeBlock);
|
| 129 |
this.score++;
|
| 130 |
this.updateScore();
|
| 131 |
|
| 132 |
+
// Create new active block (slightly narrower)
|
| 133 |
+
const newWidth = Math.max(20, this.blockWidth * 0.98);
|
| 134 |
const newY = this.tower[this.tower.length-1].y - this.blockHeight;
|
| 135 |
+
this.blockWidth = newWidth;
|
| 136 |
this.createBlock(0, newY);
|
| 137 |
|
| 138 |
// Increase speed slightly
|
| 139 |
this.speed = Math.min(5, this.speed + 0.1);
|
| 140 |
+
}
|
| 141 |
|
| 142 |
updateScore() {
|
| 143 |
this.shadowRoot.querySelector('.score-display').textContent = `Height: ${this.score}`;
|