I am working on writing the Tetris game myself using Java. I am not using any tutorials already containing the code, because I want to try to decompose it myself and see how far I can take it.
So far, not so good. I have stumbled upon the creation on the Shapes.
My idea would be to either:
1. have each basic shape (like the L, the cube, the boat) as separate classes extending or implementing Area or Shape so that I can use it as an argument for g2.fill(LShape)
.
2. each class would have some sort of state variable describing the rotation position, but that's next challenge, figuring out the rotation..
So for Step 1, I have written so far the following drafts of the LShape class:
Draft a):
public class LShape implements Shape{
private Rectangle[][] poc;
private int rotationState;
public LShape() {
rotationState = 0;
poc = new Rectangle[3][3];
poc[0][0] = new Brick(INITIAL_X - BRICK, INITIAL_Y, BRICK);
poc[1][0] = new Brick(INITIAL_X - BRICK, INITIAL_Y + BRICK, BRICK);
poc[2][0] = new Brick(INITIAL_X - BRICK, INITIAL_Y + 2 * BRICK, BRICK);
poc[2][1] = new Brick(INITIAL_X, INITIAL_Y + 2 * BRICK, BRICK);
}
}
//.....all the Shape's methods which I'm not overriding cause I don't know how
In the main class I am calling in the paint() method:
g2.fill(lShape); // where lShape is a LShape object;
And the trouble is an exception is thrown about the getPathIterator()
OR Draft b):
public class LShape extends Area{
public LShape () {
add(new Area(new Brick(INITIAL_X - BRICK, INITIAL_Y, BRICK)));
exclusiveOr(new Area(new Brick(INITIAL_X - BRICK, INITIAL_Y + BRICK, BRICK)));
exclusiveOr(new Area(new Brick(INITIAL_X - BRICK, INITIAL_Y + 2 * BRICK, BRICK)));
exclusiveOr(new Area(new Brick(INITIAL_X, INITIAL_Y + 2 * BRICK, BRICK)));
}
}
In this case when I call the g2.fill(lShape)
there is no exception and the Shape is drawn, only that I don't know how to move it. Parts of the Area are the Brick objects which are Rectangles, so I could try to access the setLocation
method on each Brick in the Area, but I don't know how to access it.
So I guess I need help either to figure out how to make the Shape implementation of a Tetris Shape not to throw the exceptions, meaning implementing all the required methods and actually showing up on the JPanel..and then I will worry about the rotation. OR figure out how to make the Area extension of a Tetris Shape to move around.
Thanks,