/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Obstacle = Container.expand(function () {
var self = Container.call(this);
self.topPipe = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.bottomPipe = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -4;
self.gapSize = 280;
self.gapPosition = 0;
self.scored = false;
self.setGapPosition = function (gapPos) {
self.gapPosition = gapPos;
var topHeight = self.gapPosition;
var bottomHeight = 2732 - self.gapPosition - self.gapSize;
self.topPipe.y = topHeight / 2;
self.topPipe.height = topHeight;
self.bottomPipe.y = self.gapPosition + self.gapSize + bottomHeight / 2;
self.bottomPipe.height = bottomHeight;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityY = 0;
self.gravity = 0.6;
self.flapPower = -12;
self.maxVelocity = 15;
self.flap = function () {
self.velocityY = self.flapPower;
LK.getSound('flap').play();
};
self.update = function () {
self.velocityY += self.gravity;
if (self.velocityY > self.maxVelocity) {
self.velocityY = self.maxVelocity;
}
self.y += self.velocityY;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var player = game.addChild(new Player());
player.x = 400;
player.y = 1366;
var obstacles = [];
var score = 0;
var gameActive = true;
var spawnTimer = 0;
var obstacleSpeed = -4;
var difficultyTimer = 0;
var minGapSize = 240;
var scoreTxt = new Text2('0', {
size: 120,
fill: 0x000000
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.x = 1024;
scoreTxt.y = 100;
LK.gui.top.addChild(scoreTxt);
function updateScore() {
scoreTxt.setText(score);
LK.setScore(score);
}
function spawnObstacle() {
var obstacle = game.addChild(new Obstacle());
obstacle.x = 2200;
obstacle.speed = obstacleSpeed;
var minGap = Math.max(minGapSize, 240 - Math.floor(score / 10) * 10);
var gapPos = Math.random() * (2732 - minGap - 200) + 100;
obstacle.gapPosition = gapPos;
obstacle.gapSize = minGap;
obstacle.setGapPosition(gapPos);
obstacles.push(obstacle);
}
function checkCollision(rect1X, rect1Y, rect1W, rect1H, rect2X, rect2Y, rect2W, rect2H) {
return rect1X - rect1W / 2 < rect2X + rect2W / 2 && rect1X + rect1W / 2 > rect2X - rect2W / 2 && rect1Y - rect1H / 2 < rect2Y + rect2H / 2 && rect1Y + rect1H / 2 > rect2Y - rect2H / 2;
}
function endGame() {
gameActive = false;
LK.showGameOver();
}
game.down = function (x, y, obj) {
if (gameActive) {
player.flap();
}
};
game.move = function (x, y, obj) {
// Not used for this game
};
game.update = function () {
if (!gameActive) {
return;
}
// Update player
player.update();
// Check if player hit top or bottom
if (player.y - 40 < 0 || player.y + 40 > 2732) {
endGame();
return;
}
// Spawn obstacles
spawnTimer++;
if (spawnTimer > 120) {
spawnObstacle();
spawnTimer = 0;
}
// Update obstacles and check collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.update();
// Check if obstacle is off screen
if (obstacle.x + 100 < 0) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
// Check if player passed obstacle for scoring
if (!obstacle.scored && obstacle.x + 75 < player.x) {
obstacle.scored = true;
score++;
updateScore();
LK.getSound('score').play();
}
// Check collision with top pipe
var topPipeHeight = obstacle.gapPosition;
if (checkCollision(player.x, player.y, 80, 80, obstacle.x, topPipeHeight / 2, 150, topPipeHeight)) {
endGame();
return;
}
// Check collision with bottom pipe
var bottomPipeY = obstacle.gapPosition + obstacle.gapSize + (2732 - obstacle.gapPosition - obstacle.gapSize) / 2;
var bottomPipeHeight = 2732 - obstacle.gapPosition - obstacle.gapSize;
if (checkCollision(player.x, player.y, 80, 80, obstacle.x, bottomPipeY, 150, bottomPipeHeight)) {
endGame();
return;
}
}
// Increase difficulty over time
difficultyTimer++;
if (difficultyTimer > 600) {
if (obstacleSpeed > -6.5) {
obstacleSpeed -= 0.3;
}
difficultyTimer = 0;
}
};
// Start game music
LK.playMusic('bgmusic', {
loop: true
});
updateScore(); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,180 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+
+/****
+* Classes
+****/
+var Obstacle = Container.expand(function () {
+ var self = Container.call(this);
+ self.topPipe = self.attachAsset('obstacle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.bottomPipe = self.attachAsset('obstacle', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -4;
+ self.gapSize = 280;
+ self.gapPosition = 0;
+ self.scored = false;
+ self.setGapPosition = function (gapPos) {
+ self.gapPosition = gapPos;
+ var topHeight = self.gapPosition;
+ var bottomHeight = 2732 - self.gapPosition - self.gapSize;
+ self.topPipe.y = topHeight / 2;
+ self.topPipe.height = topHeight;
+ self.bottomPipe.y = self.gapPosition + self.gapSize + bottomHeight / 2;
+ self.bottomPipe.height = bottomHeight;
+ };
+ self.update = function () {
+ self.x += self.speed;
+ };
+ return self;
+});
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.velocityY = 0;
+ self.gravity = 0.6;
+ self.flapPower = -12;
+ self.maxVelocity = 15;
+ self.flap = function () {
+ self.velocityY = self.flapPower;
+ LK.getSound('flap').play();
+ };
+ self.update = function () {
+ self.velocityY += self.gravity;
+ if (self.velocityY > self.maxVelocity) {
+ self.velocityY = self.maxVelocity;
+ }
+ self.y += self.velocityY;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var player = game.addChild(new Player());
+player.x = 400;
+player.y = 1366;
+var obstacles = [];
+var score = 0;
+var gameActive = true;
+var spawnTimer = 0;
+var obstacleSpeed = -4;
+var difficultyTimer = 0;
+var minGapSize = 240;
+var scoreTxt = new Text2('0', {
+ size: 120,
+ fill: 0x000000
+});
+scoreTxt.anchor.set(0.5, 0);
+scoreTxt.x = 1024;
+scoreTxt.y = 100;
+LK.gui.top.addChild(scoreTxt);
+function updateScore() {
+ scoreTxt.setText(score);
+ LK.setScore(score);
+}
+function spawnObstacle() {
+ var obstacle = game.addChild(new Obstacle());
+ obstacle.x = 2200;
+ obstacle.speed = obstacleSpeed;
+ var minGap = Math.max(minGapSize, 240 - Math.floor(score / 10) * 10);
+ var gapPos = Math.random() * (2732 - minGap - 200) + 100;
+ obstacle.gapPosition = gapPos;
+ obstacle.gapSize = minGap;
+ obstacle.setGapPosition(gapPos);
+ obstacles.push(obstacle);
+}
+function checkCollision(rect1X, rect1Y, rect1W, rect1H, rect2X, rect2Y, rect2W, rect2H) {
+ return rect1X - rect1W / 2 < rect2X + rect2W / 2 && rect1X + rect1W / 2 > rect2X - rect2W / 2 && rect1Y - rect1H / 2 < rect2Y + rect2H / 2 && rect1Y + rect1H / 2 > rect2Y - rect2H / 2;
+}
+function endGame() {
+ gameActive = false;
+ LK.showGameOver();
+}
+game.down = function (x, y, obj) {
+ if (gameActive) {
+ player.flap();
+ }
+};
+game.move = function (x, y, obj) {
+ // Not used for this game
+};
+game.update = function () {
+ if (!gameActive) {
+ return;
+ }
+ // Update player
+ player.update();
+ // Check if player hit top or bottom
+ if (player.y - 40 < 0 || player.y + 40 > 2732) {
+ endGame();
+ return;
+ }
+ // Spawn obstacles
+ spawnTimer++;
+ if (spawnTimer > 120) {
+ spawnObstacle();
+ spawnTimer = 0;
+ }
+ // Update obstacles and check collisions
+ for (var i = obstacles.length - 1; i >= 0; i--) {
+ var obstacle = obstacles[i];
+ obstacle.update();
+ // Check if obstacle is off screen
+ if (obstacle.x + 100 < 0) {
+ obstacle.destroy();
+ obstacles.splice(i, 1);
+ continue;
+ }
+ // Check if player passed obstacle for scoring
+ if (!obstacle.scored && obstacle.x + 75 < player.x) {
+ obstacle.scored = true;
+ score++;
+ updateScore();
+ LK.getSound('score').play();
+ }
+ // Check collision with top pipe
+ var topPipeHeight = obstacle.gapPosition;
+ if (checkCollision(player.x, player.y, 80, 80, obstacle.x, topPipeHeight / 2, 150, topPipeHeight)) {
+ endGame();
+ return;
+ }
+ // Check collision with bottom pipe
+ var bottomPipeY = obstacle.gapPosition + obstacle.gapSize + (2732 - obstacle.gapPosition - obstacle.gapSize) / 2;
+ var bottomPipeHeight = 2732 - obstacle.gapPosition - obstacle.gapSize;
+ if (checkCollision(player.x, player.y, 80, 80, obstacle.x, bottomPipeY, 150, bottomPipeHeight)) {
+ endGame();
+ return;
+ }
+ }
+ // Increase difficulty over time
+ difficultyTimer++;
+ if (difficultyTimer > 600) {
+ if (obstacleSpeed > -6.5) {
+ obstacleSpeed -= 0.3;
+ }
+ difficultyTimer = 0;
+ }
+};
+// Start game music
+LK.playMusic('bgmusic', {
+ loop: true
+});
+updateScore();
\ No newline at end of file