I’m a beginner in processing and I’m making the flappy bird but, I’m stacked on the bar section.
//Final_project
int x = 0;
int y = 0;
int value = 0;
Bar[] bars;
void setup()
{
size(500, 600);
background(200);
bars = new Bar[2];
for (int i=0; i<bars.length; i++)
bars[i] = new Bar(i);
}
void draw(){
background(200);
y--;
for (int i=0; i<bars.length; i++){
bars[i].show();
bars[i].move();
}
fill(255);
ellipse(300,200-y,10,10);
}
void keyPressed() {
y++;
if (key == 'b') {
y++;
y++;
y++;
y++;
y++;
y++;
y++;
y++;
}
else {
}
}
//bar
public Bar(int q)
{
barStart = -300 + (int)random(4)*50;
barHeight = random(4, 9)*30;
x = barStart;
y = barHeight;
xSpeed = 5;
i = q;
if (i%2==0)
top = true;
else
top = false;
}
public void show()
{
fill(0, 25*i, 0);
if (top)
rect(x-90*i, 0, 80,barHeight);
else
rect(x-90*i, 600, 80,-barHeight);
}
public void move()
{
if (x-90*i>1000) {//if you go off-screen
x = -300 + (int)random(4)*50;//barStart
y = random(4, 9)*30;//barHeight
}
x = x + xSpeed;
}
}
I want these bars to come from the right side to the left side and also, i want to make these bars’ height come randomly.
Could someone fix it for me plz?
(under the//bar zone is all about the bar codes.)
1 Like
Start by posting your code properly. Edit your post, select your code, then hit the button that will format it like code. The button looks like this: </>
Thx for reminding me about that. Are these fine now?
Next, try copying the code in your post, pasting it in a new sketch, and running your own code. Does it run? Nope.
So your code has some problems. There is one too many }
's. The Bar variable type is not defined. Several variables are never defined.
Are you trying to make a “Bar” be a new type of object? It seems like it, since you’re trying to have an array of two of them. But you never define a Bar class anywhere.
Try addressing these problems yourself. Define a Bar class. Maybe it can make use of the extra }
? Does it have a constructor? What variables need to be defined in the class? That is, what variables does a Bar need?
… Once you have a sketch that runs without errors, post the code again.
These r in two different tabs. one of them is in the tab named “Final_project”, and the another one is in the tab named “bar” which is from under the “//bar”
And when i run it on my processing, it runs.
You didn’t re-copy the code out of your post like I told you to, did you?
Here’s the code we’re working with. It all goes in one single tab.
int x = 0;
int y = 0;
int value = 0;
Bar[] bars;
void setup()
{
size(500, 600);
background(200);
bars = new Bar[2];
for (int i=0; i<bars.length; i++)
bars[i] = new Bar(i);
}
void draw(){
background(200);
y--;
for (int i=0; i<bars.length; i++){
bars[i].show();
bars[i].move();
}
fill(255);
ellipse(300,200-y,10,10);
}
void keyPressed() {
y++;
if (key == 'b') {
y++;
y++;
y++;
y++;
y++;
y++;
y++;
y++;
}
else {
}
}
//bar
public Bar(int q)
{
barStart = -300 + (int)random(4)*50;
barHeight = random(4, 9)*30;
x = barStart;
y = barHeight;
xSpeed = 5;
i = q;
if (i%2==0)
top = true;
else
top = false;
}
public void show()
{
fill(0, 25*i, 0);
if (top)
rect(x-90*i, 0, 80,barHeight);
else
rect(x-90*i, 600, 80,-barHeight);
}
public void move()
{
if (x-90*i>1000) {//if you go off-screen
x = -300 + (int)random(4)*50;//barStart
y = random(4, 9)*30;//barHeight
}
x = x + xSpeed;
}
}
Copy, paste, and run it. It has errors.
Here’s that same code run through the cleaners.
float x = 0;
float y = 0;
int value = 0;
Bar[] bars;
void setup() {
size(500, 600);
background(200);
bars = new Bar[2];
for (int i=0; i<bars.length; i++) {
bars[i] = new Bar(i);
}
}
void draw() {
background(200);
y--;
for (int i=0; i<bars.length; i++) {
bars[i].show();
bars[i].move();
}
fill(255);
ellipse(300, 200, 10, 10);
}
void keyPressed() {
if (key == 'b') {
y++;
}
}
//bar
class Bar {
float barStart;
float barHeight;
float xSpeed;
boolean top;
float iq;
Bar(int q) {
barStart = -300 + (int)random(4)*50;
barHeight = random(4, 9)*30;
x = barStart;
y = barHeight;
xSpeed = 5;
if (q%2==0) {
top = true;
} else {
top = false;
}
iq = q;
}
void show() {
fill(0, 25*iq, 0);
if (top) {
rect(x-90*iq, 0, 80, barHeight);
} else {
rect(x-90*iq, 600, 80, -barHeight);
}
}
void move() {
if (x-90*iq>1000) {
x = -300 + (int)random(4)*50;
y = random(4, 9)*30;
}
x = x + xSpeed;
}
} // End class Bar
Again, this all goes in one tab. Notice the changes; the most interesting change is that the Bar()
function is now the constructor for a real class for Bar
objects.