/**** * Classes ****/ var SnakeBodySegment = Container.expand(function () { var self = Container.call(this); var bodySegmentGraphics = self.attachAsset('snakeBodySegment', { anchorX: 0.5, anchorY: 0.5 }); bodySegmentGraphics.rotation = Math.PI / 2; bodySegmentGraphics.tint = 0x6fa8a8; }); var FingerIndicator = Container.expand(function () { var self = Container.call(this); var fingerIndicatorGraphics = self.attachAsset('fingerIndicator', { anchorX: 0.5, anchorY: 0.5 }); fingerIndicatorGraphics.alpha = .8; }); var TouchIndicator = Container.expand(function () { var self = Container.call(this); var touchIndicatorGraphics = self.attachAsset('touchIndicator', { anchorX: 0.5, anchorY: 0.5 }); touchIndicatorGraphics.alpha = .4; }); var Snake = Container.expand(function () { var self = Container.call(this); var snakeGraphics = self.attachAsset('snake', { anchorX: 0.5, anchorY: 0.5 }); snakeGraphics.rotation = -Math.PI / 2; self.direction = { x: 0, y: 0 }; self.speed = 7.5; self.angle = 0; self.headPositions = []; self.bodySegments = []; self.addBodySegment = function () { var segment = new SnakeBodySegment(); var lastSegmentPosition = this.bodySegments.length > 0 ? this.bodySegments[this.bodySegments.length - 1] : this; segment.x = lastSegmentPosition.x; segment.y = lastSegmentPosition.y; segment.rotation = lastSegmentPosition.rotation; this.bodySegments.push(segment); return segment; }; self.move = function () { var targetRotation = Math.atan2(self.direction.y, self.direction.x); var rotationDifference = targetRotation - self.rotation; while (rotationDifference < -Math.PI) { rotationDifference += 2 * Math.PI; } while (rotationDifference > Math.PI) { rotationDifference -= 2 * Math.PI; } var turnRate = Math.PI / 96 * 1.625; self.rotation += Math.sign(rotationDifference) * Math.min(Math.abs(rotationDifference), turnRate); self.x = self.x + Math.cos(self.rotation) * self.speed; self.y = self.y + Math.sin(self.rotation) * self.speed; if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) { LK.showGameOver(); } var thresholdDistance = 50; for (var i = 1; i < self.bodySegments.length; i++) { var dx = self.x - self.bodySegments[i].x; var dy = self.y - self.bodySegments[i].y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < thresholdDistance) { LK.showGameOver(); } } self.headPositions.push({ x: self.x, y: self.y, rotation: self.rotation }); var tailSegmentDelay = 20; while (self.headPositions.length > self.bodySegments.length * tailSegmentDelay + 1) { self.headPositions.shift(); } for (var i = 0; i < self.bodySegments.length; i++) { var segmentIndex = (i + 1) * tailSegmentDelay; if (self.headPositions.length > segmentIndex) { var pos = self.headPositions[self.headPositions.length - 1 - segmentIndex]; self.bodySegments[i].x = pos.x; self.bodySegments[i].y = pos.y; self.bodySegments[i].rotation = pos.rotation; } } }; }); var Food = Container.expand(function () { var self = Container.call(this); var foodGraphics = self.attachAsset('food', { anchorX: 0.5, anchorY: 0.5 }); self.spawn = function () { this.x = Math.random() * (2048 - 60) + 30; this.y = Math.random() * (2732 - 60) + 30; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ game.on('up', function (obj) { if (touchIndicator) { touchIndicator.destroy(); touchIndicator = null; } isMouseDown = false; if (fingerIndicator) { fingerIndicator.destroy(); fingerIndicator = null; } snake.direction.x = Math.cos(snake.rotation) * snake.speed; snake.direction.y = Math.sin(snake.rotation) * snake.speed; }); var score = 0; var background = game.attachAsset('background', { anchorX: 0.5, anchorY: 0.5 }); background.x = 2048 / 2; background.y = 2732 / 2; background.alpha = .5; game.setBackgroundColor(0xFFFFFF); var snakeContainer = game.addChild(new Container()); var snake = snakeContainer.addChild(new Snake()); var scoreTxt = new Text2('0', { size: 150, fill: '#ffffff', font: 'Impact', dropShadow: true, dropShadowColor: '#000000', dropShadowBlur: 4, dropShadowAngle: Math.PI / 6, dropShadowDistance: 6 }); scoreTxt.anchor.set(.5, 0); LK.gui.topCenter.addChild(scoreTxt); var foods = []; snake.x = 2048 / 2; snake.y = 2732 / 2; var food = new Food(); food.spawn(); foods.push(food); game.addChild(food); var dragNode = null; var initialTouchPosition = { x: 0, y: 0 }; var touchIndicator = null; var fingerIndicator = null; var isMouseDown = false; var maxDistance = 200; game.on('down', function (obj) { var event = obj.event; initialTouchPosition = event.getLocalPosition(game); if (touchIndicator) { touchIndicator.destroy(); } if (fingerIndicator) { fingerIndicator.destroy(); } touchIndicator = game.addChild(new TouchIndicator()); touchIndicator.x = initialTouchPosition.x; touchIndicator.y = initialTouchPosition.y; fingerIndicator = game.addChild(new FingerIndicator()); fingerIndicator.x = initialTouchPosition.x; fingerIndicator.y = initialTouchPosition.y; isMouseDown = true; }); function handleMove(obj) { var event = obj.event; var currentTouchPosition = event.getLocalPosition(game); var dx = currentTouchPosition.x - initialTouchPosition.x; var dy = currentTouchPosition.y - initialTouchPosition.y; var distance = Math.sqrt(dx * dx + dy * dy) * 0.7; if (distance > maxDistance) { var angle = Math.atan2(dy, dx); dx = maxDistance * Math.cos(angle); dy = maxDistance * Math.sin(angle); } if (fingerIndicator) { fingerIndicator.x = initialTouchPosition.x + dx; fingerIndicator.y = initialTouchPosition.y + dy; } if (isMouseDown) { snake.direction.x = dx; snake.direction.y = dy; } } game.on('move', handleMove); LK.on('tick', function () { snake.move(); for (var i = 0; i < foods.length; i++) { var snakeHeadRadius = snake.width / 2; var foodRadius = foods[i].width / 2; var dx = snake.x - foods[i].x; var dy = snake.y - foods[i].y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < snakeHeadRadius + foodRadius) { foods[i].destroy(); foods.splice(i, 1); score++; LK.setScore(score); scoreTxt.setText(LK.getScore()); var newSegment = snake.addBodySegment(); snakeContainer.addChildAt(newSegment, 0); var newFood = new Food(); newFood.spawn(); foods.push(newFood); game.addChild(newFood); } } });
/****
* Classes
****/
var SnakeBodySegment = Container.expand(function () {
var self = Container.call(this);
var bodySegmentGraphics = self.attachAsset('snakeBodySegment', {
anchorX: 0.5,
anchorY: 0.5
});
bodySegmentGraphics.rotation = Math.PI / 2;
bodySegmentGraphics.tint = 0x6fa8a8;
});
var FingerIndicator = Container.expand(function () {
var self = Container.call(this);
var fingerIndicatorGraphics = self.attachAsset('fingerIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
fingerIndicatorGraphics.alpha = .8;
});
var TouchIndicator = Container.expand(function () {
var self = Container.call(this);
var touchIndicatorGraphics = self.attachAsset('touchIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
touchIndicatorGraphics.alpha = .4;
});
var Snake = Container.expand(function () {
var self = Container.call(this);
var snakeGraphics = self.attachAsset('snake', {
anchorX: 0.5,
anchorY: 0.5
});
snakeGraphics.rotation = -Math.PI / 2;
self.direction = {
x: 0,
y: 0
};
self.speed = 7.5;
self.angle = 0;
self.headPositions = [];
self.bodySegments = [];
self.addBodySegment = function () {
var segment = new SnakeBodySegment();
var lastSegmentPosition = this.bodySegments.length > 0 ? this.bodySegments[this.bodySegments.length - 1] : this;
segment.x = lastSegmentPosition.x;
segment.y = lastSegmentPosition.y;
segment.rotation = lastSegmentPosition.rotation;
this.bodySegments.push(segment);
return segment;
};
self.move = function () {
var targetRotation = Math.atan2(self.direction.y, self.direction.x);
var rotationDifference = targetRotation - self.rotation;
while (rotationDifference < -Math.PI) {
rotationDifference += 2 * Math.PI;
}
while (rotationDifference > Math.PI) {
rotationDifference -= 2 * Math.PI;
}
var turnRate = Math.PI / 96 * 1.625;
self.rotation += Math.sign(rotationDifference) * Math.min(Math.abs(rotationDifference), turnRate);
self.x = self.x + Math.cos(self.rotation) * self.speed;
self.y = self.y + Math.sin(self.rotation) * self.speed;
if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) {
LK.showGameOver();
}
var thresholdDistance = 50;
for (var i = 1; i < self.bodySegments.length; i++) {
var dx = self.x - self.bodySegments[i].x;
var dy = self.y - self.bodySegments[i].y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < thresholdDistance) {
LK.showGameOver();
}
}
self.headPositions.push({
x: self.x,
y: self.y,
rotation: self.rotation
});
var tailSegmentDelay = 20;
while (self.headPositions.length > self.bodySegments.length * tailSegmentDelay + 1) {
self.headPositions.shift();
}
for (var i = 0; i < self.bodySegments.length; i++) {
var segmentIndex = (i + 1) * tailSegmentDelay;
if (self.headPositions.length > segmentIndex) {
var pos = self.headPositions[self.headPositions.length - 1 - segmentIndex];
self.bodySegments[i].x = pos.x;
self.bodySegments[i].y = pos.y;
self.bodySegments[i].rotation = pos.rotation;
}
}
};
});
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
self.spawn = function () {
this.x = Math.random() * (2048 - 60) + 30;
this.y = Math.random() * (2732 - 60) + 30;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
game.on('up', function (obj) {
if (touchIndicator) {
touchIndicator.destroy();
touchIndicator = null;
}
isMouseDown = false;
if (fingerIndicator) {
fingerIndicator.destroy();
fingerIndicator = null;
}
snake.direction.x = Math.cos(snake.rotation) * snake.speed;
snake.direction.y = Math.sin(snake.rotation) * snake.speed;
});
var score = 0;
var background = game.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5
});
background.x = 2048 / 2;
background.y = 2732 / 2;
background.alpha = .5;
game.setBackgroundColor(0xFFFFFF);
var snakeContainer = game.addChild(new Container());
var snake = snakeContainer.addChild(new Snake());
var scoreTxt = new Text2('0', {
size: 150,
fill: '#ffffff',
font: 'Impact',
dropShadow: true,
dropShadowColor: '#000000',
dropShadowBlur: 4,
dropShadowAngle: Math.PI / 6,
dropShadowDistance: 6
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var foods = [];
snake.x = 2048 / 2;
snake.y = 2732 / 2;
var food = new Food();
food.spawn();
foods.push(food);
game.addChild(food);
var dragNode = null;
var initialTouchPosition = {
x: 0,
y: 0
};
var touchIndicator = null;
var fingerIndicator = null;
var isMouseDown = false;
var maxDistance = 200;
game.on('down', function (obj) {
var event = obj.event;
initialTouchPosition = event.getLocalPosition(game);
if (touchIndicator) {
touchIndicator.destroy();
}
if (fingerIndicator) {
fingerIndicator.destroy();
}
touchIndicator = game.addChild(new TouchIndicator());
touchIndicator.x = initialTouchPosition.x;
touchIndicator.y = initialTouchPosition.y;
fingerIndicator = game.addChild(new FingerIndicator());
fingerIndicator.x = initialTouchPosition.x;
fingerIndicator.y = initialTouchPosition.y;
isMouseDown = true;
});
function handleMove(obj) {
var event = obj.event;
var currentTouchPosition = event.getLocalPosition(game);
var dx = currentTouchPosition.x - initialTouchPosition.x;
var dy = currentTouchPosition.y - initialTouchPosition.y;
var distance = Math.sqrt(dx * dx + dy * dy) * 0.7;
if (distance > maxDistance) {
var angle = Math.atan2(dy, dx);
dx = maxDistance * Math.cos(angle);
dy = maxDistance * Math.sin(angle);
}
if (fingerIndicator) {
fingerIndicator.x = initialTouchPosition.x + dx;
fingerIndicator.y = initialTouchPosition.y + dy;
}
if (isMouseDown) {
snake.direction.x = dx;
snake.direction.y = dy;
}
}
game.on('move', handleMove);
LK.on('tick', function () {
snake.move();
for (var i = 0; i < foods.length; i++) {
var snakeHeadRadius = snake.width / 2;
var foodRadius = foods[i].width / 2;
var dx = snake.x - foods[i].x;
var dy = snake.y - foods[i].y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < snakeHeadRadius + foodRadius) {
foods[i].destroy();
foods.splice(i, 1);
score++;
LK.setScore(score);
scoreTxt.setText(LK.getScore());
var newSegment = snake.addBodySegment();
snakeContainer.addChildAt(newSegment, 0);
var newFood = new Food();
newFood.spawn();
foods.push(newFood);
game.addChild(newFood);
}
}
});
Single, Anaconda snake head segment. Seen from above. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Single snake head segment. Seen from above. Cartoon. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Golden Apple Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Bright Amazing games background for modern snake game. It should be epic!
White circular touch indicator. White button like look. Game asset. No background. 2d. No shadow.