Advertisement
Guest User

Untitled

a guest
Mar 11th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas;
  2. var context;
  3. var game;
  4.  
  5. function initialize() {
  6.    
  7.     canvas = document.getElementById("canvas");
  8.     context = canvas.getContext("2d");
  9.    
  10.     game = new theGame();
  11.    
  12.     document.addEventListener('keydown', function(evt) {
  13.    
  14.         var key = evt.which;
  15.         game.input(key);
  16.     }, false);
  17.    
  18.    
  19.     mainLoop();
  20. }
  21.  
  22. function theGame() {
  23.    
  24.     this.currentKey = null;
  25.     this.lastKey = null;
  26.    
  27.     this.w = 20;
  28.    
  29.     this.score = 0;
  30.    
  31.     this.starting = { x: Math.floor((canvas.width / this.w) * 0.5) * this.w, y: Math.floor((canvas.height / this.w) * 0.5) * this.w }
  32.    
  33.     this.snake = new Rectangle(0, 0, this.w, this.w); this.snake.boxColor = "green";
  34.    
  35.     this.snake.x = this.starting.x
  36.     this.snake.y = this.starting.y
  37.    
  38.     this.tail = new Array();
  39.     this.tail.push(this.snake);
  40.    
  41.     this.re = 5;
  42.     this.limit = this.re;
  43.     this.spawnFood();
  44. }
  45.  
  46. theGame.prototype.spawnFood = function() {
  47.  
  48.     var x = Math.floor(Math.random() * (canvas.width / this.w)) * this.w;
  49.     var y = Math.floor(Math.random() * (canvas.height / this.w)) * this.w;
  50.     this.food = new Rectangle(x, y, this.w, this.w); this.food.boxColor = "red";
  51. }
  52.  
  53. theGame.prototype.reset = function() {
  54.  
  55.     this.currentKey = null;
  56.     this.lastKey = null;
  57.     this.snake.x = this.starting.x
  58.     this.snake.y = this.starting.y
  59.     this.tail = new Array();
  60.     this.score = 0;
  61.     this.tail.push(this.snake);
  62. }
  63.  
  64.  
  65. theGame.prototype.input = function(key) {
  66.  
  67.     switch(key) {
  68.    
  69.         case 38 : if (this.lastKey != "down") { this.currentKey = "up"; }
  70.         break;
  71.         case 40 : if (this.lastKey != "up") { this.currentKey = "down"; }
  72.         break;
  73.         case 37 : if (this.lastKey != "right") { this.currentKey = "left"; }
  74.         break;
  75.         case 39 : if (this.lastKey != "left") { this.currentKey = "right"; }
  76.         break;
  77.    
  78.     }
  79. }
  80.  
  81. theGame.prototype.update = function() {
  82.  
  83.     this.limit--;
  84.     if (this.limit <= 0) {
  85.    
  86.         for (var i = this.tail.length - 1; i > 0; i--) {
  87.        
  88.             if (i != 0) { this.tail[i].x = this.tail[i - 1].x; this.tail[i].y = this.tail[i - 1].y; }
  89.         }
  90.        
  91.         if (this.currentKey == "up") { this.lastKey = "up";
  92.             this.snake.y -= this.w; if (this.snake.y < 0) { this.reset(); }
  93.         }
  94.         if (this.currentKey == "down") { this.lastKey = "down";
  95.             this.snake.y += this.w; if (this.snake.y > canvas.height - this.w) { this.reset(); }
  96.         }
  97.         if (this.currentKey == "left") { this.lastKey = "left";
  98.             this.snake.x -= this.w; if (this.snake.x < 0) { this.reset(); }
  99.         }
  100.         if (this.currentKey == "right") { this.lastKey = "right";
  101.             this.snake.x += this.w; if (this.snake.x > canvas.width - this.w) { this.reset(); }
  102.         }
  103.        
  104.         for (var i = 0; i < this.tail.length; i++) {
  105.        
  106.             if (i != 0 && this.snake.contains(this.tail[i].middle())) {
  107.            
  108.                 this.reset();
  109.             }
  110.         }
  111.        
  112.         if (this.snake.contains(this.food.middle())) {
  113.            
  114.             rect = new Rectangle(-100, -100, this.w, this.w); rect.boxColor = "green";
  115.             this.tail.push(rect);
  116.            
  117.             this.spawnFood();
  118.         }
  119.         this.limit = this.re;
  120.     }
  121. }
  122.  
  123. theGame.prototype.draw = function() {
  124.  
  125.     context.clearRect(0,0,canvas.width, canvas.height);
  126.    
  127.     for (var i = 0; i < this.tail.length; i++) {
  128.    
  129.         this.tail[i].draw();
  130.     }
  131.    
  132.     this.food.draw();
  133.     context.fillStyle = "white";
  134.     context.font = "15px Arial";
  135.     context.fillText(this.score, canvas.width * 0.5, 16);
  136. }
  137.  
  138.  
  139.  
  140.  
  141. function mainLoop() {
  142.    
  143.     game.update();
  144.     game.draw();
  145.  
  146.     requestAnimationFrame(mainLoop);
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement