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 configuration var segmentLength = 40, startingSegments = 10, spawn = { x: 1024, y: 1366 }, snakeSpeed = 8, maxApples = 3, appleLife = 300, segmentsPerApple = 2, snakeWidth = 30, appleWidth = 25, cursorSize = 20, 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); } }; SGM.prototype.render = function (context) { context.lineTo(this.endX(), this.endY()); }; 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 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); apples = []; score = 0; for (var i = 0; i < startingSegments; i++) { snake.addSeg(); } snake.lastArm.target(target.x, target.y); gameState = 'play'; } init(); // Touch events handled by game object game.down = function (x, y, obj) { switch (gameState) { case 'play': 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 = appleWidth * 4, 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); // Create visual apple var appleVisual = LK.getAsset('apple', { width: appleWidth * 2, height: appleWidth * 2, color: 0x00ff00, shape: 'ellipse', anchorX: 0.5, anchorY: 0.5 }); appleVisual.x = apple.x; appleVisual.y = apple.y; game.addChild(appleVisual); appleObjects.push(appleVisual); } } } // LK rendering system - visual elements handled by game objects var snakeSegments = []; var appleObjects = []; var targetObject = null; var scoreText = null; // Initialize visual elements function initVisuals() { // Clear existing visuals for (var i = 0; i < snakeSegments.length; i++) { snakeSegments[i].destroy(); } for (var i = 0; i < appleObjects.length; i++) { appleObjects[i].destroy(); } if (targetObject) targetObject.destroy(); if (scoreText) scoreText.destroy(); snakeSegments = []; appleObjects = []; // Create score text scoreText = new Text2('Score: 0', { size: 60, fill: 0xFFFFFF }); scoreText.anchor.set(0, 0); LK.gui.topLeft.addChild(scoreText); scoreText.x = 120; scoreText.y = 20; // Create target indicator targetObject = LK.getAsset('target', { width: 30, height: 30, color: 0x0000ff, shape: 'ellipse', anchorX: 0.5, anchorY: 0.5 }); game.addChild(targetObject); } // Initialize the game init(); initVisuals(); // LK game update loop game.update = function () { if (gameState === 'play') { update(); // Update score display if (scoreText) { scoreText.setText('Score: ' + score); } // Update target position if (targetObject && !snake.lastArm.arrived) { targetObject.x = target.x; targetObject.y = target.y; targetObject.visible = true; } else if (targetObject) { targetObject.visible = false; } // Update snake visual segments updateSnakeVisuals(); // Update apple visuals updateAppleVisuals(); } }; function updateSnakeVisuals() { // Clear existing snake segments for (var i = 0; i < snakeSegments.length; i++) { snakeSegments[i].destroy(); } snakeSegments = []; // Create new snake segments for (var s = 0; s < snake.sgms.length; s++) { var seg = snake.sgms[s]; var segmentVisual = LK.getAsset('snakeSegment', { width: snakeWidth, height: segmentLength, color: 0x64ff64, shape: 'box', anchorX: 0.5, anchorY: 0 }); segmentVisual.x = seg.x; segmentVisual.y = seg.y; segmentVisual.rotation = seg.angle + Math.PI / 2; game.addChild(segmentVisual); snakeSegments.push(segmentVisual); } // Create snake head var head = LK.getAsset('snakeHead', { width: appleWidth * 1.5, height: appleWidth * 1.5, color: 0x32ff32, shape: 'ellipse', anchorX: 0.5, anchorY: 0.5 }); head.x = snake.lastArm.endX(); head.y = snake.lastArm.endY(); game.addChild(head); snakeSegments.push(head); } function updateAppleVisuals() { // Remove apples that no longer exist for (var i = appleObjects.length - 1; i >= 0; i--) { var found = false; for (var a = 0; a < apples.length; a++) { if (Math.abs(appleObjects[i].x - apples[a].x) < 5 && Math.abs(appleObjects[i].y - apples[a].y) < 5) { found = true; break; } } if (!found) { appleObjects[i].destroy(); appleObjects.splice(i, 1); } } } ; ;
===================================================================
--- original.js
+++ change.js
@@ -1,416 +1,419 @@
/****
-* Plugins
-****/
-var tween = LK.import("@upit/tween.v1");
-
-/****
-* Classes
-****/
-var Bullet = Container.expand(function () {
- var self = Container.call(this);
- var bulletGraphics = self.attachAsset('bullet', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 15;
- self.damage = 10;
- self.update = function () {
- self.y -= self.speed;
- };
- return self;
-});
-var Enemy = Container.expand(function (type) {
- var self = Container.call(this);
- self.type = type || 'scout';
- self.shootTimer = 0;
- self.shootDelay = 60;
- self.speed = 2;
- self.health = 20;
- self.maxHealth = 20;
- var enemyGraphics;
- if (self.type === 'scout') {
- enemyGraphics = self.attachAsset('enemy_scout', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.health = 20;
- self.maxHealth = 20;
- self.speed = 3;
- self.shootDelay = 90;
- } else if (self.type === 'fighter') {
- enemyGraphics = self.attachAsset('enemy_fighter', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.health = 40;
- self.maxHealth = 40;
- self.speed = 2;
- self.shootDelay = 60;
- } else if (self.type === 'boss') {
- enemyGraphics = self.attachAsset('enemy_boss', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.health = 150;
- self.maxHealth = 150;
- self.speed = 1;
- self.shootDelay = 30;
- }
- self.takeDamage = function (damage) {
- self.health -= damage;
- LK.effects.flashObject(self, 0xFF0000, 200);
- LK.getSound('enemy_hit').play();
- if (self.health <= 0) {
- self.destroyed = true;
- // Drop resources
- for (var i = 0; i < Math.random() * 3 + 1; i++) {
- var resourceType = ['crystal', 'metal', 'energy'][Math.floor(Math.random() * 3)];
- var resource = new Resource(resourceType);
- resource.x = self.x + (Math.random() - 0.5) * 50;
- resource.y = self.y + (Math.random() - 0.5) * 50;
- resources.push(resource);
- game.addChild(resource);
- }
- }
- };
- self.update = function () {
- if (self.destroyed) return;
- self.y += self.speed;
- // Simple AI movement
- if (spaceship) {
- var dx = spaceship.x - self.x;
- self.x += dx * 0.005;
- }
- // Shooting
- self.shootTimer++;
- if (self.shootTimer >= self.shootDelay) {
- self.shootTimer = 0;
- var enemyBullet = new EnemyBullet();
- enemyBullet.x = self.x;
- enemyBullet.y = self.y + 20;
- enemyBullets.push(enemyBullet);
- game.addChild(enemyBullet);
- }
- };
- return self;
-});
-var EnemyBullet = Container.expand(function () {
- var self = Container.call(this);
- var bulletGraphics = self.attachAsset('bullet', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- bulletGraphics.tint = 0xFF4444;
- self.speed = 8;
- self.damage = 15;
- self.update = function () {
- self.y += self.speed;
- };
- return self;
-});
-var Resource = Container.expand(function (type) {
- var self = Container.call(this);
- self.type = type || 'crystal';
- self.value = 1;
- var resourceGraphics;
- if (self.type === 'crystal') {
- resourceGraphics = self.attachAsset('crystal', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.value = 5;
- } else if (self.type === 'metal') {
- resourceGraphics = self.attachAsset('metal', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.value = 3;
- } else if (self.type === 'energy') {
- resourceGraphics = self.attachAsset('energy', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.value = 2;
- }
- self.floatSpeed = Math.random() * 2 + 1;
- self.floatDirection = Math.random() * Math.PI * 2;
- self.update = function () {
- self.y += self.floatSpeed;
- self.x += Math.sin(self.floatDirection + LK.ticks * 0.02) * 0.5;
- };
- return self;
-});
-var Spaceship = Container.expand(function () {
- var self = Container.call(this);
- var shipGraphics = self.attachAsset('spaceship', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.health = 100;
- self.maxHealth = 100;
- self.shootTimer = 0;
- self.shootDelay = 15;
- self.weaponLevel = 1;
- self.shieldLevel = 1;
- self.speedLevel = 1;
- self.takeDamage = function (damage) {
- var actualDamage = Math.max(1, damage - self.shieldLevel);
- self.health -= actualDamage;
- LK.effects.flashObject(self, 0xFF0000, 300);
- LK.getSound('player_hit').play();
- if (self.health <= 0) {
- LK.showGameOver();
- }
- };
- self.shoot = function () {
- if (self.shootTimer <= 0) {
- var bullet = new Bullet();
- bullet.x = self.x;
- bullet.y = self.y - 50;
- bullet.damage = 10 + (self.weaponLevel - 1) * 5;
- bullets.push(bullet);
- game.addChild(bullet);
- LK.getSound('shoot').play();
- self.shootTimer = Math.max(5, self.shootDelay - (self.weaponLevel - 1) * 3);
- }
- };
- self.update = function () {
- if (self.shootTimer > 0) {
- self.shootTimer--;
- }
- // Auto-shoot when enemies are nearby
- for (var i = 0; i < enemies.length; i++) {
- var enemy = enemies[i];
- if (!enemy.destroyed) {
- var distance = Math.sqrt(Math.pow(enemy.x - self.x, 2) + Math.pow(enemy.y - self.y, 2));
- if (distance < 400) {
- self.shoot();
- break;
- }
- }
- }
- };
- return self;
-});
-
-/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000011
+ backgroundColor: 0x000000
});
/****
* Game Code
****/
-// Game variables
-var spaceship;
-var bullets = [];
-var enemyBullets = [];
-var enemies = [];
-var resources = [];
-var waveTimer = 0;
-var waveNumber = 1;
-var resourceSpawnTimer = 0;
-var playerResources = {
- crystal: 0,
- metal: 0,
- energy: 0
+// Game configuration
+var segmentLength = 40,
+ startingSegments = 10,
+ spawn = {
+ x: 1024,
+ y: 1366
+ },
+ snakeSpeed = 8,
+ maxApples = 3,
+ appleLife = 300,
+ segmentsPerApple = 2,
+ snakeWidth = 30,
+ appleWidth = 25,
+ cursorSize = 20,
+ 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;
};
-var dragNode = null;
-// UI Elements
-var scoreText = new Text2('Score: 0', {
- size: 40,
- fill: 0xFFFFFF
-});
-scoreText.anchor.set(0, 0);
-scoreText.x = 120;
-scoreText.y = 20;
-LK.gui.topLeft.addChild(scoreText);
-var healthText = new Text2('Health: 100', {
- size: 40,
- fill: 0xFF4444
-});
-healthText.anchor.set(0.5, 0);
-LK.gui.top.addChild(healthText);
-var waveText = new Text2('Wave: 1', {
- size: 40,
- fill: 0x44FF44
-});
-waveText.anchor.set(1, 0);
-waveText.x = -20;
-waveText.y = 20;
-LK.gui.topRight.addChild(waveText);
-var resourceText = new Text2('Crystals: 0 | Metal: 0 | Energy: 0', {
- size: 35,
- fill: 0xFFFF44
-});
-resourceText.anchor.set(0.5, 1);
-resourceText.y = -20;
-LK.gui.bottom.addChild(resourceText);
-// Initialize spaceship
-spaceship = game.addChild(new Spaceship());
-spaceship.x = 1024;
-spaceship.y = 2200;
-function spawnWave() {
- var enemyCount = Math.min(3 + waveNumber, 8);
- var bossChance = waveNumber % 5 === 0;
- for (var i = 0; i < enemyCount; i++) {
- var enemy;
- if (bossChance && i === 0) {
- enemy = new Enemy('boss');
- } else if (waveNumber > 3 && Math.random() < 0.3) {
- enemy = new Enemy('fighter');
+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 {
- enemy = new Enemy('scout');
+ this.arrived = true;
}
- enemy.x = Math.random() * 1800 + 124;
- enemy.y = -100 - i * 80;
- enemies.push(enemy);
- game.addChild(enemy);
+ this.currentDist = distance({
+ x: this.endX(),
+ y: this.endY()
+ }, {
+ x: this.targetX,
+ y: this.targetY
+ });
}
- waveNumber++;
- waveText.setText('Wave: ' + waveNumber);
+ 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);
+ }
+};
+SGM.prototype.render = function (context) {
+ context.lineTo(this.endX(), this.endY());
+};
+function IKR(x, y) {
+ this.ix = x || 0;
+ this.iy = y || 0;
+ this.sgms = [];
+ this.lastArm = null;
}
-function spawnResource() {
- if (Math.random() < 0.7) {
- var resourceType = ['crystal', 'metal', 'energy'][Math.floor(Math.random() * 3)];
- var resource = new Resource(resourceType);
- resource.x = Math.random() * 1800 + 124;
- resource.y = -50;
- resources.push(resource);
- game.addChild(resource);
+;
+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 CUR(x, y) {
+ this.x = x;
+ this.y = y;
+ this.rotation = 0;
}
-function updateUI() {
- scoreText.setText('Score: ' + LK.getScore());
- healthText.setText('Health: ' + spaceship.health);
- resourceText.setText('Crystals: ' + playerResources.crystal + ' | Metal: ' + playerResources.metal + ' | Energy: ' + playerResources.energy);
+;
+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;
}
-function handleMove(x, y, obj) {
- if (dragNode) {
- dragNode.x = Math.max(40, Math.min(2008, x));
- dragNode.y = Math.max(50, Math.min(2682, y));
+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);
+ apples = [];
+ score = 0;
+ for (var i = 0; i < startingSegments; i++) {
+ snake.addSeg();
+ }
+ snake.lastArm.target(target.x, target.y);
+ gameState = 'play';
}
-game.move = handleMove;
+init();
+// Touch events handled by game object
game.down = function (x, y, obj) {
- dragNode = spaceship;
- handleMove(x, y, obj);
+ switch (gameState) {
+ case 'play':
+ target.x = x;
+ target.y = y;
+ snake.lastArm.target(target.x, target.y);
+ break;
+ case 'dead':
+ init();
+ break;
+ }
};
-game.up = function (x, y, obj) {
- dragNode = null;
-};
-// Main game loop
-game.update = function () {
- // Spawn waves
- waveTimer++;
- if (waveTimer >= 300) {
- // 5 seconds at 60fps
- spawnWave();
- waveTimer = 0;
+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;
+ }
}
- // Spawn resources
- resourceSpawnTimer++;
- if (resourceSpawnTimer >= 120) {
- // 2 seconds
- spawnResource();
- resourceSpawnTimer = 0;
+ return false;
+}
+function addScoreSegments() {
+ for (var i = 0; i < segmentsPerApple; i++) {
+ snake.grow();
}
- // Update bullets
- for (var i = bullets.length - 1; i >= 0; i--) {
- var bullet = bullets[i];
- if (bullet.lastY === undefined) bullet.lastY = bullet.y;
- // Remove off-screen bullets
- if (bullet.lastY >= -20 && bullet.y < -20) {
- bullet.destroy();
- bullets.splice(i, 1);
- continue;
+}
+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;
}
- // Check bullet-enemy collisions
- for (var j = enemies.length - 1; j >= 0; j--) {
- var enemy = enemies[j];
- if (!enemy.destroyed && bullet.intersects(enemy)) {
- enemy.takeDamage(bullet.damage);
- bullet.destroy();
- bullets.splice(i, 1);
- if (enemy.destroyed) {
- LK.setScore(LK.getScore() + (enemy.type === 'boss' ? 100 : enemy.type === 'fighter' ? 50 : 25));
- enemy.destroy();
- enemies.splice(j, 1);
+ 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();
}
- break;
}
}
- if (i < bullets.length) {
- bullet.lastY = bullet.y;
- }
- }
- // Update enemy bullets
- for (var i = enemyBullets.length - 1; i >= 0; i--) {
- var enemyBullet = enemyBullets[i];
- if (enemyBullet.lastY === undefined) enemyBullet.lastY = enemyBullet.y;
- // Remove off-screen enemy bullets
- if (enemyBullet.lastY <= 2750 && enemyBullet.y > 2750) {
- enemyBullet.destroy();
- enemyBullets.splice(i, 1);
- continue;
- }
- // Check enemy bullet-player collision
- if (enemyBullet.intersects(spaceship)) {
- spaceship.takeDamage(enemyBullet.damage);
- enemyBullet.destroy();
- enemyBullets.splice(i, 1);
- continue;
- }
- enemyBullet.lastY = enemyBullet.y;
- }
- // Update enemies
- for (var i = enemies.length - 1; i >= 0; i--) {
- var enemy = enemies[i];
- if (enemy.lastY === undefined) enemy.lastY = enemy.y;
- // Remove off-screen enemies
- if (enemy.lastY <= 2800 && enemy.y > 2800) {
- if (!enemy.destroyed) {
- enemy.destroy();
+ for (var a in apples) {
+ var apple = apples[a];
+ apple.update();
+ if (apple.life <= 0) {
+ apples.splice(a, 1);
+ continue;
}
- enemies.splice(i, 1);
- continue;
+ if (distance(apple, {
+ x: snake.lastArm.endX(),
+ y: snake.lastArm.endY()
+ }) < appleWidth * 2) {
+ score += apple.life;
+ apples.splice(a, 1);
+ addScoreSegments();
+ }
}
- // Check enemy-player collision
- if (!enemy.destroyed && enemy.intersects(spaceship)) {
- spaceship.takeDamage(30);
- enemy.takeDamage(enemy.maxHealth);
- if (enemy.destroyed) {
- enemy.destroy();
- enemies.splice(i, 1);
+ if (apples.length < maxApples && Math.random() < 0.02) {
+ var offset = appleWidth * 4,
+ 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));
}
- continue;
+ apples.push(apple);
+ // Create visual apple
+ var appleVisual = LK.getAsset('apple', {
+ width: appleWidth * 2,
+ height: appleWidth * 2,
+ color: 0x00ff00,
+ shape: 'ellipse',
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ appleVisual.x = apple.x;
+ appleVisual.y = apple.y;
+ game.addChild(appleVisual);
+ appleObjects.push(appleVisual);
}
- enemy.lastY = enemy.y;
}
- // Update resources
- for (var i = resources.length - 1; i >= 0; i--) {
- var resource = resources[i];
- if (resource.lastY === undefined) resource.lastY = resource.y;
- // Remove off-screen resources
- if (resource.lastY <= 2800 && resource.y > 2800) {
- resource.destroy();
- resources.splice(i, 1);
- continue;
+}
+// LK rendering system - visual elements handled by game objects
+var snakeSegments = [];
+var appleObjects = [];
+var targetObject = null;
+var scoreText = null;
+// Initialize visual elements
+function initVisuals() {
+ // Clear existing visuals
+ for (var i = 0; i < snakeSegments.length; i++) {
+ snakeSegments[i].destroy();
+ }
+ for (var i = 0; i < appleObjects.length; i++) {
+ appleObjects[i].destroy();
+ }
+ if (targetObject) targetObject.destroy();
+ if (scoreText) scoreText.destroy();
+ snakeSegments = [];
+ appleObjects = [];
+ // Create score text
+ scoreText = new Text2('Score: 0', {
+ size: 60,
+ fill: 0xFFFFFF
+ });
+ scoreText.anchor.set(0, 0);
+ LK.gui.topLeft.addChild(scoreText);
+ scoreText.x = 120;
+ scoreText.y = 20;
+ // Create target indicator
+ targetObject = LK.getAsset('target', {
+ width: 30,
+ height: 30,
+ color: 0x0000ff,
+ shape: 'ellipse',
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ game.addChild(targetObject);
+}
+// Initialize the game
+init();
+initVisuals();
+// LK game update loop
+game.update = function () {
+ if (gameState === 'play') {
+ update();
+ // Update score display
+ if (scoreText) {
+ scoreText.setText('Score: ' + score);
}
- // Check resource collection
- if (resource.intersects(spaceship)) {
- playerResources[resource.type] += resource.value;
- LK.setScore(LK.getScore() + resource.value);
- LK.getSound('collect').play();
- resource.destroy();
- resources.splice(i, 1);
- continue;
+ // Update target position
+ if (targetObject && !snake.lastArm.arrived) {
+ targetObject.x = target.x;
+ targetObject.y = target.y;
+ targetObject.visible = true;
+ } else if (targetObject) {
+ targetObject.visible = false;
}
- resource.lastY = resource.y;
+ // Update snake visual segments
+ updateSnakeVisuals();
+ // Update apple visuals
+ updateAppleVisuals();
}
- updateUI();
};
-// Start the first wave
-spawnWave();
\ No newline at end of file
+function updateSnakeVisuals() {
+ // Clear existing snake segments
+ for (var i = 0; i < snakeSegments.length; i++) {
+ snakeSegments[i].destroy();
+ }
+ snakeSegments = [];
+ // Create new snake segments
+ for (var s = 0; s < snake.sgms.length; s++) {
+ var seg = snake.sgms[s];
+ var segmentVisual = LK.getAsset('snakeSegment', {
+ width: snakeWidth,
+ height: segmentLength,
+ color: 0x64ff64,
+ shape: 'box',
+ anchorX: 0.5,
+ anchorY: 0
+ });
+ segmentVisual.x = seg.x;
+ segmentVisual.y = seg.y;
+ segmentVisual.rotation = seg.angle + Math.PI / 2;
+ game.addChild(segmentVisual);
+ snakeSegments.push(segmentVisual);
+ }
+ // Create snake head
+ var head = LK.getAsset('snakeHead', {
+ width: appleWidth * 1.5,
+ height: appleWidth * 1.5,
+ color: 0x32ff32,
+ shape: 'ellipse',
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ head.x = snake.lastArm.endX();
+ head.y = snake.lastArm.endY();
+ game.addChild(head);
+ snakeSegments.push(head);
+}
+function updateAppleVisuals() {
+ // Remove apples that no longer exist
+ for (var i = appleObjects.length - 1; i >= 0; i--) {
+ var found = false;
+ for (var a = 0; a < apples.length; a++) {
+ if (Math.abs(appleObjects[i].x - apples[a].x) < 5 && Math.abs(appleObjects[i].y - apples[a].y) < 5) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ appleObjects[i].destroy();
+ appleObjects.splice(i, 1);
+ }
+ }
+}
+;
+;
\ No newline at end of file