User prompt
consider modifying code game.on('down', function (obj) { var touchPos = obj.event.getLocalPosition(game); // Calculate touch direction relative to snake head var diffX = touchPos.x - snake[0].x; var diffY = touchPos.y - snake[0].y; // If snake is moving horizontally, prioritize vertical movements if (direction.x !== 0) { if (diffY < 0 && direction.y !== 1) { nextDirection = { x: 0, y: -1 }; // Move up if not moving down } else if (diffY > 0 && direction.y !== -1) { nextDirection = { x: 0, y: 1 }; // Move down if not moving up } } // If snake is moving vertically, prioritize horizontal movements if (direction.y !== 0) { if (diffX > 0 && direction.x !== -1) { nextDirection = { x: 1, y: 0 }; // Move right if not moving left } else if (diffX < 0 && direction.x !== 1) { nextDirection = { x: -1, y: 0 }; // Move left if not moving right } } // If the snake is not already moving in the determined direction var nextX = snake[0].x + nextDirection.x * gridSize; var nextY = snake[0].y + nextDirection.y * gridSize; if (!snake[0].isValidMove(nextX, nextY)) { // If the next move is not valid, keep the current direction nextDirection = { x: direction.x, y: direction.y }; } });
User prompt
move the scoreboard to the top right. Make sure the food doesn't overlap with the scoreboard
User prompt
okay add a scoreboard
User prompt
horizontal movement is still not being prioritized. If the clicks is below the snake and to the right it should turn right. This part isnot working
User prompt
adjust the touch control function. The goal is to make the snake move in the most direct line towards the touch point, preferring horizontal movement over vertical when the touch is primarily aligned with the snake's horizontal axis. Here’s how you can adjust the control logic: Prioritize Horizontal Movement: If the touch is to the left or right of the snake’s head, the snake should move horizontally if possible, even if the touch is also above or below the head. Fallback to Vertical Movement: Only move vertically if the touch is primarily above or below the head and horizontal movement isn't the immediate option.
User prompt
you need to implement a rule that checks the next move's direction against the position of the snake's body segments. If the next move would result in the snake's head overlapping with any part of its body, that move should be disallowed. Here's how you can implement this logic: Check Future Position: Before updating the snake's direction based on user input, check if the new direction would result in the snake's head moving onto one of its body segments. Disallow Invalid Moves: If the new direction would cause a collision with the snake's body, do not update the direction; instead, keep the current direction.
User prompt
refine the controls because only the head of the snake ought to move first not the tail. So if a click is "below" the head and to the right, the snake head should turn right. If the click is above and to the right, it should still move right. If the click is above it simply continues.
User prompt
okay at the moment the snake seems to die at every mouse click
User prompt
Still not responding to arrow keys or mouse
User prompt
Please fix the bug: 'Uncaught TypeError: window.addEventListener is not a function' in or related to this line: 'window.addEventListener('keydown', function (e) {' Line Number: 126
User prompt
Nice, snake now needs to respond to arrow keys
User prompt
still unplayable
User prompt
The snake seems to die immediately possibly dying when it hits the food
Initial prompt
Snake game
/**** * Classes ****/ // Food Class var Food = Container.expand(function () { var self = Container.call(this); var foodGraphics = self.attachAsset('food', { anchorX: 0.5, anchorY: 0.5 }); }); // Assets are automatically created based on usage in the code. // Snake Segment Class var SnakeSegment = Container.expand(function () { var self = Container.call(this); var segmentGraphics = self.attachAsset('snakeSegment', { anchorX: 0.5, anchorY: 0.5 }); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Global variables var snake = []; var food; var direction = { x: 0, y: -1 }; // Initial direction: up var nextDirection = { x: 0, y: -1 }; var snakeSpeed = 5; // How many frames before the snake moves one block var frameCounter = 0; var gridSize = 100; var score = 0; // Initialize snake function initSnake() { var initialLength = 5; for (var i = 0; i < initialLength; i++) { var segment = game.addChild(new SnakeSegment()); segment.x = 1024; // Center of the screen width segment.y = 1366 + i * gridSize; // Center of the screen height, plus offset for initial length snake.push(segment); } } // Initialize food function placeFood() { if (food) { food.destroy(); } // Remove old food if it exists food = game.addChild(new Food()); // Random position for food food.x = Math.floor(Math.random() * (2048 / gridSize)) * gridSize + gridSize / 2; food.y = Math.floor(Math.random() * (2732 / gridSize)) * gridSize + gridSize / 2; } // Check collision with food function checkFoodCollision() { if (snake[0].intersects(food)) { score += 10; // Increase score growSnake(); // Grow snake placeFood(); // Place new food } } // Grow snake by adding a segment at the end function growSnake() { var lastSegment = snake[snake.length - 1]; var newSegment = game.addChild(new SnakeSegment()); newSegment.x = lastSegment.x - direction.x * gridSize; newSegment.y = lastSegment.y - direction.y * gridSize; snake.push(newSegment); } // Check collision with walls or self function checkCollision() { var head = snake[0]; // Wall collision if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) { LK.showGameOver(); } // Self collision for (var i = 1; i < snake.length; i++) { if (head.intersects(snake[i])) { LK.showGameOver(); } } } // Move snake function moveSnake() { // Move each segment to the position of the previous segment for (var i = snake.length - 1; i > 0; i--) { snake[i].x = snake[i - 1].x; snake[i].y = snake[i - 1].y; } // Move head in the current direction snake[0].x += direction.x * gridSize; snake[0].y += direction.y * gridSize; } // Game tick LK.on('tick', function () { frameCounter++; if (frameCounter >= snakeSpeed) { frameCounter = 0; direction = nextDirection; // Update direction moveSnake(); checkFoodCollision(); checkCollision(); } }); // Touch controls game.on('down', function (obj) { var touchPos = obj.event.getLocalPosition(game); // Calculate touch direction relative to snake head var diffX = touchPos.x - snake[0].x; var diffY = touchPos.y - snake[0].y; if (Math.abs(diffX) > Math.abs(diffY)) { nextDirection = { x: diffX > 0 ? 1 : -1, y: 0 }; } else { nextDirection = { x: 0, y: diffY > 0 ? 1 : -1 }; } }); // Initialize game elements initSnake(); placeFood();
===================================================================
--- original.js
+++ change.js
@@ -1,137 +1,139 @@
-/****
+/****
* Classes
-****/
+****/
// Food Class
var Food = Container.expand(function () {
- var self = Container.call(this);
- var foodGraphics = self.attachAsset('food', {
- anchorX: 0.5,
- anchorY: 0.5
- });
+ var self = Container.call(this);
+ var foodGraphics = self.attachAsset('food', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
});
// Assets are automatically created based on usage in the code.
// Snake Segment Class
var SnakeSegment = Container.expand(function () {
- var self = Container.call(this);
- var segmentGraphics = self.attachAsset('snakeSegment', {
- anchorX: 0.5,
- anchorY: 0.5
- });
+ var self = Container.call(this);
+ var segmentGraphics = self.attachAsset('snakeSegment', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 // Init game with black background
+ backgroundColor: 0x000000 // Init game with black background
});
-/****
+/****
* Game Code
-****/
+****/
// Global variables
var snake = [];
var food;
var direction = {
- x: 0,
- y: -1
+ x: 0,
+ y: -1
}; // Initial direction: up
var nextDirection = {
- x: 0,
- y: -1
+ x: 0,
+ y: -1
};
var snakeSpeed = 5; // How many frames before the snake moves one block
var frameCounter = 0;
var gridSize = 100;
var score = 0;
// Initialize snake
function initSnake() {
- var initialLength = 5;
- for (var i = 0; i < initialLength; i++) {
- var segment = game.addChild(new SnakeSegment());
- segment.x = 1024; // Center of the screen width
- segment.y = 1366 + i * gridSize; // Center of the screen height, plus offset for initial length
- snake.push(segment);
- }
+ var initialLength = 5;
+ for (var i = 0; i < initialLength; i++) {
+ var segment = game.addChild(new SnakeSegment());
+ segment.x = 1024; // Center of the screen width
+ segment.y = 1366 + i * gridSize; // Center of the screen height, plus offset for initial length
+ snake.push(segment);
+ }
}
// Initialize food
function placeFood() {
- if (food) food.destroy(); // Remove old food if it exists
- food = game.addChild(new Food());
- // Random position for food
- food.x = Math.floor(Math.random() * (2048 / gridSize)) * gridSize + gridSize / 2;
- food.y = Math.floor(Math.random() * (2732 / gridSize)) * gridSize + gridSize / 2;
+ if (food) {
+ food.destroy();
+ } // Remove old food if it exists
+ food = game.addChild(new Food());
+ // Random position for food
+ food.x = Math.floor(Math.random() * (2048 / gridSize)) * gridSize + gridSize / 2;
+ food.y = Math.floor(Math.random() * (2732 / gridSize)) * gridSize + gridSize / 2;
}
// Check collision with food
function checkFoodCollision() {
- if (snake[0].intersects(food)) {
- score += 10; // Increase score
- placeFood(); // Place new food
- growSnake(); // Grow snake
- }
+ if (snake[0].intersects(food)) {
+ score += 10; // Increase score
+ growSnake(); // Grow snake
+ placeFood(); // Place new food
+ }
}
// Grow snake by adding a segment at the end
function growSnake() {
- var lastSegment = snake[snake.length - 1];
- var newSegment = game.addChild(new SnakeSegment());
- newSegment.x = lastSegment.x - direction.x * gridSize;
- newSegment.y = lastSegment.y - direction.y * gridSize;
- snake.push(newSegment);
+ var lastSegment = snake[snake.length - 1];
+ var newSegment = game.addChild(new SnakeSegment());
+ newSegment.x = lastSegment.x - direction.x * gridSize;
+ newSegment.y = lastSegment.y - direction.y * gridSize;
+ snake.push(newSegment);
}
// Check collision with walls or self
function checkCollision() {
- var head = snake[0];
- // Wall collision
- if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) {
- LK.showGameOver();
- }
- // Self collision
- for (var i = 1; i < snake.length; i++) {
- if (head.intersects(snake[i])) {
- LK.showGameOver();
- }
- }
+ var head = snake[0];
+ // Wall collision
+ if (head.x < 0 || head.x > 2048 || head.y < 0 || head.y > 2732) {
+ LK.showGameOver();
+ }
+ // Self collision
+ for (var i = 1; i < snake.length; i++) {
+ if (head.intersects(snake[i])) {
+ LK.showGameOver();
+ }
+ }
}
// Move snake
function moveSnake() {
- // Move each segment to the position of the previous segment
- for (var i = snake.length - 1; i > 0; i--) {
- snake[i].x = snake[i - 1].x;
- snake[i].y = snake[i - 1].y;
- }
- // Move head in the current direction
- snake[0].x += direction.x * gridSize;
- snake[0].y += direction.y * gridSize;
+ // Move each segment to the position of the previous segment
+ for (var i = snake.length - 1; i > 0; i--) {
+ snake[i].x = snake[i - 1].x;
+ snake[i].y = snake[i - 1].y;
+ }
+ // Move head in the current direction
+ snake[0].x += direction.x * gridSize;
+ snake[0].y += direction.y * gridSize;
}
// Game tick
LK.on('tick', function () {
- frameCounter++;
- if (frameCounter >= snakeSpeed) {
- frameCounter = 0;
- direction = nextDirection; // Update direction
- moveSnake();
- checkFoodCollision();
- checkCollision();
- }
+ frameCounter++;
+ if (frameCounter >= snakeSpeed) {
+ frameCounter = 0;
+ direction = nextDirection; // Update direction
+ moveSnake();
+ checkFoodCollision();
+ checkCollision();
+ }
});
// Touch controls
game.on('down', function (obj) {
- var touchPos = obj.event.getLocalPosition(game);
- // Calculate touch direction relative to snake head
- var diffX = touchPos.x - snake[0].x;
- var diffY = touchPos.y - snake[0].y;
- if (Math.abs(diffX) > Math.abs(diffY)) {
- nextDirection = {
- x: diffX > 0 ? 1 : -1,
- y: 0
- };
- } else {
- nextDirection = {
- x: 0,
- y: diffY > 0 ? 1 : -1
- };
- }
+ var touchPos = obj.event.getLocalPosition(game);
+ // Calculate touch direction relative to snake head
+ var diffX = touchPos.x - snake[0].x;
+ var diffY = touchPos.y - snake[0].y;
+ if (Math.abs(diffX) > Math.abs(diffY)) {
+ nextDirection = {
+ x: diffX > 0 ? 1 : -1,
+ y: 0
+ };
+ } else {
+ nextDirection = {
+ x: 0,
+ y: diffY > 0 ? 1 : -1
+ };
+ }
});
// Initialize game elements
initSnake();
placeFood();
\ No newline at end of file