MrThoe

Snake 4

Mar 17th, 2021
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Snake by Thoe
  2.  * @Author:  Allen Thoe
  3.  * 3/12/2021
  4.  */
  5. var mySnake;
  6. var w;
  7. var value;
  8. var num;
  9. var apple = [];
  10.  
  11. function setup() {
  12.   createCanvas(400, 400);
  13.   mySnake = new Snake();
  14.   num = 10;
  15.   w = width/num;
  16.   value = 220;
  17.   frameRate(5);  //Times per second
  18.   for(var i = 0; i < 1; i++){
  19.     apple.push(new Apple());
  20.   }
  21. }
  22.  
  23. function draw() {
  24.   background(value);
  25.   grid();
  26.   mySnake.show();
  27.   for(var i = 0; i < apple.length; i++){
  28.     apple[i].show();
  29.   }
  30.   eat();
  31. }
  32.  
  33. function grid(){
  34.   for(var i = 0 ; i < num; i++){
  35.     line(0, i*w, height, i*w);
  36.     line(i*w, 0, i*w, width);
  37.   }
  38. }
  39.  
  40.  
  41. function eat(){
  42.  
  43.   for(var i  =0 ; i < apple.length; i++){
  44.     if(mySnake.x[0] == apple[i].x  && mySnake.y[0] == apple[i].y){
  45.       mySnake.eatApple(apple[i].x, apple[i].y);
  46.       apple[i] = new Apple();  //RESPAWN
  47.     }
  48.   }
  49.  
  50. }
  51.  
  52.  
  53.  
  54. function keyPressed() {
  55.   if (keyCode === LEFT_ARROW && mySnake.dir != "RIGHT") {
  56.     mySnake.dir = "LEFT";
  57.   } else if (keyCode === RIGHT_ARROW && mySnake.dir != "LEFT") {
  58.     mySnake.dir = "RIGHT";
  59.   } else if (keyCode === UP_ARROW && mySnake.dir != "DOWN") {
  60.     mySnake.dir = "UP";
  61.   } else if (keyCode === DOWN_ARROW && mySnake.dir != "UP") {
  62.     mySnake.dir = "DOWN";
  63.   }
  64. }
  65.  
  66. //Class Snake
  67. class Snake{
  68.   //Constructor (make a snake)
  69.   constructor(){
  70.     this.x = [];
  71.     this.y = [];
  72.     this.x.push(1);
  73.     this.y.push(5);
  74.     this.dir = "";
  75.   }
  76.  
  77.   //Methods (as many as you want-- this.etc)
  78.   show(){
  79.     this.move();
  80.     fill(255,0,0);
  81.     for(var i = 0; i < this.x.length; i++){
  82.       rect(this.x[i]*w, this.y[i]*w, w, w);
  83.     }
  84.   }
  85.  
  86.   //TODO  -- FINISH THE OTHER 3 CASES
  87.   eatApple(x, y){
  88.     switch(this.dir){
  89.       case "LEFT":
  90.         this.x.push(x-1);
  91.         this.y.push(y);
  92.         break;
  93.       case "RIGHT":
  94.         this.x.push(x+1);
  95.         this.y.push(y);
  96.         break;
  97.       case "UP":
  98.         this.x.push(x);
  99.         this.y.push(y-1);
  100.         break;
  101.       case "DOWN":
  102.         this.x.push(x);
  103.         this.y.push(y+1);
  104.         break;
  105.     }
  106.   }
  107.  
  108.   move(){
  109.     //MOVE THE BODY --> Piece by piece
  110.     for(var i = this.x.length-1; i > 0; i--){
  111.       this.x[i] = this.x[i-1];
  112.       this.y[i] = this.y[i-1];
  113.     }
  114.    
  115.     //MOVES THE HEAD
  116.     switch(this.dir){
  117.       case "LEFT":
  118.         if(this.x[0]>0){
  119.           this.x[0]--;
  120.         } // else { this.gameState = false;}
  121.         break;
  122.        
  123.       case "RIGHT":
  124.           if(this.x[0] < num-1){
  125.             this.x[0]++;
  126.           }
  127.         break;
  128.        
  129.       case "UP":
  130.         if(this.y[0] > 0){
  131.           this.y[0]--;
  132.         }
  133.         break;
  134.        
  135.       case "DOWN":
  136.         if(this.y[0] < num-1){
  137.           this.y[0]++;
  138.         }
  139.         break;            
  140.     }
  141.   }
  142. }
  143.  
  144.  
  145. class Apple{
  146.   constructor(){
  147.     this.x = int(random(0,num));
  148.     this.y = int(random(0,num));
  149.   }
  150.  
  151.   show(){
  152.     fill(0,255,0);  //GREEN
  153.     rect(this.x*w, this.y*w, w, w);
  154.   }
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment