phaser/examples/wip/snake2.js

169 lines
3.9 KiB
JavaScript
Raw Normal View History

2013-12-14 02:22:14 +00:00
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
2013-12-13 23:56:39 +00:00
function preload() {
2013-12-14 02:22:14 +00:00
game.load.spritesheet('snake','assets/games/snake/sprites.png', 32, 32);
game.load.image('ball', 'assets/sprites/pangball.png');
2013-12-13 23:56:39 +00:00
}
2013-12-14 02:22:14 +00:00
var snake;
2013-12-13 23:56:39 +00:00
var snakeHead; //head of snake sprite
2013-12-14 02:22:14 +00:00
var snakePath = []; //arrary of positions(points) that have to be stored for the path the sections follow
var numSnakeSections = 10; //number of snake body sections
2013-12-13 23:56:39 +00:00
var snakeSpacer = 6; //parameter that sets the spacing between sections
2013-12-14 02:22:14 +00:00
var snakeSpeed = 250;
var snakeLength = 10;
var fruit;
var snakeCircles = [];
var node;
var ball;
2013-12-13 23:56:39 +00:00
function create() {
game.world.setBounds(0, 0, 800, 600);
2013-12-14 02:22:14 +00:00
game.stage.backgroundColor = '#0f4e93';
2013-12-13 23:56:39 +00:00
cursors = game.input.keyboard.createCursorKeys();
2013-12-14 02:22:14 +00:00
snake = game.add.group();
snakeHead = snake.create(400, 300, 'snake', 5);
2013-12-13 23:56:39 +00:00
snakeHead.anchor.setTo(0.5, 0.5);
2013-12-14 02:22:14 +00:00
snakeHead.animations.add('chomp', [5, 6], 10, true);
snakeHead.play('chomp');
snakeHead.body.collideWorldBounds = true;
snakeCircles.push(new Phaser.Circle(snakeHead.x, snakeHead.y, 16));
2013-12-13 23:56:39 +00:00
// Init snakeSection array
for (var i = 1; i <= numSnakeSections-1; i++)
{
2013-12-14 02:22:14 +00:00
var snakeSection = snake.create(400, 300, 'snake', 7);
snakeSection.anchor.setTo(0.5, 0.5);
snakeSection.animations.add('walk', [7, 8, 9, 10], 10, true);
snakeSection.play('walk');
snakeCircles.push(new Phaser.Circle(400, 300, 16));
if (i > snakeLength)
{
snakeSection.visible = false;
}
2013-12-13 23:56:39 +00:00
}
2013-12-14 02:22:14 +00:00
node = new Phaser.Circle(300, 300, 170);
ball = game.add.sprite(node.x, node.y, 'ball');
ball.anchor.setTo(0.5, 0.5);
// fruit = game.add.group();
// for (var i = 0; i < 20; i++)
// {
// fruit.create(game.world.randomX, game.world.randomY, 'snake', 2);
// }
2013-12-13 23:56:39 +00:00
// Init snakePath array
for (var i = 0; i <= numSnakeSections * snakeSpacer; i++)
{
snakePath[i] = new Phaser.Point(400, 300);
}
2013-12-14 02:22:14 +00:00
}
function newNode() {
node.x = game.rnd.integerInRange(100, 700);
node.y = game.rnd.integerInRange(100, 500);
ball.x = node.x;
ball.y = node.y;
ball.alpha = 1;
2013-12-13 23:56:39 +00:00
}
function update() {
2013-12-14 02:22:14 +00:00
// game.physics.collide(snakeHead, fruit, eatFruit);
var enclosed = true;
for (var i = 0; i <= numSnakeSections - 1; i++)
{
if (Phaser.Circle.intersects(snakeCircles[i], node) === false)
{
enclosed = false;
break;
}
}
if (enclosed)
{
ball.alpha -= 0.02;
if (ball.alpha <= 0)
{
newNode();
}
}
2013-12-13 23:56:39 +00:00
snakeHead.body.angularVelocity = 0;
// if (cursors.up.isDown)
// {
2013-12-14 02:22:14 +00:00
snakeHead.body.velocity.copyFrom(game.physics.velocityFromAngle(snakeHead.angle, snakeSpeed));
2013-12-13 23:56:39 +00:00
2013-12-14 02:22:14 +00:00
snakeCircles[0].x = snakeHead.x;
snakeCircles[0].y = snakeHead.y;
2013-12-13 23:56:39 +00:00
var part = snakePath.pop();
part.setTo(snakeHead.x, snakeHead.y);
snakePath.unshift(part);
for (var i = 1; i <= numSnakeSections - 1; i++)
{
2013-12-14 02:22:14 +00:00
var x = (snakePath[i * snakeSpacer]).x;
var y = (snakePath[i * snakeSpacer]).y;
snake.getAt(i).x = x;
snake.getAt(i).y = y;
snakeCircles[i].x = x;
snakeCircles[i].y = y;
2013-12-13 23:56:39 +00:00
}
// }
if (cursors.left.isDown)
{
2013-12-14 02:22:14 +00:00
snakeHead.body.angularVelocity = -220;
2013-12-13 23:56:39 +00:00
}
else if (cursors.right.isDown)
{
2013-12-14 02:22:14 +00:00
snakeHead.body.angularVelocity = 220;
2013-12-13 23:56:39 +00:00
}
}
2013-12-14 02:22:14 +00:00
function eatFruit(snakeHead, fruit) {
fruit.kill();
snakeLength++;
snake.getAt(snakeLength).visible = true;
snakeSpeed += 10;
}
2013-12-13 23:56:39 +00:00
function render() {
2013-12-14 02:22:14 +00:00
//game.debug.renderCircle(node);
for (var i = 0; i <= numSnakeSections - 1; i++)
{
//game.debug.renderCircle(snakeCircles[i], 'rgb(255,0,0)');
}
2013-12-13 23:56:39 +00:00
}