Skip to content

Commit 39cae4c

Browse files
committed
Sphere snake wip demo
1 parent 03ae324 commit 39cae4c

2 files changed

Lines changed: 106 additions & 21 deletions

File tree

5.55 KB
Loading

examples/wip/snake2.js

Lines changed: 106 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,121 @@
1-
// Snake by Patrick OReilly and Richard Davey
2-
// Twitter: @pato_reilly Web: http://patricko.byethost9.com
3-
4-
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update,render : render });
1+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
52

63
function preload() {
74

8-
game.load.image('ball','assets/sprites/shinyball.png');
5+
game.load.spritesheet('snake','assets/games/snake/sprites.png', 32, 32);
6+
game.load.image('ball', 'assets/sprites/pangball.png');
97

108
}
119

10+
var snake;
11+
1212
var snakeHead; //head of snake sprite
13-
var snakeSection = new Array(); //array of sprites that make the snake body sections
14-
var snakePath = new Array(); //arrary of positions(points) that have to be stored for the path the sections follow
15-
var numSnakeSections = 6; //number of snake body sections
13+
var snakePath = []; //arrary of positions(points) that have to be stored for the path the sections follow
14+
var numSnakeSections = 10; //number of snake body sections
1615
var snakeSpacer = 6; //parameter that sets the spacing between sections
16+
var snakeSpeed = 250;
17+
var snakeLength = 10;
18+
19+
var fruit;
20+
21+
var snakeCircles = [];
22+
var node;
23+
var ball;
1724

1825
function create() {
1926

2027
game.world.setBounds(0, 0, 800, 600);
2128

29+
game.stage.backgroundColor = '#0f4e93';
30+
2231
cursors = game.input.keyboard.createCursorKeys();
2332

24-
snakeHead = game.add.sprite(400, 300, 'ball');
33+
snake = game.add.group();
34+
35+
snakeHead = snake.create(400, 300, 'snake', 5);
2536
snakeHead.anchor.setTo(0.5, 0.5);
37+
snakeHead.animations.add('chomp', [5, 6], 10, true);
38+
snakeHead.play('chomp');
39+
snakeHead.body.collideWorldBounds = true;
40+
snakeCircles.push(new Phaser.Circle(snakeHead.x, snakeHead.y, 16));
2641

2742
// Init snakeSection array
2843
for (var i = 1; i <= numSnakeSections-1; i++)
2944
{
30-
snakeSection[i] = game.add.sprite(400, 300, 'ball');
31-
snakeSection[i].anchor.setTo(0.5, 0.5);
45+
var snakeSection = snake.create(400, 300, 'snake', 7);
46+
snakeSection.anchor.setTo(0.5, 0.5);
47+
snakeSection.animations.add('walk', [7, 8, 9, 10], 10, true);
48+
snakeSection.play('walk');
49+
snakeCircles.push(new Phaser.Circle(400, 300, 16));
50+
51+
if (i > snakeLength)
52+
{
53+
snakeSection.visible = false;
54+
}
3255
}
56+
57+
node = new Phaser.Circle(300, 300, 170);
58+
ball = game.add.sprite(node.x, node.y, 'ball');
59+
ball.anchor.setTo(0.5, 0.5);
60+
61+
// fruit = game.add.group();
62+
63+
// for (var i = 0; i < 20; i++)
64+
// {
65+
// fruit.create(game.world.randomX, game.world.randomY, 'snake', 2);
66+
// }
3367

3468
// Init snakePath array
3569
for (var i = 0; i <= numSnakeSections * snakeSpacer; i++)
3670
{
3771
snakePath[i] = new Phaser.Point(400, 300);
3872
}
3973

40-
//snakeHead.body.velocity.setTo(0
74+
}
75+
76+
function newNode() {
77+
78+
node.x = game.rnd.integerInRange(100, 700);
79+
node.y = game.rnd.integerInRange(100, 500);
80+
ball.x = node.x;
81+
ball.y = node.y;
82+
ball.alpha = 1;
4183

4284
}
4385

4486
function update() {
4587

46-
// snakeHead.body.velocity.setTo(0, 0);
88+
// game.physics.collide(snakeHead, fruit, eatFruit);
89+
90+
var enclosed = true;
91+
92+
for (var i = 0; i <= numSnakeSections - 1; i++)
93+
{
94+
if (Phaser.Circle.intersects(snakeCircles[i], node) === false)
95+
{
96+
enclosed = false;
97+
break;
98+
}
99+
}
100+
101+
if (enclosed)
102+
{
103+
ball.alpha -= 0.02;
104+
105+
if (ball.alpha <= 0)
106+
{
107+
newNode();
108+
}
109+
}
110+
47111
snakeHead.body.angularVelocity = 0;
48112

49113
// if (cursors.up.isDown)
50114
// {
51-
snakeHead.body.velocity.copyFrom(game.physics.velocityFromAngle(snakeHead.angle, 200));
115+
snakeHead.body.velocity.copyFrom(game.physics.velocityFromAngle(snakeHead.angle, snakeSpeed));
52116

53-
// Everytime the snake head moves, insert the new location at the start of the array,
54-
// and knock the last position off the end
117+
snakeCircles[0].x = snakeHead.x;
118+
snakeCircles[0].y = snakeHead.y;
55119

56120
var part = snakePath.pop();
57121

@@ -61,24 +125,45 @@ function update() {
61125

62126
for (var i = 1; i <= numSnakeSections - 1; i++)
63127
{
64-
snakeSection[i].x = (snakePath[i * snakeSpacer]).x;
65-
snakeSection[i].y = (snakePath[i * snakeSpacer]).y;
128+
var x = (snakePath[i * snakeSpacer]).x;
129+
var y = (snakePath[i * snakeSpacer]).y;
130+
snake.getAt(i).x = x;
131+
snake.getAt(i).y = y;
132+
snakeCircles[i].x = x;
133+
snakeCircles[i].y = y;
66134
}
67135
// }
68136

69137
if (cursors.left.isDown)
70138
{
71-
snakeHead.body.angularVelocity = -300;
139+
snakeHead.body.angularVelocity = -220;
72140
}
73141
else if (cursors.right.isDown)
74142
{
75-
snakeHead.body.angularVelocity = 300;
143+
snakeHead.body.angularVelocity = 220;
76144
}
77145

78146
}
79147

148+
function eatFruit(snakeHead, fruit) {
149+
150+
fruit.kill();
151+
152+
snakeLength++;
153+
154+
snake.getAt(snakeLength).visible = true;
155+
snakeSpeed += 10;
156+
157+
}
158+
80159
function render() {
81160

82-
// game.debug.renderSpriteInfo(snakeHead, 32, 32);
161+
//game.debug.renderCircle(node);
162+
163+
for (var i = 0; i <= numSnakeSections - 1; i++)
164+
{
165+
//game.debug.renderCircle(snakeCircles[i], 'rgb(255,0,0)');
166+
}
167+
83168

84169
}

0 commit comments

Comments
 (0)