/**** * Classes ****/ // Castle class to represent the goal var Castle = Container.expand(function () { var self = Container.call(this); var castleGraphics = self.attachAsset('castle', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Logic for castle if needed }; }); //<Assets used in the game will automatically appear here> // Dice class to represent each RPG dice var Dice = Container.expand(function () { var self = Container.call(this); var diceGraphics = self.attachAsset('dice', { anchorX: 0.5, anchorY: 0.5 }); self.value = Math.floor(Math.random() * 6) + 1; // Random value between 1 and 6 self.roll = function () { self.value = Math.floor(Math.random() * 6) + 1; }; self.update = function () { // Update logic for dice if needed }; }); // Dragon class to represent obstacles var Dragon = Container.expand(function () { var self = Container.call(this); var dragonGraphics = self.attachAsset('dragon', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Logic for dragon movement or behavior }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize game elements var diceArray = []; var dragons = []; var castle; // Create and position dice for (var i = 0; i < 7; i++) { var dice = new Dice(); dice.x = 200 + i * 150; // Position dice with some spacing dice.y = 200; diceArray.push(dice); game.addChild(dice); } // Create and position dragons for (var j = 0; j < 3; j++) { var dragon = new Dragon(); dragon.x = Math.random() * 2048; // Random x position dragon.y = 500 + j * 300; // Position dragons with some spacing dragons.push(dragon); game.addChild(dragon); } // Create and position castle castle = new Castle(); castle.x = 1024; // Centered horizontally castle.y = 2500; // Near the bottom game.addChild(castle); // Handle game logic game.update = function () { // Check for collisions between dice and dragons for (var i = 0; i < diceArray.length; i++) { for (var j = 0; j < dragons.length; j++) { if (diceArray[i].intersects(dragons[j])) { // Handle collision logic, e.g., reset dice position diceArray[i].x = 200 + i * 150; diceArray[i].y = 200; } } } // Check if any dice reaches the castle for (var k = 0; k < diceArray.length; k++) { if (diceArray[k].intersects(castle)) { // Handle winning logic, e.g., show a message console.log("You reached the castle!"); } } }; // Event listeners for rolling dice game.down = function (x, y, obj) { for (var i = 0; i < diceArray.length; i++) { if (diceArray[i].containsPoint({ x: x, y: y })) { diceArray[i].roll(); } } };
/****
* Classes
****/
// Castle class to represent the goal
var Castle = Container.expand(function () {
var self = Container.call(this);
var castleGraphics = self.attachAsset('castle', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Logic for castle if needed
};
});
//<Assets used in the game will automatically appear here>
// Dice class to represent each RPG dice
var Dice = Container.expand(function () {
var self = Container.call(this);
var diceGraphics = self.attachAsset('dice', {
anchorX: 0.5,
anchorY: 0.5
});
self.value = Math.floor(Math.random() * 6) + 1; // Random value between 1 and 6
self.roll = function () {
self.value = Math.floor(Math.random() * 6) + 1;
};
self.update = function () {
// Update logic for dice if needed
};
});
// Dragon class to represent obstacles
var Dragon = Container.expand(function () {
var self = Container.call(this);
var dragonGraphics = self.attachAsset('dragon', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Logic for dragon movement or behavior
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize game elements
var diceArray = [];
var dragons = [];
var castle;
// Create and position dice
for (var i = 0; i < 7; i++) {
var dice = new Dice();
dice.x = 200 + i * 150; // Position dice with some spacing
dice.y = 200;
diceArray.push(dice);
game.addChild(dice);
}
// Create and position dragons
for (var j = 0; j < 3; j++) {
var dragon = new Dragon();
dragon.x = Math.random() * 2048; // Random x position
dragon.y = 500 + j * 300; // Position dragons with some spacing
dragons.push(dragon);
game.addChild(dragon);
}
// Create and position castle
castle = new Castle();
castle.x = 1024; // Centered horizontally
castle.y = 2500; // Near the bottom
game.addChild(castle);
// Handle game logic
game.update = function () {
// Check for collisions between dice and dragons
for (var i = 0; i < diceArray.length; i++) {
for (var j = 0; j < dragons.length; j++) {
if (diceArray[i].intersects(dragons[j])) {
// Handle collision logic, e.g., reset dice position
diceArray[i].x = 200 + i * 150;
diceArray[i].y = 200;
}
}
}
// Check if any dice reaches the castle
for (var k = 0; k < diceArray.length; k++) {
if (diceArray[k].intersects(castle)) {
// Handle winning logic, e.g., show a message
console.log("You reached the castle!");
}
}
};
// Event listeners for rolling dice
game.down = function (x, y, obj) {
for (var i = 0; i < diceArray.length; i++) {
if (diceArray[i].containsPoint({
x: x,
y: y
})) {
diceArray[i].roll();
}
}
};