vencinachev

Flappy02

Aug 9th, 2021 (edited)
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1.  
  2. float obspos, obsw = 20, h, d, obsspeed = 5;
  3. boolean gameover = false;
  4. PImage flappy;
  5. float xpos = 100, ypos = 400, birdsize = 40, speed = 4;
  6.  
  7. void setup(){
  8.   size(500, 800);  
  9.   obspos = width;
  10.   h = random(50, 600);
  11.   d = random(50, 150);
  12.   flappy = loadImage("flappy.png");
  13. }
  14.  
  15. void draw(){
  16.   if (!gameover){
  17.     background(0, 0, 255);
  18.     obstacle();
  19.     bird();
  20.     collision();
  21.   } else {
  22.     textSize(50);
  23.     text("Game over", 100, 200);
  24.   }
  25. }
  26.  
  27. void obstacle(){
  28.   fill(0, 255, 0);
  29.   rect(obspos, 0, obsw, h);
  30.   rect(obspos, h + d, obsw, height - (h + d));
  31.   obspos = obspos - obsspeed;
  32.   if (obspos < 0){
  33.     obspos = width;
  34.     h = random(50, 600);
  35.     d = random(50, 150);
  36.   }
  37. }
  38.  
  39. void bird(){
  40.   image(flappy, xpos, ypos, birdsize, birdsize);
  41.   if (keyPressed && key == ' '){
  42.     ypos -= speed;
  43.   } else {
  44.     ypos += speed;
  45.   }
  46. }
  47.  
  48. void collision(){
  49.   if (ypos + birdsize > height){
  50.     gameover = true;
  51.   }
  52.   if (xpos + birdsize > obspos && xpos < obspos + obsw){
  53.     if (ypos < h || ypos + birdsize > h + d){
  54.       gameover = true;
  55.     }
  56.   }
  57. }
Add Comment
Please, Sign In to add comment