2
\$\begingroup\$

Im trying to make a simple clone of flappy bird game. The tutorial that i watched used only rectangles for all the objects. But the thing is im trying to do it with nicer images. I have handled the bird and its movements but i can not generate pipes from images and terminate them after they get out of the screen.

The way i want to do is just like the tutorial. He made an arraylist of rectangles, and simply adding rectangles to that list with the position and dimension variables. When they go out of the screen, he deletes the pipes and add the new ones with "arraylist.add(new Rectangle())", and simply draws them with Graphics.fillRect(). But i cant do it with BufferedImage type or neither the Graphics2D.

Could anyone help please? I want to be able to generate random pipes like rectangles, and wanna be able to add them to an arraylist and draw them. Its pretty like creating blocks from an image in mario game

Any help would be appreciated!

\$\endgroup\$
1
\$\begingroup\$

I assume your are using 'java.awt.*' to do this and you wanted this solution to be similar in nature. In that case I think the easiest solution would be to simply be extending the Rectangle class with a field for your BufferedImage and draw using that.

RectangleImage.java:

import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class RectangleImage extends Rectangle {

    public BufferedImage img;

    public RectangleImage(BufferedImage img, int x, int y) {
        this.x = x;
        this.y = y;
        this.img = img;
        this.width = img.getWidth();
        this.height = img.getHeight();
    }
}

This is a rectangle that contains an image and is as wide and as tall as the image and can be rendered out as any other rectangle by using fillRect() as usual. However if you want to render it out as an image, then you simply use drawImage() instead.

Example:

RectangleImage recImg = new RectangleImage(null, 0, 0);
Graphics.drawImage(recImg.img, recImg.x, recImg.y, recImg.width, recImg.height, null);
\$\endgroup\$
  • 1
    \$\begingroup\$ Dude you are totally awesome. Maybe its a really simple question for you but as a new game programmer, your answer came to me like a brilliant idea. Thank you so much! \$\endgroup\$ – Tolga Şen Jan 28 '16 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.