MrThoe

Snake 3

Mar 16th, 2021 (edited)
411
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.   //Try and make it print "num num num" when
  43.   //the snake.x[0] and apple.x are the same and the y's
  44.   for(var i  =0 ; i < apple.length; i++){
  45.     if(mySnake.x[0] == apple[i].x  && mySnake.y[0] == apple[i].y){
  46.       mySnake.eatApple(apple[i].x, apple[i].y);
  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.     }
  94.   }
  95.  
  96.   move(){
  97.        
  98.     switch(this.dir){
  99.       case "LEFT":
  100.         if(this.x[0]>0){
  101.           this.x[0]--;
  102.         } // else { this.gameState = false;}
  103.         break;
  104.        
  105.       case "RIGHT":
  106.           if(this.x[0] < num-1){
  107.             this.x[0]++;
  108.           }
  109.         break;
  110.        
  111.       case "UP":
  112.         if(this.y[0] > 0){
  113.           this.y[0]--;
  114.         }
  115.         break;
  116.        
  117.       case "DOWN":
  118.         if(this.y[0] < num-1){
  119.           this.y[0]++;
  120.         }
  121.         break;            
  122.     }
  123.   }
  124. }
  125.  
  126.  
  127. class Apple{
  128.   constructor(){
  129.     this.x = int(random(0,num));
  130.     this.y = int(random(0,num));
  131.   }
  132.  
  133.   show(){
  134.     fill(0,255,0);  //GREEN
  135.     rect(this.x*w, this.y*w, w, w);
  136.   }
  137. }
Add Comment
Please, Sign In to add comment