Code edit (1 edits merged)
Please save this source code
User prompt
Platform Jumper
Initial prompt
“Create a simple platformer game like Mario but without scrolling. The player can move left and right with A and D keys and jump with Space. Add platforms, coins to collect for score, and simple enemies that reduce health when touched. Show the score and Game Over screen when health reaches zero.”
/****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.bobSpeed = 0.1;
self.bobAmount = 10;
self.originalY = 0;
self.update = function () {
if (!self.collected) {
self.y = self.originalY + Math.sin(LK.ticks * self.bobSpeed) * self.bobAmount;
coinGraphics.rotation += 0.05;
}
};
self.collect = function () {
if (!self.collected) {
self.collected = true;
self.visible = false;
LK.getSound('coin').play();
LK.setScore(LK.getScore() + 10);
scoreText.setText('Score: ' + LK.getScore());
}
};
return self;
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = 2;
self.direction = 1;
self.patrolDistance = 200;
self.startX = 0;
self.update = function () {
self.x += self.speed * self.direction;
// Reverse direction at patrol bounds
if (self.x > self.startX + self.patrolDistance || self.x < self.startX - self.patrolDistance) {
self.direction *= -1;
}
};
self.setPatrol = function (startX, distance) {
self.startX = startX;
self.patrolDistance = distance;
self.x = startX;
};
return self;
});
var Platform = Container.expand(function (width) {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
if (width) {
platformGraphics.width = width;
}
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.velocityX = 0;
self.velocityY = 0;
self.onGround = false;
self.speed = 8;
self.jumpPower = -20;
self.gravity = 0.8;
self.maxFallSpeed = 15;
self.update = function () {
// Apply gravity
self.velocityY += self.gravity;
if (self.velocityY > self.maxFallSpeed) {
self.velocityY = self.maxFallSpeed;
}
// Apply velocity
self.x += self.velocityX;
self.y += self.velocityY;
// Apply friction
self.velocityX *= 0.85;
// Keep player in bounds
if (self.x < 30) {
self.x = 30;
self.velocityX = 0;
}
if (self.x > 2018) {
self.x = 2018;
self.velocityX = 0;
}
// Check platform collisions
self.onGround = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.intersects(platform) && self.velocityY >= 0 && self.y - 80 < platform.y - 20) {
self.y = platform.y - 20;
self.velocityY = 0;
self.onGround = true;
break;
}
}
// Check if fell off screen
if (self.y > 2800) {
self.x = 1024;
self.y = 2600;
self.velocityY = 0;
self.onGround = false;
}
};
self.jump = function () {
if (self.onGround) {
self.velocityY = self.jumpPower;
self.onGround = false;
LK.getSound('jump').play();
}
};
self.moveLeft = function () {
self.velocityX = -self.speed;
};
self.moveRight = function () {
self.velocityX = self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var player;
var platforms = [];
var coins = [];
var enemies = [];
var health = 3;
var maxHealth = 3;
// UI Elements
var scoreText = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreText.anchor.set(0, 0);
scoreText.x = 120;
scoreText.y = 50;
LK.gui.top.addChild(scoreText);
var healthText = new Text2('Health: 3', {
size: 60,
fill: 0xFFFFFF
});
healthText.anchor.set(1, 0);
healthText.x = -50;
healthText.y = 50;
LK.gui.topRight.addChild(healthText);
// Jump button
var jumpButton = game.addChild(LK.getAsset('jumpButton', {
anchorX: 0.5,
anchorY: 0.5
}));
jumpButton.x = 1850;
jumpButton.y = 2500;
// Create platforms
var platformData = [{
x: 1024,
y: 2650,
width: 800
},
// Bottom platform
{
x: 400,
y: 2400,
width: 400
}, {
x: 1600,
y: 2300,
width: 400
}, {
x: 800,
y: 2100,
width: 300
}, {
x: 1400,
y: 1900,
width: 350
}, {
x: 500,
y: 1700,
width: 400
}, {
x: 1200,
y: 1500,
width: 300
}, {
x: 300,
y: 1300,
width: 350
}, {
x: 1500,
y: 1200,
width: 400
}, {
x: 800,
y: 1000,
width: 500
}];
for (var i = 0; i < platformData.length; i++) {
var data = platformData[i];
var platform = game.addChild(new Platform(data.width));
platform.x = data.x;
platform.y = data.y;
platforms.push(platform);
}
// Create coins
var coinPositions = [{
x: 400,
y: 2300
}, {
x: 1600,
y: 2200
}, {
x: 800,
y: 2000
}, {
x: 1400,
y: 1800
}, {
x: 500,
y: 1600
}, {
x: 1200,
y: 1400
}, {
x: 300,
y: 1200
}, {
x: 1500,
y: 1100
}, {
x: 800,
y: 900
}, {
x: 1000,
y: 900
}, {
x: 600,
y: 900
}];
for (var i = 0; i < coinPositions.length; i++) {
var coinPos = coinPositions[i];
var coin = game.addChild(new Coin());
coin.x = coinPos.x;
coin.y = coinPos.y;
coin.originalY = coinPos.y;
coins.push(coin);
}
// Create enemies
var enemyData = [{
platform: 1,
offset: 0,
patrol: 150
}, {
platform: 2,
offset: 0,
patrol: 180
}, {
platform: 4,
offset: 0,
patrol: 140
}, {
platform: 6,
offset: 0,
patrol: 120
}, {
platform: 8,
offset: 0,
patrol: 160
}];
for (var i = 0; i < enemyData.length; i++) {
var enemyInfo = enemyData[i];
var platformRef = platforms[enemyInfo.platform];
var enemy = game.addChild(new Enemy());
enemy.x = platformRef.x + enemyInfo.offset;
enemy.y = platformRef.y - 20;
enemy.setPatrol(platformRef.x, enemyInfo.patrol);
enemies.push(enemy);
}
// Create player
player = game.addChild(new Player());
player.x = 1024;
player.y = 2600;
// Touch controls
var leftPressed = false;
var rightPressed = false;
game.down = function (x, y, obj) {
if (obj === jumpButton) {
player.jump();
} else if (x < 1024) {
leftPressed = true;
} else {
rightPressed = true;
}
};
game.up = function (x, y, obj) {
leftPressed = false;
rightPressed = false;
};
jumpButton.down = function (x, y, obj) {
player.jump();
};
function takeDamage() {
health--;
healthText.setText('Health: ' + health);
LK.getSound('hit').play();
LK.effects.flashObject(player, 0xff0000, 500);
if (health <= 0) {
LK.showGameOver();
}
}
game.update = function () {
// Handle movement input
if (leftPressed) {
player.moveLeft();
}
if (rightPressed) {
player.moveRight();
}
// Check coin collection
for (var i = 0; i < coins.length; i++) {
var coin = coins[i];
if (!coin.collected && player.intersects(coin)) {
coin.collect();
}
}
// Check enemy collision with damage cooldown
if (player.damageCooldown === undefined) {
player.damageCooldown = 0;
}
if (player.damageCooldown > 0) {
player.damageCooldown--;
}
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
if (player.intersects(enemy) && player.damageCooldown === 0) {
takeDamage();
player.damageCooldown = 120; // 2 seconds at 60fps
break;
}
}
}; /****
* Classes
****/
var Coin = Container.expand(function () {
var self = Container.call(this);
var coinGraphics = self.attachAsset('coin', {
anchorX: 0.5,
anchorY: 0.5
});
self.collected = false;
self.bobSpeed = 0.1;
self.bobAmount = 10;
self.originalY = 0;
self.update = function () {
if (!self.collected) {
self.y = self.originalY + Math.sin(LK.ticks * self.bobSpeed) * self.bobAmount;
coinGraphics.rotation += 0.05;
}
};
self.collect = function () {
if (!self.collected) {
self.collected = true;
self.visible = false;
LK.getSound('coin').play();
LK.setScore(LK.getScore() + 10);
scoreText.setText('Score: ' + LK.getScore());
}
};
return self;
});
var Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = 2;
self.direction = 1;
self.patrolDistance = 200;
self.startX = 0;
self.update = function () {
self.x += self.speed * self.direction;
// Reverse direction at patrol bounds
if (self.x > self.startX + self.patrolDistance || self.x < self.startX - self.patrolDistance) {
self.direction *= -1;
}
};
self.setPatrol = function (startX, distance) {
self.startX = startX;
self.patrolDistance = distance;
self.x = startX;
};
return self;
});
var Platform = Container.expand(function (width) {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
if (width) {
platformGraphics.width = width;
}
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.velocityX = 0;
self.velocityY = 0;
self.onGround = false;
self.speed = 8;
self.jumpPower = -20;
self.gravity = 0.8;
self.maxFallSpeed = 15;
self.update = function () {
// Apply gravity
self.velocityY += self.gravity;
if (self.velocityY > self.maxFallSpeed) {
self.velocityY = self.maxFallSpeed;
}
// Apply velocity
self.x += self.velocityX;
self.y += self.velocityY;
// Apply friction
self.velocityX *= 0.85;
// Keep player in bounds
if (self.x < 30) {
self.x = 30;
self.velocityX = 0;
}
if (self.x > 2018) {
self.x = 2018;
self.velocityX = 0;
}
// Check platform collisions
self.onGround = false;
for (var i = 0; i < platforms.length; i++) {
var platform = platforms[i];
if (self.intersects(platform) && self.velocityY >= 0 && self.y - 80 < platform.y - 20) {
self.y = platform.y - 20;
self.velocityY = 0;
self.onGround = true;
break;
}
}
// Check if fell off screen
if (self.y > 2800) {
self.x = 1024;
self.y = 2600;
self.velocityY = 0;
self.onGround = false;
}
};
self.jump = function () {
if (self.onGround) {
self.velocityY = self.jumpPower;
self.onGround = false;
LK.getSound('jump').play();
}
};
self.moveLeft = function () {
self.velocityX = -self.speed;
};
self.moveRight = function () {
self.velocityX = self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var player;
var platforms = [];
var coins = [];
var enemies = [];
var health = 3;
var maxHealth = 3;
// UI Elements
var scoreText = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreText.anchor.set(0, 0);
scoreText.x = 120;
scoreText.y = 50;
LK.gui.top.addChild(scoreText);
var healthText = new Text2('Health: 3', {
size: 60,
fill: 0xFFFFFF
});
healthText.anchor.set(1, 0);
healthText.x = -50;
healthText.y = 50;
LK.gui.topRight.addChild(healthText);
// Jump button
var jumpButton = game.addChild(LK.getAsset('jumpButton', {
anchorX: 0.5,
anchorY: 0.5
}));
jumpButton.x = 1850;
jumpButton.y = 2500;
// Create platforms
var platformData = [{
x: 1024,
y: 2650,
width: 800
},
// Bottom platform
{
x: 400,
y: 2400,
width: 400
}, {
x: 1600,
y: 2300,
width: 400
}, {
x: 800,
y: 2100,
width: 300
}, {
x: 1400,
y: 1900,
width: 350
}, {
x: 500,
y: 1700,
width: 400
}, {
x: 1200,
y: 1500,
width: 300
}, {
x: 300,
y: 1300,
width: 350
}, {
x: 1500,
y: 1200,
width: 400
}, {
x: 800,
y: 1000,
width: 500
}];
for (var i = 0; i < platformData.length; i++) {
var data = platformData[i];
var platform = game.addChild(new Platform(data.width));
platform.x = data.x;
platform.y = data.y;
platforms.push(platform);
}
// Create coins
var coinPositions = [{
x: 400,
y: 2300
}, {
x: 1600,
y: 2200
}, {
x: 800,
y: 2000
}, {
x: 1400,
y: 1800
}, {
x: 500,
y: 1600
}, {
x: 1200,
y: 1400
}, {
x: 300,
y: 1200
}, {
x: 1500,
y: 1100
}, {
x: 800,
y: 900
}, {
x: 1000,
y: 900
}, {
x: 600,
y: 900
}];
for (var i = 0; i < coinPositions.length; i++) {
var coinPos = coinPositions[i];
var coin = game.addChild(new Coin());
coin.x = coinPos.x;
coin.y = coinPos.y;
coin.originalY = coinPos.y;
coins.push(coin);
}
// Create enemies
var enemyData = [{
platform: 1,
offset: 0,
patrol: 150
}, {
platform: 2,
offset: 0,
patrol: 180
}, {
platform: 4,
offset: 0,
patrol: 140
}, {
platform: 6,
offset: 0,
patrol: 120
}, {
platform: 8,
offset: 0,
patrol: 160
}];
for (var i = 0; i < enemyData.length; i++) {
var enemyInfo = enemyData[i];
var platformRef = platforms[enemyInfo.platform];
var enemy = game.addChild(new Enemy());
enemy.x = platformRef.x + enemyInfo.offset;
enemy.y = platformRef.y - 20;
enemy.setPatrol(platformRef.x, enemyInfo.patrol);
enemies.push(enemy);
}
// Create player
player = game.addChild(new Player());
player.x = 1024;
player.y = 2600;
// Touch controls
var leftPressed = false;
var rightPressed = false;
game.down = function (x, y, obj) {
if (obj === jumpButton) {
player.jump();
} else if (x < 1024) {
leftPressed = true;
} else {
rightPressed = true;
}
};
game.up = function (x, y, obj) {
leftPressed = false;
rightPressed = false;
};
jumpButton.down = function (x, y, obj) {
player.jump();
};
function takeDamage() {
health--;
healthText.setText('Health: ' + health);
LK.getSound('hit').play();
LK.effects.flashObject(player, 0xff0000, 500);
if (health <= 0) {
LK.showGameOver();
}
}
game.update = function () {
// Handle movement input
if (leftPressed) {
player.moveLeft();
}
if (rightPressed) {
player.moveRight();
}
// Check coin collection
for (var i = 0; i < coins.length; i++) {
var coin = coins[i];
if (!coin.collected && player.intersects(coin)) {
coin.collect();
}
}
// Check enemy collision with damage cooldown
if (player.damageCooldown === undefined) {
player.damageCooldown = 0;
}
if (player.damageCooldown > 0) {
player.damageCooldown--;
}
for (var i = 0; i < enemies.length; i++) {
var enemy = enemies[i];
if (player.intersects(enemy) && player.damageCooldown === 0) {
takeDamage();
player.damageCooldown = 120; // 2 seconds at 60fps
break;
}
}
};