/**** * Classes ****/ var Background = Container.expand(function () { var self = Container.call(this); var bg1 = self.attachAsset('background', {}); var bg2 = self.attachAsset('background', {}); var dimOverlay = new Container(); var dimGraphics = dimOverlay.attachAsset('dimOverlay', {}); dimGraphics.width = 2048; dimGraphics.height = 2732; dimGraphics.alpha = 0.3; bg1.width = bg2.width = 2048; bg1.height = bg2.height = 2732; bg2.x = 2048; self.addChild(bg1); self.addChild(bg2); self.addChild(dimOverlay); self._move_migrated = function () { bg1.x -= 1; bg2.x -= 1; if (bg1.x <= -2048) { bg1.x = bg2.x + 2048; } if (bg2.x <= -2048) { bg2.x = bg1.x + 2048; } }; }); var Cloud = Container.expand(function () { var self = Container.call(this); var cloudGraphics = self.attachAsset('cloud', { anchorX: 0.5, anchorY: 0.5 }); cloudGraphics.alpha = 0.7; self.speed = -0.2; self._move_migrated = function () { self.x += self.speed; if (self.x <= -self.width) { self.x = 2048 + self.width; } }; }); var Enemy = Container.expand(function () { var self = Container.call(this); self.isHit = false; var enemyAssetIds = ['enemy1', 'enemy2', 'enemy3']; var selectedAssetId = enemyAssetIds[Math.floor(Math.random() * enemyAssetIds.length)]; var enemyGraphics = self.createAsset(selectedAssetId, { anchorX: 0.5, anchorY: 0.5 }); self.speedX = -1.8 * (1 + Math.random() * 0.5) * 1.5; self.speedY = 0; self.verticalMovementRange = 50; self.verticalSpeed = self.verticalMovementRange / (5 * 60); self.verticalDirection = 1; self.initialY = 0; self._move_migrated = function () { self.x += self.isHit ? self.speedX * 4 : self.speedX; if (self.initialY === 0) { self.initialY = self.y + 20; } self.y += self.verticalSpeed * self.verticalDirection; if (Math.abs(self.y - self.initialY) >= self.verticalMovementRange) { self.verticalDirection *= -1; } }; }); var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.attachAsset('hero', { anchorX: 0.5, anchorY: 0.5 }); var wingGraphics = self.attachAsset('wing', { anchorX: 0.7, anchorY: 0.8 }); wingGraphics.direction = 1; self.speed = 0; self.bullets = []; self.gravity = 0.2; self.jump = function () { self.speed = -10; }; self._update_migrated = function () { self.speed += self.gravity; self.y += self.speed; self.rotation = self.speed * 0.05; wingGraphics.y += wingGraphics.direction * 2 || -2; if (wingGraphics.y > 10 || wingGraphics.y < -10) { wingGraphics.direction = -wingGraphics.direction; } for (var i = self.bullets.length - 1; i >= 0; i--) { self.bullets[i]._move_migrated(); if (self.bullets[i].y > 2732) { self.bullets[i].destroy(); self.bullets.splice(i, 1); } } }; }); var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self._move_migrated = function () { self.y += self.speed; }; }); var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.baseSpeed = -5; self.speedIncrease = 0; self._move_migrated = function () { self.x += self.baseSpeed - self.speedIncrease; self.y += Math.sin(LK.ticks / 60) * 1; }; }); var SecondBackground = Container.expand(function () { var self = Container.call(this); var bg1 = self.attachAsset('second_background', {}); var bg2 = self.attachAsset('second_background', {}); bg1.width = bg2.width = 2048; bg1.height = bg2.height = 2732; bg2.x = 2048; self.addChild(bg1); self.addChild(bg2); self._move_migrated = function () { bg1.x += 0.5; bg2.x += 0.5; if (bg1.x >= 2048) { bg1.x = bg2.x - 2048; } if (bg2.x >= 2048) { bg2.x = bg1.x - 2048; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Check if background music is playing, if not, then play it LK.on('tick', function () { if (!LK.getSound('Background').playing) { LK.getSound('Background').play(); } }); var secondBackground = game.addChild(new SecondBackground()); var clouds = []; for (var i = 0; i < 5; i++) { var cloud = new Cloud(); cloud.x = Math.random() * 2048; cloud.y = Math.random() * 2732 * 0.3; var randomScale = 0.5 + Math.random() * 0.5; cloud.scale.x = randomScale; cloud.scale.y = randomScale; clouds.push(cloud); game.addChild(cloud); } var background = game.addChild(new Background()); game.setChildIndex(secondBackground, 0); var hero = game.addChild(new Hero()); hero.x = 2048 / 2 - 200; hero.y = 2732 / 2; var obstacles = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff", font: "Gamer", dropShadow: true, dropShadowColor: "#000000", dropShadowBlur: 4, dropShadowAngle: Math.PI / 6, dropShadowDistance: 6 }); LK.gui.top.addChild(scoreTxt); scoreTxt.anchor.set(.5, 0); var isGameOver = false; var tickOffset = 0; var speedIncreaseFactor = 0; var obstacleSpawnInterval = 36 * 4; LK.on('tick', function () { secondBackground._move_migrated(); clouds.forEach(function (cloud) { cloud._move_migrated(); }); background._move_migrated(); hero._update_migrated(); if (hero.y > 2732 || hero.y < 0) { isGameOver = true; LK.getSound('Gameover').play(); } for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i]._move_migrated(); if (obstacles[i].x < -50) { obstacles[i].destroy(); obstacles.splice(i, 1); scoreTxt.setText(score); } var intersectionThreshold = 200; if (hero.intersects(obstacles[i]) && Math.sqrt(Math.pow(hero.x - obstacles[i].x, 2) + Math.pow(hero.y - obstacles[i].y, 2)) < intersectionThreshold) { isGameOver = true; } else { for (var j = hero.bullets.length - 1; j >= 0; j--) { if (hero.bullets[j].intersects(obstacles[i])) { if (obstacles[i] instanceof Enemy) { obstacles[i].isHit = true; obstacles[i].addChild(hero.bullets[j]); hero.bullets[j].x = 0; hero.bullets[j].y = -250; hero.bullets[j].width = 100; hero.bullets[j].height = 100; if (!obstacles[i].scoreAwarded) { score++; obstacles[i].scoreAwarded = true; } scoreTxt.setText(score); // Play 'Scream' sound when bullet hits enemy LK.getSound('Scream').play(); } else { hero.bullets[j].destroy(); hero.bullets.splice(j, 1); // Play 'Hit' sound when bullet hits obstacle LK.getSound('Hit').play(); } } } } } if (tickOffset++ % obstacleSpawnInterval == 0) { var newObstacle = new Obstacle(); newObstacle.x = 2048; newObstacle.y = Math.random() * (2732 - 500 - newObstacle.height) + newObstacle.height; newObstacle.speedIncrease = speedIncreaseFactor; obstacles.push(newObstacle); game.addChild(newObstacle); } if (tickOffset % 600 == 0) { speedIncreaseFactor += 0.5; obstacleSpawnInterval = Math.max(30, obstacleSpawnInterval - 15); } var enemySpawnVariance = Math.random() > 0.5 ? 1.5 : 0.5; if (tickOffset % Math.floor(360 * enemySpawnVariance) == 0) { var enemy = new Enemy(); enemy.x = 2048; enemy.y = 2732 - enemy.height / 2; game.addChild(enemy); obstacles.push(enemy); } if (isGameOver) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } }); game.on('down', function (x, y, obj) { hero.jump(); var bullet = new HeroBullet(); bullet.x = hero.x; bullet.y = hero.y; game.addChild(bullet); hero.bullets.push(bullet); LK.getSound('Plop').play(); }); // Play background sound on game start LK.getSound('Background').play();
/****
* Classes
****/
var Background = Container.expand(function () {
var self = Container.call(this);
var bg1 = self.attachAsset('background', {});
var bg2 = self.attachAsset('background', {});
var dimOverlay = new Container();
var dimGraphics = dimOverlay.attachAsset('dimOverlay', {});
dimGraphics.width = 2048;
dimGraphics.height = 2732;
dimGraphics.alpha = 0.3;
bg1.width = bg2.width = 2048;
bg1.height = bg2.height = 2732;
bg2.x = 2048;
self.addChild(bg1);
self.addChild(bg2);
self.addChild(dimOverlay);
self._move_migrated = function () {
bg1.x -= 1;
bg2.x -= 1;
if (bg1.x <= -2048) {
bg1.x = bg2.x + 2048;
}
if (bg2.x <= -2048) {
bg2.x = bg1.x + 2048;
}
};
});
var Cloud = Container.expand(function () {
var self = Container.call(this);
var cloudGraphics = self.attachAsset('cloud', {
anchorX: 0.5,
anchorY: 0.5
});
cloudGraphics.alpha = 0.7;
self.speed = -0.2;
self._move_migrated = function () {
self.x += self.speed;
if (self.x <= -self.width) {
self.x = 2048 + self.width;
}
};
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
self.isHit = false;
var enemyAssetIds = ['enemy1', 'enemy2', 'enemy3'];
var selectedAssetId = enemyAssetIds[Math.floor(Math.random() * enemyAssetIds.length)];
var enemyGraphics = self.createAsset(selectedAssetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = -1.8 * (1 + Math.random() * 0.5) * 1.5;
self.speedY = 0;
self.verticalMovementRange = 50;
self.verticalSpeed = self.verticalMovementRange / (5 * 60);
self.verticalDirection = 1;
self.initialY = 0;
self._move_migrated = function () {
self.x += self.isHit ? self.speedX * 4 : self.speedX;
if (self.initialY === 0) {
self.initialY = self.y + 20;
}
self.y += self.verticalSpeed * self.verticalDirection;
if (Math.abs(self.y - self.initialY) >= self.verticalMovementRange) {
self.verticalDirection *= -1;
}
};
});
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.attachAsset('hero', {
anchorX: 0.5,
anchorY: 0.5
});
var wingGraphics = self.attachAsset('wing', {
anchorX: 0.7,
anchorY: 0.8
});
wingGraphics.direction = 1;
self.speed = 0;
self.bullets = [];
self.gravity = 0.2;
self.jump = function () {
self.speed = -10;
};
self._update_migrated = function () {
self.speed += self.gravity;
self.y += self.speed;
self.rotation = self.speed * 0.05;
wingGraphics.y += wingGraphics.direction * 2 || -2;
if (wingGraphics.y > 10 || wingGraphics.y < -10) {
wingGraphics.direction = -wingGraphics.direction;
}
for (var i = self.bullets.length - 1; i >= 0; i--) {
self.bullets[i]._move_migrated();
if (self.bullets[i].y > 2732) {
self.bullets[i].destroy();
self.bullets.splice(i, 1);
}
}
};
});
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self._move_migrated = function () {
self.y += self.speed;
};
});
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.baseSpeed = -5;
self.speedIncrease = 0;
self._move_migrated = function () {
self.x += self.baseSpeed - self.speedIncrease;
self.y += Math.sin(LK.ticks / 60) * 1;
};
});
var SecondBackground = Container.expand(function () {
var self = Container.call(this);
var bg1 = self.attachAsset('second_background', {});
var bg2 = self.attachAsset('second_background', {});
bg1.width = bg2.width = 2048;
bg1.height = bg2.height = 2732;
bg2.x = 2048;
self.addChild(bg1);
self.addChild(bg2);
self._move_migrated = function () {
bg1.x += 0.5;
bg2.x += 0.5;
if (bg1.x >= 2048) {
bg1.x = bg2.x - 2048;
}
if (bg2.x >= 2048) {
bg2.x = bg1.x - 2048;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Check if background music is playing, if not, then play it
LK.on('tick', function () {
if (!LK.getSound('Background').playing) {
LK.getSound('Background').play();
}
});
var secondBackground = game.addChild(new SecondBackground());
var clouds = [];
for (var i = 0; i < 5; i++) {
var cloud = new Cloud();
cloud.x = Math.random() * 2048;
cloud.y = Math.random() * 2732 * 0.3;
var randomScale = 0.5 + Math.random() * 0.5;
cloud.scale.x = randomScale;
cloud.scale.y = randomScale;
clouds.push(cloud);
game.addChild(cloud);
}
var background = game.addChild(new Background());
game.setChildIndex(secondBackground, 0);
var hero = game.addChild(new Hero());
hero.x = 2048 / 2 - 200;
hero.y = 2732 / 2;
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff",
font: "Gamer",
dropShadow: true,
dropShadowColor: "#000000",
dropShadowBlur: 4,
dropShadowAngle: Math.PI / 6,
dropShadowDistance: 6
});
LK.gui.top.addChild(scoreTxt);
scoreTxt.anchor.set(.5, 0);
var isGameOver = false;
var tickOffset = 0;
var speedIncreaseFactor = 0;
var obstacleSpawnInterval = 36 * 4;
LK.on('tick', function () {
secondBackground._move_migrated();
clouds.forEach(function (cloud) {
cloud._move_migrated();
});
background._move_migrated();
hero._update_migrated();
if (hero.y > 2732 || hero.y < 0) {
isGameOver = true;
LK.getSound('Gameover').play();
}
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i]._move_migrated();
if (obstacles[i].x < -50) {
obstacles[i].destroy();
obstacles.splice(i, 1);
scoreTxt.setText(score);
}
var intersectionThreshold = 200;
if (hero.intersects(obstacles[i]) && Math.sqrt(Math.pow(hero.x - obstacles[i].x, 2) + Math.pow(hero.y - obstacles[i].y, 2)) < intersectionThreshold) {
isGameOver = true;
} else {
for (var j = hero.bullets.length - 1; j >= 0; j--) {
if (hero.bullets[j].intersects(obstacles[i])) {
if (obstacles[i] instanceof Enemy) {
obstacles[i].isHit = true;
obstacles[i].addChild(hero.bullets[j]);
hero.bullets[j].x = 0;
hero.bullets[j].y = -250;
hero.bullets[j].width = 100;
hero.bullets[j].height = 100;
if (!obstacles[i].scoreAwarded) {
score++;
obstacles[i].scoreAwarded = true;
}
scoreTxt.setText(score);
// Play 'Scream' sound when bullet hits enemy
LK.getSound('Scream').play();
} else {
hero.bullets[j].destroy();
hero.bullets.splice(j, 1);
// Play 'Hit' sound when bullet hits obstacle
LK.getSound('Hit').play();
}
}
}
}
}
if (tickOffset++ % obstacleSpawnInterval == 0) {
var newObstacle = new Obstacle();
newObstacle.x = 2048;
newObstacle.y = Math.random() * (2732 - 500 - newObstacle.height) + newObstacle.height;
newObstacle.speedIncrease = speedIncreaseFactor;
obstacles.push(newObstacle);
game.addChild(newObstacle);
}
if (tickOffset % 600 == 0) {
speedIncreaseFactor += 0.5;
obstacleSpawnInterval = Math.max(30, obstacleSpawnInterval - 15);
}
var enemySpawnVariance = Math.random() > 0.5 ? 1.5 : 0.5;
if (tickOffset % Math.floor(360 * enemySpawnVariance) == 0) {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 - enemy.height / 2;
game.addChild(enemy);
obstacles.push(enemy);
}
if (isGameOver) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
});
game.on('down', function (x, y, obj) {
hero.jump();
var bullet = new HeroBullet();
bullet.x = hero.x;
bullet.y = hero.y;
game.addChild(bullet);
hero.bullets.push(bullet);
LK.getSound('Plop').play();
});
// Play background sound on game start
LK.getSound('Background').play();