Guest User

Untitled

a guest
Aug 26th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var snake, apple, squareSize, score, speed,
  2. updateDelay, direction, new_direction,
  3. addNew, cursors, scoreTextValue, speedTextValue,
  4. textStyle_Key, textStyle_Value;
  5.  
  6. var Game = {
  7.  
  8. preload : function() {
  9. // Here we load all the needed resources for the level.
  10. // In our case, that's just two squares - one for the snake body and one for the apple.
  11. game.load.image('snake', './assets/images/snake.png');
  12. game.load.image('apple', './assets/images/apple.png');
  13. },
  14.  
  15. create : function() {
  16.  
  17. // By setting up global variables in the create function, we initialise them on game start.
  18. // We need them to be globally available so that the update function can alter them.
  19.  
  20. snake = []; // This will work as a stack, containing the parts of our snake
  21. apple = {}; // An object for the apple;
  22. squareSize = 15; // The length of a side of the squares. Our image is 15x15 pixels.
  23. score = 0; // Game score.
  24. speed = 0; // Game speed.
  25. updateDelay = 0; // A variable for control over update rates.
  26. direction = 'right'; // The direction of our snake.
  27. new_direction = null; // A buffer to store the new direction into.
  28. addNew = false; // A variable used when an apple has been eaten.
  29.  
  30. // Set up a Phaser controller for keyboard input.
  31. cursors = game.input.keyboard.createCursorKeys();
  32.  
  33. game.stage.backgroundColor = '#061f27';
  34.  
  35. // Generate the initial snake stack. Our snake will be 10 elements long.
  36. // Beginning at X=150 Y=150 and increasing the X on every iteration.
  37. for(var i = 0; i < 10; i++){
  38. snake[i] = game.add.sprite(150+i*squareSize, 150, 'snake'); // Parameters are (X coordinate, Y coordinate, image)
  39. }
  40.  
  41.  
  42. // Genereate the first apple.
  43. this.generateApple();
  44.  
  45.  
  46. // Add Text to top of game.
  47. textStyle_Key = { font: "bold 14px sans-serif", fill: "#46c0f9", align: "center" };
  48. textStyle_Value = { font: "bold 18px sans-serif", fill: "#fff", align: "center" };
  49.  
  50. // Score.
  51. game.add.text(30, 20, "SCORE", textStyle_Key);
  52. scoreTextValue = game.add.text(90, 18, score.toString(), textStyle_Value);
  53. // Speed.
  54. game.add.text(500, 20, "SPEED", textStyle_Key);
  55. speedTextValue = game.add.text(558, 18, speed.toString(), textStyle_Value);
  56.  
  57. },
  58.  
  59.  
  60.  
  61. update: function() {
  62. // The update function is called constantly at a high rate (somewhere around 60fps),
  63. // updating the game field every time.
  64. // We are going to leave that one empty for now.
  65. },
  66.  
  67.  
  68. generateApple: function(){
  69.  
  70. // Chose a random place on the grid.
  71. // X is between 0 and 585 (39*15)
  72. // Y is between 0 and 435 (29*15)
  73.  
  74. var randomX = Math.floor(Math.random() * 40 ) * squareSize,
  75. randomY = Math.floor(Math.random() * 30 ) * squareSize;
  76.  
  77. // Add a new apple.
  78. apple = game.add.sprite(randomX, randomY, 'apple');
  79. }
  80.  
  81. };
Advertisement
Add Comment
Please, Sign In to add comment