/**** * Classes ****/ // Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.83, scaleY: 0.83 }); self.speed = 8; self.passed = false; self.update = function () { self.x -= self.speed; if (self.x < -50) { self.destroy(); } }; self.getHitbox = function () { return { x: self.x - 75, y: self.y - 150, width: 200, // Increase the width of the hitbox height: 300 }; }; }); // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.attachAsset('player', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.95, scaleY: 0.95 }); self.speed = 1000; self.jumpHeight = 30; self.isJumping = false; self.velocityY = 0; self.update = function () { if (self.isJumping) { self.y += self.velocityY; self.velocityY += 0.7; if (self.y >= 2732 / 2) { self.y = 2732 / 2; self.isJumping = false; self.velocityY = 0; } } }; self.jump = function () { if (!self.isJumping) { self.isJumping = true; self.velocityY = -self.jumpHeight; } }; self.getHitbox = function () { return { x: self.x - 75, y: self.y - 150, width: 150, height: 300 }; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Background var background = game.addChild(LK.getAsset('background', { anchorX: 0.5, anchorY: 0.5, x: 2048 / 2, y: 2732 / 2 })); // Player var player = game.addChild(new Player()); player.x = 2048 / 2 - 100; player.y = 2732 / 2; // Enemy array var enemies = []; var enemySpawnInterval = 100; var enemySpawnCounter = 0; // Score Text var scoreText = new Text2('0', { size: 100, fill: 0xFFFFFF }); LK.gui.top.addChild(scoreText); scoreText.x = 2048 / 2; scoreText.y = 0; // Collision function function checkCollision(rect1, rect2) { return rect1.x < rect2.x + rect2.width && rect1.x + rect1.width > rect2.x && rect1.y < rect2.y + rect2.height && rect1.y + rect1.height > rect2.y; } // Spawn Enemy function spawnEnemy() { var enemy = new Enemy(); enemy.x = 2048; enemy.y = 2732 / 2; enemy.passed = false; enemies.push(enemy); game.addChild(enemy); } // Game loop game.update = function () { player.update(); // Enemy spawn enemySpawnCounter++; if (enemySpawnCounter >= enemySpawnInterval) { spawnEnemy(); enemySpawnInterval = Math.floor(Math.random() * 100) + 50; enemySpawnCounter = 0; } // Update enemies for (var j = enemies.length - 1; j >= 0; j--) { enemies[j].update(); // Collision detection if (checkCollision(player.getHitbox(), enemies[j].getHitbox())) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); LK.stopMusic(); LK.playMusic('Music'); } // Score increment else if (enemies[j].x < player.x && !enemies[j].passed) { enemies[j].passed = true; LK.setScore(LK.getScore() + 1); scoreText.setText(LK.getScore()); } // Remove offscreen enemy if (enemies[j].x < -50) { enemies[j].destroy(); enemies.splice(j, 1); } } // Speed increase if (LK.ticks % 300 == 0) { for (var i = 0; i < enemies.length; i++) { enemies[i].speed += 1; } } }; // Tap to jump game.down = function (x, y, obj) { player.jump(); }; // Music LK.playMusic('Music');
/****
* Classes
****/
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.83,
scaleY: 0.83
});
self.speed = 8;
self.passed = false;
self.update = function () {
self.x -= self.speed;
if (self.x < -50) {
self.destroy();
}
};
self.getHitbox = function () {
return {
x: self.x - 75,
y: self.y - 150,
width: 200,
// Increase the width of the hitbox
height: 300
};
};
});
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.95,
scaleY: 0.95
});
self.speed = 1000;
self.jumpHeight = 30;
self.isJumping = false;
self.velocityY = 0;
self.update = function () {
if (self.isJumping) {
self.y += self.velocityY;
self.velocityY += 0.7;
if (self.y >= 2732 / 2) {
self.y = 2732 / 2;
self.isJumping = false;
self.velocityY = 0;
}
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.velocityY = -self.jumpHeight;
}
};
self.getHitbox = function () {
return {
x: self.x - 75,
y: self.y - 150,
width: 150,
height: 300
};
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Background
var background = game.addChild(LK.getAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
}));
// Player
var player = game.addChild(new Player());
player.x = 2048 / 2 - 100;
player.y = 2732 / 2;
// Enemy array
var enemies = [];
var enemySpawnInterval = 100;
var enemySpawnCounter = 0;
// Score Text
var scoreText = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
LK.gui.top.addChild(scoreText);
scoreText.x = 2048 / 2;
scoreText.y = 0;
// Collision function
function checkCollision(rect1, rect2) {
return rect1.x < rect2.x + rect2.width && rect1.x + rect1.width > rect2.x && rect1.y < rect2.y + rect2.height && rect1.y + rect1.height > rect2.y;
}
// Spawn Enemy
function spawnEnemy() {
var enemy = new Enemy();
enemy.x = 2048;
enemy.y = 2732 / 2;
enemy.passed = false;
enemies.push(enemy);
game.addChild(enemy);
}
// Game loop
game.update = function () {
player.update();
// Enemy spawn
enemySpawnCounter++;
if (enemySpawnCounter >= enemySpawnInterval) {
spawnEnemy();
enemySpawnInterval = Math.floor(Math.random() * 100) + 50;
enemySpawnCounter = 0;
}
// Update enemies
for (var j = enemies.length - 1; j >= 0; j--) {
enemies[j].update();
// Collision detection
if (checkCollision(player.getHitbox(), enemies[j].getHitbox())) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
LK.stopMusic();
LK.playMusic('Music');
}
// Score increment
else if (enemies[j].x < player.x && !enemies[j].passed) {
enemies[j].passed = true;
LK.setScore(LK.getScore() + 1);
scoreText.setText(LK.getScore());
}
// Remove offscreen enemy
if (enemies[j].x < -50) {
enemies[j].destroy();
enemies.splice(j, 1);
}
}
// Speed increase
if (LK.ticks % 300 == 0) {
for (var i = 0; i < enemies.length; i++) {
enemies[i].speed += 1;
}
}
};
// Tap to jump
game.down = function (x, y, obj) {
player.jump();
};
// Music
LK.playMusic('Music');