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 ****/ var cnv = document.getElementById('gameCanvas'), ctx = cnv.getContext('2d'), segmentLength = 10, startingSegments = 20, spawn = { x: 0, y: cnv.height / 2 }, snakeSpeed = 5, maxApples = 5, appleLife = 500, segmentsPerApple = 3, snakeWidth = 5, appleWidth = 5, cursorSize = 10, 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, snake, cursor, 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(); cnv.addEventListener('mousemove', function (e) { switch (gameState) { case 'play': cursor.x = e.offsetX; cursor.y = e.offsetY; break; } }); cnv.addEventListener('mousedown', function (e) { switch (gameState) { case 'play': target.x = e.offsetX; target.y = e.offsetY; 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() > cnv.width - 2 || snake.lastArm.endX() < 2 || snake.lastArm.endY() > cnv.height - 2 || snake.lastArm.endY() < 2) { gameState = 'dead'; deathMeans = 'Duvara çarptın...'; 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() < .1) { var offset = appleWidth * 10, apple = new Apple(offset / 2 + Math.floor(Math.random() * (cnv.width - offset)), offset / 2 + Math.floor(Math.random() * (cnv.height - offset))); while (badPlacement(apple)) { apple.x = offset / 2 + Math.floor(Math.random() * (cnv.width - offset)); apple.y = offset / 2 + Math.floor(Math.random() * (cnv.height - offset)); } apples.push(apple); } } } function drawTarget(targetModFactor) { if (!snake.lastArm.arrived) { ctx.strokeStyle = 'rgba(' + (targetColor[0] + targetColorMod[0] * targetModFactor).toFixed(0) + ',' + (targetColor[1] + targetColorMod[1] * targetModFactor).toFixed(0) + ',' + (targetColor[2] + targetColorMod[2] * targetModFactor).toFixed(0) + ',' + (targetColor[3] + targetColorMod[3] * targetModFactor).toFixed(0) + ')'; ctx.lineWidth = 1; target.render(ctx); } } function drawSnake() { ctx.beginPath(); ctx.strokeStyle = ctx.fillStyle = 'rgba(' + snakeColor[0] + ',' + snakeColor[1] + ',' + snakeColor[2] + ',' + snakeColor[3] + ')'; ctx.lineWidth = snakeWidth; ctx.moveTo(snake.sgms[0].x, snake.sgms[0].y); for (var s in snake.sgms) { snake.sgms[s].render(ctx); } ctx.stroke(); ctx.beginPath(); ctx.arc(snake.lastArm.endX(), snake.lastArm.endY(), appleWidth * .75, 0, Math.PI * 2); ctx.fill(); } function drawCursor(targetModFactor) { ctx.strokeStyle = 'rgba(' + (cursorColor[0] + cursorColorMod[0] * targetModFactor).toFixed(0) + ',' + (cursorColor[1] + cursorColorMod[1] * targetModFactor).toFixed(0) + ',' + (cursorColor[2] + cursorColorMod[2] * targetModFactor).toFixed(0) + ',' + (cursorColor[3] + cursorColorMod[3] * targetModFactor).toFixed(0) + ')'; ctx.lineWidth = 1; cursor.render(ctx); } function drawApples() { ctx.fillStyle = 'rgba(' + appleColor[0] + ',' + appleColor[1] + ',' + appleColor[2] + ',' + appleColor[3] + ')'; for (var a in apples) { var apple = apples[a], appleTargetMod = 1 - apple.life / appleLife; ctx.strokeStyle = 'rgba(' + (cursorColor[0] + cursorColorMod[0] * appleTargetMod).toFixed(0) + ',' + (cursorColor[1] + cursorColorMod[1] * appleTargetMod).toFixed(0) + ',' + (cursorColor[2] + cursorColorMod[2] * appleTargetMod).toFixed(0) + ',' + (cursorColor[3] + cursorColorMod[3] * appleTargetMod).toFixed(0) + ')'; ctx.lineWidth = 1; apple.render(ctx); } } function render() { var targetModFactor = 1 - snake.lastArm.currentDist / snake.lastArm.totalDist; switch (gameState) { case 'play': ctx.clearRect(0, 0, cnv.width, cnv.height); drawTarget(targetModFactor); drawSnake(); drawApples(); drawCursor(targetModFactor); ctx.fillStyle = 'rgba(' + scoreColor[0] + ',' + scoreColor[1] + ',' + scoreColor[2] + ',' + scoreColor[3] + ')'; ctx.textAlign = 'left'; ctx.textBaseline = 'top'; ctx.fillText('Puan: ' + score, 10, 10); break; case 'dead': ctx.clearRect(0, 0, cnv.width, cnv.height); drawSnake(); drawApples(); ctx.fillStyle = 'rgba(255,0,0,0.5)'; ctx.fillRect(100, 100, cnv.width - 200, cnv.height - 200); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillStyle = ctx.strokeStyle = 'white'; ctx.font = 'bold 30px sans-serif'; ctx.fillText('Öldün', cnv.width / 2, cnv.height / 2 - 70); ctx.font = 'bold 25px sans-serif'; ctx.fillText(deathMeans, cnv.width / 2, cnv.height / 2 - 30); ctx.font = '20px sans-serif'; ctx.fillText('Puan:', cnv.width / 2, cnv.height / 2 + 15); ctx.font = 'bold 60px sans-serif'; ctx.fillText(score, cnv.width / 2, cnv.height / 2 + 60); ctx.font = 'lighter 10px sans-serif'; ctx.lineWidth = 1; ctx.strokeRect(103, 103, cnv.width - 206, cnv.height - 206); break; } } function animate() { update(); render(); requestAnimationFrame(animate); } snake.lastArm.target(target.x, target.y); animate();
===================================================================
--- original.js
+++ change.js
@@ -1,368 +1,383 @@
/****
-* Classes
-****/
-var Bullet = Container.expand(function () {
- var self = Container.call(this);
- var bulletGraphics = self.attachAsset('bullet', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.speed = 12;
- self.isPlayerBullet = false;
- self.update = function () {
- if (self.isPlayerBullet) {
- self.y -= self.speed;
- } else {
- self.y += self.speed;
- }
- // Remove if off screen
- if (self.y < -50 || self.y > 2732 + 50) {
- self.destroy();
- }
- };
- return self;
-});
-var Enemy = Container.expand(function () {
- var self = Container.call(this);
- var enemyGraphics = self.attachAsset('enemy', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.health = 50;
- self.speed = 3;
- self.shootCooldown = 0;
- self.movePattern = Math.random() * 3; // 0-2 for different movement patterns
- self.update = function () {
- // Movement patterns
- if (self.movePattern < 1) {
- self.y += self.speed;
- } else if (self.movePattern < 2) {
- self.y += self.speed;
- self.x += Math.sin(self.y * 0.01) * 2;
- } else {
- self.y += self.speed * 0.8;
- self.x += (player.x - self.x) * 0.01;
- }
- // Shoot occasionally
- if (self.shootCooldown > 0) {
- self.shootCooldown--;
- } else if (Math.random() < 0.005) {
- self.shoot();
- }
- // Remove if off screen
- if (self.y > 2732 + 100) {
- self.destroy();
- }
- };
- self.shoot = function () {
- var bullet = new Bullet();
- bullet.x = self.x;
- bullet.y = self.y + 25;
- bullet.isPlayerBullet = false;
- bullets.push(bullet);
- game.addChild(bullet);
- self.shootCooldown = 60 + Math.random() * 60;
- };
- return self;
-});
-var Player = Container.expand(function () {
- var self = Container.call(this);
- var playerGraphics = self.attachAsset('player', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.health = 100;
- self.maxHealth = 100;
- self.shootCooldown = 0;
- self.speed = 8;
- self.update = function () {
- if (self.shootCooldown > 0) {
- self.shootCooldown--;
- }
- };
- self.shoot = function () {
- if (self.shootCooldown <= 0) {
- var bullet = new Bullet();
- bullet.x = self.x;
- bullet.y = self.y - 40;
- bullet.isPlayerBullet = true;
- bullets.push(bullet);
- game.addChild(bullet);
- self.shootCooldown = 10;
- LK.getSound('shoot').play();
- }
- };
- return self;
-});
-var Resource = Container.expand(function () {
- var self = Container.call(this);
- var resourceGraphics = self.attachAsset('resource', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.value = 10;
- self.speed = 2;
- self.floatOffset = Math.random() * Math.PI * 2;
- self.update = function () {
- self.y += self.speed;
- self.x += Math.sin(LK.ticks * 0.05 + self.floatOffset) * 0.5;
- // Remove if off screen
- if (self.y > 2732 + 50) {
- self.destroy();
- }
- };
- return self;
-});
-
-/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000033
+ backgroundColor: 0x000000
});
/****
* Game Code
****/
-// Game variables
-var player;
-var bullets = [];
-var enemies = [];
-var resources = [];
-var wave = 1;
-var enemySpawnTimer = 0;
-var resourceSpawnTimer = 0;
-var gameState = 'playing';
-var dragNode = null;
-var resourceCount = 0;
-var upgradeLevel = 1;
-// Utility functions
-function distance(obj1, obj2) {
- var dx = obj1.x - obj2.x;
- var dy = obj1.y - obj2.y;
+var cnv = document.getElementById('gameCanvas'),
+ ctx = cnv.getContext('2d'),
+ segmentLength = 10,
+ startingSegments = 20,
+ spawn = {
+ x: 0,
+ y: cnv.height / 2
+ },
+ snakeSpeed = 5,
+ maxApples = 5,
+ appleLife = 500,
+ segmentsPerApple = 3,
+ snakeWidth = 5,
+ appleWidth = 5,
+ cursorSize = 10,
+ 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,
+ snake,
+ cursor,
+ 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 spawnEnemy() {
- var enemy = new Enemy();
- enemy.x = Math.random() * 1800 + 124; // Keep away from edges
- enemy.y = -100;
- enemies.push(enemy);
- game.addChild(enemy);
+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 spawnResource() {
- var resource = new Resource();
- resource.x = Math.random() * 1800 + 124;
- resource.y = -50;
- resources.push(resource);
- game.addChild(resource);
+function SGM(angle, x, y) {
+ this.x = x || 0;
+ this.y = y || 0;
+ this.angle = angle || 0;
+ this.parent = null;
}
-function checkCollisions() {
- // Player bullets vs enemies
- for (var b = bullets.length - 1; b >= 0; b--) {
- var bullet = bullets[b];
- if (!bullet.isPlayerBullet) continue;
- for (var e = enemies.length - 1; e >= 0; e--) {
- var enemy = enemies[e];
- if (bullet.intersects(enemy)) {
- enemy.health -= 25;
- bullet.destroy();
- bullets.splice(b, 1);
- LK.getSound('hit').play();
- if (enemy.health <= 0) {
- LK.setScore(LK.getScore() + 100);
- enemy.destroy();
- enemies.splice(e, 1);
- }
- break;
- }
+;
+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
+ });
}
- // Enemy bullets vs player
- for (var b = bullets.length - 1; b >= 0; b--) {
- var bullet = bullets[b];
- if (bullet.isPlayerBullet) continue;
- if (bullet.intersects(player)) {
- player.health -= 10;
- bullet.destroy();
- bullets.splice(b, 1);
- LK.effects.flashObject(player, 0xff0000, 300);
- if (player.health <= 0) {
- gameState = 'gameOver';
- LK.showGameOver();
- }
- }
+ 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);
}
- // Player vs resources
- for (var r = resources.length - 1; r >= 0; r--) {
- var resource = resources[r];
- if (distance(player, resource) < 40) {
- resourceCount += resource.value;
- LK.setScore(LK.getScore() + resource.value);
- LK.getSound('collect').play();
- resource.destroy();
- resources.splice(r, 1);
- }
- }
- // Player vs enemies (collision damage)
- for (var e = 0; e < enemies.length; e++) {
- var enemy = enemies[e];
- if (distance(player, enemy) < 50) {
- player.health -= 5;
- enemy.health -= 25;
- LK.effects.flashObject(player, 0xff0000, 200);
- if (player.health <= 0) {
- gameState = 'gameOver';
- LK.showGameOver();
- }
- if (enemy.health <= 0) {
- LK.setScore(LK.getScore() + 50);
- enemy.destroy();
- enemies.splice(e, 1);
- }
- }
- }
+};
+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 cleanupObjects() {
- // Clean up bullets
- for (var b = bullets.length - 1; b >= 0; b--) {
- var bullet = bullets[b];
- if (bullet.y < -100 || bullet.y > 2832) {
- bullet.destroy();
- bullets.splice(b, 1);
- }
+;
+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;
}
- // Clean up enemies
- for (var e = enemies.length - 1; e >= 0; e--) {
- var enemy = enemies[e];
- if (enemy.y > 2832) {
- enemy.destroy();
- enemies.splice(e, 1);
- }
- }
- // Clean up resources
- for (var r = resources.length - 1; r >= 0; r--) {
- var resource = resources[r];
- if (resource.y > 2832) {
- resource.destroy();
- resources.splice(r, 1);
- }
- }
+ 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;
}
-// Initialize game
-function initGame() {
- player = new Player();
- player.x = 1024;
- player.y = 2200;
- game.addChild(player);
- bullets = [];
- enemies = [];
- resources = [];
- wave = 1;
- enemySpawnTimer = 0;
- resourceSpawnTimer = 0;
- gameState = 'playing';
- resourceCount = 0;
- upgradeLevel = 1;
- LK.setScore(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;
}
-// Create UI elements
-var scoreTxt = new Text2('Score: 0', {
- size: 60,
- fill: 0xFFFFFF
-});
-scoreTxt.anchor.set(0, 0);
-scoreTxt.x = 120;
-scoreTxt.y = 20;
-LK.gui.topLeft.addChild(scoreTxt);
-var healthTxt = new Text2('Health: 100', {
- size: 50,
- fill: 0x00FF00
-});
-healthTxt.anchor.set(0, 0);
-healthTxt.x = 120;
-healthTxt.y = 90;
-LK.gui.topLeft.addChild(healthTxt);
-var resourceTxt = new Text2('Resources: 0', {
- size: 50,
- fill: 0xFFFF00
-});
-resourceTxt.anchor.set(0, 0);
-resourceTxt.x = 120;
-resourceTxt.y = 150;
-LK.gui.topLeft.addChild(resourceTxt);
-var waveTxt = new Text2('Wave: 1', {
- size: 50,
- fill: 0x00FFFF
-});
-waveTxt.anchor.set(1, 0);
-waveTxt.x = 1928;
-waveTxt.y = 20;
-LK.gui.topRight.addChild(waveTxt);
-// Event handlers
-game.move = function (x, y, obj) {
- if (gameState === 'playing' && dragNode) {
- dragNode.x = Math.max(50, Math.min(1998, x));
- dragNode.y = Math.max(50, Math.min(2682, y));
- }
+Apple.prototype.update = function () {
+ this.life--;
};
-game.down = function (x, y, obj) {
- if (gameState === 'playing') {
- dragNode = player;
- player.shoot();
+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);
}
};
-game.up = function (x, y, obj) {
- dragNode = null;
-};
-// Main game loop
-game.update = function () {
- if (gameState !== 'playing') return;
- // Update player
- player.update();
- // Update bullets
- for (var b = 0; b < bullets.length; b++) {
- bullets[b].update();
+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();
}
- // Update enemies
- for (var e = 0; e < enemies.length; e++) {
- enemies[e].update();
+ snake.lastArm.target(target.x, target.y);
+ gameState = 'play';
+}
+init();
+cnv.addEventListener('mousemove', function (e) {
+ switch (gameState) {
+ case 'play':
+ cursor.x = e.offsetX;
+ cursor.y = e.offsetY;
+ break;
}
- // Update resources
- for (var r = 0; r < resources.length; r++) {
- resources[r].update();
+});
+cnv.addEventListener('mousedown', function (e) {
+ switch (gameState) {
+ case 'play':
+ target.x = e.offsetX;
+ target.y = e.offsetY;
+ snake.lastArm.target(target.x, target.y);
+ break;
+ case 'dead':
+ init();
+ break;
}
- // Spawn enemies
- enemySpawnTimer++;
- var spawnRate = Math.max(60 - wave * 5, 20);
- if (enemySpawnTimer >= spawnRate) {
- spawnEnemy();
- enemySpawnTimer = 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 >= 180) {
- spawnResource();
- resourceSpawnTimer = 0;
+ return false;
+}
+function addScoreSegments() {
+ for (var i = 0; i < segmentsPerApple; i++) {
+ snake.grow();
}
- // Auto-shoot
- if (LK.ticks % 15 === 0) {
- player.shoot();
+}
+function update() {
+ if (gameState !== 'dead') {
+ snake.lastArm.gotoTarget();
+ if (snake.lastArm.endX() > cnv.width - 2 || snake.lastArm.endX() < 2 || snake.lastArm.endY() > cnv.height - 2 || snake.lastArm.endY() < 2) {
+ gameState = 'dead';
+ deathMeans = 'Duvara çarptın...';
+ 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() < .1) {
+ var offset = appleWidth * 10,
+ apple = new Apple(offset / 2 + Math.floor(Math.random() * (cnv.width - offset)), offset / 2 + Math.floor(Math.random() * (cnv.height - offset)));
+ while (badPlacement(apple)) {
+ apple.x = offset / 2 + Math.floor(Math.random() * (cnv.width - offset));
+ apple.y = offset / 2 + Math.floor(Math.random() * (cnv.height - offset));
+ }
+ apples.push(apple);
+ }
}
- // Check collisions
- checkCollisions();
- // Clean up off-screen objects
- cleanupObjects();
- // Update wave progression
- if (LK.ticks % 1800 === 0) {
- // Every 30 seconds
- wave++;
- waveTxt.setText('Wave: ' + wave);
+}
+function drawTarget(targetModFactor) {
+ if (!snake.lastArm.arrived) {
+ ctx.strokeStyle = 'rgba(' + (targetColor[0] + targetColorMod[0] * targetModFactor).toFixed(0) + ',' + (targetColor[1] + targetColorMod[1] * targetModFactor).toFixed(0) + ',' + (targetColor[2] + targetColorMod[2] * targetModFactor).toFixed(0) + ',' + (targetColor[3] + targetColorMod[3] * targetModFactor).toFixed(0) + ')';
+ ctx.lineWidth = 1;
+ target.render(ctx);
}
- // Update UI
- scoreTxt.setText('Score: ' + LK.getScore());
- healthTxt.setText('Health: ' + player.health);
- healthTxt.fill = player.health > 50 ? 0x00FF00 : player.health > 25 ? 0xFFFF00 : 0xFF0000;
- resourceTxt.setText('Resources: ' + resourceCount);
-};
-// Start the game
-initGame();
\ No newline at end of file
+}
+function drawSnake() {
+ ctx.beginPath();
+ ctx.strokeStyle = ctx.fillStyle = 'rgba(' + snakeColor[0] + ',' + snakeColor[1] + ',' + snakeColor[2] + ',' + snakeColor[3] + ')';
+ ctx.lineWidth = snakeWidth;
+ ctx.moveTo(snake.sgms[0].x, snake.sgms[0].y);
+ for (var s in snake.sgms) {
+ snake.sgms[s].render(ctx);
+ }
+ ctx.stroke();
+ ctx.beginPath();
+ ctx.arc(snake.lastArm.endX(), snake.lastArm.endY(), appleWidth * .75, 0, Math.PI * 2);
+ ctx.fill();
+}
+function drawCursor(targetModFactor) {
+ ctx.strokeStyle = 'rgba(' + (cursorColor[0] + cursorColorMod[0] * targetModFactor).toFixed(0) + ',' + (cursorColor[1] + cursorColorMod[1] * targetModFactor).toFixed(0) + ',' + (cursorColor[2] + cursorColorMod[2] * targetModFactor).toFixed(0) + ',' + (cursorColor[3] + cursorColorMod[3] * targetModFactor).toFixed(0) + ')';
+ ctx.lineWidth = 1;
+ cursor.render(ctx);
+}
+function drawApples() {
+ ctx.fillStyle = 'rgba(' + appleColor[0] + ',' + appleColor[1] + ',' + appleColor[2] + ',' + appleColor[3] + ')';
+ for (var a in apples) {
+ var apple = apples[a],
+ appleTargetMod = 1 - apple.life / appleLife;
+ ctx.strokeStyle = 'rgba(' + (cursorColor[0] + cursorColorMod[0] * appleTargetMod).toFixed(0) + ',' + (cursorColor[1] + cursorColorMod[1] * appleTargetMod).toFixed(0) + ',' + (cursorColor[2] + cursorColorMod[2] * appleTargetMod).toFixed(0) + ',' + (cursorColor[3] + cursorColorMod[3] * appleTargetMod).toFixed(0) + ')';
+ ctx.lineWidth = 1;
+ apple.render(ctx);
+ }
+}
+function render() {
+ var targetModFactor = 1 - snake.lastArm.currentDist / snake.lastArm.totalDist;
+ switch (gameState) {
+ case 'play':
+ ctx.clearRect(0, 0, cnv.width, cnv.height);
+ drawTarget(targetModFactor);
+ drawSnake();
+ drawApples();
+ drawCursor(targetModFactor);
+ ctx.fillStyle = 'rgba(' + scoreColor[0] + ',' + scoreColor[1] + ',' + scoreColor[2] + ',' + scoreColor[3] + ')';
+ ctx.textAlign = 'left';
+ ctx.textBaseline = 'top';
+ ctx.fillText('Puan: ' + score, 10, 10);
+ break;
+ case 'dead':
+ ctx.clearRect(0, 0, cnv.width, cnv.height);
+ drawSnake();
+ drawApples();
+ ctx.fillStyle = 'rgba(255,0,0,0.5)';
+ ctx.fillRect(100, 100, cnv.width - 200, cnv.height - 200);
+ ctx.textAlign = 'center';
+ ctx.textBaseline = 'middle';
+ ctx.fillStyle = ctx.strokeStyle = 'white';
+ ctx.font = 'bold 30px sans-serif';
+ ctx.fillText('Öldün', cnv.width / 2, cnv.height / 2 - 70);
+ ctx.font = 'bold 25px sans-serif';
+ ctx.fillText(deathMeans, cnv.width / 2, cnv.height / 2 - 30);
+ ctx.font = '20px sans-serif';
+ ctx.fillText('Puan:', cnv.width / 2, cnv.height / 2 + 15);
+ ctx.font = 'bold 60px sans-serif';
+ ctx.fillText(score, cnv.width / 2, cnv.height / 2 + 60);
+ ctx.font = 'lighter 10px sans-serif';
+ ctx.lineWidth = 1;
+ ctx.strokeRect(103, 103, cnv.width - 206, cnv.height - 206);
+ break;
+ }
+}
+function animate() {
+ update();
+ render();
+ requestAnimationFrame(animate);
+}
+snake.lastArm.target(target.x, target.y);
+animate();
\ No newline at end of file