Advertisement
Guest User

a simple snake

a guest
Apr 2nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. ArrayList<PVector> ver=new ArrayList<PVector>();
  2. int direction=0;
  3. PVector foodPosition=new PVector();
  4. boolean isFood=false, isPlaying=false;
  5. int frameSpeed=10;
  6. int highScore;
  7. int score, pScore;
  8. int foodEaten;
  9. int foodMove;
  10. String[] highScoreA=new String[1];
  11. void setup() {
  12.   size(400, 420);
  13.   frameRate(frameSpeed);
  14.   String highScoreS=new String();
  15.   if (loadStrings("highScore.txt")!=null)highScoreS=loadStrings("highScore.txt")[0];
  16.   else {
  17.     highScoreA[0]=str(highScore);
  18.     saveStrings("highScore.txt", highScoreA);
  19.     highScoreS=loadStrings("highScore.txt")[0];
  20.   }
  21.   highScore=Integer.parseInt(highScoreS);
  22.   init();
  23.   rectMode(CENTER);
  24. }
  25. //-----------------------------------------------
  26. void draw() {
  27.   background(0);
  28.  
  29.   if (isPlaying) {
  30.     update();
  31.     background(0);
  32.     stroke(0, 153, 0);
  33.     noFill();
  34.     drawGrid();
  35.     fill(0, 153, 0);
  36.     noStroke();
  37.     for (int i=0; i<ver.size(); i++) {
  38.       pushStyle();
  39.  
  40.       rect(ver.get(i).x, ver.get(i).y, 8, 8);
  41.       popStyle();
  42.     }
  43.     if (isFood)ellipse(foodPosition.x, foodPosition.y, 10, 10);
  44.   } else {
  45.     drawGrid();
  46.     drawSnake();
  47.   }
  48. }
  49. //-----------------------------------------------
  50. void update() {
  51.   //we copy the position from the previous worm part
  52.   for (int i=ver.size()-1; i>0; i--) {
  53.     PVector newPos=new PVector();
  54.     newPos.set(ver.get(i-1));
  55.     ver.set(i, newPos);
  56.   }
  57.   //set new position of the head
  58.   ver.get(0).x-=cos(radians(direction))*10;
  59.   ver.get(0).y-=sin(radians(direction))*10;
  60.   // if it touches itself
  61.   for (int i=1; i<ver.size(); i++) {
  62.     if (ver.get(0).dist(ver.get(i))<3)init();
  63.   }
  64.   // if it touches the border
  65.   if (ver.get(0).x<=2||ver.get(0).x>width-2||ver.get(0).y<=22||ver.get(0).y>height-2)init();
  66.   if (frameCount%frameSpeed*3==0&&isFood==false)isFood=true;
  67.   //if food just been turned true, place it inside
  68.   if (isFood&&foodPosition.x<0) {
  69.     int fX=(2+(int)random(10, (width-12))/10)*10;
  70.     int fY= (int)random(2.2, (height-2)/10)*10;
  71.     fX=constrain(fX, 10, width-10);
  72.     fY=constrain(fY, 30, height-10);
  73.     foodPosition.set((float)fX, (float)fY);
  74.     foodMove=10;
  75.   }
  76.   // if head touch the food, set a dist lower than 3, incase something make the snake not fully aligned
  77.   if (ver.get(0).dist(foodPosition)<3) {
  78.     PVector newPart=new PVector();
  79.     newPart.set(ver.get(ver.size()-1));
  80.     // increase the lenght of snake, more he eats, more he grow faster
  81.     for (int i=0; i<3+foodEaten; i++)ver.add(newPart);
  82.     //reset food
  83.     isFood=false;
  84.     foodPosition.x=-100;
  85.     if (frameSpeed<30)frameSpeed++;
  86.     frameRate(frameSpeed);
  87.     score+=foodMove;
  88.     foodEaten+=1;
  89.     println(score);
  90.   }
  91.   //if moved more than the food allow it, it reset
  92.   if (foodMove==0&&isFood&&foodPosition.x>0){
  93.   isFood=false;
  94.   foodPosition.x=-100;
  95.   foodMove=10;
  96.   }
  97.   //set new highscore
  98.     if (highScore<score)highScore=score;
  99. }
  100. //----------------------------------------------------------
  101. void exit() {
  102.   highScoreA[0]=str(highScore);
  103.   saveStrings("highScore.txt", highScoreA);
  104. }
  105. //-----------------------------------------------
  106. void keyPressed() {
  107.   if (isPlaying) {
  108.  
  109.     if (keyCode==LEFT||key=='E'||key=='e') {
  110.       direction-=90;
  111.       if(isFood&&foodPosition.x>0&&foodMove>0)foodMove--;
  112.     } else if (keyCode==RIGHT||key=='T'||key=='t') {
  113.       direction+=90;
  114.       if(isFood&&foodPosition.x>0&&foodMove>0)foodMove--;
  115.     }
  116.   } else isPlaying=true;
  117.   println(ver.get(0).x+" : "+ver.get(0).y);
  118. }
  119. void init() {
  120.   direction=0;
  121.   frameSpeed=10;
  122.   foodEaten=0;
  123.   frameRate(frameSpeed);
  124.   ver.clear();
  125.   for (int i=0; i<8; i++) {
  126.     PVector pos=new PVector();
  127.     pos.x=width/2+10*i;
  128.     pos.y=height/2;
  129.     ver.add(pos);
  130.   }
  131.   foodPosition.x=-100;
  132.   isFood=false;
  133.   isPlaying=false;
  134.  
  135.   pScore=score;
  136.   score=0;
  137. }
  138. void drawGrid() {
  139.   pushStyle();
  140.   noFill();
  141.   stroke(0, 153, 0);
  142.   beginShape();
  143.   vertex(2, 22);
  144.   vertex(width-2, 22);
  145.   vertex(width-2, height-2);
  146.   vertex(2, height-2);
  147.   endShape(CLOSE);
  148.   fill(0, 153, 0);
  149.   text("score :"+nf(score, 5)+" highScore :"+nf(highScore, 5), 10, 20);
  150.   popStyle();
  151. }
  152. void drawSnake() {
  153.   pushStyle();
  154.   fill(0, 153, 0);
  155.   noStroke();
  156.   for (int i=0; i<ver.size(); i++) {
  157.  
  158.  
  159.     rect(ver.get(i).x, ver.get(i).y, 8, 8);
  160.   }
  161.   popStyle();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement