/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var BadCoffee = Container.expand(function () {
var self = Container.call(this);
var coffeeGraphics = self.attachAsset('badCoffee', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 7;
self.damage = 1;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 20;
self.fireRate = 60; // Frames between shots
self.lastShot = 0;
self.moveDirection = 1;
self.moveSpeed = 3;
self.update = function () {
// Move back and forth
self.x += self.moveDirection * self.moveSpeed;
// Change direction when reaching edges
if (self.x <= 500 || self.x >= 1548) {
self.moveDirection *= -1;
}
};
self.takeDamage = function (amount) {
self.health -= amount;
// Flash red when hit
tween(enemyGraphics, {
tint: 0xFF0000
}, {
duration: 200,
onFinish: function onFinish() {
tween(enemyGraphics, {
tint: 0xFFFFFF
}, {
duration: 200
});
}
});
return self.health <= 0;
};
return self;
});
var EspressoShot = Container.expand(function () {
var self = Container.call(this);
var shotGraphics = self.attachAsset('espressoShot', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -10;
self.power = 1;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 3;
self.fireRate = 30; // Frames between shots
self.lastShot = 0;
self.shotPower = 1;
self.moveSpeed = 10;
// Power-up effects
self.powerUpTimeLeft = 0;
self.powerUpType = null;
self.applyPowerUp = function (type) {
self.powerUpType = type;
self.powerUpTimeLeft = 300; // 5 seconds (60 fps * 5)
if (type === "speed") {
self.moveSpeed = 20;
playerGraphics.tint = 0xF0D290; // Yellow tint
} else if (type === "power") {
self.shotPower = 2;
playerGraphics.tint = 0xD24726; // Cinnamon color
} else if (type === "rapid") {
self.fireRate = 10;
playerGraphics.tint = 0x5E2605; // Chocolate color
}
};
self.endPowerUp = function () {
self.powerUpType = null;
self.moveSpeed = 10;
self.shotPower = 1;
self.fireRate = 30;
playerGraphics.tint = 0xFFFFFF; // Reset tint
};
self.update = function () {
// Manage power-up duration
if (self.powerUpTimeLeft > 0) {
self.powerUpTimeLeft--;
if (self.powerUpTimeLeft <= 0) {
self.endPowerUp();
}
}
// Keep player on the foam platform
if (self.x < 150) {
self.x = 150;
}
if (self.x > 1898) {
self.x = 1898;
}
if (self.y < 1400) {
self.y = 1400;
}
if (self.y > 1700) {
self.y = 1700;
}
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = "speed"; // default type
self.active = true;
// Randomly choose powerup type
self.setRandomType = function () {
var types = ["speed", "power", "rapid"];
self.type = types[Math.floor(Math.random() * types.length)];
// Update color based on type
if (self.type === "speed") {
powerUpGraphics.tint = 0xF0D290; // Yellow-ish
} else if (self.type === "power") {
powerUpGraphics.tint = 0xD24726; // Cinnamon color
} else if (self.type === "rapid") {
powerUpGraphics.tint = 0x5E2605; // Chocolate color
}
};
self.update = function () {
self.y += 2;
// Make it wiggle slightly
self.x += Math.sin(LK.ticks / 10) * 2;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x7A5230 // Coffee brown background
});
/****
* Game Code
****/
// Create game arena (giant coffee cup)
var coffeeArena = game.addChild(LK.getAsset('coffeeArena', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 1600
}));
// Create foam platform
var coffeeFoam = game.addChild(LK.getAsset('coffeeFoam', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 1550
}));
// Create player
var player = new Player();
player.x = 2048 / 2;
player.y = 1500;
game.addChild(player);
// Create enemy
var enemy = new Enemy();
enemy.x = 2048 / 2;
enemy.y = 800;
game.addChild(enemy);
// Game elements arrays
var espressoShots = [];
var badCoffees = [];
var powerUps = [];
// Score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Health display
var healthTxt = new Text2('Health: 3', {
size: 60,
fill: 0xFFFFFF
});
healthTxt.anchor.set(0, 0);
healthTxt.x = 140;
healthTxt.y = 20;
LK.gui.topRight.addChild(healthTxt);
// Game state management
var gameActive = true;
var dragNode = null;
var powerUpTimer = 0;
// Spawn a power-up
function spawnPowerUp() {
var powerUp = new PowerUp();
powerUp.setRandomType();
powerUp.x = 400 + Math.random() * 1248; // Random position across arena width
powerUp.y = 400; // Start from top area
powerUps.push(powerUp);
game.addChild(powerUp);
}
// Handle player movement
function handleMove(x, y, obj) {
if (dragNode && gameActive) {
// Calculate new position based on movement
dragNode.x = x;
// Keep player on foam platform
if (dragNode.x < 150) {
dragNode.x = 150;
}
if (dragNode.x > 1898) {
dragNode.x = 1898;
}
}
}
// Shoot espresso
function shootEspresso() {
if (!gameActive) {
return;
}
if (LK.ticks - player.lastShot >= player.fireRate) {
var shot = new EspressoShot();
shot.x = player.x;
shot.y = player.y - 40;
shot.power = player.shotPower;
espressoShots.push(shot);
game.addChild(shot);
player.lastShot = LK.ticks;
// Play shoot sound
LK.getSound('shoot').play();
}
}
// Enemy fires bad coffee
function enemyShoot() {
if (!gameActive) {
return;
}
if (LK.ticks - enemy.lastShot >= enemy.fireRate) {
var badCoffee = new BadCoffee();
badCoffee.x = enemy.x;
badCoffee.y = enemy.y + 50;
badCoffees.push(badCoffee);
game.addChild(badCoffee);
enemy.lastShot = LK.ticks;
}
}
// Update player health display
function updateHealthDisplay() {
healthTxt.setText('Health: ' + player.health);
}
// Mouse/Touch Event Handlers
game.move = handleMove;
game.down = function (x, y, obj) {
dragNode = player;
shootEspresso();
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
};
// Game update loop
game.update = function () {
if (!gameActive) {
return;
}
// Enemy shooting
enemyShoot();
// Power-up generation
powerUpTimer++;
if (powerUpTimer >= 300) {
// Every 5 seconds
spawnPowerUp();
powerUpTimer = 0;
}
// Update espresso shots
for (var i = espressoShots.length - 1; i >= 0; i--) {
var shot = espressoShots[i];
// Check if shot is off screen
if (shot.y < -50) {
shot.destroy();
espressoShots.splice(i, 1);
continue;
}
// Check for collision with enemy
if (shot.intersects(enemy)) {
// Deal damage to enemy
var defeated = enemy.takeDamage(shot.power);
if (defeated) {
// Win condition
LK.setScore(LK.getScore() + 100); // Bonus for defeating enemy
scoreTxt.setText(LK.getScore());
LK.showYouWin();
gameActive = false;
} else {
// Add score
LK.setScore(LK.getScore() + 10);
scoreTxt.setText(LK.getScore());
// Play hit sound
LK.getSound('hit').play();
}
// Remove shot
shot.destroy();
espressoShots.splice(i, 1);
}
}
// Update bad coffees
for (var j = badCoffees.length - 1; j >= 0; j--) {
var coffee = badCoffees[j];
// Check if coffee is off screen
if (coffee.y > 2800) {
coffee.destroy();
badCoffees.splice(j, 1);
continue;
}
// Check for collision with player
if (coffee.intersects(player)) {
// Reduce player health
player.health -= coffee.damage;
updateHealthDisplay();
// Check for game over
if (player.health <= 0) {
LK.effects.flashScreen(0xFF0000, 1000);
LK.showGameOver();
gameActive = false;
}
// Remove coffee
coffee.destroy();
badCoffees.splice(j, 1);
}
}
// Update power-ups
for (var k = powerUps.length - 1; k >= 0; k--) {
var powerUp = powerUps[k];
// Check if power-up is off screen
if (powerUp.y > 2800) {
powerUp.destroy();
powerUps.splice(k, 1);
continue;
}
// Check for collision with player
if (powerUp.active && powerUp.intersects(player)) {
// Apply power-up effect
player.applyPowerUp(powerUp.type);
// Play power-up sound
LK.getSound('powerup').play();
// Remove power-up
powerUp.destroy();
powerUps.splice(k, 1);
}
}
};
// Start background music
LK.playMusic('bgMusic', {
fade: {
start: 0,
end: 0.4,
duration: 1000
}
}); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,386 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var BadCoffee = Container.expand(function () {
+ var self = Container.call(this);
+ var coffeeGraphics = self.attachAsset('badCoffee', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 7;
+ self.damage = 1;
+ self.update = function () {
+ self.y += self.speed;
+ };
+ return self;
+});
+var Enemy = Container.expand(function () {
+ var self = Container.call(this);
+ var enemyGraphics = self.attachAsset('enemy', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.health = 20;
+ self.fireRate = 60; // Frames between shots
+ self.lastShot = 0;
+ self.moveDirection = 1;
+ self.moveSpeed = 3;
+ self.update = function () {
+ // Move back and forth
+ self.x += self.moveDirection * self.moveSpeed;
+ // Change direction when reaching edges
+ if (self.x <= 500 || self.x >= 1548) {
+ self.moveDirection *= -1;
+ }
+ };
+ self.takeDamage = function (amount) {
+ self.health -= amount;
+ // Flash red when hit
+ tween(enemyGraphics, {
+ tint: 0xFF0000
+ }, {
+ duration: 200,
+ onFinish: function onFinish() {
+ tween(enemyGraphics, {
+ tint: 0xFFFFFF
+ }, {
+ duration: 200
+ });
+ }
+ });
+ return self.health <= 0;
+ };
+ return self;
+});
+var EspressoShot = Container.expand(function () {
+ var self = Container.call(this);
+ var shotGraphics = self.attachAsset('espressoShot', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -10;
+ self.power = 1;
+ self.update = function () {
+ self.y += self.speed;
+ };
+ return self;
+});
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.health = 3;
+ self.fireRate = 30; // Frames between shots
+ self.lastShot = 0;
+ self.shotPower = 1;
+ self.moveSpeed = 10;
+ // Power-up effects
+ self.powerUpTimeLeft = 0;
+ self.powerUpType = null;
+ self.applyPowerUp = function (type) {
+ self.powerUpType = type;
+ self.powerUpTimeLeft = 300; // 5 seconds (60 fps * 5)
+ if (type === "speed") {
+ self.moveSpeed = 20;
+ playerGraphics.tint = 0xF0D290; // Yellow tint
+ } else if (type === "power") {
+ self.shotPower = 2;
+ playerGraphics.tint = 0xD24726; // Cinnamon color
+ } else if (type === "rapid") {
+ self.fireRate = 10;
+ playerGraphics.tint = 0x5E2605; // Chocolate color
+ }
+ };
+ self.endPowerUp = function () {
+ self.powerUpType = null;
+ self.moveSpeed = 10;
+ self.shotPower = 1;
+ self.fireRate = 30;
+ playerGraphics.tint = 0xFFFFFF; // Reset tint
+ };
+ self.update = function () {
+ // Manage power-up duration
+ if (self.powerUpTimeLeft > 0) {
+ self.powerUpTimeLeft--;
+ if (self.powerUpTimeLeft <= 0) {
+ self.endPowerUp();
+ }
+ }
+ // Keep player on the foam platform
+ if (self.x < 150) {
+ self.x = 150;
+ }
+ if (self.x > 1898) {
+ self.x = 1898;
+ }
+ if (self.y < 1400) {
+ self.y = 1400;
+ }
+ if (self.y > 1700) {
+ self.y = 1700;
+ }
+ };
+ return self;
+});
+var PowerUp = Container.expand(function () {
+ var self = Container.call(this);
+ var powerUpGraphics = self.attachAsset('powerUp', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.type = "speed"; // default type
+ self.active = true;
+ // Randomly choose powerup type
+ self.setRandomType = function () {
+ var types = ["speed", "power", "rapid"];
+ self.type = types[Math.floor(Math.random() * types.length)];
+ // Update color based on type
+ if (self.type === "speed") {
+ powerUpGraphics.tint = 0xF0D290; // Yellow-ish
+ } else if (self.type === "power") {
+ powerUpGraphics.tint = 0xD24726; // Cinnamon color
+ } else if (self.type === "rapid") {
+ powerUpGraphics.tint = 0x5E2605; // Chocolate color
+ }
+ };
+ self.update = function () {
+ self.y += 2;
+ // Make it wiggle slightly
+ self.x += Math.sin(LK.ticks / 10) * 2;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
+ backgroundColor: 0x7A5230 // Coffee brown background
+});
+
+/****
+* Game Code
+****/
+// Create game arena (giant coffee cup)
+var coffeeArena = game.addChild(LK.getAsset('coffeeArena', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 / 2,
+ y: 1600
+}));
+// Create foam platform
+var coffeeFoam = game.addChild(LK.getAsset('coffeeFoam', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 / 2,
+ y: 1550
+}));
+// Create player
+var player = new Player();
+player.x = 2048 / 2;
+player.y = 1500;
+game.addChild(player);
+// Create enemy
+var enemy = new Enemy();
+enemy.x = 2048 / 2;
+enemy.y = 800;
+game.addChild(enemy);
+// Game elements arrays
+var espressoShots = [];
+var badCoffees = [];
+var powerUps = [];
+// Score display
+var scoreTxt = new Text2('0', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+// Health display
+var healthTxt = new Text2('Health: 3', {
+ size: 60,
+ fill: 0xFFFFFF
+});
+healthTxt.anchor.set(0, 0);
+healthTxt.x = 140;
+healthTxt.y = 20;
+LK.gui.topRight.addChild(healthTxt);
+// Game state management
+var gameActive = true;
+var dragNode = null;
+var powerUpTimer = 0;
+// Spawn a power-up
+function spawnPowerUp() {
+ var powerUp = new PowerUp();
+ powerUp.setRandomType();
+ powerUp.x = 400 + Math.random() * 1248; // Random position across arena width
+ powerUp.y = 400; // Start from top area
+ powerUps.push(powerUp);
+ game.addChild(powerUp);
+}
+// Handle player movement
+function handleMove(x, y, obj) {
+ if (dragNode && gameActive) {
+ // Calculate new position based on movement
+ dragNode.x = x;
+ // Keep player on foam platform
+ if (dragNode.x < 150) {
+ dragNode.x = 150;
+ }
+ if (dragNode.x > 1898) {
+ dragNode.x = 1898;
+ }
+ }
+}
+// Shoot espresso
+function shootEspresso() {
+ if (!gameActive) {
+ return;
+ }
+ if (LK.ticks - player.lastShot >= player.fireRate) {
+ var shot = new EspressoShot();
+ shot.x = player.x;
+ shot.y = player.y - 40;
+ shot.power = player.shotPower;
+ espressoShots.push(shot);
+ game.addChild(shot);
+ player.lastShot = LK.ticks;
+ // Play shoot sound
+ LK.getSound('shoot').play();
+ }
+}
+// Enemy fires bad coffee
+function enemyShoot() {
+ if (!gameActive) {
+ return;
+ }
+ if (LK.ticks - enemy.lastShot >= enemy.fireRate) {
+ var badCoffee = new BadCoffee();
+ badCoffee.x = enemy.x;
+ badCoffee.y = enemy.y + 50;
+ badCoffees.push(badCoffee);
+ game.addChild(badCoffee);
+ enemy.lastShot = LK.ticks;
+ }
+}
+// Update player health display
+function updateHealthDisplay() {
+ healthTxt.setText('Health: ' + player.health);
+}
+// Mouse/Touch Event Handlers
+game.move = handleMove;
+game.down = function (x, y, obj) {
+ dragNode = player;
+ shootEspresso();
+ handleMove(x, y, obj);
+};
+game.up = function (x, y, obj) {
+ dragNode = null;
+};
+// Game update loop
+game.update = function () {
+ if (!gameActive) {
+ return;
+ }
+ // Enemy shooting
+ enemyShoot();
+ // Power-up generation
+ powerUpTimer++;
+ if (powerUpTimer >= 300) {
+ // Every 5 seconds
+ spawnPowerUp();
+ powerUpTimer = 0;
+ }
+ // Update espresso shots
+ for (var i = espressoShots.length - 1; i >= 0; i--) {
+ var shot = espressoShots[i];
+ // Check if shot is off screen
+ if (shot.y < -50) {
+ shot.destroy();
+ espressoShots.splice(i, 1);
+ continue;
+ }
+ // Check for collision with enemy
+ if (shot.intersects(enemy)) {
+ // Deal damage to enemy
+ var defeated = enemy.takeDamage(shot.power);
+ if (defeated) {
+ // Win condition
+ LK.setScore(LK.getScore() + 100); // Bonus for defeating enemy
+ scoreTxt.setText(LK.getScore());
+ LK.showYouWin();
+ gameActive = false;
+ } else {
+ // Add score
+ LK.setScore(LK.getScore() + 10);
+ scoreTxt.setText(LK.getScore());
+ // Play hit sound
+ LK.getSound('hit').play();
+ }
+ // Remove shot
+ shot.destroy();
+ espressoShots.splice(i, 1);
+ }
+ }
+ // Update bad coffees
+ for (var j = badCoffees.length - 1; j >= 0; j--) {
+ var coffee = badCoffees[j];
+ // Check if coffee is off screen
+ if (coffee.y > 2800) {
+ coffee.destroy();
+ badCoffees.splice(j, 1);
+ continue;
+ }
+ // Check for collision with player
+ if (coffee.intersects(player)) {
+ // Reduce player health
+ player.health -= coffee.damage;
+ updateHealthDisplay();
+ // Check for game over
+ if (player.health <= 0) {
+ LK.effects.flashScreen(0xFF0000, 1000);
+ LK.showGameOver();
+ gameActive = false;
+ }
+ // Remove coffee
+ coffee.destroy();
+ badCoffees.splice(j, 1);
+ }
+ }
+ // Update power-ups
+ for (var k = powerUps.length - 1; k >= 0; k--) {
+ var powerUp = powerUps[k];
+ // Check if power-up is off screen
+ if (powerUp.y > 2800) {
+ powerUp.destroy();
+ powerUps.splice(k, 1);
+ continue;
+ }
+ // Check for collision with player
+ if (powerUp.active && powerUp.intersects(player)) {
+ // Apply power-up effect
+ player.applyPowerUp(powerUp.type);
+ // Play power-up sound
+ LK.getSound('powerup').play();
+ // Remove power-up
+ powerUp.destroy();
+ powerUps.splice(k, 1);
+ }
+ }
+};
+// Start background music
+LK.playMusic('bgMusic', {
+ fade: {
+ start: 0,
+ end: 0.4,
+ duration: 1000
+ }
});
\ No newline at end of file