/****
* Classes
****/
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGfx = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
// Ball velocity
self.vx = 13;
self.vy = 15;
// Ball update: move and bounce
self.update = function () {
self.x += self.vx;
self.y += self.vy;
// Bounce off left/right
if (self.x - self.width / 2 <= 0 && self.vx < 0) {
self.x = self.width / 2;
self.vx *= -1;
}
if (self.x + self.width / 2 >= 2048 && self.vx > 0) {
self.x = 2048 - self.width / 2;
self.vx *= -1;
}
// Bounce off top/bottom
if (self.y - self.height / 2 <= 0 && self.vy < 0) {
self.y = self.height / 2;
self.vy *= -1;
}
if (self.y + self.height / 2 >= 2732 && self.vy > 0) {
self.y = 2732 - self.height / 2;
self.vy *= -1;
}
};
return self;
});
// Directional Button class
var DirButton = Container.expand(function () {
var self = Container.call(this);
// assetId: btn_up, btn_down, btn_left, btn_right
// arrowText: "↑", "↓", "←", "→"
self.init = function (assetId, arrowText) {
var btnGfx = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
var arrow = new Text2(arrowText, {
size: 110,
fill: 0xFFFFFF
});
arrow.anchor.set(0.5, 0.5);
arrow.x = 0;
arrow.y = 0;
self.addChild(arrow);
};
return self;
});
// No plugins needed for MVP
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGfx = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
// For collision, use self.width/self.height
self.speed = 18; // Movement speed per tick when button held
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf7f7f7
});
/****
* Game Code
****/
// Center play area
// Player character: blue box
// Ball: red ellipse
// Directional buttons: gray boxes with arrows
var playWidth = 2048;
var playHeight = 2732;
// Create player
var player = new Player();
game.addChild(player);
player.x = playWidth / 2;
player.y = playHeight - 400;
// Create balls array and spawn initial ball
var balls = [];
function spawnBall(x, y, vx, vy) {
var b = new Ball();
game.addChild(b);
b.x = x;
b.y = y;
// Optionally randomize velocity for new balls
if (typeof vx === "number") b.vx = vx;
if (typeof vy === "number") b.vy = vy;
// Track lastX/lastY for bounce detection
b.lastX = b.x;
b.lastY = b.y;
balls.push(b);
return b;
}
// Spawn the first ball in the center
spawnBall(playWidth / 2, playHeight / 2);
// Track which directions are pressed
var moveDir = {
up: false,
down: false,
left: false,
right: false
};
// Create directional buttons and add to GUI
var btnSize = 180;
var btnMargin = 60;
var btnY = LK.gui.height - btnSize / 2 - btnMargin; // bottom row
var btnXCenter = LK.gui.width / 2;
var btns = {};
// Up button
btns.up = new DirButton();
btns.up.init('btn_up', "↑");
btns.up.x = btnXCenter;
btns.up.y = LK.gui.height - btnSize * 2 - btnMargin * 2;
LK.gui.addChild(btns.up);
// Down button
btns.down = new DirButton();
btns.down.init('btn_down', "↓");
btns.down.x = btnXCenter;
btns.down.y = btnY;
LK.gui.addChild(btns.down);
// Left button
btns.left = new DirButton();
btns.left.init('btn_left', "←");
btns.left.x = btnXCenter - btnSize - btnMargin;
btns.left.y = LK.gui.height - btnSize - btnMargin;
LK.gui.addChild(btns.left);
// Right button
btns.right = new DirButton();
btns.right.init('btn_right', "→");
btns.right.x = btnXCenter + btnSize + btnMargin;
btns.right.y = LK.gui.height - btnSize - btnMargin;
LK.gui.addChild(btns.right);
// Button event handlers
function btnDown(dir) {
moveDir[dir] = true;
}
function btnUp(dir) {
moveDir[dir] = false;
}
// Attach events to buttons
btns.up.down = function (x, y, obj) {
btnDown('up');
};
btns.up.up = function (x, y, obj) {
btnUp('up');
};
btns.down.down = function (x, y, obj) {
btnDown('down');
};
btns.down.up = function (x, y, obj) {
btnUp('down');
};
btns.left.down = function (x, y, obj) {
btnDown('left');
};
btns.left.up = function (x, y, obj) {
btnUp('left');
};
btns.right.down = function (x, y, obj) {
btnDown('right');
};
btns.right.up = function (x, y, obj) {
btnUp('right');
};
// Timer text (shows survival time)
var timerTxt = new Text2("0.00", {
size: 110,
fill: 0x222222
});
timerTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(timerTxt);
// Timer variables
var startTime = Date.now();
var lastSurvival = 0;
// Game update
game.update = function () {
// Move player
var dx = 0,
dy = 0;
if (moveDir.up) dy -= 1;
if (moveDir.down) dy += 1;
if (moveDir.left) dx -= 1;
if (moveDir.right) dx += 1;
if (dx !== 0 || dy !== 0) {
// Normalize diagonal movement
var mag = Math.sqrt(dx * dx + dy * dy);
dx /= mag;
dy /= mag;
player.x += dx * player.speed;
player.y += dy * player.speed;
// Clamp to play area
var halfW = player.width / 2,
halfH = player.height / 2;
if (player.x < halfW) player.x = halfW;
if (player.x > playWidth - halfW) player.x = playWidth - halfW;
if (player.y < halfH) player.y = halfH;
if (player.y > playHeight - halfH) player.y = playHeight - halfH;
}
// Update all balls and handle bounces/collisions
for (var i = 0; i < balls.length; ++i) {
var b = balls[i];
// Store previous position for bounce detection
if (typeof b.lastX !== "number") b.lastX = b.x;
if (typeof b.lastY !== "number") b.lastY = b.y;
// Save old velocities to detect bounce
var bounced = false;
// Check for bounce on left
if (b.lastX - b.width / 2 > 0 && b.x - b.width / 2 <= 0 && b.vx < 0) bounced = true;
// Check for bounce on right
if (b.lastX + b.width / 2 < playWidth && b.x + b.width / 2 >= playWidth && b.vx > 0) bounced = true;
// Check for bounce on top
if (b.lastY - b.height / 2 > 0 && b.y - b.height / 2 <= 0 && b.vy < 0) bounced = true;
// Check for bounce on bottom
if (b.lastY + b.height / 2 < playHeight && b.y + b.height / 2 >= playHeight && b.vy > 0) bounced = true;
b.update();
// If bounced, spawn a new ball at this position with random velocity
if (bounced) {
// Randomize direction and speed a bit for new balls
var angle = Math.random() * Math.PI * 2;
var speed = 12 + Math.random() * 8;
var vx = Math.cos(angle) * speed;
var vy = Math.sin(angle) * speed;
spawnBall(b.x, b.y, vx, vy);
}
// Collision: player & this ball
if (player.intersects(b)) {
LK.effects.flashScreen(0xff0000, 800);
LK.showGameOver();
return;
}
// Update lastX/lastY for next frame
b.lastX = b.x;
b.lastY = b.y;
}
// Update timer
var now = Date.now();
var survival = (now - startTime) / 1000;
if (Math.floor(survival * 100) !== Math.floor(lastSurvival * 100)) {
timerTxt.setText(survival.toFixed(2));
lastSurvival = survival;
}
};
// Create spawn button
var spawnBtn = new Container();
var spawnBtnGfx = spawnBtn.attachAsset('btn_spawn', {
anchorX: 0.5,
anchorY: 0.5
});
var spawnBtnText = new Text2("SPAWN", {
size: 40,
fill: 0xFFFFFF
});
spawnBtnText.anchor.set(0.5, 0.5);
spawnBtnText.x = 0;
spawnBtnText.y = 0;
spawnBtn.addChild(spawnBtnText);
spawnBtn.x = LK.gui.width - 120;
spawnBtn.y = 80;
LK.gui.addChild(spawnBtn);
// Spawn button event handlers
spawnBtn.down = function (x, y, obj) {
// Spawn a new ball at random position with random velocity
var randomX = 200 + Math.random() * (playWidth - 400);
var randomY = 200 + Math.random() * (playHeight - 400);
var angle = Math.random() * Math.PI * 2;
var speed = 10 + Math.random() * 10;
var vx = Math.cos(angle) * speed;
var vy = Math.sin(angle) * speed;
spawnBall(randomX, randomY, vx, vy);
};
// Reset timer on game start
startTime = Date.now();
lastSurvival = 0;
timerTxt.setText("0.00");
; /****
* Classes
****/
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
var ballGfx = self.attachAsset('ball', {
anchorX: 0.5,
anchorY: 0.5
});
// Ball velocity
self.vx = 13;
self.vy = 15;
// Ball update: move and bounce
self.update = function () {
self.x += self.vx;
self.y += self.vy;
// Bounce off left/right
if (self.x - self.width / 2 <= 0 && self.vx < 0) {
self.x = self.width / 2;
self.vx *= -1;
}
if (self.x + self.width / 2 >= 2048 && self.vx > 0) {
self.x = 2048 - self.width / 2;
self.vx *= -1;
}
// Bounce off top/bottom
if (self.y - self.height / 2 <= 0 && self.vy < 0) {
self.y = self.height / 2;
self.vy *= -1;
}
if (self.y + self.height / 2 >= 2732 && self.vy > 0) {
self.y = 2732 - self.height / 2;
self.vy *= -1;
}
};
return self;
});
// Directional Button class
var DirButton = Container.expand(function () {
var self = Container.call(this);
// assetId: btn_up, btn_down, btn_left, btn_right
// arrowText: "↑", "↓", "←", "→"
self.init = function (assetId, arrowText) {
var btnGfx = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
var arrow = new Text2(arrowText, {
size: 110,
fill: 0xFFFFFF
});
arrow.anchor.set(0.5, 0.5);
arrow.x = 0;
arrow.y = 0;
self.addChild(arrow);
};
return self;
});
// No plugins needed for MVP
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGfx = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
// For collision, use self.width/self.height
self.speed = 18; // Movement speed per tick when button held
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xf7f7f7
});
/****
* Game Code
****/
// Center play area
// Player character: blue box
// Ball: red ellipse
// Directional buttons: gray boxes with arrows
var playWidth = 2048;
var playHeight = 2732;
// Create player
var player = new Player();
game.addChild(player);
player.x = playWidth / 2;
player.y = playHeight - 400;
// Create balls array and spawn initial ball
var balls = [];
function spawnBall(x, y, vx, vy) {
var b = new Ball();
game.addChild(b);
b.x = x;
b.y = y;
// Optionally randomize velocity for new balls
if (typeof vx === "number") b.vx = vx;
if (typeof vy === "number") b.vy = vy;
// Track lastX/lastY for bounce detection
b.lastX = b.x;
b.lastY = b.y;
balls.push(b);
return b;
}
// Spawn the first ball in the center
spawnBall(playWidth / 2, playHeight / 2);
// Track which directions are pressed
var moveDir = {
up: false,
down: false,
left: false,
right: false
};
// Create directional buttons and add to GUI
var btnSize = 180;
var btnMargin = 60;
var btnY = LK.gui.height - btnSize / 2 - btnMargin; // bottom row
var btnXCenter = LK.gui.width / 2;
var btns = {};
// Up button
btns.up = new DirButton();
btns.up.init('btn_up', "↑");
btns.up.x = btnXCenter;
btns.up.y = LK.gui.height - btnSize * 2 - btnMargin * 2;
LK.gui.addChild(btns.up);
// Down button
btns.down = new DirButton();
btns.down.init('btn_down', "↓");
btns.down.x = btnXCenter;
btns.down.y = btnY;
LK.gui.addChild(btns.down);
// Left button
btns.left = new DirButton();
btns.left.init('btn_left', "←");
btns.left.x = btnXCenter - btnSize - btnMargin;
btns.left.y = LK.gui.height - btnSize - btnMargin;
LK.gui.addChild(btns.left);
// Right button
btns.right = new DirButton();
btns.right.init('btn_right', "→");
btns.right.x = btnXCenter + btnSize + btnMargin;
btns.right.y = LK.gui.height - btnSize - btnMargin;
LK.gui.addChild(btns.right);
// Button event handlers
function btnDown(dir) {
moveDir[dir] = true;
}
function btnUp(dir) {
moveDir[dir] = false;
}
// Attach events to buttons
btns.up.down = function (x, y, obj) {
btnDown('up');
};
btns.up.up = function (x, y, obj) {
btnUp('up');
};
btns.down.down = function (x, y, obj) {
btnDown('down');
};
btns.down.up = function (x, y, obj) {
btnUp('down');
};
btns.left.down = function (x, y, obj) {
btnDown('left');
};
btns.left.up = function (x, y, obj) {
btnUp('left');
};
btns.right.down = function (x, y, obj) {
btnDown('right');
};
btns.right.up = function (x, y, obj) {
btnUp('right');
};
// Timer text (shows survival time)
var timerTxt = new Text2("0.00", {
size: 110,
fill: 0x222222
});
timerTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(timerTxt);
// Timer variables
var startTime = Date.now();
var lastSurvival = 0;
// Game update
game.update = function () {
// Move player
var dx = 0,
dy = 0;
if (moveDir.up) dy -= 1;
if (moveDir.down) dy += 1;
if (moveDir.left) dx -= 1;
if (moveDir.right) dx += 1;
if (dx !== 0 || dy !== 0) {
// Normalize diagonal movement
var mag = Math.sqrt(dx * dx + dy * dy);
dx /= mag;
dy /= mag;
player.x += dx * player.speed;
player.y += dy * player.speed;
// Clamp to play area
var halfW = player.width / 2,
halfH = player.height / 2;
if (player.x < halfW) player.x = halfW;
if (player.x > playWidth - halfW) player.x = playWidth - halfW;
if (player.y < halfH) player.y = halfH;
if (player.y > playHeight - halfH) player.y = playHeight - halfH;
}
// Update all balls and handle bounces/collisions
for (var i = 0; i < balls.length; ++i) {
var b = balls[i];
// Store previous position for bounce detection
if (typeof b.lastX !== "number") b.lastX = b.x;
if (typeof b.lastY !== "number") b.lastY = b.y;
// Save old velocities to detect bounce
var bounced = false;
// Check for bounce on left
if (b.lastX - b.width / 2 > 0 && b.x - b.width / 2 <= 0 && b.vx < 0) bounced = true;
// Check for bounce on right
if (b.lastX + b.width / 2 < playWidth && b.x + b.width / 2 >= playWidth && b.vx > 0) bounced = true;
// Check for bounce on top
if (b.lastY - b.height / 2 > 0 && b.y - b.height / 2 <= 0 && b.vy < 0) bounced = true;
// Check for bounce on bottom
if (b.lastY + b.height / 2 < playHeight && b.y + b.height / 2 >= playHeight && b.vy > 0) bounced = true;
b.update();
// If bounced, spawn a new ball at this position with random velocity
if (bounced) {
// Randomize direction and speed a bit for new balls
var angle = Math.random() * Math.PI * 2;
var speed = 12 + Math.random() * 8;
var vx = Math.cos(angle) * speed;
var vy = Math.sin(angle) * speed;
spawnBall(b.x, b.y, vx, vy);
}
// Collision: player & this ball
if (player.intersects(b)) {
LK.effects.flashScreen(0xff0000, 800);
LK.showGameOver();
return;
}
// Update lastX/lastY for next frame
b.lastX = b.x;
b.lastY = b.y;
}
// Update timer
var now = Date.now();
var survival = (now - startTime) / 1000;
if (Math.floor(survival * 100) !== Math.floor(lastSurvival * 100)) {
timerTxt.setText(survival.toFixed(2));
lastSurvival = survival;
}
};
// Create spawn button
var spawnBtn = new Container();
var spawnBtnGfx = spawnBtn.attachAsset('btn_spawn', {
anchorX: 0.5,
anchorY: 0.5
});
var spawnBtnText = new Text2("SPAWN", {
size: 40,
fill: 0xFFFFFF
});
spawnBtnText.anchor.set(0.5, 0.5);
spawnBtnText.x = 0;
spawnBtnText.y = 0;
spawnBtn.addChild(spawnBtnText);
spawnBtn.x = LK.gui.width - 120;
spawnBtn.y = 80;
LK.gui.addChild(spawnBtn);
// Spawn button event handlers
spawnBtn.down = function (x, y, obj) {
// Spawn a new ball at random position with random velocity
var randomX = 200 + Math.random() * (playWidth - 400);
var randomY = 200 + Math.random() * (playHeight - 400);
var angle = Math.random() * Math.PI * 2;
var speed = 10 + Math.random() * 10;
var vx = Math.cos(angle) * speed;
var vy = Math.sin(angle) * speed;
spawnBall(randomX, randomY, vx, vy);
};
// Reset timer on game start
startTime = Date.now();
lastSurvival = 0;
timerTxt.setText("0.00");
;