/**** * Classes ****/ // Player class to represent the player character var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); self.shoot = function () { var newWave = new SoundWave(); newWave.x = self.x; newWave.y = self.y; soundWaves.push(newWave); game.addChild(newWave); // Play shooting sound effect LK.getSound('shoot').play(); }; }); // PuzzlePiece class to represent puzzle pieces in the game var PuzzlePiece = Container.expand(function () { var self = Container.call(this); var pieceGraphics = self.attachAsset('piece', { anchorX: 0.5, anchorY: 0.5 }); var numberText = new Text2('0', { size: 50, fill: "#000000" }); numberText.anchor.set(0.5, 0.5); self.addChild(numberText); self.speed = Math.random() * 3 + 1; // Random speed between 1 and 4 self.interact = function () { // Define interaction behavior var colors = { 0xffff00: 1, // Yellow 0x800080: 2, // Purple 0x0000FF: 3, // Blue 0x00FF00: 4 // Green }; var randomColor = Object.keys(colors)[Math.floor(Math.random() * Object.keys(colors).length)]; pieceGraphics.tint = randomColor; // Change number on the piece based on color numberText.setText(colors[randomColor].toString()); }; }); //<Assets used in the game will automatically appear here> // SoundWave class to represent sound waves in the game var SoundWave = Container.expand(function () { var self = Container.call(this); var waveGraphics = self.attachAsset('wave', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.update = function () { self.y -= self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Play background music LK.playMusic('musicId', { volume: 0.5 }); // Initialize arrays and variables var soundWaves = []; var puzzlePieces = []; var player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 200; // Initialize score display var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(1, 0); LK.gui.topRight.addChild(scoreTxt); // Create puzzle pieces and add them to the game for (var i = 0; i < 10; i++) { var piece = new PuzzlePiece(); piece.x = Math.random() * 2048; piece.y = 200 + i * 300; var colors = { 0xffff00: 1, // Yellow 0x800080: 2, // Purple 0x0000FF: 3, // Blue 0x00FF00: 4 // Green }; var randomColor = Object.keys(colors)[Math.floor(Math.random() * Object.keys(colors).length)]; piece.children[0].tint = randomColor; piece.children[1].setText(colors[randomColor].toString()); puzzlePieces.push(piece); game.addChild(piece); } // Handle player movement game.move = function (x, y, obj) { player.x = x; player.y = y; }; // Handle player shooting game.down = function (x, y, obj) { player.shoot(); }; // Update game state game.update = function () { for (var i = soundWaves.length - 1; i >= 0; i--) { soundWaves[i].update(); for (var j = puzzlePieces.length - 1; j >= 0; j--) { puzzlePieces[j].y += puzzlePieces[j].speed; // Make puzzle pieces fall down based on their speed if (puzzlePieces[j].y > 2732) { puzzlePieces[j].y = 0; // Reset position to top if it goes off screen puzzlePieces[j].x = Math.random() * 2048; // Randomize x position } if (soundWaves[i].intersects(puzzlePieces[j])) { var pieceSpeed = puzzlePieces[j].speed; var scoreIncrement = Math.ceil(pieceSpeed); // Increase score based on the speed of the puzzle piece puzzlePieces[j].interact(); // Update score based on the speed of the puzzle piece LK.setScore(LK.getScore() + scoreIncrement); scoreTxt.setText(LK.getScore()); break; } // Check for game over condition if (player.intersects(puzzlePieces[j])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } if (soundWaves[i] && soundWaves[i].y < -50) { soundWaves[i].destroy(); soundWaves.splice(i, 1); } } };
/****
* Classes
****/
// Player class to represent the player character
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.shoot = function () {
var newWave = new SoundWave();
newWave.x = self.x;
newWave.y = self.y;
soundWaves.push(newWave);
game.addChild(newWave);
// Play shooting sound effect
LK.getSound('shoot').play();
};
});
// PuzzlePiece class to represent puzzle pieces in the game
var PuzzlePiece = Container.expand(function () {
var self = Container.call(this);
var pieceGraphics = self.attachAsset('piece', {
anchorX: 0.5,
anchorY: 0.5
});
var numberText = new Text2('0', {
size: 50,
fill: "#000000"
});
numberText.anchor.set(0.5, 0.5);
self.addChild(numberText);
self.speed = Math.random() * 3 + 1; // Random speed between 1 and 4
self.interact = function () {
// Define interaction behavior
var colors = {
0xffff00: 1,
// Yellow
0x800080: 2,
// Purple
0x0000FF: 3,
// Blue
0x00FF00: 4 // Green
};
var randomColor = Object.keys(colors)[Math.floor(Math.random() * Object.keys(colors).length)];
pieceGraphics.tint = randomColor;
// Change number on the piece based on color
numberText.setText(colors[randomColor].toString());
};
});
//<Assets used in the game will automatically appear here>
// SoundWave class to represent sound waves in the game
var SoundWave = Container.expand(function () {
var self = Container.call(this);
var waveGraphics = self.attachAsset('wave', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y -= self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Play background music
LK.playMusic('musicId', {
volume: 0.5
});
// Initialize arrays and variables
var soundWaves = [];
var puzzlePieces = [];
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreTxt);
// Create puzzle pieces and add them to the game
for (var i = 0; i < 10; i++) {
var piece = new PuzzlePiece();
piece.x = Math.random() * 2048;
piece.y = 200 + i * 300;
var colors = {
0xffff00: 1,
// Yellow
0x800080: 2,
// Purple
0x0000FF: 3,
// Blue
0x00FF00: 4 // Green
};
var randomColor = Object.keys(colors)[Math.floor(Math.random() * Object.keys(colors).length)];
piece.children[0].tint = randomColor;
piece.children[1].setText(colors[randomColor].toString());
puzzlePieces.push(piece);
game.addChild(piece);
}
// Handle player movement
game.move = function (x, y, obj) {
player.x = x;
player.y = y;
};
// Handle player shooting
game.down = function (x, y, obj) {
player.shoot();
};
// Update game state
game.update = function () {
for (var i = soundWaves.length - 1; i >= 0; i--) {
soundWaves[i].update();
for (var j = puzzlePieces.length - 1; j >= 0; j--) {
puzzlePieces[j].y += puzzlePieces[j].speed; // Make puzzle pieces fall down based on their speed
if (puzzlePieces[j].y > 2732) {
puzzlePieces[j].y = 0; // Reset position to top if it goes off screen
puzzlePieces[j].x = Math.random() * 2048; // Randomize x position
}
if (soundWaves[i].intersects(puzzlePieces[j])) {
var pieceSpeed = puzzlePieces[j].speed;
var scoreIncrement = Math.ceil(pieceSpeed); // Increase score based on the speed of the puzzle piece
puzzlePieces[j].interact();
// Update score based on the speed of the puzzle piece
LK.setScore(LK.getScore() + scoreIncrement);
scoreTxt.setText(LK.getScore());
break;
}
// Check for game over condition
if (player.intersects(puzzlePieces[j])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
if (soundWaves[i] && soundWaves[i].y < -50) {
soundWaves[i].destroy();
soundWaves.splice(i, 1);
}
}
};