Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<PVector> ver=new ArrayList<PVector>();
- int direction=0;
- PVector foodPosition=new PVector();
- boolean isFood=false, isPlaying=false;
- int frameSpeed=10;
- int highScore;
- int score, pScore;
- int foodEaten;
- int foodMove;
- String[] highScoreA=new String[1];
- void setup() {
- size(400, 420);
- frameRate(frameSpeed);
- String highScoreS=new String();
- if (loadStrings("highScore.txt")!=null)highScoreS=loadStrings("highScore.txt")[0];
- else {
- highScoreA[0]=str(highScore);
- saveStrings("highScore.txt", highScoreA);
- highScoreS=loadStrings("highScore.txt")[0];
- }
- highScore=Integer.parseInt(highScoreS);
- init();
- rectMode(CENTER);
- }
- //-----------------------------------------------
- void draw() {
- background(0);
- if (isPlaying) {
- update();
- background(0);
- stroke(0, 153, 0);
- noFill();
- drawGrid();
- fill(0, 153, 0);
- noStroke();
- for (int i=0; i<ver.size(); i++) {
- pushStyle();
- rect(ver.get(i).x, ver.get(i).y, 8, 8);
- popStyle();
- }
- if (isFood)ellipse(foodPosition.x, foodPosition.y, 10, 10);
- } else {
- drawGrid();
- drawSnake();
- }
- }
- //-----------------------------------------------
- void update() {
- //we copy the position from the previous worm part
- for (int i=ver.size()-1; i>0; i--) {
- PVector newPos=new PVector();
- newPos.set(ver.get(i-1));
- ver.set(i, newPos);
- }
- //set new position of the head
- ver.get(0).x-=cos(radians(direction))*10;
- ver.get(0).y-=sin(radians(direction))*10;
- // if it touches itself
- for (int i=1; i<ver.size(); i++) {
- if (ver.get(0).dist(ver.get(i))<3)init();
- }
- // if it touches the border
- if (ver.get(0).x<=2||ver.get(0).x>width-2||ver.get(0).y<=22||ver.get(0).y>height-2)init();
- if (frameCount%frameSpeed*3==0&&isFood==false)isFood=true;
- //if food just been turned true, place it inside
- if (isFood&&foodPosition.x<0) {
- int fX=(2+(int)random(10, (width-12))/10)*10;
- int fY= (int)random(2.2, (height-2)/10)*10;
- fX=constrain(fX, 10, width-10);
- fY=constrain(fY, 30, height-10);
- foodPosition.set((float)fX, (float)fY);
- foodMove=10;
- }
- // if head touch the food, set a dist lower than 3, incase something make the snake not fully aligned
- if (ver.get(0).dist(foodPosition)<3) {
- PVector newPart=new PVector();
- newPart.set(ver.get(ver.size()-1));
- // increase the lenght of snake, more he eats, more he grow faster
- for (int i=0; i<3+foodEaten; i++)ver.add(newPart);
- //reset food
- isFood=false;
- foodPosition.x=-100;
- if (frameSpeed<30)frameSpeed++;
- frameRate(frameSpeed);
- score+=foodMove;
- foodEaten+=1;
- println(score);
- }
- //if moved more than the food allow it, it reset
- if (foodMove==0&&isFood&&foodPosition.x>0){
- isFood=false;
- foodPosition.x=-100;
- foodMove=10;
- }
- //set new highscore
- if (highScore<score)highScore=score;
- }
- //----------------------------------------------------------
- void exit() {
- highScoreA[0]=str(highScore);
- saveStrings("highScore.txt", highScoreA);
- }
- //-----------------------------------------------
- void keyPressed() {
- if (isPlaying) {
- if (keyCode==LEFT||key=='E'||key=='e') {
- direction-=90;
- if(isFood&&foodPosition.x>0&&foodMove>0)foodMove--;
- } else if (keyCode==RIGHT||key=='T'||key=='t') {
- direction+=90;
- if(isFood&&foodPosition.x>0&&foodMove>0)foodMove--;
- }
- } else isPlaying=true;
- println(ver.get(0).x+" : "+ver.get(0).y);
- }
- void init() {
- direction=0;
- frameSpeed=10;
- foodEaten=0;
- frameRate(frameSpeed);
- ver.clear();
- for (int i=0; i<8; i++) {
- PVector pos=new PVector();
- pos.x=width/2+10*i;
- pos.y=height/2;
- ver.add(pos);
- }
- foodPosition.x=-100;
- isFood=false;
- isPlaying=false;
- pScore=score;
- score=0;
- }
- void drawGrid() {
- pushStyle();
- noFill();
- stroke(0, 153, 0);
- beginShape();
- vertex(2, 22);
- vertex(width-2, 22);
- vertex(width-2, height-2);
- vertex(2, height-2);
- endShape(CLOSE);
- fill(0, 153, 0);
- text("score :"+nf(score, 5)+" highScore :"+nf(highScore, 5), 10, 20);
- popStyle();
- }
- void drawSnake() {
- pushStyle();
- fill(0, 153, 0);
- noStroke();
- for (int i=0; i<ver.size(); i++) {
- rect(ver.get(i).x, ver.get(i).y, 8, 8);
- }
- popStyle();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement