Advertisement
Guest User

snake

a guest
Dec 8th, 2016
2,538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Board = function() {
  2.     var SIZE = {
  3.         WIDTH: 30,
  4.         HEIGHT: 30
  5.     }
  6.  
  7.     var score = 0
  8.     var snake
  9.     var apples = []
  10.  
  11.     var canvas = document.querySelector('.container')
  12.  
  13.     if(canvas.getContext) {
  14.         var ctx = canvas.getContext('2d')
  15.     } else {
  16.         console.error('Canvas not supported')
  17.     }
  18.  
  19.     function addSnake(snakeObj) {
  20.         snake = snakeObj
  21.     }
  22.  
  23.     function addApple(apple) {
  24.         apples.push(apple)
  25.         apple.notify('game-start', {board: null})
  26.     }
  27.  
  28.     function render() {
  29.         //render snake
  30.         var positions = snake.getPositions()
  31.         positions.forEach(function(pos) {
  32.             ctx.fillRect(pos.x*15, pos.y*15, 15, 15)
  33.         })
  34.  
  35.         //render apples
  36.         apples.forEach(function(apple) {
  37.             var pos = apple.getPosition()
  38.             ctx.fillRect(pos.x*15, pos.y*15, 15, 15)
  39.         })
  40.     }
  41.  
  42.     function clear() {
  43.         ctx.clearRect(0, 0, 600, 600)
  44.     }
  45.  
  46.     function getAvailablePositions() {}
  47.  
  48.     return {
  49.         addSnake: addSnake,
  50.         addApple: addApple,
  51.         render: render,
  52.         clear: clear,
  53.         getAvailablePositions: getAvailablePositions
  54.     }
  55. }
  56.  
  57. var Snake = function() {
  58.     var DIRECTIONS = {
  59.         RIGHT: 'right',
  60.         LEFT: 'left',
  61.         DOWN: 'down',
  62.         UP: 'up'
  63.     }
  64.  
  65.     var direction = DIRECTIONS.RIGHT
  66.  
  67.     var snake = [
  68.         {x: 0, y: 0},
  69.         {x: 1, y: 0},
  70.         {x: 2, y: 0},
  71.         {x: 3, y: 0}
  72.     ]
  73.  
  74.     function getPositions() {
  75.         return snake
  76.     }
  77.  
  78.     function advance() {
  79.         if(direction == DIRECTIONS.RIGHT) {
  80.             snake = snake.map(function(pos) {
  81.                 return {
  82.                     x: pos.x + 1,
  83.                     y: pos.y
  84.                 }
  85.             })
  86.         }
  87.     }
  88.  
  89.     return {
  90.         getPositions: getPositions,
  91.         advance: advance
  92.     }
  93. }
  94.  
  95. var Apple = function() {
  96.     var EVENTS = {
  97.         GAME_START: 'game-start'
  98.     }
  99.  
  100.     var apple
  101.  
  102.     function getPosition() {
  103.         return apple
  104.     }
  105.  
  106.     function notify(event, data) {
  107.         if(event === EVENTS.GAME_START) {
  108.             //TODO: use data.board to exclude random aquired squares
  109.             var randomPosition = Math.floor(Math.random() * 30)
  110.  
  111.             apple = {
  112.                 x: randomPosition,
  113.                 y: randomPosition
  114.             }
  115.         }
  116.     }
  117.  
  118.     return {
  119.         getPosition: getPosition,
  120.         notify: notify
  121.     }
  122. }
  123.  
  124. var Game = (function() {
  125.     function run() {
  126.         var board = new Board()
  127.         var snake = new Snake()
  128.         var apple = new Apple()
  129.         board.addSnake(snake)
  130.         board.addApple(apple)
  131.  
  132.         var gameloop = setInterval(function() {
  133.             board.clear()
  134.             board.render()
  135.             snake.advance()
  136.         }, 60)
  137.     }
  138.  
  139.     return {
  140.         run: run
  141.     }
  142. })()
  143.  
  144. Game.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement