昨天看同学在做俄罗斯方块,自己心痒痒的也想做个,费了九牛二虎之力,才写了这么个简单的 Applet 来,继续努力,争取尽快完善.
(运行后点击界面激活窗口后就可以用键盘控制了, 方向键 上: 旋转, 下:加速, 左:左移, 右:右移)
import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;


public class Game extends Applet implements Runnable
{
boolean[][] Map = new boolean[21][12];
boolean pause = false;
long sleep_time = 1000;
Thread runer;
Image buffer_img;
Graphics g;
TShape shape;


public void init()
{
this.resize(170, 320);
for (int i = 0; i < 20; i++)
for (int j = 0; j < 12; j++)
Map[i][j] = false;
for (int i = 0; i < 12; i++)
Map[20][i] = true;

for (int i = 0; i < 20; i++)
{
Map[i][0] = true;
Map[i][11] = true;
}
buffer_img = createImage(this.size().width, this.size().height);
g = buffer_img.getGraphics();
shape = new TShape();
shape.next();
}


public void start()
{

if (runer == null)
{
runer = new Thread(this);
runer.start();
}
}


public void stop()
{

if (runer != null)
{
runer.stop();
runer = null;
}
}


private boolean checkPoint()
{
Point[] ps = shape.getPoint();

for (int i = 0; i < ps.length; i++)
{
if (ps[i].x < 0)
return false;
if (ps[i].x >= 11)
return false;
if (ps[i].y >= 0)
if (Map[ps[i].y][ps[i].x])
return false;
}
return true;
}


private void check()
{
shape.move(0, 1);

if (!checkPoint())
{
shape.move(0, -1);
Point[] ps2 = shape.getPoint();
for (int j = 0; j < ps2.length; j++)
if (ps2[j].x >= 0 && ps2[j].y >= 0)
Map[ps2[j].y][ps2[j].x] = true;
checkFull();
shape.next();
System.gc();

if (!checkPoint())
{
runer = null;
System.out.println("失败!游戏结束");
pause = true;
return;
}
}
}


private void checkFull()
{
boolean flag;

for (int i = 19; i >= 0; i--)
{
flag = true;
for (int j = 1; j < 11; j++)
if (!Map[i][j])
flag = false;

if (flag)
{
g.setColor(Color.WHITE);
for (int k = 1; k < 11; k++)
g.fillRect(k * 15 - 3, 12 + i * 15, 12, 12);
myPaint();

try
{Thread.sleep(100);} catch (Exception e)
{}
g.setColor(Color.RED);
for (int k = 1; k < 11; k++)
g.fillRect(k * 15 - 3, 12 + i * 15, 12, 12);
myPaint();

try
{Thread.sleep(100);} catch (Exception e)
{}
for (int j = i; j > 0; j--)
for (int k = 1; k < 11; k++)
Map[j][k] = Map[j - 1][k];
i++;
}
}
}


public boolean keyDown(Event e, int KeyCode)
{

if (KeyCode == 1004)
{
shape.deasil();

if (!checkPoint())
{
shape.anticlockwise();
return true;
}
this.myPaint();

} else if (KeyCode == 1005)
{
sleep_time = 100;

} else if (KeyCode == 1006)
{
shape.move(-1, 0);

if (!checkPoint())
{
shape.move(1, 0);
return true;
}
this.myPaint();

} else if (KeyCode == 1007)
{
shape.move(1, 0);

if (!checkPoint())
{
shape.move(-1, 0);
return true;
}
this.myPaint();
}
return true;
}


public boolean keyUp(Event e, int KeyCode)
{
if (KeyCode == 1005)
sleep_time = 1000;
return true;
}


public void run()
{
g.setColor(Color.RED);
g.fillRect(6, 6, 159, 308);
this.myPaint();

while (!pause)
{

try
{ Thread.sleep(sleep_time); } catch (Exception e)
{}
check();
this.myPaint();
}
}


public void myPaint()
{
g.setColor(Color.BLACK);
g.fillRect(10, 10, 151, 301);
g.setColor(Color.WHITE);
for (int i = 0; i < 20; i++)
for (int j = 1; j < 11; j++)
if (Map[i][j])
g.drawRect(j * 15 - 5, 10 + i * 15, 15, 15);
g.setColor(Color.GREEN);
for (int i = 0; i < 20; i++)
for (int j = 1; j < 11; j++)
if (Map[i][j])
g.fillRect(j * 15 - 3, 12 + i * 15, 12, 12);
Point[] ps = shape.getPoint();
g.setColor(Color.WHITE);
for (int i = 0; i < ps.length; i++)
if (ps[i].x >= 0 && ps[i].y >= 0)
g.drawRect(ps[i].x * 15 - 5, 10 + ps[i].y * 15, 15, 15);
g.setColor(Color.YELLOW);
for (int i = 0; i < ps.length; i++)
if (ps[i].x > 0 && ps[i].y >= 0)
g.fillRect(ps[i].x * 15 - 3, 12 + ps[i].y * 15, 12, 12);
this.repaint();
}


public void update(Graphics g)
{
g.drawImage(buffer_img, 0, 0, this);
}
}


class Point
{
public int x, y;

public Point(int x, int y)
{
this.x = x; this.y = y;
}
}


class TShape
{
int[][][] data;
int[] a, b, num;
int x, y;
int style, orientation;


public TShape()
{

b = new int[]
{ 0, 2, 1, 0, 1, 1, 1 };

data = new int[][][]
{

{
{ 0x00ffff00, 0x00ffff00, 0x00000000, 0x00000000 }},

{
{ 0x00000000, 0x00000000, 0xffffffff, 0x00000000 },

{ 0x00ff0000, 0x00ff0000, 0x00ff0000, 0x00ff0000 }},

{
{ 0x00ff0000, 0x00ffff00, 0x0000ff00, 0x00000000 },

{ 0x00000000, 0x00ffff00, 0xffff0000, 0x00000000 }},

{
{ 0x0000ff00, 0x00ffff00, 0x00ff0000, 0x00000000 },

{ 0x00000000, 0x00ffff00, 0x0000ffff, 0x00000000 }},

{
{ 0x00000000, 0x00ff0000, 0xffffff00, 0x00000000 },

{ 0xff000000, 0xffff0000, 0xff000000, 0x00000000 },

{ 0x00000000, 0xffffff00, 0x00ff0000, 0x00000000 },

{ 0x00ff0000, 0xffff0000, 0x00ff0000, 0x00000000 }},

{
{ 0x00000000, 0x0000ff00, 0xffffff00, 0x00000000 },

{ 0x00ff0000, 0x00ff0000, 0x00ffff00, 0x00000000 },

{ 0x00000000, 0xffffff00, 0xff000000, 0x00000000 },

{ 0x00ffff00, 0x0000ff00, 0x0000ff00, 0x00000000 }},

{
{ 0x00000000, 0x00ff0000, 0x00ffffff, 0x00000000 },

{ 0x00ffff00, 0x00ff0000, 0x00ff0000, 0x00000000 },

{ 0x00000000, 0x00ffffff, 0x000000ff, 0x00000000 },

{ 0x0000ff00, 0x0000ff00, 0x00ffff00, 0x00000000 }}
};
orientation = (int)(Math.random() * 7);
}


public void next()
{
x = y = 0;
style = (int) (Math.random() * 7);
orientation = (int)(Math.random() * (data[style].length));
}


public void move(int x, int y)
{
this.x += x;
this.y += y;
}


public Point[] getPoint()
{
Point[] ps = new Point[4];
int l = 0;
int[] arr = this.data[style][orientation];
for (int i = 0; i < arr.length; i++)
for (int n = 0; n < 4; n++)

if ((0x000000ff & arr[i] >> (n * 8)) == 0x000000ff)
{
ps[l] = new Point(7 - n + x, i - b[style] + y);
l++;
}
return ps;
}


public void deasil()
{
orientation = (orientation + data[style].length + 1) % data[style].length;
}


public void anticlockwise()
{
orientation = (orientation + data[style].length - 1) % data[style].length;
}
}