/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityY = 0;
self.gravity = 0.8;
self.jumpForce = -15;
self.jump = function () {
self.velocityY = self.jumpForce;
LK.getSound('jump').play();
};
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY;
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
// Top pipe
self.topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
// Bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.speed = -4;
self.gapSize = 300;
self.scored = false;
self.setGapPosition = function (centerY) {
self.topPipe.y = centerY - self.gapSize / 2;
self.bottomPipe.y = centerY + self.gapSize / 2;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var character;
var pipes = [];
var gameStarted = false;
var spawnTimer = 0;
var spawnInterval = 120; // 2 seconds at 60fps
// Create score display
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create start instruction
var startTxt = new Text2('TAP TO START', {
size: 80,
fill: 0xFFFFFF
});
startTxt.anchor.set(0.5, 0.5);
startTxt.x = 2048 / 2;
startTxt.y = 2732 / 2 + 200;
game.addChild(startTxt);
// Initialize character
character = game.addChild(new Character());
character.x = 2048 / 4;
character.y = 2732 / 2;
function startGame() {
if (!gameStarted) {
gameStarted = true;
character.jump();
game.removeChild(startTxt);
}
}
function spawnPipe() {
var pipe = new Pipe();
pipe.x = 2048 + 100;
// Random gap position (avoid too high or too low)
var minY = 300;
var maxY = 2732 - 300;
var gapCenterY = minY + Math.random() * (maxY - minY);
pipe.setGapPosition(gapCenterY);
pipes.push(pipe);
game.addChild(pipe);
}
function checkCollisions() {
// Check if character is off screen
if (character.y < -100 || character.y > 2732 + 100) {
LK.showGameOver();
return;
}
// Check pipe collisions
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
// Check if character passed through pipe for scoring
if (!pipe.scored && character.x > pipe.x + 60) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
}
// Check collision with pipes
if (character.intersects(pipe.topPipe) || character.intersects(pipe.bottomPipe)) {
LK.showGameOver();
return;
}
}
}
function cleanupPipes() {
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
if (pipe.x < -200) {
pipe.destroy();
pipes.splice(i, 1);
}
}
}
// Game input handling
game.down = function (x, y, obj) {
if (!gameStarted) {
startGame();
} else {
character.jump();
}
};
// Main game loop
game.update = function () {
if (!gameStarted) return;
// Update character
character.update();
// Update pipes
for (var i = 0; i < pipes.length; i++) {
pipes[i].update();
}
// Spawn new pipes
spawnTimer++;
if (spawnTimer >= spawnInterval) {
spawnPipe();
spawnTimer = 0;
}
// Check collisions
checkCollisions();
// Clean up off-screen pipes
cleanupPipes();
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,165 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Character = Container.expand(function () {
+ var self = Container.call(this);
+ var characterGraphics = self.attachAsset('character', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.velocityY = 0;
+ self.gravity = 0.8;
+ self.jumpForce = -15;
+ self.jump = function () {
+ self.velocityY = self.jumpForce;
+ LK.getSound('jump').play();
+ };
+ self.update = function () {
+ self.velocityY += self.gravity;
+ self.y += self.velocityY;
+ };
+ return self;
+});
+var Pipe = Container.expand(function () {
+ var self = Container.call(this);
+ // Top pipe
+ self.topPipe = self.attachAsset('pipe', {
+ anchorX: 0.5,
+ anchorY: 1
+ });
+ // Bottom pipe
+ self.bottomPipe = self.attachAsset('pipe', {
+ anchorX: 0.5,
+ anchorY: 0
+ });
+ self.speed = -4;
+ self.gapSize = 300;
+ self.scored = false;
+ self.setGapPosition = function (centerY) {
+ self.topPipe.y = centerY - self.gapSize / 2;
+ self.bottomPipe.y = centerY + self.gapSize / 2;
+ };
+ self.update = function () {
+ self.x += self.speed;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var character;
+var pipes = [];
+var gameStarted = false;
+var spawnTimer = 0;
+var spawnInterval = 120; // 2 seconds at 60fps
+// Create score display
+var scoreTxt = new Text2('0', {
+ size: 100,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+// Create start instruction
+var startTxt = new Text2('TAP TO START', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+startTxt.anchor.set(0.5, 0.5);
+startTxt.x = 2048 / 2;
+startTxt.y = 2732 / 2 + 200;
+game.addChild(startTxt);
+// Initialize character
+character = game.addChild(new Character());
+character.x = 2048 / 4;
+character.y = 2732 / 2;
+function startGame() {
+ if (!gameStarted) {
+ gameStarted = true;
+ character.jump();
+ game.removeChild(startTxt);
+ }
+}
+function spawnPipe() {
+ var pipe = new Pipe();
+ pipe.x = 2048 + 100;
+ // Random gap position (avoid too high or too low)
+ var minY = 300;
+ var maxY = 2732 - 300;
+ var gapCenterY = minY + Math.random() * (maxY - minY);
+ pipe.setGapPosition(gapCenterY);
+ pipes.push(pipe);
+ game.addChild(pipe);
+}
+function checkCollisions() {
+ // Check if character is off screen
+ if (character.y < -100 || character.y > 2732 + 100) {
+ LK.showGameOver();
+ return;
+ }
+ // Check pipe collisions
+ for (var i = 0; i < pipes.length; i++) {
+ var pipe = pipes[i];
+ // Check if character passed through pipe for scoring
+ if (!pipe.scored && character.x > pipe.x + 60) {
+ pipe.scored = true;
+ LK.setScore(LK.getScore() + 1);
+ scoreTxt.setText(LK.getScore());
+ LK.getSound('score').play();
+ }
+ // Check collision with pipes
+ if (character.intersects(pipe.topPipe) || character.intersects(pipe.bottomPipe)) {
+ LK.showGameOver();
+ return;
+ }
+ }
+}
+function cleanupPipes() {
+ for (var i = pipes.length - 1; i >= 0; i--) {
+ var pipe = pipes[i];
+ if (pipe.x < -200) {
+ pipe.destroy();
+ pipes.splice(i, 1);
+ }
+ }
+}
+// Game input handling
+game.down = function (x, y, obj) {
+ if (!gameStarted) {
+ startGame();
+ } else {
+ character.jump();
+ }
+};
+// Main game loop
+game.update = function () {
+ if (!gameStarted) return;
+ // Update character
+ character.update();
+ // Update pipes
+ for (var i = 0; i < pipes.length; i++) {
+ pipes[i].update();
+ }
+ // Spawn new pipes
+ spawnTimer++;
+ if (spawnTimer >= spawnInterval) {
+ spawnPipe();
+ spawnTimer = 0;
+ }
+ // Check collisions
+ checkCollisions();
+ // Clean up off-screen pipes
+ cleanupPipes();
+};
\ No newline at end of file