forked from yardimli/phaser-snake-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
86 lines (59 loc) · 2.31 KB
/
snake.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Snake by Patrick OReilly and Richard Davey
// Twitter: @pato_reilly Web: http://patricko.byethost9.com
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update,render : render });
function preload() {
game.load.image('ball','assets/sprites/shinyball.png');
}
var snakeHead; //head of snake sprite
var snakeSection = new Array(); //array of sprites that make the snake body sections
var snakePath = new Array(); //arrary of positions(points) that have to be stored for the path the sections follow
var numSnakeSections = 30; //number of snake body sections
var snakeSpacer = 6; //parameter that sets the spacing between sections
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.world.setBounds(0, 0, 800, 600);
cursors = game.input.keyboard.createCursorKeys();
snakeHead = game.add.sprite(400, 300, 'ball');
snakeHead.anchor.setTo(0.5, 0.5);
game.physics.enable(snakeHead, Phaser.Physics.ARCADE);
// Init snakeSection array
for (var i = 1; i <= numSnakeSections-1; i++)
{
snakeSection[i] = game.add.sprite(400, 300, 'ball');
snakeSection[i].anchor.setTo(0.5, 0.5);
}
// Init snakePath array
for (var i = 0; i <= numSnakeSections * snakeSpacer; i++)
{
snakePath[i] = new Phaser.Point(400, 300);
}
}
function update() {
snakeHead.body.velocity.setTo(0, 0);
snakeHead.body.angularVelocity = 0;
if (cursors.up.isDown)
{
snakeHead.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(snakeHead.angle, 300));
// Everytime the snake head moves, insert the new location at the start of the array,
// and knock the last position off the end
var part = snakePath.pop();
part.setTo(snakeHead.x, snakeHead.y);
snakePath.unshift(part);
for (var i = 1; i <= numSnakeSections - 1; i++)
{
snakeSection[i].x = (snakePath[i * snakeSpacer]).x;
snakeSection[i].y = (snakePath[i * snakeSpacer]).y;
}
}
if (cursors.left.isDown)
{
snakeHead.body.angularVelocity = -300;
}
else if (cursors.right.isDown)
{
snakeHead.body.angularVelocity = 300;
}
}
function render() {
game.debug.spriteInfo(snakeHead, 32, 32);
}