Advertisement
Guest User

hiss

a guest
Aug 12th, 2011
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>Untitled Document</title>
  6. </head>
  7.  
  8. <body>
  9.  
  10.     <canvas id='canvas' height="500" width="1000"></canvas>
  11.     <script>
  12.    
  13.         // Initial Global Variables
  14.         var canvas = document.getElementById('canvas');
  15.         var context = canvas.getContext('2d');
  16.         var xspeed, speed = 10;
  17.         var yspeed = 0;
  18.         var color = 'red';
  19.         var size = 10;
  20.         var interval;
  21.         //------------------------
  22.    
  23.         //snake object, handles coordinates for each segment and clears the canvas after drawing each segment
  24.         function snake(segments){
  25.             this.segments = segments;
  26.             //'add function is called whenever snake collides with target.
  27.             this.positions = [];
  28.            
  29.             //looks through position store and returns on duplicate, indicating a collision.
  30.             this.collide = function(){
  31.                 for(var z=0; z<this.positions.length-1; z++){
  32.                     for(var q=z+1; q<this.positions.length-1; q++){
  33.                         return this.positions[z][0] == this.positions[q][0] && this.positions[z][1] == this.positions[q][1];
  34.                     }
  35.                 }
  36.             }
  37.            
  38.             this.add = function(){
  39.                 this.segments.push(new segment(this.segments[this.segments.length-1].xPos-size, this.segments[this.segments.length-1].yPos, false, size, color));
  40.             }
  41.             //handles clearing, and passing necessary variables to the segments to draw
  42.             //also handles detecting collisions with snake
  43.             this.draw = function(){
  44.                 if(this.collide()){
  45.                     initiate();
  46.                 }
  47.                
  48.                 //reset frame's position store
  49.                 this.positions.length = 0;
  50.                
  51.                 //reset canvas
  52.                 canvas.width = canvas.width;
  53.                
  54.                 //add border to canvas
  55.                 context.fillStyle = 'white';
  56.                 context.strokeStyle = 'black';
  57.                 context.lineWidth = 4;
  58.                 context.strokeRect(0,0,canvas.width,canvas.height);
  59.                
  60.                
  61.                 //loop through segments
  62.                 for(var i = 0; i<this.segments.length; i++){
  63.                     if(this.segments[i].isHead){
  64.                         var oldX = this.segments[i].xPos;
  65.                         var oldY = this.segments[i].yPos;
  66.                         this.segments[i].draw(xspeed+this.segments[i].xPos, yspeed+this.segments[i].yPos);
  67.                     }
  68.                     else{
  69.                         var oldXTemp = this.segments[i].xPos;
  70.                         var oldYTemp = this.segments[i].yPos;
  71.                         this.segments[i].draw(oldX, oldY);
  72.                         oldX = oldXTemp;
  73.                         oldY = oldYTemp;
  74.                     }
  75.                 }
  76.                
  77.  
  78.             }
  79.         }
  80.         //End Snake
  81.        
  82.         //Segment Object. Building block of snake, and target
  83.         //Only one segment in the snake object is a 'Head'.
  84.         //This segment is the one that actually turns, and all subsequent coordinates
  85.         //are passed from it
  86.         function segment(xPos, yPos, isHead, size, color){
  87.             this.xPos = xPos;
  88.             this.yPos = yPos;
  89.             this.isHead = isHead;
  90.             this.size = size;
  91.             this.color = color;
  92.            
  93.             //determines whether segment is still on canvas
  94.             //also, simply draws at coordinates given to it by snake object
  95.             this.draw = function(x,y){
  96.                 //update position
  97.                 this.xPos = x;
  98.                 this.yPos = y;
  99.            
  100.                 //If snake is off the canvas, restart.
  101.                 if(this.xPos< 0 || this.xPos>canvas.width || this.yPos < 0 || this.yPos>canvas.width){
  102.                     initiate();
  103.                 }
  104.                
  105.                 //add location to location array for current frame. Then draw
  106.                 snakey.positions.push([x,y]);
  107.                 context.fillStyle = this.color;
  108.                 context.strokeStyle = 'pink';
  109.                 context.strokeRect(x,y,this.size,this.size);
  110.                 context.fillRect(x, y , this.size, this.size);
  111.             }
  112.         }
  113.         //End Segment
  114.  
  115.         //handles turning. Modifies x/y speed variables
  116.         //these variables are passed to the head segment with each frame
  117.         function turn(oEvent){
  118.             switch(oEvent.keyCode){
  119.                 case 83:
  120.                     if(yspeed!=-speed){
  121.                         xspeed=0;
  122.                         yspeed=speed;
  123.                     }
  124.                     break;
  125.                    
  126.                 case 87:
  127.                     if(yspeed!=speed){
  128.                         xspeed=0;
  129.                         yspeed = -speed;
  130.                     }
  131.                     break;
  132.                    
  133.                 case 68:
  134.                     if(xspeed!=-speed){
  135.                         yspeed=0;
  136.                         xspeed = speed;
  137.                     }
  138.                     break;
  139.                    
  140.                 case 65:
  141.                     if(xspeed!=speed){
  142.                         yspeed=0;
  143.                         xspeed = -speed;
  144.                     }
  145.                     break;
  146.             }
  147.         }
  148.         //End Turn
  149.        
  150.        
  151.         //initate snake
  152.         //also serves to restart
  153.         function initiate(){
  154.             snakey = new snake([new segment(200,50,true,size,color)]);
  155.             for(var t = 0; t<20; t++){
  156.             snakey.segments.push(new segment(snakey.segments[0].xPos-t*size, snakey.segments[0].yPos,false,size,color));
  157.             }
  158.             xspeed =speed;
  159.             yspeed =0;
  160.         }
  161.        
  162.        
  163.        
  164.         window.addEventListener('keydown',turn,true);
  165.  
  166.         initiate();
  167.         interval = setInterval("snakey.draw()", 50);
  168.     </script>
  169.    
  170. </body>
  171. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement