/**** * Classes ****/ // Class for the laser var Laser = Container.expand(function () { var self = Container.call(this); var laserGraphics = self.attachAsset('laser', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.direction = Math.random() < 0.5 ? 1 : -1; // Random initial direction self.update = function () { // Move the laser self.x += self.speed * self.direction; // Change direction if it hits a wall if (self.x <= 0 || self.x >= 2048) { self.direction *= -1; } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Class for 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.speed = 10; self.update = function () { self.y += self.speed; // Update player's vertical position self.speed += 1; // Apply gravity if (self.y > 2500) { // Prevent player from falling through the ground self.y = 2500; self.speed = 0; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize player var player = game.addChild(new Player()); player.x = 1024; // Center horizontally player.y = 2500; // Near the bottom // Initialize lasers var lasers = []; for (var i = 0; i < 7; i++) { var laser = new Laser(); laser.x = Math.random() * 2048; laser.y = (i + 1) * 300; // Spread lasers vertically lasers.push(laser); game.addChild(laser); } // Score tracking var score = 0; var scoreTxt = new Text2('Score: 0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Handle player movement game.down = function (x, y, obj) { player.x = x; // Allow jump regardless of player's position player.speed = -30; // Apply upward force }; // Update game logic game.update = function () { // Update lasers lasers.forEach(function (laser) { laser.update(); // Check collision with player if (player.intersects(laser)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } }); // Check if player reached the top or bottom if (player.y <= 0 || player.y >= 2732) { score++; scoreTxt.setText('Score: ' + score); player.y = 2500; // Reset player position lasers.forEach(function (laser) { laser.speed += 0.5; // Increase laser speed }); } };
===================================================================
--- original.js
+++ change.js
@@ -1,15 +1,7 @@
/****
* Classes
****/
-// Class for the coin
-var Coin = Container.expand(function () {
- var self = Container.call(this);
- var coinGraphics = self.attachAsset('coin', {
- anchorX: 0.5,
- anchorY: 0.5
- });
-});
// Class for the laser
var Laser = Container.expand(function () {
var self = Container.call(this);
var laserGraphics = self.attachAsset('laser', {
@@ -61,12 +53,8 @@
// Initialize player
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 2500; // Near the bottom
-// Initialize coin
-var coin = game.addChild(new Coin());
-coin.x = 1024; // Center horizontally
-coin.y = 232; // Near the top
// Initialize lasers
var lasers = [];
for (var i = 0; i < 7; i++) {
var laser = new Laser();
@@ -108,13 +96,5 @@
lasers.forEach(function (laser) {
laser.speed += 0.5; // Increase laser speed
});
}
- // Check if player intersects with the coin
- if (player.intersects(coin)) {
- score++;
- scoreTxt.setText('Score: ' + score);
- // Reset coin position
- coin.x = Math.random() * 2048;
- coin.y = Math.random() * 2732;
- }
};
\ No newline at end of file