import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Tetris extends Applet implements Runnable {
int ax , bx , cx ,dx ,ay, by, cy , dy, x , y;
boolean on = true;
Thread t;
int state = 1;
boolean[][] coords = new boolean[11][21];
boolean hit;
int rate = 500;
int shape = 0;
int color = 1;
int[][] colors = new int[11][21];
public void init() {
setBackground(Color.black);
setLayout( new BorderLayout() );
Button start = new Button("Start");
add("North", start);
for (int i = 0; i < 10; i++)
coords[i][20] = true;
}
public void start() {
t = new Thread(this);
if (t != null) {
t.start();
}
}
public void paint (Graphics g) {
for (int i = 1; i < 5; i++)
{
if (i == 1)
{
x = ax;
y = ay;
}
else if (i==2)
{
x = bx;
y = by;
}
else if (i==3)
{
x = cx;
y = cy;
}
else if (i==4)
{
x = dx;
y = dy;
}
g.setColor(Color.black);
g.drawRect((20 * x),(20 * y), 20,20);
switch(color) {
case (1) :
g.setColor(Color.red);
break;
case (2) :
g.setColor(Color.blue);
break;
case (3) :
g.setColor(Color.green);
break;
}
g.fillRect((20 * x + 1),(20 * y + 1), 19,19);
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 20; j++) {
if (coords[i][j] == true) {
g.setColor(Color.black);
g.drawRect((20 * i),(20 * j), 20,20);
switch(colors[i][j]) {
case (1) :
g.setColor(Color.red);
break;
case (2) :
g.setColor(Color.blue);
break;
case (3) :
g.setColor(Color.green);
break;
}
g.fillRect((20 * i + 1),(20 * j + 1), 19,19);
}
}
}
}
public void run() {
newpiece();
while(on == true) {
repaint();
move();
check();
if (hit == true) {
hit = false;
rate = 400;
newpiece();
checkline();
}
try {
t.sleep(rate);
}
catch (InterruptedException e ) {}
}
}
public void move() {
ay += 1;
by += 1;
cy += 1;
dy += 1;
}
public void stop() {
if (t != null) {
t.stop();
t = null;
}
}
public boolean keyDown(Event e , int key) {
switch (key) {
case Event.UP :
rotate();
break;
case Event.RIGHT :
moveright();
break;
case Event.LEFT :
moveleft();
break;
case Event.DOWN :
rate = 20;
default :
break;
case Event.F1 :
on = true;
}
return true;
}
public void rotate() {
if (shape == 1) {
if (state == 1) {
bx += 1;
dx -= 1;
ay -= 1;
cy += 1;
dy += 2;
}
else if (state == 2) {
ax -= 1;
cx += 1;
dx += 2;
by -= 1;
dy += 1;
}
state++;
if (state == 3)
state = 1;
}
}
public void check() {
if ((coords[ax][ay + 1] == true) || (coords[bx][by + 1] == true) ||
(coords[cx][cy + 1] == true) || (coords[dx][dy+1] == true))
{
coords[ax][ay] = true;
colors[ax][ay] = color;
coords[bx][by] = true;
colors[bx][by] = color;
coords[cx][cy] = true;
colors[cx][cy] = color;
coords[dx][dy] = true;
colors[dx][dy] = color;
hit = true;
}
}
public void newpiece() {
state = 1;
shape++;
if (shape == 4)
shape = 1;
switch(shape) {
case ( 1) :
ax = 3;
ay = 3;
bx = 2;
by = 3;
cx = 2;
cy = 2;
dx = 2;
dy = 1;
color = 1;
break;
case (2) :
ax = 4;
ay = 0;
bx = 5;
by = 0;
cx = 4;
cy = 1;
dx = 5;
dy = 1;
color = 3;
break;
case (3) :
ax = 3;
bx = 4;
cx = 5;
dx = 6;
ay = 0;
by = 0;
cy = 0;
dy = 0;
color = 2;
break;
default :
break;
}
System.out.println(shape);
}
public void checkline() {
for (int y = 19; y > 0; y--) {
boolean line = true;
for (int x = 0; x < 10; x++) {
if (coords[x][y] == false)
line = false;
}
if (line == true)
eraseline();
}
}
public void eraseline() {
for ( int x = 0; x < 10; x++)
for (int y = 19; y > 0; y--) {
if (coords[x][y] == false)
coords[x][y + 1] = false;
}
}
public void moveright() {
if (!((ax == 9) || (bx ==9) || (cx == 9) || (dx == 9))) {
ax += 1;
bx += 1;
cx += 1;
dx += 1;
}
}
public void moveleft() {
if (!((ax == 0) || (bx ==0) || (cx == 0) || (dx == 0))) {
ax--;
bx--;
cx--;
dx--;
}
}
}