User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(projectile);' Line Number: 289
User prompt
add more cubes like gold diamond etc
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(self.healthBarContainer);' Line Number: 219
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(self.healthBarContainer);' Line Number: 219
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(self.healthBarContainer);' Line Number: 217
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(self.healthBarContainer);' Line Number: 217
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(self.healthBarContainer);' Line Number: 217
User prompt
There is a bug in the boss fight, it says error addchild etc. fix that bug
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(self.healthBarContainer);' Line Number: 216
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(self.healthBarContainer);' Line Number: 217
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'addChild')' in or related to this line: 'game.addChild(self.healthBarContainer);' Line Number: 217
User prompt
change the bosses face and fix the health bar and have different phases of the boss, have 3 different phases and make the boss's health bar at the top so that it is visible when the boss comes
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'y')' in or related to this line: 'var angle = Math.atan2(player.y - self.y, player.x - self.x);' Line Number: 142
User prompt
Make the boss a little better, let his type be different and let us have a health bar and the boss's health bar is crooked, fix that too and in the boss fight, all the green and red hearts will be scared and run away and then watch us ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'TypeError: Cannot read properties of null (reading 'x')' in or related to this line: 'var dx = target.x - self.x;' Line Number: 521
User prompt
When you reach the final 2500 points, there will be a boss fight and when you win, all the cups will be saved and there will be enemies and then suddenly an alien will appear on the screen and it will say to be continued
User prompt
Let the game be finalized when you reach 5500 points instead of 50 levels.
User prompt
Let's develop the game more and level up etc., let's earn weapons etc. as we level up for every 100 points and let's have different cups to collect, for example 20 point or 30 point cups and let's see these as we level up
User prompt
Please fix the bug: 'TypeError: Cannot set properties of undefined (setting 'fill')' in or related to this line: 'timerText.style.fill = 0x2ecc71;' Line Number: 392
User prompt
Edit the game assests, make the main character's face visible and how many points we have at the moment, make the obstacles have faces and make the graphics better, make the background colorful ↪💡 Consider importing and using the following plugins: @upit/tween.v1
Code edit (1 edits merged)
Please save this source code
User prompt
Cube Collector
Initial prompt
Here’s the idea in English: Game Concept: "Collect the Cubes" Gameplay: The player controls a square (character) on the screen and the goal is to collect randomly placed small cubes. The cubes will appear at different locations each time, and the player gains points by collecting them. Mechanics: The player moves the square up, down, left, or right using arrow keys or touch controls. The cubes will randomly appear on the screen, and the player needs to collect them. Over time, the speed of the cubes increases, or more obstacles might appear on the screen, challenging the player to stay alert. Each collected cube earns the player a point. The game can either last for a set amount of time or end once the player reaches a specific score. Extra Features: Obstacles: Moving obstacles on the screen could limit the player's movement area or cause the game to end if hit. Time Limit: The player earns more points for collecting cubes within a certain time frame. Scoreboard: A high score table could be added to track the best scores. It’s a simple game, but with increasing difficulty, it can keep players engaged and challenged.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var CollectableCube = Container.expand(function () {
var self = Container.call(this);
var cubeGraphics = self.attachAsset('collectableCube', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = 10;
self.spawnTime = 0;
self.pulseAnimation = function () {
tween(cubeGraphics, {
scaleX: 1.2,
scaleY: 1.2
}, {
duration: 300,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(cubeGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 300,
easing: tween.easeInOut
});
}
});
};
self.randomPosition = function (minX, maxX, minY, maxY) {
self.x = minX + Math.random() * (maxX - minX);
self.y = minY + Math.random() * (maxY - minY);
self.spawnTime = Date.now();
self.pulseAnimation();
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
// Bounce off screen edges
if (self.x < obstacleGraphics.width / 2 || self.x > 2048 - obstacleGraphics.width / 2) {
self.speedX *= -1;
}
if (self.y < obstacleGraphics.height / 2 || self.y > 2732 - obstacleGraphics.height / 2) {
self.speedY *= -1;
}
};
self.setRandomSpeed = function (maxSpeed) {
// Ensure obstacle always moves by setting minimum speed
var minSpeed = maxSpeed * 0.3;
// Generate random speeds between min and max
self.speedX = (Math.random() > 0.5 ? 1 : -1) * (minSpeed + Math.random() * (maxSpeed - minSpeed));
self.speedY = (Math.random() > 0.5 ? 1 : -1) * (minSpeed + Math.random() * (maxSpeed - minSpeed));
};
self.randomPosition = function (minX, maxX, minY, maxY) {
self.x = minX + Math.random() * (maxX - minX);
self.y = minY + Math.random() * (maxY - minY);
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('playerCube', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.targetX = 0;
self.targetY = 0;
self.moving = false;
self.setTarget = function (x, y) {
self.targetX = x;
self.targetY = y;
self.moving = true;
};
self.update = function () {
if (self.moving) {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
self.x = self.targetX;
self.y = self.targetY;
self.moving = false;
} else {
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
}
}
};
return self;
});
var TimerBar = Container.expand(function () {
var self = Container.call(this);
var background = self.attachAsset('timerBarBackground', {
anchorX: 0,
anchorY: 0.5
});
var bar = self.attachAsset('timerBar', {
anchorX: 0,
anchorY: 0.5
});
self.maxTime = 30000; // 30 seconds in milliseconds
self.timeRemaining = self.maxTime;
self.update = function (delta) {
self.timeRemaining -= delta;
if (self.timeRemaining < 0) {
self.timeRemaining = 0;
}
var percentage = self.timeRemaining / self.maxTime;
bar.scale.x = percentage;
// Change color as time runs out
if (percentage < 0.2) {
bar.tint = 0xe74c3c; // Red
} else if (percentage < 0.5) {
bar.tint = 0xf39c12; // Orange
} else {
bar.tint = 0x2ecc71; // Green
}
};
self.reset = function () {
self.timeRemaining = self.maxTime;
bar.scale.x = 1;
bar.tint = 0x2ecc71;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x34495e
});
/****
* Game Code
****/
// Game constants
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
var PLAYER_SPEED_BASE = 10;
var OBSTACLE_SPEED_BASE = 3;
var CUBE_SPAWN_INTERVAL_BASE = 1500; // ms
var OBSTACLE_SPAWN_INTERVAL = 5000; // ms
var LEVEL_UP_SCORE = 100;
var TIME_BONUS_MAX = 50;
var TIME_BONUS_THRESHOLD = 1000; // ms
// Game variables
var player;
var collectableCubes = [];
var obstacles = [];
var timerBar;
var score = 0;
var level = 1;
var lastCubeSpawnTime = 0;
var lastObstacleSpawnTime = 0;
var cubeSpawnInterval = CUBE_SPAWN_INTERVAL_BASE;
var playerSpeed = PLAYER_SPEED_BASE;
var obstacleSpeed = OBSTACLE_SPEED_BASE;
var lastTime = Date.now();
var gameActive = true;
var highScore = storage.highScore || 0;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 70,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
scoreTxt.x = -scoreTxt.width - 20;
scoreTxt.y = 20;
var levelTxt = new Text2('Level: 1', {
size: 70,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(levelTxt);
levelTxt.x = -levelTxt.width - 20;
levelTxt.y = scoreTxt.y + scoreTxt.height + 10;
var highScoreTxt = new Text2('High Score: ' + highScore, {
size: 70,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(highScoreTxt);
highScoreTxt.x = -highScoreTxt.width - 20;
highScoreTxt.y = levelTxt.y + levelTxt.height + 10;
// Initialize game elements
function initGame() {
// Create and position player
player = new Player();
player.x = GAME_WIDTH / 2;
player.y = GAME_HEIGHT / 2;
player.speed = playerSpeed;
game.addChild(player);
// Create timer bar
timerBar = new TimerBar();
timerBar.x = (GAME_WIDTH - 1800) / 2;
timerBar.y = 100;
game.addChild(timerBar);
// Start with a few cubes and obstacles
spawnCollectableCube();
spawnObstacle();
// Play background music
LK.playMusic('gameMusic');
}
function updateGame() {
if (!gameActive) {
return;
}
var currentTime = Date.now();
var delta = currentTime - lastTime;
lastTime = currentTime;
// Update timer
timerBar.update(delta);
if (timerBar.timeRemaining <= 0) {
gameOver();
return;
}
// Spawn new cubes
if (currentTime - lastCubeSpawnTime > cubeSpawnInterval) {
spawnCollectableCube();
lastCubeSpawnTime = currentTime;
}
// Spawn new obstacles
if (currentTime - lastObstacleSpawnTime > OBSTACLE_SPAWN_INTERVAL) {
spawnObstacle();
lastObstacleSpawnTime = currentTime;
}
// Check for collisions
checkCollisions();
}
function spawnCollectableCube() {
var cube = new CollectableCube();
cube.randomPosition(100, GAME_WIDTH - 100, 200, GAME_HEIGHT - 200);
// Make sure cube doesn't spawn on obstacles
var validPosition = false;
var maxAttempts = 10;
var attempts = 0;
while (!validPosition && attempts < maxAttempts) {
validPosition = true;
for (var i = 0; i < obstacles.length; i++) {
var obstacle = obstacles[i];
var dx = cube.x - obstacle.x;
var dy = cube.y - obstacle.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 150) {
validPosition = false;
cube.randomPosition(100, GAME_WIDTH - 100, 200, GAME_HEIGHT - 200);
break;
}
}
attempts++;
}
collectableCubes.push(cube);
game.addChild(cube);
}
function spawnObstacle() {
var obstacle = new Obstacle();
obstacle.randomPosition(100, GAME_WIDTH - 100, 200, GAME_HEIGHT - 200);
obstacle.setRandomSpeed(obstacleSpeed);
// Make sure obstacle doesn't spawn on player
var dx = obstacle.x - player.x;
var dy = obstacle.y - player.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 200) {
obstacle.x = GAME_WIDTH - obstacle.x;
obstacle.y = GAME_HEIGHT - obstacle.y;
}
obstacles.push(obstacle);
game.addChild(obstacle);
}
function checkCollisions() {
// Check collisions with collectables
for (var i = collectableCubes.length - 1; i >= 0; i--) {
var cube = collectableCubes[i];
if (player.intersects(cube)) {
// Calculate time bonus
var collectionTime = Date.now() - cube.spawnTime;
var timeBonus = 0;
if (collectionTime < TIME_BONUS_THRESHOLD) {
timeBonus = Math.round(TIME_BONUS_MAX * (1 - collectionTime / TIME_BONUS_THRESHOLD));
}
// Add score and remove cube
addScore(cube.value + timeBonus);
game.removeChild(cube);
collectableCubes.splice(i, 1);
// Play collect sound
LK.getSound('collect').play();
// Check for level up
checkLevelUp();
}
}
// Check collisions with obstacles
for (var j = 0; j < obstacles.length; j++) {
var obstacle = obstacles[j];
if (player.intersects(obstacle)) {
// Player hit an obstacle
LK.getSound('collision').play();
LK.effects.flashScreen(0xe74c3c, 300);
// Reduce timer as penalty
timerBar.timeRemaining -= 3000; // 3 second penalty
// Push player away from obstacle
var dx = player.x - obstacle.x;
var dy = player.y - obstacle.y;
var angle = Math.atan2(dy, dx);
player.x += Math.cos(angle) * 100;
player.y += Math.sin(angle) * 100;
// Keep player within bounds
player.x = Math.max(40, Math.min(GAME_WIDTH - 40, player.x));
player.y = Math.max(40, Math.min(GAME_HEIGHT - 40, player.y));
}
}
}
function addScore(points) {
score += points;
scoreTxt.setText('Score: ' + score);
// Flash score text
tween(scoreTxt, {
scale: 1.2
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(scoreTxt, {
scale: 1
}, {
duration: 200,
easing: tween.easeIn
});
}
});
}
function checkLevelUp() {
if (score >= level * LEVEL_UP_SCORE) {
level++;
levelTxt.setText('Level: ' + level);
// Increase difficulty
cubeSpawnInterval = Math.max(500, CUBE_SPAWN_INTERVAL_BASE - (level - 1) * 150);
playerSpeed = PLAYER_SPEED_BASE + (level - 1) * 1;
obstacleSpeed = OBSTACLE_SPEED_BASE + (level - 1) * 0.5;
// Update player speed
player.speed = playerSpeed;
// Update obstacle speeds
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].setRandomSpeed(obstacleSpeed);
}
// Visual feedback for level up
LK.effects.flashScreen(0x2ecc71, 500);
// Add time bonus for leveling up
timerBar.timeRemaining += 5000; // 5 second bonus
if (timerBar.timeRemaining > timerBar.maxTime) {
timerBar.timeRemaining = timerBar.maxTime;
}
// Flash level text
tween(levelTxt, {
scale: 1.3
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(levelTxt, {
scale: 1
}, {
duration: 300,
easing: tween.easeIn
});
}
});
}
}
function gameOver() {
gameActive = false;
// Update high score
if (score > highScore) {
highScore = score;
storage.highScore = highScore;
highScoreTxt.setText('High Score: ' + highScore);
}
// Show game over
LK.showGameOver();
}
function resetGame() {
// Reset game variables
score = 0;
level = 1;
cubeSpawnInterval = CUBE_SPAWN_INTERVAL_BASE;
playerSpeed = PLAYER_SPEED_BASE;
obstacleSpeed = OBSTACLE_SPEED_BASE;
lastTime = Date.now();
lastCubeSpawnTime = 0;
lastObstacleSpawnTime = 0;
gameActive = true;
// Clear all game elements
while (collectableCubes.length > 0) {
var cube = collectableCubes.pop();
game.removeChild(cube);
}
while (obstacles.length > 0) {
var obstacle = obstacles.pop();
game.removeChild(obstacle);
}
if (player) {
game.removeChild(player);
}
if (timerBar) {
game.removeChild(timerBar);
}
// Reset UI
scoreTxt.setText('Score: 0');
levelTxt.setText('Level: 1');
highScoreTxt.setText('High Score: ' + highScore);
// Initialize game again
initGame();
}
// Event handlers
game.down = function (x, y, obj) {
if (gameActive) {
player.setTarget(x, y);
}
};
game.move = function (x, y, obj) {
if (gameActive && obj.down) {
player.setTarget(x, y);
}
};
game.update = function () {
updateGame();
// Update player
if (player) {
player.update();
}
// Update obstacles
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
}
};
// Initialize the game
initGame(); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,456 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var CollectableCube = Container.expand(function () {
+ var self = Container.call(this);
+ var cubeGraphics = self.attachAsset('collectableCube', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.value = 10;
+ self.spawnTime = 0;
+ self.pulseAnimation = function () {
+ tween(cubeGraphics, {
+ scaleX: 1.2,
+ scaleY: 1.2
+ }, {
+ duration: 300,
+ easing: tween.easeInOut,
+ onFinish: function onFinish() {
+ tween(cubeGraphics, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 300,
+ easing: tween.easeInOut
+ });
+ }
+ });
+ };
+ self.randomPosition = function (minX, maxX, minY, maxY) {
+ self.x = minX + Math.random() * (maxX - minX);
+ self.y = minY + Math.random() * (maxY - minY);
+ self.spawnTime = Date.now();
+ self.pulseAnimation();
+ };
+ return self;
+});
+var Obstacle = Container.expand(function () {
+ var self = Container.call(this);
+ var obstacleGraphics = self.attachAsset('obstacle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speedX = 0;
+ self.speedY = 0;
+ self.update = function () {
+ self.x += self.speedX;
+ self.y += self.speedY;
+ // Bounce off screen edges
+ if (self.x < obstacleGraphics.width / 2 || self.x > 2048 - obstacleGraphics.width / 2) {
+ self.speedX *= -1;
+ }
+ if (self.y < obstacleGraphics.height / 2 || self.y > 2732 - obstacleGraphics.height / 2) {
+ self.speedY *= -1;
+ }
+ };
+ self.setRandomSpeed = function (maxSpeed) {
+ // Ensure obstacle always moves by setting minimum speed
+ var minSpeed = maxSpeed * 0.3;
+ // Generate random speeds between min and max
+ self.speedX = (Math.random() > 0.5 ? 1 : -1) * (minSpeed + Math.random() * (maxSpeed - minSpeed));
+ self.speedY = (Math.random() > 0.5 ? 1 : -1) * (minSpeed + Math.random() * (maxSpeed - minSpeed));
+ };
+ self.randomPosition = function (minX, maxX, minY, maxY) {
+ self.x = minX + Math.random() * (maxX - minX);
+ self.y = minY + Math.random() * (maxY - minY);
+ };
+ return self;
+});
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('playerCube', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 10;
+ self.targetX = 0;
+ self.targetY = 0;
+ self.moving = false;
+ self.setTarget = function (x, y) {
+ self.targetX = x;
+ self.targetY = y;
+ self.moving = true;
+ };
+ self.update = function () {
+ if (self.moving) {
+ var dx = self.targetX - self.x;
+ var dy = self.targetY - self.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance < self.speed) {
+ self.x = self.targetX;
+ self.y = self.targetY;
+ self.moving = false;
+ } else {
+ var angle = Math.atan2(dy, dx);
+ self.x += Math.cos(angle) * self.speed;
+ self.y += Math.sin(angle) * self.speed;
+ }
+ }
+ };
+ return self;
+});
+var TimerBar = Container.expand(function () {
+ var self = Container.call(this);
+ var background = self.attachAsset('timerBarBackground', {
+ anchorX: 0,
+ anchorY: 0.5
+ });
+ var bar = self.attachAsset('timerBar', {
+ anchorX: 0,
+ anchorY: 0.5
+ });
+ self.maxTime = 30000; // 30 seconds in milliseconds
+ self.timeRemaining = self.maxTime;
+ self.update = function (delta) {
+ self.timeRemaining -= delta;
+ if (self.timeRemaining < 0) {
+ self.timeRemaining = 0;
+ }
+ var percentage = self.timeRemaining / self.maxTime;
+ bar.scale.x = percentage;
+ // Change color as time runs out
+ if (percentage < 0.2) {
+ bar.tint = 0xe74c3c; // Red
+ } else if (percentage < 0.5) {
+ bar.tint = 0xf39c12; // Orange
+ } else {
+ bar.tint = 0x2ecc71; // Green
+ }
+ };
+ self.reset = function () {
+ self.timeRemaining = self.maxTime;
+ bar.scale.x = 1;
+ bar.tint = 0x2ecc71;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x34495e
+});
+
+/****
+* Game Code
+****/
+// Game constants
+var GAME_WIDTH = 2048;
+var GAME_HEIGHT = 2732;
+var PLAYER_SPEED_BASE = 10;
+var OBSTACLE_SPEED_BASE = 3;
+var CUBE_SPAWN_INTERVAL_BASE = 1500; // ms
+var OBSTACLE_SPAWN_INTERVAL = 5000; // ms
+var LEVEL_UP_SCORE = 100;
+var TIME_BONUS_MAX = 50;
+var TIME_BONUS_THRESHOLD = 1000; // ms
+// Game variables
+var player;
+var collectableCubes = [];
+var obstacles = [];
+var timerBar;
+var score = 0;
+var level = 1;
+var lastCubeSpawnTime = 0;
+var lastObstacleSpawnTime = 0;
+var cubeSpawnInterval = CUBE_SPAWN_INTERVAL_BASE;
+var playerSpeed = PLAYER_SPEED_BASE;
+var obstacleSpeed = OBSTACLE_SPEED_BASE;
+var lastTime = Date.now();
+var gameActive = true;
+var highScore = storage.highScore || 0;
+// UI Elements
+var scoreTxt = new Text2('Score: 0', {
+ size: 70,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(scoreTxt);
+scoreTxt.x = -scoreTxt.width - 20;
+scoreTxt.y = 20;
+var levelTxt = new Text2('Level: 1', {
+ size: 70,
+ fill: 0xFFFFFF
+});
+levelTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(levelTxt);
+levelTxt.x = -levelTxt.width - 20;
+levelTxt.y = scoreTxt.y + scoreTxt.height + 10;
+var highScoreTxt = new Text2('High Score: ' + highScore, {
+ size: 70,
+ fill: 0xFFFFFF
+});
+highScoreTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(highScoreTxt);
+highScoreTxt.x = -highScoreTxt.width - 20;
+highScoreTxt.y = levelTxt.y + levelTxt.height + 10;
+// Initialize game elements
+function initGame() {
+ // Create and position player
+ player = new Player();
+ player.x = GAME_WIDTH / 2;
+ player.y = GAME_HEIGHT / 2;
+ player.speed = playerSpeed;
+ game.addChild(player);
+ // Create timer bar
+ timerBar = new TimerBar();
+ timerBar.x = (GAME_WIDTH - 1800) / 2;
+ timerBar.y = 100;
+ game.addChild(timerBar);
+ // Start with a few cubes and obstacles
+ spawnCollectableCube();
+ spawnObstacle();
+ // Play background music
+ LK.playMusic('gameMusic');
+}
+function updateGame() {
+ if (!gameActive) {
+ return;
+ }
+ var currentTime = Date.now();
+ var delta = currentTime - lastTime;
+ lastTime = currentTime;
+ // Update timer
+ timerBar.update(delta);
+ if (timerBar.timeRemaining <= 0) {
+ gameOver();
+ return;
+ }
+ // Spawn new cubes
+ if (currentTime - lastCubeSpawnTime > cubeSpawnInterval) {
+ spawnCollectableCube();
+ lastCubeSpawnTime = currentTime;
+ }
+ // Spawn new obstacles
+ if (currentTime - lastObstacleSpawnTime > OBSTACLE_SPAWN_INTERVAL) {
+ spawnObstacle();
+ lastObstacleSpawnTime = currentTime;
+ }
+ // Check for collisions
+ checkCollisions();
+}
+function spawnCollectableCube() {
+ var cube = new CollectableCube();
+ cube.randomPosition(100, GAME_WIDTH - 100, 200, GAME_HEIGHT - 200);
+ // Make sure cube doesn't spawn on obstacles
+ var validPosition = false;
+ var maxAttempts = 10;
+ var attempts = 0;
+ while (!validPosition && attempts < maxAttempts) {
+ validPosition = true;
+ for (var i = 0; i < obstacles.length; i++) {
+ var obstacle = obstacles[i];
+ var dx = cube.x - obstacle.x;
+ var dy = cube.y - obstacle.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance < 150) {
+ validPosition = false;
+ cube.randomPosition(100, GAME_WIDTH - 100, 200, GAME_HEIGHT - 200);
+ break;
+ }
+ }
+ attempts++;
+ }
+ collectableCubes.push(cube);
+ game.addChild(cube);
+}
+function spawnObstacle() {
+ var obstacle = new Obstacle();
+ obstacle.randomPosition(100, GAME_WIDTH - 100, 200, GAME_HEIGHT - 200);
+ obstacle.setRandomSpeed(obstacleSpeed);
+ // Make sure obstacle doesn't spawn on player
+ var dx = obstacle.x - player.x;
+ var dy = obstacle.y - player.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance < 200) {
+ obstacle.x = GAME_WIDTH - obstacle.x;
+ obstacle.y = GAME_HEIGHT - obstacle.y;
+ }
+ obstacles.push(obstacle);
+ game.addChild(obstacle);
+}
+function checkCollisions() {
+ // Check collisions with collectables
+ for (var i = collectableCubes.length - 1; i >= 0; i--) {
+ var cube = collectableCubes[i];
+ if (player.intersects(cube)) {
+ // Calculate time bonus
+ var collectionTime = Date.now() - cube.spawnTime;
+ var timeBonus = 0;
+ if (collectionTime < TIME_BONUS_THRESHOLD) {
+ timeBonus = Math.round(TIME_BONUS_MAX * (1 - collectionTime / TIME_BONUS_THRESHOLD));
+ }
+ // Add score and remove cube
+ addScore(cube.value + timeBonus);
+ game.removeChild(cube);
+ collectableCubes.splice(i, 1);
+ // Play collect sound
+ LK.getSound('collect').play();
+ // Check for level up
+ checkLevelUp();
+ }
+ }
+ // Check collisions with obstacles
+ for (var j = 0; j < obstacles.length; j++) {
+ var obstacle = obstacles[j];
+ if (player.intersects(obstacle)) {
+ // Player hit an obstacle
+ LK.getSound('collision').play();
+ LK.effects.flashScreen(0xe74c3c, 300);
+ // Reduce timer as penalty
+ timerBar.timeRemaining -= 3000; // 3 second penalty
+ // Push player away from obstacle
+ var dx = player.x - obstacle.x;
+ var dy = player.y - obstacle.y;
+ var angle = Math.atan2(dy, dx);
+ player.x += Math.cos(angle) * 100;
+ player.y += Math.sin(angle) * 100;
+ // Keep player within bounds
+ player.x = Math.max(40, Math.min(GAME_WIDTH - 40, player.x));
+ player.y = Math.max(40, Math.min(GAME_HEIGHT - 40, player.y));
+ }
+ }
+}
+function addScore(points) {
+ score += points;
+ scoreTxt.setText('Score: ' + score);
+ // Flash score text
+ tween(scoreTxt, {
+ scale: 1.2
+ }, {
+ duration: 200,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ tween(scoreTxt, {
+ scale: 1
+ }, {
+ duration: 200,
+ easing: tween.easeIn
+ });
+ }
+ });
+}
+function checkLevelUp() {
+ if (score >= level * LEVEL_UP_SCORE) {
+ level++;
+ levelTxt.setText('Level: ' + level);
+ // Increase difficulty
+ cubeSpawnInterval = Math.max(500, CUBE_SPAWN_INTERVAL_BASE - (level - 1) * 150);
+ playerSpeed = PLAYER_SPEED_BASE + (level - 1) * 1;
+ obstacleSpeed = OBSTACLE_SPEED_BASE + (level - 1) * 0.5;
+ // Update player speed
+ player.speed = playerSpeed;
+ // Update obstacle speeds
+ for (var i = 0; i < obstacles.length; i++) {
+ obstacles[i].setRandomSpeed(obstacleSpeed);
+ }
+ // Visual feedback for level up
+ LK.effects.flashScreen(0x2ecc71, 500);
+ // Add time bonus for leveling up
+ timerBar.timeRemaining += 5000; // 5 second bonus
+ if (timerBar.timeRemaining > timerBar.maxTime) {
+ timerBar.timeRemaining = timerBar.maxTime;
+ }
+ // Flash level text
+ tween(levelTxt, {
+ scale: 1.3
+ }, {
+ duration: 300,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ tween(levelTxt, {
+ scale: 1
+ }, {
+ duration: 300,
+ easing: tween.easeIn
+ });
+ }
+ });
+ }
+}
+function gameOver() {
+ gameActive = false;
+ // Update high score
+ if (score > highScore) {
+ highScore = score;
+ storage.highScore = highScore;
+ highScoreTxt.setText('High Score: ' + highScore);
+ }
+ // Show game over
+ LK.showGameOver();
+}
+function resetGame() {
+ // Reset game variables
+ score = 0;
+ level = 1;
+ cubeSpawnInterval = CUBE_SPAWN_INTERVAL_BASE;
+ playerSpeed = PLAYER_SPEED_BASE;
+ obstacleSpeed = OBSTACLE_SPEED_BASE;
+ lastTime = Date.now();
+ lastCubeSpawnTime = 0;
+ lastObstacleSpawnTime = 0;
+ gameActive = true;
+ // Clear all game elements
+ while (collectableCubes.length > 0) {
+ var cube = collectableCubes.pop();
+ game.removeChild(cube);
+ }
+ while (obstacles.length > 0) {
+ var obstacle = obstacles.pop();
+ game.removeChild(obstacle);
+ }
+ if (player) {
+ game.removeChild(player);
+ }
+ if (timerBar) {
+ game.removeChild(timerBar);
+ }
+ // Reset UI
+ scoreTxt.setText('Score: 0');
+ levelTxt.setText('Level: 1');
+ highScoreTxt.setText('High Score: ' + highScore);
+ // Initialize game again
+ initGame();
+}
+// Event handlers
+game.down = function (x, y, obj) {
+ if (gameActive) {
+ player.setTarget(x, y);
+ }
+};
+game.move = function (x, y, obj) {
+ if (gameActive && obj.down) {
+ player.setTarget(x, y);
+ }
+};
+game.update = function () {
+ updateGame();
+ // Update player
+ if (player) {
+ player.update();
+ }
+ // Update obstacles
+ for (var i = 0; i < obstacles.length; i++) {
+ obstacles[i].update();
+ }
+};
+// Initialize the game
+initGame();
\ No newline at end of file