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