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, 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
@@ -7,42 +7,26 @@
/****
* Game Code
****/
-var gameWidth = 2048,
- gameHeight = 2732,
- segmentLength = 20,
- startingSegments = 20,
+// Game variables
+// Initialize assets
+var segmentLength = 40,
+ startingSegments = 8,
spawn = {
- x: gameWidth / 4,
- y: gameHeight / 2
+ x: 1024,
+ y: 1366
},
- snakeSpeed = 5,
- maxApples = 5,
- appleLife = 500,
- segmentsPerApple = 3,
- snakeWidth = 10,
- appleWidth = 10,
- cursorSize = 20,
- snakeColor = [100, 255, 100, 1],
- appleColor = [0, 255, 0, 1],
- cursorColor = [255, 255, 255, 1],
- cursorColorMod = [0, -255, -255, 0],
- targetColor = [0, 0, 255, 1],
- targetColorMod = [255, 0, -255, 0],
- scoreColor = [255, 255, 255, 1],
- cursorSpin = 0.075,
+ snakeSpeed = 8,
+ maxApples = 3,
+ appleLife = 300,
+ segmentsPerApple = 2,
snake,
- cursor,
target,
apples,
score,
gameState,
- deathMeans,
- snakeContainer,
- applesContainer,
- targetContainer,
- cursorContainer;
+ deathMeans;
function distance(p1, p2) {
var dx = p2.x - p1.x;
var dy = p2.y - p1.y;
return Math.sqrt(dx * dx + dy * dy);
@@ -116,11 +100,9 @@
if (this.parent) {
this.parent.drag(this.x, this.y);
}
};
-SGM.prototype.render = function (context) {
- context.lineTo(this.endX(), this.endY());
-};
+// Removed Canvas rendering - using LK visual system
function IKR(x, y) {
this.ix = x || 0;
this.iy = y || 0;
this.sgms = [];
@@ -150,80 +132,42 @@
};
IKR.prototype.drag = function (x, y) {
this.lastArm.drag(x, y);
};
-function CUR(x, y) {
- this.x = x;
- this.y = y;
- this.rotation = 0;
-}
-;
-CUR.prototype.render = function (context) {
- context.save();
- context.translate(this.x, this.y);
- context.rotate(this.rotation);
- context.beginPath();
- context.moveTo(0, -cursorSize);
- context.lineTo(0, -cursorSize / 2);
- context.moveTo(0, cursorSize / 2);
- context.lineTo(0, cursorSize);
- context.moveTo(-cursorSize, 0);
- context.lineTo(-cursorSize / 2, 0);
- context.moveTo(cursorSize / 2, 0);
- context.lineTo(cursorSize, 0);
- context.stroke();
- context.restore();
- this.rotation = (this.rotation + cursorSpin) % 360;
-};
function Apple(x, y) {
this.x = x;
this.y = y;
this.life = appleLife;
- this.rotation = 0;
}
Apple.prototype.update = function () {
this.life--;
};
-Apple.prototype.render = function (context) {
- context.beginPath();
- context.arc(this.x, this.y, appleWidth, 0, Math.PI * 2);
- context.fill();
- if (gameState !== 'dead') {
- context.save();
- context.fillStyle = 'white';
- context.font = '8px sans-serif';
- context.fillText(this.life, this.x + 10, this.y + 10);
- context.restore();
- CUR.prototype.render.call(this, context);
- }
-};
function init() {
snake = new IKR(spawn.x, spawn.y);
- cursor = new CUR(-20, -20);
- target = new CUR(spawn.x + segmentLength * (startingSegments + 5), 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();
}
-init();
-game.move = function (x, y, obj) {
- switch (gameState) {
- case 'play':
- cursor.x = x;
- cursor.y = y;
- break;
- }
-};
+// LK touch event handlers
game.down = function (x, y, obj) {
switch (gameState) {
case 'play':
- target.x = x;
- target.y = y;
- snake.lastArm.target(target.x, target.y);
+ if (target) {
+ target.x = x;
+ target.y = y;
+ snake.lastArm.target(target.x, target.y);
+ }
break;
case 'dead':
init();
break;
@@ -251,11 +195,12 @@
}
function update() {
if (gameState !== 'dead') {
snake.lastArm.gotoTarget();
- if (snake.lastArm.endX() > gameWidth - 2 || snake.lastArm.endX() < 2 || snake.lastArm.endY() > gameHeight - 2 || snake.lastArm.endY() < 2) {
+ if (snake.lastArm.endX() > 2048 - 50 || snake.lastArm.endX() < 50 || snake.lastArm.endY() > 2732 - 50 || snake.lastArm.endY() < 50) {
gameState = 'dead';
- deathMeans = 'Duvara çarptın...';
+ deathMeans = 'Hit the wall!';
+ LK.showGameOver();
return;
}
for (var s = 0; s < snake.sgms.length - 2; s++) {
var seg = snake.sgms[s];
@@ -294,23 +239,114 @@
apples.splice(a, 1);
addScoreSegments();
}
}
- if (apples.length < maxApples && Math.random() < .1) {
- var offset = appleWidth * 10,
- apple = new Apple(offset / 2 + Math.floor(Math.random() * (gameWidth - offset)), offset / 2 + Math.floor(Math.random() * (gameHeight - offset)));
+ 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 / 2 + Math.floor(Math.random() * (gameWidth - offset));
- apple.y = offset / 2 + Math.floor(Math.random() * (gameHeight - offset));
+ apple.x = offset + Math.floor(Math.random() * (2048 - offset * 2));
+ apple.y = offset + Math.floor(Math.random() * (2732 - offset * 2));
}
apples.push(apple);
}
}
}
-// Simplified rendering will be handled by LK game objects
-// Canvas-specific rendering functions removed - LK will handle visual representation
-// Initialize game elements
-init();
-// Replace animate loop with LK game update
+// 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 () {
- update();
-};
\ No newline at end of file
+ if (gameState === 'play') {
+ update();
+ // Update visuals
+ updateSnakeVisuals();
+ updateAppleVisuals();
+ updateTargetVisual();
+ // Update score display
+ scoreTxt.setText('Score: ' + score);
+ }
+};
+// Initialize game
+init();
\ No newline at end of file