/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Ball class to represent the bouncing ball var Ball = Container.expand(function () { var self = Container.call(this); var ballGraphics = self.attachAsset('ball', { anchorX: 0.5, anchorY: 0.5 }); self.vx = 0; // Velocity in x self.vy = 0; // Velocity in y self.gravity = 0.5; // Gravity effect self.elasticity = 0.8; // Bounciness self.update = function () { self.vy += self.gravity; // Apply gravity self.x += self.vx; self.y += self.vy; // Check for collision with walls if (self.x - ballGraphics.width / 2 < 0 || self.x + ballGraphics.width / 2 > 2048) { self.vx *= -self.elasticity; // Reverse and reduce velocity self.x = Math.max(ballGraphics.width / 2, Math.min(2048 - ballGraphics.width / 2, self.x)); } if (self.y + ballGraphics.height / 2 > 2732) { self.vy *= -self.elasticity; // Reverse and reduce velocity self.y = 2732 - ballGraphics.height / 2; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * 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. // 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 */ //Note game dimensions are 2048x2732 //Change background color game.setBackgroundColor(0x008080); //Change background color to 0x008080 // Create and position the ball var ball = game.addChild(new Ball()); ball.x = 1024; // Center horizontally ball.y = 100; // Start near the top // Create walls var topWall = LK.getAsset('wall', { x: 0, y: 0 }); var bottomWall = LK.getAsset('wall', { x: 0, y: 2732 - 50 }); game.addChild(topWall); game.addChild(bottomWall); // Handle game updates game.update = function () { ball.update(); }; // Handle touch/mouse events to adjust ball velocity game.down = function (x, y, obj) { ball.vx = (x - ball.x) * 0.1; // Adjust velocity based on touch position ball.vy = (y - ball.y) * 0.1; };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Ball class to represent the bouncing ball
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGraphics = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
self.vx = 0; // Velocity in x
self.vy = 0; // Velocity in y
self.gravity = 0.5; // Gravity effect
self.elasticity = 0.8; // Bounciness
self.update = function () {
self.vy += self.gravity; // Apply gravity
self.x += self.vx;
self.y += self.vy;
// Check for collision with walls
if (self.x - ballGraphics.width / 2 < 0 || self.x + ballGraphics.width / 2 > 2048) {
self.vx *= -self.elasticity; // Reverse and reduce velocity
self.x = Math.max(ballGraphics.width / 2, Math.min(2048 - ballGraphics.width / 2, self.x));
}
if (self.y + ballGraphics.height / 2 > 2732) {
self.vy *= -self.elasticity; // Reverse and reduce velocity
self.y = 2732 - ballGraphics.height / 2;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* 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.
// 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
*/
//Note game dimensions are 2048x2732
//Change background color
game.setBackgroundColor(0x008080); //Change background color to 0x008080
// Create and position the ball
var ball = game.addChild(new Ball());
ball.x = 1024; // Center horizontally
ball.y = 100; // Start near the top
// Create walls
var topWall = LK.getAsset('wall', {
x: 0,
y: 0
});
var bottomWall = LK.getAsset('wall', {
x: 0,
y: 2732 - 50
});
game.addChild(topWall);
game.addChild(bottomWall);
// Handle game updates
game.update = function () {
ball.update();
};
// Handle touch/mouse events to adjust ball velocity
game.down = function (x, y, obj) {
ball.vx = (x - ball.x) * 0.1; // Adjust velocity based on touch position
ball.vy = (y - ball.y) * 0.1;
};