/**** * Classes ****/ // Hero class var Hero = Container.expand(function () { var self = Container.call(this); var heroGraphics = self.createAsset('hero', 'Hero character', 0.5, 0.5); self.speed = 9; self.jumpPower = -18; self.gravity = 0.5; self.isJumping = false; self.velocityY = 0; self.update = function () { // Apply gravity if (self.isJumping || self.isFalling) { self.velocityY += self.gravity; self.y += self.velocityY; if (self.y > game.height - heroGraphics.height / 2) { self.isJumping = false; self.isFalling = false; self.jumpCount = 0; self.y = game.height - heroGraphics.height / 2; } } self.isFalling = false; }; self.jumpCount = 0; self.maxJumps = 2; self.isFalling = false; self.jump = function () { if (self.jumpCount < self.maxJumps) { self.jumpCount++; self.isJumping = true; self.velocityY = self.jumpPower; } }; self.fallQuickly = function () { self.isFalling = true; self.velocityY = self.gravity * 50; }; }); // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.createAsset('obstacle', 'Obstacle', 0.5, 1); self.speed = -10; self.move = function () { self.x += self.speed; }; }); // Bullet class var HeroBullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet', 0.5, 0.5); bulletGraphics.scale.set(1 / 5); self.speed = 15; self.move = function () { self.x += self.speed; if (self.x > game.width) { self.destroy(); } }; self.checkCollisionWithObstacles = function (obstacles) { for (var i = obstacles.length - 1; i >= 0; i--) { if (self.intersects(obstacles[i])) { obstacles[i].destroy(); obstacles.splice(i, 1); self.destroy(); break; } } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize hero var hero = game.addChild(new Hero()); hero.x = game.width / 4; hero.y = game.height - hero.height / 2; // Initialize hero bullets array var heroBullets = []; // Initialize obstacles array var obstacles = []; // Initialize score var score = 0; var scoreTxt = new Text2(score.toString(), { size: 150, fill: "#ffffff" }); // Create shoot button var shootButton = game.addChild(LK.getAsset('shootButton', 'Shoot Button', 1, 1)); shootButton.scale.set(5); shootButton.x = game.width - shootButton.width * shootButton.scale.x / 2; shootButton.y = game.height - shootButton.height * shootButton.scale.y / 2; var canShoot = true; shootButton.on('down', function (obj) { if (canShoot) { canShoot = false; var newBullet = new HeroBullet(); newBullet.x = hero.x + hero.width / 2; newBullet.y = hero.y; heroBullets.push(newBullet); game.addChild(newBullet); LK.setTimeout(function () { canShoot = true; }, 300); } }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Game tick event LK.on('tick', function () { // Update hero hero.update(); // Move and check obstacles for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].move(); // Check for collision with hero if (hero.intersects(obstacles[i])) { // Check if hero jumps on top of the obstacle if (hero.y + hero.height / 2 <= obstacles[i].y - obstacles[i].height / 2 && hero.velocityY > 0) { // Remove the obstacle if jumped on top obstacles[i].destroy(); obstacles.splice(i, 1); } else { // Game over if collided from the side LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } } // Remove off-screen obstacles if (obstacles[i].x < -obstacles[i].width) { obstacles[i].destroy(); obstacles.splice(i, 1); } } // Move and check collisions for hero bullets for (var b = heroBullets.length - 1; b >= 0; b--) { heroBullets[b].move(); heroBullets[b].checkCollisionWithObstacles(obstacles); // Remove bullets that are off-screen or destroyed if (!heroBullets[b].parent) { heroBullets.splice(b, 1); } } // Increment score score++; scoreTxt.setText(score.toString()); // Add new obstacles if (LK.ticks % 100 == 0) { var obstacle = new Obstacle(); obstacle.x = game.width; obstacle.y = game.height; obstacles.push(obstacle); game.addChild(obstacle); } }); // Prepare for swipe detection var swipeStart = null; var swipeThreshold = 50; // Minimum distance for a swipe to count // Touch start event to record the start position of a swipe game.on('down', function (obj) { var event = obj.event; swipeStart = event.getLocalPosition(game); }); // Touch end event to detect a swipe game.on('up', function (obj) { var event = obj.event; var swipeEnd = event.getLocalPosition(game); if (swipeStart) { if (Math.abs(swipeEnd.y - swipeStart.y) > swipeThreshold) { if (swipeStart.y > swipeEnd.y) { hero.jump(); } else if (swipeStart.y < swipeEnd.y) { hero.fallQuickly(); } } } swipeStart = null; // Reset swipe start position });
/****
* Classes
****/
// Hero class
var Hero = Container.expand(function () {
var self = Container.call(this);
var heroGraphics = self.createAsset('hero', 'Hero character', 0.5, 0.5);
self.speed = 9;
self.jumpPower = -18;
self.gravity = 0.5;
self.isJumping = false;
self.velocityY = 0;
self.update = function () {
// Apply gravity
if (self.isJumping || self.isFalling) {
self.velocityY += self.gravity;
self.y += self.velocityY;
if (self.y > game.height - heroGraphics.height / 2) {
self.isJumping = false;
self.isFalling = false;
self.jumpCount = 0;
self.y = game.height - heroGraphics.height / 2;
}
}
self.isFalling = false;
};
self.jumpCount = 0;
self.maxJumps = 2;
self.isFalling = false;
self.jump = function () {
if (self.jumpCount < self.maxJumps) {
self.jumpCount++;
self.isJumping = true;
self.velocityY = self.jumpPower;
}
};
self.fallQuickly = function () {
self.isFalling = true;
self.velocityY = self.gravity * 50;
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.createAsset('obstacle', 'Obstacle', 0.5, 1);
self.speed = -10;
self.move = function () {
self.x += self.speed;
};
});
// Bullet class
var HeroBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.createAsset('heroBullet', 'Hero Bullet', 0.5, 0.5);
bulletGraphics.scale.set(1 / 5);
self.speed = 15;
self.move = function () {
self.x += self.speed;
if (self.x > game.width) {
self.destroy();
}
};
self.checkCollisionWithObstacles = function (obstacles) {
for (var i = obstacles.length - 1; i >= 0; i--) {
if (self.intersects(obstacles[i])) {
obstacles[i].destroy();
obstacles.splice(i, 1);
self.destroy();
break;
}
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize hero
var hero = game.addChild(new Hero());
hero.x = game.width / 4;
hero.y = game.height - hero.height / 2;
// Initialize hero bullets array
var heroBullets = [];
// Initialize obstacles array
var obstacles = [];
// Initialize score
var score = 0;
var scoreTxt = new Text2(score.toString(), {
size: 150,
fill: "#ffffff"
}); // Create shoot button
var shootButton = game.addChild(LK.getAsset('shootButton', 'Shoot Button', 1, 1));
shootButton.scale.set(5);
shootButton.x = game.width - shootButton.width * shootButton.scale.x / 2;
shootButton.y = game.height - shootButton.height * shootButton.scale.y / 2;
var canShoot = true;
shootButton.on('down', function (obj) {
if (canShoot) {
canShoot = false;
var newBullet = new HeroBullet();
newBullet.x = hero.x + hero.width / 2;
newBullet.y = hero.y;
heroBullets.push(newBullet);
game.addChild(newBullet);
LK.setTimeout(function () {
canShoot = true;
}, 300);
}
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Game tick event
LK.on('tick', function () {
// Update hero
hero.update();
// Move and check obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
// Check for collision with hero
if (hero.intersects(obstacles[i])) {
// Check if hero jumps on top of the obstacle
if (hero.y + hero.height / 2 <= obstacles[i].y - obstacles[i].height / 2 && hero.velocityY > 0) {
// Remove the obstacle if jumped on top
obstacles[i].destroy();
obstacles.splice(i, 1);
} else {
// Game over if collided from the side
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
}
// Remove off-screen obstacles
if (obstacles[i].x < -obstacles[i].width) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
}
// Move and check collisions for hero bullets
for (var b = heroBullets.length - 1; b >= 0; b--) {
heroBullets[b].move();
heroBullets[b].checkCollisionWithObstacles(obstacles);
// Remove bullets that are off-screen or destroyed
if (!heroBullets[b].parent) {
heroBullets.splice(b, 1);
}
}
// Increment score
score++;
scoreTxt.setText(score.toString());
// Add new obstacles
if (LK.ticks % 100 == 0) {
var obstacle = new Obstacle();
obstacle.x = game.width;
obstacle.y = game.height;
obstacles.push(obstacle);
game.addChild(obstacle);
}
});
// Prepare for swipe detection
var swipeStart = null;
var swipeThreshold = 50; // Minimum distance for a swipe to count
// Touch start event to record the start position of a swipe
game.on('down', function (obj) {
var event = obj.event;
swipeStart = event.getLocalPosition(game);
});
// Touch end event to detect a swipe
game.on('up', function (obj) {
var event = obj.event;
var swipeEnd = event.getLocalPosition(game);
if (swipeStart) {
if (Math.abs(swipeEnd.y - swipeStart.y) > swipeThreshold) {
if (swipeStart.y > swipeEnd.y) {
hero.jump();
} else if (swipeStart.y < swipeEnd.y) {
hero.fallQuickly();
}
}
}
swipeStart = null; // Reset swipe start position
});