/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
//Storage library which should be used for persistent game data
// var storage = LK.import('@upit/storage.v1');
//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');
//Classes can only be defined here. You cannot create inline classes in the games code.
var Bullet = Container.expand(function () {
var self = Container.call(this);
//Create and attach asset. This is the same as calling var xxx = self.addChild(LK.getAsset(...))
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
//Set bullet speed
self.speed = 20; // Bullet speed
//If this instance of bullet is attached, this method will be called every tick by the LK engine automatically.
//To not manually call .updated methods. If you want to manually call a method every tick on a class, use a different name than update.
//As .update is called from the LK engine directly, .update cannot have method arguments.
self.update = function () {
self.y -= self.speed; // Move bullet upwards
};
return self; //You must return self if you want other classes to be able to inherit from this class
});
//Make a Character class by using the LK expand method to extend Container.
var PlayerCharacter = Container.expand(function () {
var self = Container.call(this);
//Get and automatically addChild to self asset with id 'character' with the anchor point set to .5, .5
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
return self; //You must return self if you want other classes to be able to inherit from this class
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
//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
// Blue character
// Purple alien
// Yellow bullet
// 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
/*
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
*/
//Init game with black background
//Note game dimensions are 2048x2732
// Global variables
var alien;
var character;
var bullets = [];
var scoreTxt;
var dragNode = null;
var lastCharacterIntersectingAlien = false;
var WIN_SCORE = 50; // Score to win
// Change background color
game.setBackgroundColor(0x1a1a1a); // Dark grey background
// Create and position the alien in the center
alien = game.addChild(LK.getAsset('alien', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
}));
alien.lastIntersectingCharacter = false; // Initialize for intersection tracking
alien.lastIntersectingBullet = false; // Initialize for intersection tracking
// Create and position the player character at the bottom center
character = game.addChild(new PlayerCharacter());
character.x = 2048 / 2;
character.y = 2732 - character.height / 2 - 100; // Position near bottom
character.lastIntersectingAlien = false; // Initialize for intersection tracking
// Create and add score display
scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to handle game move events (dragging character)
function handleMove(x, y, obj) {
if (dragNode) {
// Clamp character position to be within the bottom half of the screen
dragNode.x = Math.max(dragNode.width / 2, Math.min(2048 - dragNode.width / 2, x));
dragNode.y = Math.max(2732 / 2, Math.min(2732 - dragNode.height / 2, y));
}
}
// Mouse or touch down event on the game object
game.down = function (x, y, obj) {
// Check if the press is on the character
var charLocalPos = character.toLocal({
x: x,
y: y
}, game);
if (character.containsPoint(charLocalPos)) {
dragNode = character;
handleMove(x, y, obj); // Call move handler to make it instant
}
};
// Mouse or touch move event on the game object
game.move = handleMove;
// Mouse or touch up event on the game object
game.up = function (x, y, obj) {
dragNode = null;
};
// Ask LK engine to update game every game tick
game.update = function () {
// Update and manage bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// We always keep track of last and current values to detect state changes
if (bullet.lastY === undefined) bullet.lastY = bullet.y; // Initialize lastY for tracking changes on Y
if (bullet.lastIntersectingAlien === undefined) bullet.lastIntersectingAlien = false; // Initialize for intersection tracking
// Off-Screen Detection: Destroy bullet if it goes off the top edge
if (bullet.lastY >= -bullet.height && bullet.y < -bullet.height) {
bullet.destroy(); // Destroy can only be called from the main 'Game' class
bullets.splice(i, 1);
continue; // Skip further checks for this bullet
}
// Intersection Detection: Check for bullet collision with alien
var currentIntersectingAlien = bullet.intersects(alien);
if (!bullet.lastIntersectingAlien && currentIntersectingAlien) {
// Intersection just started
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('hit').play(); // Play hit sound
// Winning condition
if (LK.getScore() >= WIN_SCORE) {
LK.showYouWin(); // Show "you win" and reset
}
bullet.destroy(); // Destroy bullet on hit
bullets.splice(i, 1);
continue; // Skip further checks for this bullet
}
// Update last known states
bullet.lastY = bullet.y;
bullet.lastIntersectingAlien = currentIntersectingAlien;
}
// Check for character collision with alien
var currentCharacterIntersectingAlien = character.intersects(alien);
if (!lastCharacterIntersectingAlien && currentCharacterIntersectingAlien) {
// Collision just started
LK.effects.flashScreen(0xff0000, 1000); // Flash screen red
LK.showGameOver(); // Show game over and reset
}
lastCharacterIntersectingAlien = currentCharacterIntersectingAlien;
// Fire a bullet periodically (every 15 ticks = ~4 times per second)
if (LK.ticks % 15 == 0) {
var newBullet = new Bullet();
// We always keep track of last and current values
newBullet.x = character.x;
newBullet.y = character.y;
newBullet.lastY = newBullet.y; // Initialize lastY for tracking changes on Y
newBullet.lastIntersectingAlien = newBullet.intersects(alien); // Initialize for intersection tracking
bullets.push(newBullet);
game.addChild(newBullet);
LK.getSound('shoot').play(); // Play shoot sound
}
};
// Play background music
LK.playMusic('bgmusic');
// Legacy event model examples (commented out as per instructions)
/*
Lk.on('tick', function(){
//Called every tick.
})
game.on('down', function(x,y,obj){
console.log("Game was clicked at", x,y)
})
*/ ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,221 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+//Storage library which should be used for persistent game data
+// var storage = LK.import('@upit/storage.v1');
+//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');
+//Classes can only be defined here. You cannot create inline classes in the games code.
+var Bullet = Container.expand(function () {
+ var self = Container.call(this);
+ //Create and attach asset. This is the same as calling var xxx = self.addChild(LK.getAsset(...))
+ var bulletGraphics = self.attachAsset('bullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ //Set bullet speed
+ self.speed = 20; // Bullet speed
+ //If this instance of bullet is attached, this method will be called every tick by the LK engine automatically.
+ //To not manually call .updated methods. If you want to manually call a method every tick on a class, use a different name than update.
+ //As .update is called from the LK engine directly, .update cannot have method arguments.
+ self.update = function () {
+ self.y -= self.speed; // Move bullet upwards
+ };
+ return self; //You must return self if you want other classes to be able to inherit from this class
+});
+//Make a Character class by using the LK expand method to extend Container.
+var PlayerCharacter = Container.expand(function () {
+ var self = Container.call(this);
+ //Get and automatically addChild to self asset with id 'character' with the anchor point set to .5, .5
+ var characterGraphics = self.attachAsset('character', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ return self; //You must return self if you want other classes to be able to inherit from this class
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x000000
-});
\ No newline at end of file
+});
+
+/****
+* Game Code
+****/
+//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
+// Blue character
+// Purple alien
+// Yellow bullet
+// 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
+/*
+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
+*/
+//Init game with black background
+//Note game dimensions are 2048x2732
+// Global variables
+var alien;
+var character;
+var bullets = [];
+var scoreTxt;
+var dragNode = null;
+var lastCharacterIntersectingAlien = false;
+var WIN_SCORE = 50; // Score to win
+// Change background color
+game.setBackgroundColor(0x1a1a1a); // Dark grey background
+// Create and position the alien in the center
+alien = game.addChild(LK.getAsset('alien', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 2048 / 2,
+ y: 2732 / 2
+}));
+alien.lastIntersectingCharacter = false; // Initialize for intersection tracking
+alien.lastIntersectingBullet = false; // Initialize for intersection tracking
+// Create and position the player character at the bottom center
+character = game.addChild(new PlayerCharacter());
+character.x = 2048 / 2;
+character.y = 2732 - character.height / 2 - 100; // Position near bottom
+character.lastIntersectingAlien = false; // Initialize for intersection tracking
+// Create and add score display
+scoreTxt = new Text2('0', {
+ size: 150,
+ fill: 0xFFFFFF
+});
+scoreTxt.setText(LK.getScore());
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+// Function to handle game move events (dragging character)
+function handleMove(x, y, obj) {
+ if (dragNode) {
+ // Clamp character position to be within the bottom half of the screen
+ dragNode.x = Math.max(dragNode.width / 2, Math.min(2048 - dragNode.width / 2, x));
+ dragNode.y = Math.max(2732 / 2, Math.min(2732 - dragNode.height / 2, y));
+ }
+}
+// Mouse or touch down event on the game object
+game.down = function (x, y, obj) {
+ // Check if the press is on the character
+ var charLocalPos = character.toLocal({
+ x: x,
+ y: y
+ }, game);
+ if (character.containsPoint(charLocalPos)) {
+ dragNode = character;
+ handleMove(x, y, obj); // Call move handler to make it instant
+ }
+};
+// Mouse or touch move event on the game object
+game.move = handleMove;
+// Mouse or touch up event on the game object
+game.up = function (x, y, obj) {
+ dragNode = null;
+};
+// Ask LK engine to update game every game tick
+game.update = function () {
+ // Update and manage bullets
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ var bullet = bullets[i];
+ // We always keep track of last and current values to detect state changes
+ if (bullet.lastY === undefined) bullet.lastY = bullet.y; // Initialize lastY for tracking changes on Y
+ if (bullet.lastIntersectingAlien === undefined) bullet.lastIntersectingAlien = false; // Initialize for intersection tracking
+ // Off-Screen Detection: Destroy bullet if it goes off the top edge
+ if (bullet.lastY >= -bullet.height && bullet.y < -bullet.height) {
+ bullet.destroy(); // Destroy can only be called from the main 'Game' class
+ bullets.splice(i, 1);
+ continue; // Skip further checks for this bullet
+ }
+ // Intersection Detection: Check for bullet collision with alien
+ var currentIntersectingAlien = bullet.intersects(alien);
+ if (!bullet.lastIntersectingAlien && currentIntersectingAlien) {
+ // Intersection just started
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText(LK.getScore());
+ LK.getSound('hit').play(); // Play hit sound
+ // Winning condition
+ if (LK.getScore() >= WIN_SCORE) {
+ LK.showYouWin(); // Show "you win" and reset
+ }
+ bullet.destroy(); // Destroy bullet on hit
+ bullets.splice(i, 1);
+ continue; // Skip further checks for this bullet
+ }
+ // Update last known states
+ bullet.lastY = bullet.y;
+ bullet.lastIntersectingAlien = currentIntersectingAlien;
+ }
+ // Check for character collision with alien
+ var currentCharacterIntersectingAlien = character.intersects(alien);
+ if (!lastCharacterIntersectingAlien && currentCharacterIntersectingAlien) {
+ // Collision just started
+ LK.effects.flashScreen(0xff0000, 1000); // Flash screen red
+ LK.showGameOver(); // Show game over and reset
+ }
+ lastCharacterIntersectingAlien = currentCharacterIntersectingAlien;
+ // Fire a bullet periodically (every 15 ticks = ~4 times per second)
+ if (LK.ticks % 15 == 0) {
+ var newBullet = new Bullet();
+ // We always keep track of last and current values
+ newBullet.x = character.x;
+ newBullet.y = character.y;
+ newBullet.lastY = newBullet.y; // Initialize lastY for tracking changes on Y
+ newBullet.lastIntersectingAlien = newBullet.intersects(alien); // Initialize for intersection tracking
+ bullets.push(newBullet);
+ game.addChild(newBullet);
+ LK.getSound('shoot').play(); // Play shoot sound
+ }
+};
+// Play background music
+LK.playMusic('bgmusic');
+// Legacy event model examples (commented out as per instructions)
+/*
+Lk.on('tick', function(){
+ //Called every tick.
+})
+game.on('down', function(x,y,obj){
+ console.log("Game was clicked at", x,y)
+})
+*/
\ No newline at end of file
A blue demon alien. In-Game asset. High contrast. No shadows
The character is a little boy that is sad and 17 years old. In-Game asset. 2d. High contrast. No shadows. Kid
Key. In-Game asset. High contrast. No shadows. 2d
Door. In-Game asset. No shadows. 2d
A note that says dear player I ate classmates from alien. In-Game asset. High contrast. No shadows