/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Maze class to handle maze structure and logic
var Maze = Container.expand(function () {
var self = Container.call(this);
self.grid = [];
self.cellSize = 100;
// Initialize a simple maze structure
self.initMaze = function (rows, cols) {
for (var i = 0; i < rows; i++) {
self.grid[i] = [];
for (var j = 0; j < cols; j++) {
self.grid[i][j] = Math.random() > 0.2 ? 0 : 1; // 0 for path, 1 for wall
}
}
// Ensure start and end are paths
self.grid[0][0] = 0;
self.grid[rows - 1][cols - 1] = 0;
};
// Render the maze
self.renderMaze = function () {
for (var i = 0; i < self.grid.length; i++) {
for (var j = 0; j < self.grid[i].length; j++) {
var cell = LK.getAsset('box', {
width: self.cellSize,
height: self.cellSize,
color: self.grid[i][j] === 0 ? 0xffffff : 0x000000
});
cell.x = j * self.cellSize;
cell.y = i * self.cellSize;
self.addChild(cell);
}
}
};
});
// Player class to handle player movement and logic
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
// Move player based on direction
self.move = function (direction) {
switch (direction) {
case 'up':
self.y -= self.speed;
break;
case 'down':
self.y += self.speed;
break;
case 'left':
self.x -= self.speed;
break;
case 'right':
self.x += self.speed;
break;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize maze and player
var maze = new Maze();
maze.initMaze(10, 10);
maze.renderMaze();
game.addChild(maze);
var player = new Player();
player.x = 50;
player.y = 50;
game.addChild(player);
// Handle player movement
game.down = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
if (game_position.x < player.x) {
player.move('left');
} else if (game_position.x > player.x) {
player.move('right');
}
if (game_position.y < player.y) {
player.move('up');
} else if (game_position.y > player.y) {
player.move('down');
}
};
// Update game logic
game.update = function () {
// Check for collision with maze walls
var playerGridX = Math.floor(player.x / maze.cellSize);
var playerGridY = Math.floor(player.y / maze.cellSize);
if (maze.grid[playerGridY][playerGridX] === 1) {
// Collision detected, reset player position
player.x = 50;
player.y = 50;
}
// Check for reaching the exit
if (playerGridX === maze.grid[0].length - 1 && playerGridY === maze.grid.length - 1) {
LK.showGameOver(); // Player reached the exit
}
}; /****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Maze class to handle maze structure and logic
var Maze = Container.expand(function () {
var self = Container.call(this);
self.grid = [];
self.cellSize = 100;
// Initialize a simple maze structure
self.initMaze = function (rows, cols) {
for (var i = 0; i < rows; i++) {
self.grid[i] = [];
for (var j = 0; j < cols; j++) {
self.grid[i][j] = Math.random() > 0.2 ? 0 : 1; // 0 for path, 1 for wall
}
}
// Ensure start and end are paths
self.grid[0][0] = 0;
self.grid[rows - 1][cols - 1] = 0;
};
// Render the maze
self.renderMaze = function () {
for (var i = 0; i < self.grid.length; i++) {
for (var j = 0; j < self.grid[i].length; j++) {
var cell = LK.getAsset('box', {
width: self.cellSize,
height: self.cellSize,
color: self.grid[i][j] === 0 ? 0xffffff : 0x000000
});
cell.x = j * self.cellSize;
cell.y = i * self.cellSize;
self.addChild(cell);
}
}
};
});
// Player class to handle player movement and logic
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
// Move player based on direction
self.move = function (direction) {
switch (direction) {
case 'up':
self.y -= self.speed;
break;
case 'down':
self.y += self.speed;
break;
case 'left':
self.x -= self.speed;
break;
case 'right':
self.x += self.speed;
break;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize maze and player
var maze = new Maze();
maze.initMaze(10, 10);
maze.renderMaze();
game.addChild(maze);
var player = new Player();
player.x = 50;
player.y = 50;
game.addChild(player);
// Handle player movement
game.down = function (x, y, obj) {
var game_position = game.toLocal(obj.global);
if (game_position.x < player.x) {
player.move('left');
} else if (game_position.x > player.x) {
player.move('right');
}
if (game_position.y < player.y) {
player.move('up');
} else if (game_position.y > player.y) {
player.move('down');
}
};
// Update game logic
game.update = function () {
// Check for collision with maze walls
var playerGridX = Math.floor(player.x / maze.cellSize);
var playerGridY = Math.floor(player.y / maze.cellSize);
if (maze.grid[playerGridY][playerGridX] === 1) {
// Collision detected, reset player position
player.x = 50;
player.y = 50;
}
// Check for reaching the exit
if (playerGridX === maze.grid[0].length - 1 && playerGridY === maze.grid.length - 1) {
LK.showGameOver(); // Player reached the exit
}
};