Making my 'bird' flappy fly

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).

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;
  }
}
1 Like

Maybe your bird is too far right

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

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

this is wrong: you want y -= 10;

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

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

Chrisir

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:

  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 :

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

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

Same for these line in draw():

  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

  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:

  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

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)


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 
//
2 Likes

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.

2 Likes

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

Please come back with any questions.

Chrisir