Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help for Tetris code

I'm new to java, and trying to figure out how to paint the different shapes/blocks of tetris, and make them fall at random. Specifically, I need help on searching the array to find out which shape to paint. Here's the code so far:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class Shapes extends Applet{

int blockshape = 0;

private static final int[][][][] pieces = {
{ // ####
{{0,1},{0,0},{0,-1},{0,-2}},
{{-1,0},{0,0},{1,0},{2,0}},
{{0,1},{0,0},{0,-1},{0,-2}},
{{-1,0},{0,0},{1,0},{2,0}}
},
{ // #
// ##
// #
{{-1,0},{0,1},{1,0},{0,0}},
{{0,1},{1,0},{0,-1},{0,0}},
{{1,0},{0,-1},{-1,0},{0,0}},
{{0,-1},{-1,0},{0,1},{0,0}}
},
{
// ##
// ##
{{0,0},{0,-1},{1,0},{1,-1}},
{{0,0},{0,-1},{1,0},{1,-1}},
{{0,0},{0,-1},{1,0},{1,-1}},
{{0,0},{0,-1},{1,0},{1,-1}}
},
{
// ##
// ##
{{-1,0},{0,0},{0,1},{1,1}},
{{1,-1},{1,0},{0,0},{0,1}},
{{-1,0},{0,0},{0,1},{1,1}},
{{1,-1},{1,0},{0,0},{0,1}}
},
{
// ##
// ##
{{-1,1},{0,1},{0,0},{1,0}},
{{0,-1},{0,0},{1,0},{1,1}},
{{-1,1},{0,1},{0,0},{1,0}},
{{0,-1},{0,0},{1,0},{1,1}}
},
{
// #
// ###
{{-1,-1},{-1,0},{0,0},{1,0}},
{{-1,1},{0,1},{0,0},{0,-1}},
{{1,1},{1,0},{0,0},{-1,0}},
{{1,-1},{0,-1},{0,0},{0,1}}
},
{
// #
// ###
{{-1,1},{-1,0},{0,0},{1,0}},
{{-1,-1},{0,-1},{0,0},{0,1}},
{{1,-1},{1,0},{0,0},{-1,0}},
{{1,1},{0,1},{0,0},{0,-1}}
}
};

public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;

for(int i=0; i < 4, i++)
{

g2d.setPaint(Color.blue);
g2d.fill(new Rectangle2D.Double(50, 50, 20, 20));

g2d.setStroke(new BasicStroke(3.0f));
g2d.setPaint(Color.black);
g2d.draw(new Rectangle2D.Double(50, 50, 20, 20));

}

public int generateShape()
{
blockshape = (int)(Math.random() * 7);

switch(blockshape)
{
case 0: pieces[0][0][0][0];
break;

case 1: pieces[1][0][0][0];
break;

case 2: pieces[2][0][0][0];
break;

case 3: pieces[3][0][0][0];
break;

case 4: pieces[4][0][0][0];
break;

case 5: pieces[5][0][0][0];
break;

case 6: pieces[6][0][0][0];
break;

}


}

Currently I am working on the loop before the paint method.

Any suggestions?
[3326 byte] By [NoIdea] at [2007-11-11 8:17:54]
# 1 Re: Help for Tetris code
lots of numbers there and i don't get what they mean.
why do you use two coordinates for a block representation, and then also so many different values?
my first suggestion is to use more object-orientation.
second: make a simpler and clearer encoding of your blocks, eg:
// #
// ###
as
// 0000
// 0000
// 0010
// 0111
as
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1}
when using objectorientation, a block would be represented by a class having following attributes:
size:java.awt.Dimension // represents width and height of a block, eg. 3/1
position:java.awt.Point // repreents the left top position of a block on the field, eg 0/0
color:java.awt.Color // represents the color of a block
also several methods are needed, eg
rotateLeft, rotateRight, moveLeft, moveRight, moveDown
the methods then have also to check, if a position on the field already is beeing populated by another block
graviton at 2007-11-11 22:35:50 >
# 2 Re: Help for Tetris code
I'm a Vietnamese so i speak English not well.
If you want to write a TetrisPiece you can do:
1. Using static final int variable to distinguied these different shape (L, J, S, Z, O, I, T shape). Example:
static final int L_PIECE = 0;
static final int I_PIECE = 1;
...
2. To store piece, you can use java.awt.Point to store the center coordinate, and use Point[] array to store the coordinate of the relative blocks with the center Point.

... You can consult this way at address: http://www-128.ibm.com/developerworks/java/library/j-tetris/

Wish you success!
tiendat_vnit at 2007-11-11 22:36:49 >