Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //default engine declaration stuff for the renderer
  2. var renderer = PIXI.autoDetectRenderer(800, 800, {
  3.     backgroundColor : 0x1099bb
  4. });
  5. // attach to HTML element -> body
  6. document.body.appendChild(renderer.view);
  7.  
  8. // create the root of the scene graph
  9. var stage = new PIXI.Container();
  10.  
  11. // create leading head
  12. var snakeHead = new PIXI.Graphics();
  13. snakeHead.beginFill(0xFFFF00);
  14. snakeHead.lineStyle(5, 0xFF0000);
  15. snakeHead.drawRect(0, 0, 20, 20);
  16. snakeHead.x = 0;
  17. snakeHead.y = 0;
  18.  
  19. // just get some random x,y coords within our board
  20. function getRandomLoc() {
  21.     // 0 to 40 multiplied by a step of 20 (0-800)
  22.     var x = Math.floor((Math.random() * 40) + 0) * 20;
  23.     var y = Math.floor((Math.random() * 40) + 0) * 20;
  24.     var value = {
  25.     x : x,
  26.     y : y
  27.     }
  28.     return value;
  29. }
  30.  
  31. // generating food primitive for the first and last time
  32. var food = new PIXI.Graphics();
  33. food.beginFill(0xFFFF00);
  34. food.lineStyle(5, 0xFF0000);
  35. var loc = getRandomLoc();
  36. food.drawRect(0, 0, 20, 20);
  37. food.x = loc.x;
  38. food.y = loc.y;
  39. var loc = undefined;
  40.  
  41. // using this to create body parts
  42. function createPrimitive(x, y) {
  43.     var primitive = new PIXI.Graphics();
  44.     primitive.beginFill(0xFFFF00);
  45.     primitive.lineStyle(5, 0xFF0000);
  46.     primitive.drawRect(0, 0, 20, 20);
  47.     primitive.x = x;
  48.     primitive.y = y;
  49.     return primitive;
  50. }
  51.  
  52. // stage.addChild(ship);
  53. stage.addChild(snakeHead);
  54. stage.addChild(food);
  55.  
  56. // snake velocity X and velocity Y
  57. var snakeVx = 0;
  58. var snakeVy = 0;
  59.  
  60. // declare key presses
  61. var wKey = keyboard(87);
  62. var sKey = keyboard(83);
  63. var aKey = keyboard(65);
  64. var dKey = keyboard(68);
  65.  
  66. // declare velocities depending on keypress
  67. wKey.press = function() {
  68.     snakeVy = -1;
  69.     snakeVx = 0;
  70. };
  71. sKey.press = function() {
  72.     snakeVy = 1;
  73.     snakeVx = 0;
  74. };
  75. aKey.press = function() {
  76.     snakeVy = 0;
  77.     snakeVx = -1;
  78. };
  79. dKey.press = function() {
  80.     snakeVy = 0;
  81.     snakeVx = 1;
  82. };
  83.  
  84. // some generic Deque (using to push front/pop back)
  85. function Deque() {
  86.     this.stac = new Array();
  87.     this.popback = function() {
  88.     return this.stac.pop();
  89.     }
  90.     this.pushback = function(item) {
  91.     this.stac.push(item);
  92.     }
  93.     this.popfront = function() {
  94.     return this.stac.shift();
  95.     }
  96.     this.pushfront = function(item) {
  97.     this.stac.unshift(item);
  98.     }
  99. }
  100.  
  101. // create a Deque with previous locations
  102. var prevLocs = new Deque();
  103. // array of snakePart primitives
  104. var snakeParts = [];
  105. // size of the snake
  106. var snakeSize = 0;
  107. // frame counter
  108. var counter = 0;
  109.  
  110. var curVx = 0;
  111. var curVy = 0;
  112. // start animating
  113. animate();
  114. function animate() {
  115.     requestAnimationFrame(animate);
  116.     // counting frames on each execution of our animate loop
  117.     counter++;
  118.     // we take actions every 5 frames which is around 1s/(60/5) so around every
  119.     // 83ms
  120.  
  121.     // move the snake on every frame with temp velocity
  122.     snakeHead.x += curVx * 4;
  123.     snakeHead.y += curVy * 4;
  124.  
  125.     // I suppose need to determine this value from average framerate to keep it
  126.     // consistent
  127.     if (counter == 5) {
  128.     // first check if snake head is colliding with food
  129.     if (snakeHead.x == food.x && snakeHead.y == food.y) {
  130.         snakeSize++;
  131.         // create a new primitive snake body part
  132.         var newPart = createPrimitive(snakeHead.x, snakeHead.y)
  133.         // add it to stage & snakeParts array
  134.         stage.addChild(newPart);
  135.         snakeParts.push(newPart);
  136.         // move the food to a new location
  137.         var randomLoc = getRandomLoc();
  138.         food.x = randomLoc.x;
  139.         food.y = randomLoc.y;
  140.     }
  141.     // change temporary velocity every 20 pixels
  142.     curVx = snakeVx;
  143.     curVy = snakeVy;
  144.     // check if snakeHead went out of bounds and return it on the other side
  145.     if (snakeHead.x > 800) {
  146.         snakeHead.x = 0;
  147.     } else if (snakeHead.x < 0) {
  148.         snakeHead.x = 800;
  149.     }
  150.     if (snakeHead.y > 800) {
  151.         snakeHead.y = 0;
  152.     } else if (snakeHead.y < 0) {
  153.         snakeHead.y = 800;
  154.     }
  155.     // reset counter
  156.     counter = 0;
  157.     }
  158.  
  159.     // Move each snake part to every 5th of the previous snakeHead locations
  160.     for (var i = 0; i < snakeSize; i++) {
  161.     snakeParts[i].x = prevLocs.stac[(i + 1) * 5].x;
  162.     snakeParts[i].y = prevLocs.stac[(i + 1) * 5].y;
  163.     }
  164.     // push current location to previous locations
  165.     prevLocs.pushfront({
  166.     x : snakeHead.x,
  167.     y : snakeHead.y
  168.     });
  169.     // remove last location if we have more locations than snakeparts*5(so we
  170.     // don't overflow)
  171.     if (5 * prevLocs.size > snakeSize) {
  172.     prevLocs.popback;
  173.     }
  174.  
  175.     // render the stage
  176.     renderer.render(stage);
  177. }
  178.  
  179. // initializes a keyListener for a keycode (took it from API docs)
  180. function keyboard(keyCode) {
  181.     var key = {};
  182.     key.code = keyCode;
  183.     key.isDown = false;
  184.     key.isUp = true;
  185.     key.press = undefined;
  186.     key.release = undefined;
  187.     // The `downHandler`
  188.     key.downHandler = function(event) {
  189.     if (event.keyCode === key.code) {
  190.         if (key.isUp && key.press)
  191.         key.press();
  192.         key.isDown = true;
  193.         key.isUp = false;
  194.     }
  195.     event.preventDefault();
  196.     };
  197.  
  198.     // The `upHandler`
  199.     key.upHandler = function(event) {
  200.     if (event.keyCode === key.code) {
  201.         if (key.isDown && key.release)
  202.         key.release();
  203.         key.isDown = false;
  204.         key.isUp = true;
  205.     }
  206.     event.preventDefault();
  207.     };
  208.  
  209.     // Attach event listeners
  210.     window.addEventListener("keydown", key.downHandler.bind(key), false);
  211.     window.addEventListener("keyup", key.upHandler.bind(key), false);
  212.     return key;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement