# Making my 'bird' flappy fly

**URL:** <https://discourse.processing.org/t/making-my-bird-flappy-fly/18749>\
**Category:** Coding Questions\
**Created:** [March 17, 2020, 8:19pm UTC](https://discourse.processing.org/t/making-my-bird-flappy-fly/18749 "2020-03-17T20:19:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bry\_pk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bry_pk/32/8426_2.png) [@bry\_pk](https://discourse.processing.org/u/bry_pk)\
**Post date:** [March 17, 2020, 8:19pm UTC](https://discourse.processing.org/t/making-my-bird-flappy-fly/18749/1 "2020-03-17T20:19:10Z")

</div>

Here is my code for this game I’m making, it is a knockoff flappy birds. My Bird just disappears off-screen and will not return even when I press my key(s).

```auto
int x = 0;

void setup() {

PImage tubeImg = loadImage("TUBE.png");
PImage birbImg = loadImage("BIRB.png");
PImage birb2 = loadImage("BIRB (2).png");
PImage background = loadImage("BACKGROUND.png");
PImage bird = loadImage("BIRD.png");
PImage birdy = loadImage("BIRD2.png");
PImage circle = loadImage("circle.png");
PImage flappy = loadImage("flappy2.png");
int gamestate = 0;

frameRate(60);
fullScreen();
background(0);
noStroke();
float x;
float y;

imageMode(CENTER);
image(background, width/2, height/2);

imageMode(CENTER);
image(flappy, width/2, height/2);

}
void draw(){
  int gamestate = 0;
   int x = 0, y = 0;
  
 PImage background = loadImage("BACKGROUND.png"); 
 PImage flappy = loadImage("flappy2.png");

 if (gamestate == 0);
 imageMode(CORNER);
 image(background, x, 0); 
 image(background, x+background.width, 8);
 x -= 6;
  image(flappy, width/1, 200);
 y =+ 7;
}

void keyPressed() {
  PImage flappy = loadImage("flappy2.png");
  int y = 0;
  if (key == 'w');{
  image(flappy, width/1, y);
   y =- 10;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 17, 2020, 8:31pm UTC](https://discourse.processing.org/t/making-my-bird-flappy-fly/18749/2 "2020-03-17T20:31:55Z")

</div>

> [@bry\_pk](#):
>
> image(flappy, width/1, 200);

Maybe your bird is too far right

Try image(flappy, width/2, 200);

---

<div class="post-metadata">

**Author:** ![bry\_pk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bry_pk/32/8426_2.png) [@bry\_pk](https://discourse.processing.org/u/bry_pk)\
**Post date:** [March 18, 2020, 5:20am UTC](https://discourse.processing.org/t/making-my-bird-flappy-fly/18749/3 "2020-03-18T05:20:52Z")

</div>

Just tried this… did this on my second to last line of code. Still disappears off screen. Thank you very much tho! Appreciate it.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 18, 2020, 9:03am UTC](https://discourse.processing.org/t/making-my-bird-flappy-fly/18749/4 "2020-03-18T09:03:55Z")

</div>

> [@bry\_pk](#):
>
> y =- 10; // sets y to a value of -10

this is wrong: you want `y -= 10;`

`y -= 10;` is short for `y = y - 10;`

might be too fast, try `y = y - 2;`

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 18, 2020, 10:43am UTC](https://discourse.processing.org/t/making-my-bird-flappy-fly/18749/5 "2020-03-18T10:43:49Z")

</div>

Hello!

Welcome to the forum! Nice to have you here.

I want to mention :

**setup() and draw()**

- The function setup() runs only once, draw() runs on and on (60 times per second).
- When you want to _load_ images do it therefore in setup(), not in draw().
- When you want to _display_ images do it in draw(), not in setup().
- The function keyPressed() is only called once, when you press a key. So no use to display the image of flappy there. Display it in draw().

**Global variables.**

- When you declare a variable inside a function, it’s local (only known inside that function).
- When you declare a variable before setup(), it’s global (known in ALL functions). You want this. But at the moment, you declare x and y everywhere, this won’t work.

* * *

**Some errors**

There are a few things to be discussed here.

**The if-clause:**

` if (gamestate == 0);`

won’t work because the **;** ends the if-clause.

It says if gamestate is 0 do everything between the closing bracket ) and the ; which is **nothing**. Bad.

Instead use {…} to enclose the lines of code you want to execute when the condition is true:

```auto
  if (gamestate == 0) {
    imageMode(CORNER);
    image(background, x, 0);
    image(background, x+background.width, 8);
    x -= 6;
    image(flappy, width/1, 200);
    y += 7; // I corrected this
  }

```

same in keyPressed():

`if (key == ‘w’);{`  
won’t work!!! delete the ; please.

* * *

**Another point:**

y =+ 7; wrong. You want y += 7;

* * *

**Loading images**

This :

```auto
void keyPressed() {
  PImage flappy = loadImage("flappy2.png");

```

is not necessary; you loaded the image in setup() already.

Same for these line in draw():

```auto
  PImage background = loadImage("BACKGROUND.png");
  PImage flappy = loadImage("flappy2.png");

```

It’s very time consuming to load images, that’s why we do it in setup() and never in draw() or keyPressed().

* * *

**Images in setup()**

No use to show images in setup() - it gets overwritten

* * *

**Your variables**

you have this in draw int x = 0, y = 0;  
this in keyPressed() int y = 0;  
this in setup

```auto
  float x;
  float y;

```

delete all this. Because you reset it 0 every time.

Instead have the int x = 0, y = 0; before setup(). This means it’s **global** and known in every function (draw, keyPressed…). Only then flappy keeps its position and doesn’t fall back to 0.

global must also be

int gamestate = 0;

and all images:

```auto
  PImage tubeImg = loadImage("TUBE.png");
  PImage birbImg = loadImage("BIRB.png");
  PImage birb2 = loadImage("BIRB (2).png");
  PImage background = loadImage("BACKGROUND.png");
  PImage bird = loadImage("BIRD.png");
  PImage birdy = loadImage("BIRD2.png");
  PImage circle = loadImage("circle.png");
  PImage flappy = loadImage("flappy2.png");

```

so it belongs **before** setup() as well!!!

* * *

**Use your variables**

> [@bry\_pk](#):
>
> image(flappy, width/1, 200);

No, you rather want image(flappy, x,y); !!! Use the variables!  
(or you want flappy to stay put and move the background underneath, then you want  
`image(flappy, width/2, y);`. You need width/2 instead of width/1 and you need `y`)

* * *

**Images in keyPressed()**

You have this in keyPressed - but no use since keyPressed is executed only briefly  
` image(flappy, width/1, y);`

Regards, Chrisir

* * *

**Full Code**

(moving the bird here and not the background)

```auto

int x = 0, y = 0;
int gamestate = 0;

void setup() {
  fullScreen();
  frameRate(60);

  x = 30;
  y = height/2;

  background(0);
  noStroke();
  imageMode(CENTER);
  textSize(32);
}

void draw() {
  if (gamestate == 0) {
    imageMode(CORNER);
    background(0); 
    x += 1;
    fill(255);
    text("flappy", x, y);
    text("USE w and s for Flappy", width/2-133, 140);
  }
}

void keyPressed() {
  if (key == 'w') {
    y -= 10;
  } else if (key == 's') {
    y += 10;
  }
}//func 
//

```

---

<div class="post-metadata">

**Author:** ![bry\_pk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bry_pk/32/8426_2.png) [@bry\_pk](https://discourse.processing.org/u/bry_pk)\
**Post date:** [March 19, 2020, 7:55am UTC](https://discourse.processing.org/t/making-my-bird-flappy-fly/18749/6 "2020-03-19T07:55:11Z")

</div>

Hey Chrisir!  
Just wanted to say thank you for the time you took out of your day to help me out… it really means a lot. I spent most of my day today at home (due to the quarantine) and I’m not gonna lie to you… I am still a little confuzzled but I will sleep for a bit right now as it is almost 1am here. Again, I greatly appreciate it, definitely needed the help.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 19, 2020, 10:01am UTC](https://discourse.processing.org/t/making-my-bird-flappy-fly/18749/7 "2020-03-19T10:01:16Z")

</div>

no worries, the start of learning to program is always a bit awkward.

Please come back with any questions.

Chrisir
