/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Coin = Container.expand(function () { var self = Container.call(this); self.attachAsset('coin', { anchorX: 0.5, anchorY: 0.5 }); return self; }); var Obstacle = Container.expand(function () { var self = Container.call(this); self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); return self; }); //Library for using the camera (the background becomes the user's camera video feed) and the microphone. It can access face coordinates for interactive play, as well detect microphone volume / voice interactions // var facekit = LK.import('@upit/facekit.v1'); // Not needed for this game //Classes can only be defined here. You cannot create inline classes in the games code. var Player = Container.expand(function () { var self = Container.call(this); self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x90EE90 // Light green background (representing the garden) }); /**** * Game Code ****/ // Note game dimensions are 2048x2732 /* Supported Types: 1. Shape: - Simple geometric figures with these properties: * width: (required) pixel width of the shape. * height: (required) pixel height of the shape. * color: (required) color of the shape. * shape: (required) type of shape. Valid options: 'box', 'ellipse'. 2. Image: - Imported images with these properties: * width: (required) pixel resolution width. * height: (required) pixel resolution height. * id: (required) identifier for the image. * flipX: (optional) horizontal flip. Valid values: 0 (no flip), 1 (flip). * flipY: (optional) vertical flip. Valid values: 0 (no flip), 1 (flip). * orientation: (optional) rotation in multiples of 90 degrees, clockwise. Valid values: - 0: No rotation. - 1: Rotate 90 degrees. - 2: Rotate 180 degrees. - 3: Rotate 270 degrees. Note: Width and height remain unchanged upon flipping. 3. Sound: - Sound effects with these properties: * id: (required) identifier for the sound. * volume: (optional) custom volume. Valid values are a float from 0 to 1. 4. Music: - In contract to sound effects, only one music can be played at a time - Music is using the same API to initilize just like sound. - Music loops by default - Music with these config options: * id: (required) identifier for the sound. * volume: (optional) custom volume. Valid values are a float from 0 to 1. * start: (optional) a float from 0 to 1 used for cropping and indicates the start of the cropping * end: (optional) a float from 0 to 1 used for cropping and indicates the end of the cropping */ // Add garden background image to the game scene var gardenBg = LK.getAsset('gardenBg', { anchorX: 0, anchorY: 0, x: 0, y: 0 }); game.addChild(gardenBg); // Assets are automatically created and loaded either dynamically during gameplay // or via static code analysis based on their usage in the code. // Initialize assets used in this game. Scale them according to what is needed for the game. // Lime green player (representing the gatherer) // Gold coin // Brown obstacle (rock or bush) // Initialize music //We have access to the following plugins. (Note that the variable names used are mandetory for each plugin) //Only include the plugins you need to create the game. //Minimalistic tween library which should be used for animations over time, including tinting / colouring an object, scaling, rotating, or changing any game object property. //Storage library which should be used for persistent game data var player; var coins = []; var obstacles = []; var scoreTxt; var gameTimer; var timeRemaining = 60; // seconds var timerTxt; var spawnTimer; // Declare method for handling move events on game. function handleMove(x, y, obj) { // Get event position in relation to game object if (player) { player.x = x; player.y = y; } } // Mouse or touch move on game object. game.move = handleMove; // Mouse or touch down on game object game.down = function (x, y, obj) { // Set player position on down to allow instant movement handleMove(x, y, obj); }; // Initialize game elements player = game.addChild(new Player()); player.x = 2048 / 2; player.y = 2732 - 100; // Start near the bottom scoreTxt = new Text2('Score: 0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); timerTxt = new Text2('Time: ' + timeRemaining, { size: 80, fill: 0xFFFFFF }); timerTxt.anchor.set(0.5, 0); // Position timer text to the right of the score text (adjust as needed) timerTxt.x = LK.gui.top.width / 2 + scoreTxt.width / 2 + 50; LK.gui.top.addChild(timerTxt); // Function to spawn a random coin function spawnCoin() { var newCoin = new Coin(); newCoin.x = Math.random() * (2048 - newCoin.width) + newCoin.width / 2; // Avoid spawning too close to the top menu area (adjust 150 as needed) newCoin.y = Math.random() * (2732 - newCoin.height - 150) + newCoin.height / 2 + 150; coins.push(newCoin); game.addChild(newCoin); } // Function to spawn a random obstacle function spawnObstacle() { var newObstacle = new Obstacle(); newObstacle.x = Math.random() * (2048 - newObstacle.width) + newObstacle.width / 2; // Avoid spawning too close to the top menu area (adjust 150 as needed) newObstacle.y = Math.random() * (2732 - newObstacle.height - 150) + newObstacle.height / 2 + 150; obstacles.push(newObstacle); game.addChild(newObstacle); } // Start spawning coins and obstacles periodically spawnTimer = LK.setInterval(function () { spawnCoin(); if (Math.random() < 0.3) { // 30% chance to spawn an obstacle spawnObstacle(); } }, 1000); // Spawn every 1 second // Game timer gameTimer = LK.setInterval(function () { timeRemaining--; timerTxt.setText('Time: ' + timeRemaining); if (timeRemaining <= 0) { LK.clearInterval(gameTimer); LK.clearInterval(spawnTimer); LK.showGameOver(); // Game over when time runs out } }, 1000); // Decrement timer every 1 second // Ask LK engine to update game every game tick game.update = function () { // Collision detection for coins for (var i = coins.length - 1; i >= 0; i--) { var coin = coins[i]; if (player.intersects(coin)) { LK.setScore(LK.getScore() + 10); // Increase score scoreTxt.setText('Score: ' + LK.getScore()); LK.getSound('collectCoin').play(); coin.destroy(); coins.splice(i, 1); } } // Collision detection for obstacles for (var j = obstacles.length - 1; j >= 0; j--) { var obstacle = obstacles[j]; if (player.intersects(obstacle)) { LK.getSound('hitObstacle').play(); LK.effects.flashScreen(0xFF0000, 500); // Flash screen red LK.showGameOver(); // Game over on hitting obstacle break; // Exit loop as game is over } } }; // Play music track LK.playMusic('gardenMusic');
===================================================================
--- original.js
+++ change.js
@@ -44,55 +44,63 @@
/****
* Game Code
****/
-//Storage library which should be used for persistent game data
-//Minimalistic tween library which should be used for animations over time, including tinting / colouring an object, scaling, rotating, or changing any game object property.
-//Only include the plugins you need to create the game.
-//We have access to the following plugins. (Note that the variable names used are mandetory for each plugin)
-// Initialize music
-// Brown obstacle (rock or bush)
-// Gold coin
-// Lime green player (representing the gatherer)
-// Initialize assets used in this game. Scale them according to what is needed for the game.
-// or via static code analysis based on their usage in the code.
-// Assets are automatically created and loaded either dynamically during gameplay
-/*
+// Note game dimensions are 2048x2732
+/*
Supported Types:
1. Shape:
- - Simple geometric figures with these properties:
- * width: (required) pixel width of the shape.
- * height: (required) pixel height of the shape.
- * color: (required) color of the shape.
- * shape: (required) type of shape. Valid options: 'box', 'ellipse'.
+- Simple geometric figures with these properties:
+* width: (required) pixel width of the shape.
+* height: (required) pixel height of the shape.
+* color: (required) color of the shape.
+* shape: (required) type of shape. Valid options: 'box', 'ellipse'.
2. Image:
- - Imported images with these properties:
- * width: (required) pixel resolution width.
- * height: (required) pixel resolution height.
- * id: (required) identifier for the image.
- * flipX: (optional) horizontal flip. Valid values: 0 (no flip), 1 (flip).
- * flipY: (optional) vertical flip. Valid values: 0 (no flip), 1 (flip).
- * orientation: (optional) rotation in multiples of 90 degrees, clockwise. Valid values:
- - 0: No rotation.
- - 1: Rotate 90 degrees.
- - 2: Rotate 180 degrees.
- - 3: Rotate 270 degrees.
- Note: Width and height remain unchanged upon flipping.
+- Imported images with these properties:
+* width: (required) pixel resolution width.
+* height: (required) pixel resolution height.
+* id: (required) identifier for the image.
+* flipX: (optional) horizontal flip. Valid values: 0 (no flip), 1 (flip).
+* flipY: (optional) vertical flip. Valid values: 0 (no flip), 1 (flip).
+* orientation: (optional) rotation in multiples of 90 degrees, clockwise. Valid values:
+- 0: No rotation.
+- 1: Rotate 90 degrees.
+- 2: Rotate 180 degrees.
+- 3: Rotate 270 degrees.
+Note: Width and height remain unchanged upon flipping.
3. Sound:
- - Sound effects with these properties:
- * id: (required) identifier for the sound.
- * volume: (optional) custom volume. Valid values are a float from 0 to 1.
+- Sound effects with these properties:
+* id: (required) identifier for the sound.
+* volume: (optional) custom volume. Valid values are a float from 0 to 1.
4. Music:
- In contract to sound effects, only one music can be played at a time
- Music is using the same API to initilize just like sound.
- Music loops by default
- Music with these config options:
- * id: (required) identifier for the sound.
- * volume: (optional) custom volume. Valid values are a float from 0 to 1.
- * start: (optional) a float from 0 to 1 used for cropping and indicates the start of the cropping
- * end: (optional) a float from 0 to 1 used for cropping and indicates the end of the cropping
+* id: (required) identifier for the sound.
+* volume: (optional) custom volume. Valid values are a float from 0 to 1.
+* start: (optional) a float from 0 to 1 used for cropping and indicates the start of the cropping
+* end: (optional) a float from 0 to 1 used for cropping and indicates the end of the cropping
*/
-// Note game dimensions are 2048x2732
+// Add garden background image to the game scene
+var gardenBg = LK.getAsset('gardenBg', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: 0
+});
+game.addChild(gardenBg);
+// Assets are automatically created and loaded either dynamically during gameplay
+// or via static code analysis based on their usage in the code.
+// Initialize assets used in this game. Scale them according to what is needed for the game.
+// Lime green player (representing the gatherer)
+// Gold coin
+// Brown obstacle (rock or bush)
+// Initialize music
+//We have access to the following plugins. (Note that the variable names used are mandetory for each plugin)
+//Only include the plugins you need to create the game.
+//Minimalistic tween library which should be used for animations over time, including tinting / colouring an object, scaling, rotating, or changing any game object property.
+//Storage library which should be used for persistent game data
var player;
var coins = [];
var obstacles = [];
var scoreTxt;