/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.update = function () {
self.x -= gameSpeed;
coinGraphics.rotation += 0.1;
coinGraphics.scaleX = 0.8 + Math.sin(LK.ticks * 0.1) * 0.2;
coinGraphics.scaleY = 0.8 + Math.sin(LK.ticks * 0.1) * 0.2;
};
return self;
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 1.0
});
self.update = function () {
self.x -= gameSpeed;
};
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0,
anchorY: 0
});
self.update = function () {
self.x -= gameSpeed;
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.velocityY = 0;
self.isGrounded = false;
self.jumpPower = -25;
self.gravity = 1.2;
self.groundY = 0;
self.jump = function () {
if (self.isGrounded) {
self.velocityY = self.jumpPower;
self.isGrounded = false;
LK.getSound('jump').play();
}
};
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY;
if (self.y >= self.groundY) {
self.y = self.groundY;
self.velocityY = 0;
self.isGrounded = true;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var gameSpeed = 8;
var baseSpeed = 8;
var maxSpeed = 20;
var speedIncrement = 0.01;
var distance = 0;
var coins = 0;
var groundLevel = 2400;
var nextPlatformX = 0;
var nextObstacleX = 0;
var nextCoinX = 0;
var platforms = [];
var obstacles = [];
var gameCoins = [];
var player = game.addChild(new Player());
player.x = 300;
player.y = groundLevel;
player.groundY = groundLevel;
var scoreText = new Text2('Distance: 0', {
size: 60,
fill: '#FFFFFF'
});
scoreText.anchor.set(0, 0);
LK.gui.topLeft.addChild(scoreText);
var coinText = new Text2('Coins: 0', {
size: 60,
fill: '#FFD700'
});
coinText.anchor.set(0, 0);
coinText.y = 80;
LK.gui.topLeft.addChild(coinText);
var highScoreText = new Text2('Best: ' + (storage.highScore || 0), {
size: 50,
fill: '#CCCCCC'
});
highScoreText.anchor.set(1, 0);
LK.gui.topRight.addChild(highScoreText);
function generatePlatform() {
var platform = new Platform();
platform.x = nextPlatformX;
platform.y = groundLevel;
platforms.push(platform);
game.addChild(platform);
nextPlatformX += Math.random() * 300 + 200;
}
function generateObstacle() {
if (Math.random() < 0.3) {
var obstacle = new Obstacle();
obstacle.x = nextObstacleX;
obstacle.y = groundLevel;
obstacles.push(obstacle);
game.addChild(obstacle);
}
nextObstacleX += Math.random() * 400 + 300;
}
function generateCoin() {
if (Math.random() < 0.6) {
var coin = new Coin();
coin.x = nextCoinX;
coin.y = groundLevel - Math.random() * 200 - 50;
gameCoins.push(coin);
game.addChild(coin);
}
nextCoinX += Math.random() * 200 + 150;
}
for (var i = 0; i < 20; i++) {
generatePlatform();
}
for (var i = 0; i < 10; i++) {
generateObstacle();
generateCoin();
}
var isJumping = false;
game.down = function (x, y, obj) {
player.jump();
isJumping = true;
};
game.up = function (x, y, obj) {
isJumping = false;
};
game.update = function () {
distance += gameSpeed;
if (gameSpeed < maxSpeed) {
gameSpeed += speedIncrement;
}
if (isJumping && player.velocityY > player.jumpPower * 0.5) {
player.velocityY = player.jumpPower * 0.5;
}
for (var i = platforms.length - 1; i >= 0; i--) {
var platform = platforms[i];
if (platform.x < -platform.width) {
platform.destroy();
platforms.splice(i, 1);
}
}
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
if (obstacle.x < -obstacle.width) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
if (player.intersects(obstacle)) {
var finalScore = Math.floor(distance / 10) + coins * 5;
LK.setScore(finalScore);
if (!storage.highScore || finalScore > storage.highScore) {
storage.highScore = finalScore;
}
LK.effects.flashScreen(0xFF0000, 1000);
LK.showGameOver();
return;
}
}
for (var i = gameCoins.length - 1; i >= 0; i--) {
var coin = gameCoins[i];
if (coin.x < -coin.width) {
coin.destroy();
gameCoins.splice(i, 1);
continue;
}
if (!coin.collected && player.intersects(coin)) {
coin.collected = true;
coins++;
LK.getSound('coin').play();
tween(coin, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
coin.destroy();
}
});
gameCoins.splice(i, 1);
coinText.setText('Coins: ' + coins);
}
}
if (player.y > 2732) {
var finalScore = Math.floor(distance / 10) + coins * 5;
LK.setScore(finalScore);
if (!storage.highScore || finalScore > storage.highScore) {
storage.highScore = finalScore;
}
LK.showGameOver();
return;
}
while (nextPlatformX < player.x + 2500) {
generatePlatform();
}
while (nextObstacleX < player.x + 2500) {
generateObstacle();
}
while (nextCoinX < player.x + 2500) {
generateCoin();
}
scoreText.setText('Distance: ' + Math.floor(distance / 10));
game.x = -(player.x - 400);
};
LK.playMusic('bgmusic'); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,246 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var Coin = Container.expand(function () {
+ var self = Container.call(this);
+ var coinGraphics = self.attachAsset('coin', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.collected = false;
+ self.update = function () {
+ self.x -= gameSpeed;
+ coinGraphics.rotation += 0.1;
+ coinGraphics.scaleX = 0.8 + Math.sin(LK.ticks * 0.1) * 0.2;
+ coinGraphics.scaleY = 0.8 + Math.sin(LK.ticks * 0.1) * 0.2;
+ };
+ return self;
+});
+var Obstacle = Container.expand(function () {
+ var self = Container.call(this);
+ var obstacleGraphics = self.attachAsset('obstacle', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ self.update = function () {
+ self.x -= gameSpeed;
+ };
+ return self;
+});
+var Platform = Container.expand(function () {
+ var self = Container.call(this);
+ var platformGraphics = self.attachAsset('platform', {
+ anchorX: 0,
+ anchorY: 0
+ });
+ self.update = function () {
+ self.x -= gameSpeed;
+ };
+ return self;
+});
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ self.velocityY = 0;
+ self.isGrounded = false;
+ self.jumpPower = -25;
+ self.gravity = 1.2;
+ self.groundY = 0;
+ self.jump = function () {
+ if (self.isGrounded) {
+ self.velocityY = self.jumpPower;
+ self.isGrounded = false;
+ LK.getSound('jump').play();
+ }
+ };
+ self.update = function () {
+ self.velocityY += self.gravity;
+ self.y += self.velocityY;
+ if (self.y >= self.groundY) {
+ self.y = self.groundY;
+ self.velocityY = 0;
+ self.isGrounded = true;
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+var gameSpeed = 8;
+var baseSpeed = 8;
+var maxSpeed = 20;
+var speedIncrement = 0.01;
+var distance = 0;
+var coins = 0;
+var groundLevel = 2400;
+var nextPlatformX = 0;
+var nextObstacleX = 0;
+var nextCoinX = 0;
+var platforms = [];
+var obstacles = [];
+var gameCoins = [];
+var player = game.addChild(new Player());
+player.x = 300;
+player.y = groundLevel;
+player.groundY = groundLevel;
+var scoreText = new Text2('Distance: 0', {
+ size: 60,
+ fill: '#FFFFFF'
+});
+scoreText.anchor.set(0, 0);
+LK.gui.topLeft.addChild(scoreText);
+var coinText = new Text2('Coins: 0', {
+ size: 60,
+ fill: '#FFD700'
+});
+coinText.anchor.set(0, 0);
+coinText.y = 80;
+LK.gui.topLeft.addChild(coinText);
+var highScoreText = new Text2('Best: ' + (storage.highScore || 0), {
+ size: 50,
+ fill: '#CCCCCC'
+});
+highScoreText.anchor.set(1, 0);
+LK.gui.topRight.addChild(highScoreText);
+function generatePlatform() {
+ var platform = new Platform();
+ platform.x = nextPlatformX;
+ platform.y = groundLevel;
+ platforms.push(platform);
+ game.addChild(platform);
+ nextPlatformX += Math.random() * 300 + 200;
+}
+function generateObstacle() {
+ if (Math.random() < 0.3) {
+ var obstacle = new Obstacle();
+ obstacle.x = nextObstacleX;
+ obstacle.y = groundLevel;
+ obstacles.push(obstacle);
+ game.addChild(obstacle);
+ }
+ nextObstacleX += Math.random() * 400 + 300;
+}
+function generateCoin() {
+ if (Math.random() < 0.6) {
+ var coin = new Coin();
+ coin.x = nextCoinX;
+ coin.y = groundLevel - Math.random() * 200 - 50;
+ gameCoins.push(coin);
+ game.addChild(coin);
+ }
+ nextCoinX += Math.random() * 200 + 150;
+}
+for (var i = 0; i < 20; i++) {
+ generatePlatform();
+}
+for (var i = 0; i < 10; i++) {
+ generateObstacle();
+ generateCoin();
+}
+var isJumping = false;
+game.down = function (x, y, obj) {
+ player.jump();
+ isJumping = true;
+};
+game.up = function (x, y, obj) {
+ isJumping = false;
+};
+game.update = function () {
+ distance += gameSpeed;
+ if (gameSpeed < maxSpeed) {
+ gameSpeed += speedIncrement;
+ }
+ if (isJumping && player.velocityY > player.jumpPower * 0.5) {
+ player.velocityY = player.jumpPower * 0.5;
+ }
+ for (var i = platforms.length - 1; i >= 0; i--) {
+ var platform = platforms[i];
+ if (platform.x < -platform.width) {
+ platform.destroy();
+ platforms.splice(i, 1);
+ }
+ }
+ for (var i = obstacles.length - 1; i >= 0; i--) {
+ var obstacle = obstacles[i];
+ if (obstacle.x < -obstacle.width) {
+ obstacle.destroy();
+ obstacles.splice(i, 1);
+ continue;
+ }
+ if (player.intersects(obstacle)) {
+ var finalScore = Math.floor(distance / 10) + coins * 5;
+ LK.setScore(finalScore);
+ if (!storage.highScore || finalScore > storage.highScore) {
+ storage.highScore = finalScore;
+ }
+ LK.effects.flashScreen(0xFF0000, 1000);
+ LK.showGameOver();
+ return;
+ }
+ }
+ for (var i = gameCoins.length - 1; i >= 0; i--) {
+ var coin = gameCoins[i];
+ if (coin.x < -coin.width) {
+ coin.destroy();
+ gameCoins.splice(i, 1);
+ continue;
+ }
+ if (!coin.collected && player.intersects(coin)) {
+ coin.collected = true;
+ coins++;
+ LK.getSound('coin').play();
+ tween(coin, {
+ alpha: 0,
+ scaleX: 2,
+ scaleY: 2
+ }, {
+ duration: 300,
+ easing: tween.easeOut,
+ onFinish: function onFinish() {
+ coin.destroy();
+ }
+ });
+ gameCoins.splice(i, 1);
+ coinText.setText('Coins: ' + coins);
+ }
+ }
+ if (player.y > 2732) {
+ var finalScore = Math.floor(distance / 10) + coins * 5;
+ LK.setScore(finalScore);
+ if (!storage.highScore || finalScore > storage.highScore) {
+ storage.highScore = finalScore;
+ }
+ LK.showGameOver();
+ return;
+ }
+ while (nextPlatformX < player.x + 2500) {
+ generatePlatform();
+ }
+ while (nextObstacleX < player.x + 2500) {
+ generateObstacle();
+ }
+ while (nextCoinX < player.x + 2500) {
+ generateCoin();
+ }
+ scoreText.setText('Distance: ' + Math.floor(distance / 10));
+ game.x = -(player.x - 400);
+};
+LK.playMusic('bgmusic');
\ No newline at end of file