Flappy Code

edited June 2015 in Share Your Work

Some flappy code for processing i have made. Currently 128 lines, it would be interesting to see how minimal the code could be and still be functional.

 //Flappy Code
    bird b = new bird();
    pillar[] p = new pillar[3];
    boolean end=false;
    boolean intro=true;
    int score=0;
    void setup(){
      size(500,800);
      for(int i = 0;i<3;i++){
      p[i]=new pillar(i);
      }
    }
    void draw(){
      background(0);
      if(end){
      b.move();
      }
      b.drawBird();
      if(end){
      b.drag();
      }
      b.checkCollisions();
      for(int i = 0;i<3;i++){
      p[i].drawPillar();
      p[i].checkPosition();
      }
      fill(0);
      stroke(255);
      textSize(32);
      if(end){
      rect(20,20,100,50);
      fill(255);
      text(score,30,58);
      }else{
      rect(150,100,200,50);
      rect(150,200,200,50);
      fill(255);
      if(intro){
        text("Flappy Code",155,140);
        text("Click to Play",155,240);
      }else{
      text("game over",170,140);
      text("score",180,240);
      text(score,280,240);
      }
      }
    }
    class bird{
      float xPos,yPos,ySpeed;
    bird(){
    xPos = 250;
    yPos = 400;
    }
    void drawBird(){
      stroke(255);
      noFill();
      strokeWeight(2);
      ellipse(xPos,yPos,20,20);
    }
    void jump(){
     ySpeed=-10; 
    }
    void drag(){
     ySpeed+=0.4; 
    }
    void move(){
     yPos+=ySpeed; 
     for(int i = 0;i<3;i++){
      p[i].xPos-=3;
     }
    }
    void checkCollisions(){
     if(yPos>800){
      end=false;
     }
    for(int i = 0;i<3;i++){
    if((xPos<p[i].xPos+10&&xPos>p[i].xPos-10)&&(yPos<p[i].opening-100||yPos>p[i].opening+100)){
     end=false; 
    }
    }
    } 
    }
    class pillar{
      float xPos, opening;
      boolean cashed = false;
     pillar(int i){
      xPos = 100+(i*200);
      opening = random(600)+100;
     }
     void drawPillar(){
       line(xPos,0,xPos,opening-100);  
       line(xPos,opening+100,xPos,800);
     }
     void checkPosition(){
      if(xPos<0){
       xPos+=(200*3);
       opening = random(600)+100;
       cashed=false;
      } 
      if(xPos<250&&cashed==false){
       cashed=true;
       score++; 
      }
     }

    }
    void reset(){
     end=true;
     score=0;
     b.yPos=400;
     for(int i = 0;i<3;i++){
      p[i].xPos+=550;
      p[i].cashed = false;
     }
    }
    void mousePressed(){
     b.jump();
     intro=false;
     if(end==false){
       reset();
     }
    }
    void keyPressed(){
     b.jump(); 
     intro=false;
     if(end==false){
       reset();
     }
    }

Comments

  • edited March 2014

    Super funny. I've reached 45 points:

    Flappy Code

  • Nice :) Ugly but nice to play. Bit too easy i think ;)

    Link

  • I haven't played the original game but I consider this to be a good substitute.

  • That beautiful. Thanks for sharing this. I'm gonna totally pimp it with some customs made sprites.

  • Just for fun I got it down to 1 line of 1569 characters

    bird b=new bird();pillar[] p=new pillar[3];boolean end=false;boolean intro=true;int score=0;void setup(){size(500,800);for(int i=0;i<3;i++)p[i]=new pillar(i);}void draw(){background(0);if(end)b.move();b.drawBird();if(end)b.drag();b.checkCollisions();for(int i=0;i<3;i++){p[i].drawPillar();p[i].checkPosition();}fill(0);stroke(255);textSize(32);if(end){rect(20,20,100,50);fill(255);text(score,30,58);}else {rect(150,100,200,50);rect(150,200,200,50);fill(255);if(intro){text("Flappy Code",155,140);text("Click to Play",155,240);}else {text("game over",170,140);text("score",180,240);text(score,280,240);}}}class bird {float xPos,yPos,ySpeed;bird(){xPos=250;yPos=400;}void drawBird(){stroke(255);noFill();strokeWeight(2);ellipse(xPos,yPos,20,20);}void jump(){ySpeed=-10;}void drag(){ySpeed+=0.4;}void move(){yPos+=ySpeed;for(int i=0;i<3;i++)p[i].xPos-=3;}void checkCollisions(){if(yPos>800)end=false;for(int i=0;i<3;i++)if((xPos<p[i].xPos+10&&xPos>p[i].xPos-10)&&(yPos<p[i].opening-100||yPos>p[i].opening+100))end=false;}}class pillar {float xPos,opening;boolean cashed=false;pillar(int i){xPos=100+(i*200);opening=random(600)+100;}void drawPillar(){line(xPos,0,xPos,opening-100);line(xPos,opening+100,xPos,800);}void checkPosition(){if(xPos<0){xPos+=(200*3);opening=random(600)+100;cashed=false;}if(xPos<250&&cashed==false){cashed=true;score++;}}}void reset(){end=true;score=0;b.yPos=400;for(int i=0;i<3;i++){p[i].xPos+=550;p[i].cashed=false;}}void mousePressed(){b.jump();intro=false;if(end==false)reset();}void keyPressed(){b.jump();intro=false;if(end==false)reset();}
    
  • Booleans default to false, so you can remove their assignments and make it shorter. if(end==false) can be if(!end), which is shorter. Single variable names for even more reduced length.

  • edited April 2014

    Thanks for posting this. Funny!

  • edited April 2014

    Challenge Accepted! Here's the shortest (with minimal output changes) I could come up with 852 characters...

    P[]p=new P[3];char e=0;char I=1;int S=0,h=100,n=200,j,t=255;float x=t,y=400,q;void setup(){size(500,800);for(j=0;j<3;j++)p[j]=new P(j);}void draw(){clear();stroke(t);if(e>0){y+=q;for(P f:p)f.x-=3;}noFill();strokeWeight(2);ellipse(x,y,20,20);if(e>0)q+=0.4;for(P f:p)if((y>800)||(x<f.x+10&&x>f.x-10)&&(y<f.o-h||y>f.o+h))e=0;for(P f:p)f.d();fill(0);textSize(32);if(e>0){rect(20,20,h,50);fill(t);text(S,30,58);}else{rect(150,h,n,50);rect(150,n,n,50);fill(t);if(I>0)text("Flappy Code\n\nClick to Play",155,140);else text("game over\n\n score "+S,170,140);}}class P{float x,o=random(600)+h;char c=0;P(int i){x=h+(i*n);}void d(){line(x,0,x,o-h);line(x,o+h,x,800);if(x<0){x+=(n*3);o=random(600)+h;c=0;}if(x<t&&c<1){c=1;S++;}}}void R(){e=1;S=0;y=400;for(P f:p){f.x+=550;f.c=0;}}void mousePressed(){q=-10;I=0;if(e<1)R();}void keyPressed(){q=-10;I=0;if(e<1)R();}

  • edited April 2014

    813 Vs. 851 characters/bytes: >:)

    P[]p=new P[3];int e=0,I,S=0,h=100,n=200,j,t=255;float x=t,y=400,q;void setup(){size(500,800);strokeWeight(2);textSize(32);for(j=0;j<3;p[j]=new P(j++));}void draw(){clear();if(e>0){y+=q;for(P f:p)f.x-=3;}stroke(t);noFill();ellipse(x,y,20,20);if(e>0)q+=.4;for(P f:p)if(y>800|x<f.x+10&x>f.x-10&&y<f.o-h|y>f.o+h)e=0;for(P f:p)f.d();fill(0);if(e>0){rect(20,20,h,50);fill(t);text(S,30,58);}else{rect(150,h,n,50);rect(150,n,n,50);fill(t);if(I<1)text("Flappy Code\n\nClick to Play",155,140);else text("Game Over\n\n Score: "+S,170,140);}}void mousePressed(){keyPressed();}void keyPressed(){q=-10;I=1;if(e<1)R();}void R(){e=1;S=0;y=400;for(P f:p){f.x+=550;f.c=0;}}class P{float x,o=random(600)+h;int c;P(int i){x=h+i*n;}void d(){line(x,0,x,o-h);line(x,o+h,x,800);if(x<0){x+=n*3;o=random(600)+h;c=0;}if(x<t&c<1){c=1;S++;}}}

  • down to 799 characters

    P[]p=new P[3];int e,I,S,h=10,n=20,j,t=255;float x=25.5,y=40,q;void setup(){size(500, 800);strokeWeight(.2);textSize(3.2);for(;j<3;p[j++]=new P(j));}void draw(){scale(h);clear();if(e>0){y+=q;for(P f:p)f.x-=.3;}stroke(t);noFill();ellipse(x,y,2,2);if(e>0)q+=.04;for(P f:p)if(y>80|x<f.x+1&x>f.x-1&&y<f.o-h|y>f.o+h)e=0;for(P f:p)f.d();fill(0);if(e>0){rect(2,2,h,5);fill(t);text(S,3,5.8);}else{rect(15,h,n,5);rect(15,n,n,5);fill(t);if(I<1)text("Flappy Code\n\nClick to Play",15.5,14);else text("Game Over\n\n Score: "+S,17,14);}}void mousePressed(){keyPressed();}void keyPressed(){q=-1;I=1;if(e<1)R();}void R(){S=0;y=e=40;for(P f:p){f.x+=55;f.c=0;}}class P{float x,o=random(60)+h;int c;P(int i){x=h+i*n;}void d(){line(x,0,x,o-h);line(x,o+h,x,80);if(x<0){x+=60;o=random(60)+h;c=0;}if(x<25.5&c<1){c=++S;}}}

  • edited April 2014

    Down to 711 now: \m/
    P[]p=new P[3];int I,S,e,j,x=25,h=10,n=20,k=14,t=-1;float q,y=40;void setup(){size(500,800);strokeWeight(.2);textSize(3);while(j<3)p[j]=new P(j++);}void draw(){clear();scale(h);stroke(t);noFill();ellipse(x,y,2,2);for(P f:p)f.d();fill(0);if(e>0){y+=q+=.04;rect(2,2,h,5);fill(t);text(S,3,6);}else{rect(k,h,n,5);rect(k,n,n,5);fill(t);text(I>t?"Flappy Code\n\nClick to Play":"Game Over\n\nScore: "+S,16,k);}}void mousePressed(){keyPressed();}void keyPressed(){q=I=t;if(e<1)R();}void R(){S=0;y=e=40;for(P f:p)f.c=f.a+=55;}class P{float a,b,c;P(int i){a=i*n-r();}void d(){e=y>80|x==int(a<0?r():a)&&y<b-h|y>b+h?0:e;c=a<25&c>0?-S++:c;line(a-=e>0?.4:0,0,a,b-h);line(a,b+h,a,80);}float r(){c=b=random(60)+h;return a+=60;}}

  • This expression is great... e=y>80|x==int(a<0?r():a)&&y<b-h|y>b+h?0:e;

    700...

    P[]p=new P[3];int I,S,e,j,x=25,h=10,n=20,k=14,t=-1;float q,y=40;void setup(){size(500,800);strokeWeight(.2);textSize(3);while(j<3)p[j]=new P(j++);}void draw(){clear();scale(h);stroke(t);noFill();ellipse(x,y,2,2);for(P f:p)f.d();fill(0);if(e>0){y+=q+=.04;rect(2,2,h,5);fill(t);text(S,3,6);}else{rect(k,h,n,5);rect(k,n,n,5);fill(t);text(I>t?"Flappy Code\n\nClick to Play":"Game Over\n\n Score: "+S,16,k);}}void mousePressed(){keyPressed();}void keyPressed(){q=I=t;if(e<1){S=0;y=e=40;for(P f:p)f.c=f.a+=55;}}class P{float a,b,c;P(int i){a=i*n-r();}void d(){e=y>80|x==int(a<0?r():a)&&y<b-h|y>b+h?0:e;c=a<25&c>0?-S++:c;line(a-=e>0?.4:0,0,a,b-h);line(a,b+h,a,80);}float r(){c=b=random(60)+h;return a+=60;}}

  • edited April 2014

    Good idea about getting rid of R(). Since it's called in 1 place only anyways! :>

  • edited June 2015

    My preliminary "Flappy Code" compact attempt on CoffeeScript mode! 8-X
    For now it's just 675 bytes! A gr8 start I might add! $-)

    P.S.: Remove the <br> just after y=e=40. Posting bug! :-(
    Also notice that "sketch.properties" is configured as coffee.editor.tabs.size=2

    p=[];I=S=e=j=q=0;x=25;y=40;h=10;n=20;k=14;t=-1
    setup:->
      size 500,800;p[j]=new P j++ while j<3;strokeWeight .2;textSize 3
    draw:->background 0;scale h;stroke t;noFill();ellipse x,y,2,2;f.d() for f in p;fill 0;if e>0 then y+=q+=.04;rect 2,2,h,5;fill t;text S,3,6;else rect k,h,n,5;rect k,n,n,5;fill t;text(`I>t?"Flappy Code\n\n\nClick to Play":"Game Over\n\n\nScore: "+S`,16,k)
    mousePressed:->@keyPressed()
    keyPressed:->q=I=t;if e<1 then f.c=f.a+=55 for f in p;S=0;y=e=40
    class P  
      constructor:(i)->@a=0;@a=i*n-@r()
      d:->@a=@r() if @a<0;e=0 if y>80|x==~~@a&&y<@b-h|y>@b+h;@c=-S++ if @a<25&@c>0;@a-=.4 if e>0;line @a,0,@a,@b-h;line @a,@b+h,@a,80
      r:->@c=@b=h+random 60;@a+=60
    
  • edited April 2014

    687 character flappy bird code I made it from scratch rather than compacting anyone elses! I might be able to make it smaller (:|

    float p=400,a;int g,s,v=166,w=500,z=200;boolean d,f,t;float[]y={z,w,v};void setup(){size(w,800);strokeWeight(3);textSize(25);}void draw(){clear();if(d&&f){int x=w-frameCount*2%v;if(x==w){y[0]=y[1];y[1]=y[2];y[2]=random(w);};a+=.3;rect(x-v,-5,v,805);rect(x-332,0,0,800);stroke(0);int c=3;for(int i=x;i>x-333;i-=v)line(i,y[c-=1],i,y[c]+z);stroke(255);d=get(100,int(p=max(min(p+a,795),5)))==color(0);if(d&&x==400)s++;ellipse(100,p,20,20);r(0,0,str(s));}else{r(v,300,(f?"gameover":"flappycode"));r(v,w,(f?"score:"+s:"clicktoplay"));}if(mousePressed||keyPressed)c();}void c(){if(!d)s=0;a=-7;d=f=!t;}void r(int x,int y,String s){stroke(255);rect(x,y,z,40);fill(255);text(s,x+30,y+30);fill(0);}

    Is that the best for the normal Java mode so far?

  • I can only take short spans of code crushing... All I can say is, amazing work GoToLoop!

    DCRaven here are some tips - switch your booleans to ints with 1 being true and 0 being false, try using 'scale()' to kill a few decimal places, and, of course, more ternary operators!

    Here's to the day we can have a 1 character flappy bird clone!

  • edited May 2014

    @benvancitters I did think of the Boolean to int change and though that would work well but no... You can't do that in the version of processing I have currently :( 'cannot convert from int to Boolean' I could change it out and just use strictly '0 & 1' but that would be larger as with Booleans you can just if(variable) for true.

  • edited June 2015

    I beat all of you guys!: import flappyCode.*; :) hahaha 1 line with only 20 characters!

  • haha...

  • import f.*;

  • I think this game would also be nice where you have a flock of birds and you loose every bird that is colliding.

  • I made a cool flappy bird once.

Sign In or Register to comment.