/**** * Classes ****/ // Player class var Player = Container.expand(function () { var self = Container.call(this); var playerGraphics = self.createAsset('player', 'Player character', 0.5, 1); self.speed = 5; self.isJumping = false; self.isFlipping = false; self.isDashing = false; self.jumpHeight = -22; self.gravity = 0.5; self.velocityY = 0; self.jump = function () { if (!self.isJumping) { self.isJumping = true; self.velocityY = self.jumpHeight; } }; self.flip = function () { if (self.isJumping && !self.isFlipping) { self.isFlipping = true; playerGraphics.rotation += Math.PI; } }; self.dash = function () { if (self.isJumping && !self.isDashing) { self.isDashing = true; self.x += 50; } }; self.update = function () { if (self.isJumping) { self.y += self.velocityY; self.velocityY += self.gravity; if (self.y > game.height - playerGraphics.height) { self.y = game.height - playerGraphics.height; self.isJumping = false; self.isFlipping = false; self.isDashing = false; playerGraphics.rotation = 0; } } }; }); // Obstacle class var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.createAsset('obstacle', 'Obstacle', 0.5, 1); obstacleGraphics.scale.set(0.8); self.speed = Obstacle.baseSpeed; self.move = function () { self.x -= self.speed; if (self.x < -obstacleGraphics.width) { self.destroy(); } }; }); // Block class var Block = Container.expand(function () { var self = Container.call(this); var blockGraphics = self.createAsset('block', 'Harmful block', 0.5, 1); blockGraphics.scale.set(0.8); self.speed = Obstacle.baseSpeed; self.move = function () { self.x -= self.speed; if (self.x < -blockGraphics.width) { self.destroy(); } }; self.isHarmful = function () { // Check if the player's bottom is above the top of the block, which means the player is jumping on it var playerBottom = player.y + player.height; var blockTop = self.y - blockGraphics.height / 2; return playerBottom < blockTop; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize player Obstacle.baseSpeed = 3; Obstacle.increaseSpeed = function () { Obstacle.baseSpeed += 1; }; var player = game.addChild(new Player()); player.x = game.width / 4; player.y = game.height - 150; // Initialize obstacles array var obstacles = []; // Game tick event LK.on('tick', function () { player.update(); // Move each obstacle and check for collisions for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].move(); if (player.intersects(obstacles[i]) && !(obstacles[i] instanceof Block && !obstacles[i].isHarmful())) { LK.effects.flashScreen(0xff0000, 500); LK.showGameOver(); } } // Increase obstacle speed every 25 seconds and make it 10 times faster if (LK.ticks % 1500 === 0) { Obstacle.baseSpeed = 3; } // Spawn obstacles and blocks if (LK.ticks % 180 === 0) { var obstacleType = Math.random() < 0.5 ? Obstacle : Block; var obstacle = new obstacleType(); obstacle.x = game.width; obstacle.y = game.height - 100; obstacles.push(obstacle); game.addChild(obstacle); } }); // Keyboard event listener to make the player jump game.on('down', function (obj) { player.jump(); }); game.on('move', function (obj) { var pos = obj.event.getLocalPosition(game); if (pos.x > game.width / 2) { player.dash(); } else { player.flip(); } }); game.on('up', function (obj) { // Up event not used in this game });
/****
* Classes
****/
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.createAsset('player', 'Player character', 0.5, 1);
self.speed = 5;
self.isJumping = false;
self.isFlipping = false;
self.isDashing = false;
self.jumpHeight = -22;
self.gravity = 0.5;
self.velocityY = 0;
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
self.velocityY = self.jumpHeight;
}
};
self.flip = function () {
if (self.isJumping && !self.isFlipping) {
self.isFlipping = true;
playerGraphics.rotation += Math.PI;
}
};
self.dash = function () {
if (self.isJumping && !self.isDashing) {
self.isDashing = true;
self.x += 50;
}
};
self.update = function () {
if (self.isJumping) {
self.y += self.velocityY;
self.velocityY += self.gravity;
if (self.y > game.height - playerGraphics.height) {
self.y = game.height - playerGraphics.height;
self.isJumping = false;
self.isFlipping = false;
self.isDashing = false;
playerGraphics.rotation = 0;
}
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.createAsset('obstacle', 'Obstacle', 0.5, 1);
obstacleGraphics.scale.set(0.8);
self.speed = Obstacle.baseSpeed;
self.move = function () {
self.x -= self.speed;
if (self.x < -obstacleGraphics.width) {
self.destroy();
}
};
});
// Block class
var Block = Container.expand(function () {
var self = Container.call(this);
var blockGraphics = self.createAsset('block', 'Harmful block', 0.5, 1);
blockGraphics.scale.set(0.8);
self.speed = Obstacle.baseSpeed;
self.move = function () {
self.x -= self.speed;
if (self.x < -blockGraphics.width) {
self.destroy();
}
};
self.isHarmful = function () {
// Check if the player's bottom is above the top of the block, which means the player is jumping on it
var playerBottom = player.y + player.height;
var blockTop = self.y - blockGraphics.height / 2;
return playerBottom < blockTop;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize player
Obstacle.baseSpeed = 3;
Obstacle.increaseSpeed = function () {
Obstacle.baseSpeed += 1;
};
var player = game.addChild(new Player());
player.x = game.width / 4;
player.y = game.height - 150;
// Initialize obstacles array
var obstacles = [];
// Game tick event
LK.on('tick', function () {
player.update();
// Move each obstacle and check for collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].move();
if (player.intersects(obstacles[i]) && !(obstacles[i] instanceof Block && !obstacles[i].isHarmful())) {
LK.effects.flashScreen(0xff0000, 500);
LK.showGameOver();
}
}
// Increase obstacle speed every 25 seconds and make it 10 times faster
if (LK.ticks % 1500 === 0) {
Obstacle.baseSpeed = 3;
}
// Spawn obstacles and blocks
if (LK.ticks % 180 === 0) {
var obstacleType = Math.random() < 0.5 ? Obstacle : Block;
var obstacle = new obstacleType();
obstacle.x = game.width;
obstacle.y = game.height - 100;
obstacles.push(obstacle);
game.addChild(obstacle);
}
});
// Keyboard event listener to make the player jump
game.on('down', function (obj) {
player.jump();
});
game.on('move', function (obj) {
var pos = obj.event.getLocalPosition(game);
if (pos.x > game.width / 2) {
player.dash();
} else {
player.flip();
}
});
game.on('up', function (obj) {
// Up event not used in this game
});