Code edit (2 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
increase the animation speed for the enemy_rower
User prompt
enemy_rower needs to have an animation comprised of 2 frames. the second frame is enemy_rower_1. alternate between these frames once every 500 miliseconds
Code edit (3 edits merged)
Please save this source code
User prompt
move the enemy_rower 50 pixels lower
User prompt
attack the asset Enemy_Rower to the Obstacle similar to how you attached the rower to the player. the enemy_rower needs to overlap the obstacle
User prompt
attack the asset Enemy_Rower to the Obstacle similar to how you attached the rower to the player
User prompt
attack the asset Enemy_Rower to the Obstacle
User prompt
I dont see the 2 instances of the enemy_rower attached to the obstacle. this is a bug, attach them to the obstacle
User prompt
attack the asset Enemy_Rower to the Obstacle. but attach 2 of them with a 50 pixels distance between them
Code edit (1 edits merged)
Please save this source code
User prompt
spawn even more coins
Code edit (1 edits merged)
Please save this source code
/****
* Classes
****/
// Coin class
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.move = function () {
if (self.spawnDirection === 'right') {
self.x -= coinSpeed;
} else {
self.x += coinSpeed;
}
// Destroy coin when it moves off screen and remove it from the coins array
if (self.x < -100 || self.x > 2048 + 100) {
self.destroy();
var index = coins.indexOf(self);
if (index > -1) {
coins.splice(index, 1);
}
}
};
});
// Enemy class
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.acceleration = 0;
self.move = function () {
self.acceleration += 0.1;
if (self.spawnDirection === 'right') {
self.x -= enemySpeed + self.acceleration;
} else {
self.x += enemySpeed + self.acceleration;
}
// Destroy enemy when it moves off screen and remove it from the enemies array
if (self.x < -100 || self.x > 2048 + 100) {
self.destroy();
var index = enemies.indexOf(self);
if (index > -1) {
enemies.splice(index, 1);
}
// Spawn a new enemy if the current number of enemies is less than the maximum number of enemies
if (enemies.length < maxEnemies) {
spawnEnemy();
}
}
};
});
// Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = obstacleSpeed;
self.move = function () {
self.x += self.speed;
// Destroy obstacle when it moves off screen
if (self.x < -100) {
self.destroy();
}
};
});
// Assets will be automatically generated based on usage in the code.
// Player class
var Player = Container.expand(function () {
var self = Container.call(this);
playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.rowerFrame = 0;
self.rowerGraphics = [];
self.rowerGraphics.push(self.attachAsset('rower', {
anchorX: 0.5,
anchorY: 0.5
}));
self.rowerGraphics.push(self.attachAsset('Rower_1', {
anchorX: 0.5,
anchorY: 0.5
}));
self.rowerGraphics[1].visible = false;
self.gravity = -1; // Start with gravity pulling upwards
self.acceleration = 0; // Initialize acceleration
self.flipGravity = function () {
self.gravity *= -1;
};
self.touchingEdge = false;
self.update = function () {
self.y += (playerSpeed + self.acceleration) * self.gravity;
// Update Rower animation when player changes direction
if (self.gravity == -1 && self.rowerFrame != 1) {
self.rowerGraphics[self.rowerFrame].visible = false;
self.rowerFrame = 1;
self.rowerGraphics[self.rowerFrame].visible = true;
} else if (self.gravity == 1 && self.rowerFrame != 0) {
self.rowerGraphics[self.rowerFrame].visible = false;
self.rowerFrame = 0;
self.rowerGraphics[self.rowerFrame].visible = true;
}
self.acceleration += 0.8; // Increase acceleration
// Prevent player from going out of bounds
if (self.y - playerGraphics.height / 2 <= 0) {
self.y = playerGraphics.height / 2;
self.acceleration = 0; // Reset acceleration
if (self.gravity == -1 && !self.touchingEdge) {
self.touchingEdge = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore().toString());
scoreTxt.scale.set(1.2, 1.2);
LK.setTimeout(function () {
scoreTxt.scale.set(1, 1);
}, 100);
maxEnemies = Math.floor(LK.getScore() / 10);
if (enemies.length < maxEnemies) {
spawnEnemy();
}
}
} else if (self.y + playerGraphics.height / 2 >= 2732) {
self.y = 2732 - playerGraphics.height / 2;
self.acceleration = 0; // Reset acceleration
if (self.gravity == 1 && !self.touchingEdge) {
self.touchingEdge = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore().toString());
maxEnemies = Math.floor(LK.getScore() / 10);
if (enemies.length < maxEnemies) {
spawnEnemy();
}
}
} else {
self.touchingEdge = false;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var BackgroundContainer = new Container();
var MidgroundContainer = new Container();
var ForegroundContainer = new Container();
game.addChild(BackgroundContainer);
game.addChild(MidgroundContainer);
game.addChild(ForegroundContainer);
var background = BackgroundContainer.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366,
width: 2048,
height: 2732
});
// Game Variables
// Assets will be automatically generated based on usage in the code.
var playerGraphics;
var player;
var playerSpeed = 5;
var score = 0;
var scoreTxt;
var obstacles = []; // Array to keep track of obstacles
var obstacleSpeed = -10;
var coins = []; // Initialize coins array
var coinSpeed = 15;
var coinSpawnDirection = 'right'; // Initialize coin spawn direction
var enemies = []; // Initialize enemies array
var maxEnemies = 0; // Initialize maximum number of enemies that can be spawned
var enemySpawnDirection = 'left'; // Initialize enemy spawn direction
var enemySpeed = 5; // Define enemy speed
// Initialize Player
player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 1366; // Center vertically
// Initialize Score Display
scoreTxt = new Text2(score.toString(), {
size: 200,
fill: "#ffffff",
stroke: "#000000",
strokeThickness: 20
});
scoreTxt.anchor.set(0.5, 0.5);
scoreTxt.x = 1024;
scoreTxt.y = 1366;
MidgroundContainer.addChild(scoreTxt);
// Create obstacles and coins at intervals
function spawnEnemy() {
// Check if the score is less than 10
if (LK.getScore() < 10) {
return;
}
// Determine the number of enemies to spawn based on the player's score
var numEnemies = Math.floor(LK.getScore() / 15);
// Determine the spawn side based on the last spawn side
var spawnSide = enemySpawnDirection;
// Alternate the spawn side
enemySpawnDirection = enemySpawnDirection === 'right' ? 'left' : 'right';
// Spawn enemies if the current number of enemies is less than the maximum number of enemies
if (enemies.length < numEnemies) {
var enemy = new Enemy();
enemy.spawnDirection = spawnSide;
enemy.x = spawnSide === 'right' ? 2048 + 100 : -100;
enemy.y = Math.random() * (2732 - 800) + 400; // Random height with 200 pixel padding at the top and bottom
enemy.acceleration = 0;
enemies.push(enemy);
game.addChild(enemy);
}
}
var enemyTimer = LK.setInterval(spawnEnemy, 1000); // Create an enemy every 1 second
var coinTimer = LK.setInterval(function () {
var coin;
// Check if there are any coins that have exited the screen
var exitedCoins = coins.filter(function (coin) {
return coin.x < -100 || coin.x > 2048 + 100;
});
// If there are exited coins, recycle the first one
if (exitedCoins.length > 0) {
coin = exitedCoins[0];
// Reset coin properties
coin.spawnDirection = coinSpawnDirection;
coin.y = Math.random() * (2732 - 700) + 350; // Random height with 200 pixel padding at the top and bottom
} else {
// If there are no exited coins, create a new one
coin = new Coin();
coin.spawnDirection = coinSpawnDirection;
coin.y = Math.random() * (2732 - 700) + 350; // Random height with 200 pixel padding at the top and bottom
coins.push(coin);
game.addChild(coin);
}
// Spawn coin from right or left alternately
if (coinSpawnDirection === 'right') {
coin.x = 2048;
} else {
coin.x = 0;
}
// Switch coin spawn direction
coinSpawnDirection = coinSpawnDirection === 'right' ? 'left' : 'right';
}, 200); // Create a coin every 200 milliseconds
// Listen for touch events to flip gravity
game.on('down', function () {
if (player.touchingEdge) {
player.flipGravity();
}
});
// Update game state on each tick
LK.on('tick', function () {
player.update();
obstacles.forEach(function (obstacle) {
obstacle.move();
// Check for collision with player
if (player.intersects(obstacle)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
});
// Update coins and remove destroyed coins
coins.forEach(function (coin, index) {
coin.move();
// Check for collision with player
if (player.intersects(coin)) {
// Increase score by 1
LK.setScore(LK.getScore() + 1);
// Update score text
scoreTxt.setText(LK.getScore().toString());
// Add a small bump to the score whenever it increments by 1
scoreTxt.scale.set(1.2, 1.2);
LK.setTimeout(function () {
scoreTxt.scale.set(1, 1);
}, 100);
// Destroy the coin
coin.destroy();
// Remove the coin from the coins array
coins.splice(index, 1);
}
});
// Update enemies and remove destroyed enemies
enemies.forEach(function (enemy, index) {
enemy.move();
// Check for collision with player
if (player.intersects(enemy)) {
// Flash screen red for 1 second (1000ms) to show we are dead.
LK.effects.flashScreen(0xff0000, 1000);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
}
});
// Remove obstacles that have been destroyed
obstacles = obstacles.filter(function (obstacle) {
return !obstacle._destroyed;
});
});