User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'getElementById')' in or related to this line: 'var cnv = document.getElementById('gameCanvas'),' Line Number: 11
Code edit (1 edits merged)
Please save this source code
User prompt
restore the game to its previous state
User prompt
Please fix the bug: 'ReferenceError: appleWidth is not defined' in or related to this line: 'return Math.sqrt(dx * dx + dy * dy);' Line Number: 40
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'getElementById')' in or related to this line: 'var cnv = document.getElementById('gameCanvas'),' Line Number: 11
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'getElementById')' in or related to this line: 'var cnv = document.getElementById('gameCanvas'),' Line Number: 11
Code edit (2 edits merged)
Please save this source code
User prompt
The snake's head is not visible
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'getElementById')' in or related to this line: 'var cnv = document.getElementById('gameCanvas'),' Line Number: 11
Code edit (1 edits merged)
Please save this source code
User prompt
Cosmic Frontier
Initial prompt
make a game like darkorbit
/**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Game variables // Initialize assets var segmentLength = 40, startingSegments = 8, spawn = { x: 1024, y: 1366 }, snakeSpeed = 8, maxApples = 3, appleLife = 300, segmentsPerApple = 2, appleWidth = 25, // Define appleWidth based on apple asset width snake, target, apples, score, gameState, deathMeans; function distance(p1, p2) { var dx = p2.x - p1.x; var dy = p2.y - p1.y; return Math.sqrt(dx * dx + dy * dy); } function lineIntersect(p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y) { var s1_x = p1_x - p0_x, s1_y = p1_y - p0_y, s2_x = p3_x - p2_x, s2_y = p3_y - p2_y, s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y), t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y); if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { return true; } return false; } function SGM(angle, x, y) { this.x = x || 0; this.y = y || 0; this.angle = angle || 0; this.parent = null; } ; SGM.prototype.endX = function () { return this.x + Math.cos(this.angle) * segmentLength; }; SGM.prototype.endY = function () { return this.y + Math.sin(this.angle) * segmentLength; }; SGM.prototype.pointAt = function (x, y) { var dx = x - this.x, dy = y - this.y; this.angle = Math.atan2(dy, dx); }; SGM.prototype.target = function (x, y) { this.targetX = x; this.targetY = y; this.arrived = false; this.totalDist = distance({ x: this.endX(), y: this.endY() }, { x: this.targetX, y: this.targetY }); this.currentDist = parseInt(this.totalDist); }; SGM.prototype.gotoTarget = function () { if (!this.arrived) { if (this.targetX > this.x + segmentLength || this.targetX < this.x - segmentLength || this.targetY > this.y + segmentLength || this.targetY < this.y - segmentLength) { this.pointAt(this.targetX, this.targetY); } else { this.arrived = true; } this.currentDist = distance({ x: this.endX(), y: this.endY() }, { x: this.targetX, y: this.targetY }); } this.x += (this.endX() - this.x) / snakeSpeed; this.y += (this.endY() - this.y) / snakeSpeed; this.parent.drag(this.x, this.y); }; SGM.prototype.drag = function (x, y) { this.pointAt(x, y); this.x = x - Math.cos(this.angle) * segmentLength; this.y = y - Math.sin(this.angle) * segmentLength; if (this.parent) { this.parent.drag(this.x, this.y); } }; // Removed Canvas rendering - using LK visual system function IKR(x, y) { this.ix = x || 0; this.iy = y || 0; this.sgms = []; this.lastArm = null; } ; IKR.prototype.addSeg = function (angle) { var arm = new SGM(angle); if (this.lastArm !== null) { arm.x = this.lastArm.endX(); arm.y = this.lastArm.endY(); arm.parent = this.lastArm; } else { arm.x = this.ix; arm.y = this.iy; } this.sgms.push(arm); this.lastArm = arm; }; IKR.prototype.grow = function () { var tail = this.sgms[0], arm = new SGM(tail.angle); arm.x = tail.x - Math.cos(tail.angle) * segmentLength; arm.y = tail.y - Math.sin(tail.angle) * segmentLength; tail.parent = arm; this.sgms.unshift(arm); }; IKR.prototype.drag = function (x, y) { this.lastArm.drag(x, y); }; function Apple(x, y) { this.x = x; this.y = y; this.life = appleLife; } Apple.prototype.update = function () { this.life--; }; function init() { snake = new IKR(spawn.x, spawn.y); target = { x: spawn.x + segmentLength * (startingSegments + 2), y: spawn.y }; apples = []; score = 0; LK.setScore(0); for (var i = 0; i < startingSegments; i++) { snake.addSeg(); } snake.lastArm.target(target.x, target.y); gameState = 'play'; // Create initial visuals createSnakeVisuals(); } // LK touch event handlers game.down = function (x, y, obj) { switch (gameState) { case 'play': if (target) { target.x = x; target.y = y; snake.lastArm.target(target.x, target.y); } break; case 'dead': init(); break; } }; function badPlacement(apple) { for (var s = 0; s < snake.sgms.length; s++) { var seg = snake.sgms[s]; if (Math.min(distance(apple, { x: seg.endX(), y: seg.endY() }), distance(apple, { x: seg.x, y: seg.y })) < appleWidth * 2) { return true; } } return false; } function addScoreSegments() { for (var i = 0; i < segmentsPerApple; i++) { snake.grow(); } } function update() { if (gameState !== 'dead') { snake.lastArm.gotoTarget(); if (snake.lastArm.endX() > 2048 - 50 || snake.lastArm.endX() < 50 || snake.lastArm.endY() > 2732 - 50 || snake.lastArm.endY() < 50) { gameState = 'dead'; deathMeans = 'Hit the wall!'; LK.showGameOver(); return; } for (var s = 0; s < snake.sgms.length - 2; s++) { var seg = snake.sgms[s]; if (lineIntersect(snake.lastArm.x, snake.lastArm.y, snake.lastArm.endX(), snake.lastArm.endY(), seg.x, seg.y, seg.endX(), seg.endY())) { gameState = 'dead'; deathMeans = 'Kendini ısırdın!'; return; } for (var a in apples) { var apple = apples[a]; if (Math.min(distance(apple, { x: seg.endX(), y: seg.endY() }), distance(apple, { x: seg.x, y: seg.y })) < appleWidth * 2) { score += Math.round(apple.life / 2); // half score if absorbed by the tail apples.splice(a, 1); addScoreSegments(); } } } for (var a in apples) { var apple = apples[a]; apple.update(); if (apple.life <= 0) { apples.splice(a, 1); continue; } if (distance(apple, { x: snake.lastArm.endX(), y: snake.lastArm.endY() }) < appleWidth * 2) { score += apple.life; apples.splice(a, 1); addScoreSegments(); } } if (apples.length < maxApples && Math.random() < 0.02) { var offset = 100, apple = new Apple(offset + Math.floor(Math.random() * (2048 - offset * 2)), offset + Math.floor(Math.random() * (2732 - offset * 2))); while (badPlacement(apple)) { apple.x = offset + Math.floor(Math.random() * (2048 - offset * 2)); apple.y = offset + Math.floor(Math.random() * (2732 - offset * 2)); } apples.push(apple); } } } // Canvas rendering functions removed - replaced with LK visual system // Create score display var scoreTxt = new Text2('Score: 0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Initialize visual elements var snakeSegments = []; var appleVisuals = []; var targetVisual; function createSnakeVisuals() { // Clear existing visuals for (var i = 0; i < snakeSegments.length; i++) { if (snakeSegments[i].parent) { snakeSegments[i].parent.removeChild(snakeSegments[i]); } } snakeSegments = []; // Create new visuals for each segment for (var s = 0; s < snake.sgms.length; s++) { var segment = snake.sgms[s]; var visual; if (s === snake.sgms.length - 1) { // Head segment visual = game.addChild(LK.getAsset('snakeHead', { anchorX: 0.5, anchorY: 0.5 })); } else { // Body segment visual = game.addChild(LK.getAsset('snakeSegment', { anchorX: 0.5, anchorY: 0.5 })); } visual.x = segment.x; visual.y = segment.y; snakeSegments.push(visual); } } function updateSnakeVisuals() { for (var s = 0; s < snake.sgms.length && s < snakeSegments.length; s++) { var segment = snake.sgms[s]; var visual = snakeSegments[s]; if (visual) { visual.x = segment.x; visual.y = segment.y; } } } function updateAppleVisuals() { // Remove old apple visuals for (var i = 0; i < appleVisuals.length; i++) { if (appleVisuals[i].parent) { appleVisuals[i].parent.removeChild(appleVisuals[i]); } } appleVisuals = []; // Create new apple visuals for (var a = 0; a < apples.length; a++) { var apple = apples[a]; var visual = game.addChild(LK.getAsset('apple', { anchorX: 0.5, anchorY: 0.5 })); visual.x = apple.x; visual.y = apple.y; appleVisuals.push(visual); } } function updateTargetVisual() { if (!targetVisual) { targetVisual = game.addChild(LK.getAsset('target', { anchorX: 0.5, anchorY: 0.5 })); } if (target) { targetVisual.x = target.x; targetVisual.y = target.y; targetVisual.alpha = snake.lastArm.arrived ? 0.3 : 0.7; } } // LK game update loop game.update = function () { if (gameState === 'play') { update(); // Update visuals updateSnakeVisuals(); updateAppleVisuals(); updateTargetVisual(); // Update score display scoreTxt.setText('Score: ' + score); } }; // Initialize game init();
===================================================================
--- original.js
+++ change.js
@@ -19,8 +19,10 @@
snakeSpeed = 8,
maxApples = 3,
appleLife = 300,
segmentsPerApple = 2,
+ appleWidth = 25,
+ // Define appleWidth based on apple asset width
snake,
target,
apples,
score,