User prompt
Please fix the bug: 'Uncaught TypeError: Cannot set properties of undefined (setting '13.66')' in or related to this line: 'self.grid[x][y] = obj;' Line Number: 37
User prompt
add a grid and discrete movement for the mouse
User prompt
Add a maze and collision
User prompt
Lerp mouse position to input position
User prompt
Pero mouse position to touch position when input is detected
User prompt
Mouse moves to touch position only when touch input is detracted
User prompt
Make it a stealth game, add patrolling cats
Initial prompt
Cheese chase
/**** * Classes ****/ // Cheese class var Cheese = Container.expand(function () { var self = Container.call(this); var cheeseGraphics = self.attachAsset('cheese', { anchorX: 0.5, anchorY: 0.5 }); }); // Grid class var Grid = Container.expand(function () { var self = Container.call(this); self.grid = []; self.gridSize = 100; self.gridWidth = 2048 / self.gridSize; self.gridHeight = 2732 / self.gridSize; // Initialize the grid with empty cells for (var i = 0; i < self.gridWidth; i++) { self.grid[i] = []; for (var j = 0; j < self.gridHeight; j++) { self.grid[i][j] = null; } } // Method to add an object to the grid self.addObject = function (obj, x, y) { self.grid[x][y] = obj; obj.x = x * self.gridSize; obj.y = y * self.gridSize; }; // Method to move an object in the grid self.moveObject = function (obj, oldX, oldY, newX, newY) { self.grid[oldX][oldY] = null; self.addObject(obj, newX, newY); }; }); // Hole class var Hole = Container.expand(function () { var self = Container.call(this); var holeGraphics = self.attachAsset('hole', { anchorX: 0.5, anchorY: 0.5 }); }); // Assets will be automatically created based on usage in the code. // Mouse class var Mouse = Container.expand(function () { var self = Container.call(this); var mouseGraphics = self.attachAsset('mouse', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5; self.move = function (direction) { var oldX = self.x / grid.gridSize; var oldY = self.y / grid.gridSize; var newX = oldX; var newY = oldY; if (direction === 'left') { newX--; } if (direction === 'right') { newX++; } if (direction === 'up') { newY--; } if (direction === 'down') { newY++; } // Check if the new position is within the grid and not occupied if (newX >= 0 && newX < grid.gridWidth && newY >= 0 && newY < grid.gridHeight && grid.grid[newX][newY] === null) { grid.moveObject(self, oldX, oldY, newX, newY); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xF4D03F // Init game with a cheese-like background color }); /**** * Game Code ****/ var grid = game.addChild(new Grid()); var mouse = new Mouse(); grid.addObject(mouse, 1, grid.gridHeight / 2); var cheese = new Cheese(); grid.addObject(cheese, grid.gridWidth / 2, grid.gridHeight / 2); var hole = new Hole(); grid.addObject(hole, grid.gridWidth - 1, grid.gridHeight / 2); var dragNode = null; function handleMove(obj) { var event = obj.event; var pos = event.getLocalPosition(game); if (dragNode) { dragNode.x = pos.x; dragNode.y = pos.y; } if (mouse.intersects(cheese)) { // Mouse got the cheese, move cheese to mouse position cheese.x = mouse.x; cheese.y = mouse.y; } if (cheese.intersects(hole)) { // Cheese delivered to the hole, show game win LK.effects.flashScreen(0x00FF00, 1000); LK.showGameOver(); } } game.on('down', function (obj) { dragNode = mouse; handleMove(obj); }); game.on('move', handleMove); game.on('up', function (obj) { dragNode = null; }); LK.on('tick', function () { // Game logic to be executed each tick });
===================================================================
--- original.js
+++ change.js
@@ -1,39 +1,41 @@
/****
* Classes
****/
-// Cat class
-var Cat = Container.expand(function () {
- var self = Container.call(this);
- var catGraphics = self.attachAsset('cat', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 2;
- self.direction = 'left';
- self.patrol = function () {
- if (self.direction === 'left') {
- self.x -= self.speed;
- if (self.x < 0) {
- self.direction = 'right';
- }
- }
- if (self.direction === 'right') {
- self.x += self.speed;
- if (self.x > 2048) {
- self.direction = 'left';
- }
- }
- };
-});
// Cheese class
var Cheese = Container.expand(function () {
var self = Container.call(this);
var cheeseGraphics = self.attachAsset('cheese', {
anchorX: 0.5,
anchorY: 0.5
});
});
+// Grid class
+var Grid = Container.expand(function () {
+ var self = Container.call(this);
+ self.grid = [];
+ self.gridSize = 100;
+ self.gridWidth = 2048 / self.gridSize;
+ self.gridHeight = 2732 / self.gridSize;
+ // Initialize the grid with empty cells
+ for (var i = 0; i < self.gridWidth; i++) {
+ self.grid[i] = [];
+ for (var j = 0; j < self.gridHeight; j++) {
+ self.grid[i][j] = null;
+ }
+ }
+ // Method to add an object to the grid
+ self.addObject = function (obj, x, y) {
+ self.grid[x][y] = obj;
+ obj.x = x * self.gridSize;
+ obj.y = y * self.gridSize;
+ };
+ // Method to move an object in the grid
+ self.moveObject = function (obj, oldX, oldY, newX, newY) {
+ self.grid[oldX][oldY] = null;
+ self.addObject(obj, newX, newY);
+ };
+});
// Hole class
var Hole = Container.expand(function () {
var self = Container.call(this);
var holeGraphics = self.attachAsset('hole', {
@@ -50,30 +52,30 @@
anchorY: 0.5
});
self.speed = 5;
self.move = function (direction) {
+ var oldX = self.x / grid.gridSize;
+ var oldY = self.y / grid.gridSize;
+ var newX = oldX;
+ var newY = oldY;
if (direction === 'left') {
- self.x -= self.speed;
+ newX--;
}
if (direction === 'right') {
- self.x += self.speed;
+ newX++;
}
if (direction === 'up') {
- self.y -= self.speed;
+ newY--;
}
if (direction === 'down') {
- self.y += self.speed;
+ newY++;
}
+ // Check if the new position is within the grid and not occupied
+ if (newX >= 0 && newX < grid.gridWidth && newY >= 0 && newY < grid.gridHeight && grid.grid[newX][newY] === null) {
+ grid.moveObject(self, oldX, oldY, newX, newY);
+ }
};
});
-// Wall class
-var Wall = Container.expand(function () {
- var self = Container.call(this);
- var wallGraphics = self.attachAsset('wall', {
- anchorX: 0.5,
- anchorY: 0.5
- });
-});
/****
* Initialize Game
****/
@@ -83,43 +85,22 @@
/****
* Game Code
****/
-var mouse = game.addChild(new Mouse());
-mouse.x = 100;
-mouse.y = 2732 / 2;
-var cheese = game.addChild(new Cheese());
-cheese.x = 2048 / 2;
-cheese.y = 2732 / 2;
-var hole = game.addChild(new Hole());
-hole.x = 2048 - 100;
-hole.y = 2732 / 2;
-var cat = game.addChild(new Cat());
-cat.x = 2048 / 2;
-cat.y = 2732 / 2;
-// Add walls to the game
-var walls = [];
-for (var i = 0; i < 10; i++) {
- var wall = game.addChild(new Wall());
- wall.x = i * 200;
- wall.y = 2732 / 2;
- walls.push(wall);
-}
+var grid = game.addChild(new Grid());
+var mouse = new Mouse();
+grid.addObject(mouse, 1, grid.gridHeight / 2);
+var cheese = new Cheese();
+grid.addObject(cheese, grid.gridWidth / 2, grid.gridHeight / 2);
+var hole = new Hole();
+grid.addObject(hole, grid.gridWidth - 1, grid.gridHeight / 2);
var dragNode = null;
function handleMove(obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
if (dragNode) {
- var dx = pos.x - dragNode.x;
- var dy = pos.y - dragNode.y;
- var distance = Math.sqrt(dx * dx + dy * dy);
- if (distance > 1) {
- dragNode.x += dx * 0.05;
- dragNode.y += dy * 0.05;
- } else {
- dragNode.x = pos.x;
- dragNode.y = pos.y;
- }
+ dragNode.x = pos.x;
+ dragNode.y = pos.y;
}
if (mouse.intersects(cheese)) {
// Mouse got the cheese, move cheese to mouse position
cheese.x = mouse.x;
@@ -136,26 +117,9 @@
handleMove(obj);
});
game.on('move', handleMove);
game.on('up', function (obj) {
- var event = obj.event;
- var pos = event.getLocalPosition(game);
- mouse.x = pos.x;
- mouse.y = pos.y;
dragNode = null;
});
LK.on('tick', function () {
// Game logic to be executed each tick
- cat.patrol();
- if (mouse.intersects(cat)) {
- // Cat caught the mouse, show game over
- LK.effects.flashScreen(0xFF0000, 1000);
- LK.showGameOver();
- }
- // Check for collision with walls
- for (var i = 0; i < walls.length; i++) {
- if (mouse.intersects(walls[i])) {
- // Mouse hit a wall, stop movement
- dragNode = null;
- }
- }
});
\ No newline at end of file
grey square, black border. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
simple light yellow button front view game console, clean, rounded edges, high resolution, graphic. Single Game Texture. In-Game asset. 2d. Blank background. High contrast.
Worn out sticker for a video game, 90s style, cheese, simple, vintage. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
wall view from top-down, game asset videogame, black color, simple. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. worn out sticker. 90's style. vintage. simple. top-down view. poster. sticker
top-down view, videogame character enemy, roomba, 90s style sticker, flat, no perspective, silhouette, black and white, cartoon, fun, simple, from above. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
top-down view, videogame heart, 90s style sticker, flat, no perspective, silhouette, white, cartoon, fun, simple, from above. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Black square with white outline. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.