貌似最近流行俄罗斯方块?我也来一个

猿敲月下码 2010-02-20 03:05:29
注明:不是我写的,是网上找来的,供大家学习,嘿嘿
就只有2个文件
// Matrix.java
package com.gui.sharp;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

/**
* 运行该文件
*/
public class Matrix extends JPanel {
private JLabel matrix[][];
public static final int ROW = 20;
public static final int COLUMN = 15;
private MatrixModel model;

public Matrix() {
this.setLayout(new GridLayout(ROW, COLUMN));
init();
Dimension size = new Dimension(COLUMN * 20, ROW * 20);
this.setPreferredSize(size);
this.setMinimumSize(size);
this.setMaximumSize(size);
this.addKeyListener(new MyKeyListener());
this.setFocusable(true);
model = new MatrixModel(this);
}

public void start() {
model.start();
}

private void init() {
matrix = new JLabel[ROW][COLUMN];
JLabel label = null;
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COLUMN; j++) {
label = new JLabel(" ");
label.setBorder(new LineBorder(Color.GRAY));
label.setOpaque(true);
matrix[i][j] = label;
add(matrix[i][j]);
}
}
}

public void displayState(int[][] state) {
for (int i = 0; i < state.length; i++) {
for (int j = 0; j < state[i].length; j++) {
if (state[i][j] == 1) {
matrix[i][j].setBackground(Color.red);
} else {
matrix[i][j].setBackground(Color.black);
}
}
}
}

class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
model.moveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
model.moveRight();
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
model.down();
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
model.rotateSharp();
}
}

}

public static void main(String arg[]) {
JFrame frame = new JFrame();
Matrix matrix = new Matrix();
matrix.start();
frame.add(new JScrollPane(matrix));
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
/*
* int i[][] = {{10,20}, {11, 21}, {21, 31}}; int j[][] = new int[3][2];
* System.arraycopy(i, 0, j, 0, i.length); for(int x=0; x<j.length;
* x++){ for(int y=0; y<j[x].length; y++){ System.out.print(j[x][y]+"
* "); } System.out.println(); }
*/
}
}


// MatrixModel.java
package com.gui.sharp;

import java.util.*;

public class MatrixModel {
private int model[][];
private int sharp[][];
private int state[][];
private int currentX, currentY;
private Matrix myParent;
private RunThread runThread;
private int currentIndex;

public MatrixModel(Matrix parent) {
myParent = parent;
model = new int[Matrix.ROW][Matrix.COLUMN];
state = new int[Matrix.ROW][Matrix.COLUMN];
newSharp();
}

public void start() {
runThread = new RunThread();
runThread.start();
}

private int[][] randomSharp() {
currentIndex = (int) (MatrixSharp.NUMBER * Math.random());
return MatrixSharp.sharp[currentIndex];
}

private synchronized boolean isMoveAvaliable(int currentX, int currentY) {
if (currentX > Matrix.COLUMN - 1) {
return false;
}
if (currentX < 0) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < Math.abs(currentX); j++)
if (sharp[i][j] != 0) {
return false;
}
}
}
if (currentY < 0 || currentY > Matrix.ROW - 1) {
return false;
}
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if (currentX + x > -1 && currentX + x < Matrix.COLUMN
&& currentY + y < Matrix.ROW) {
if ((sharp[y][x] & model[currentY + y][currentX + x]) != 0) {
return false;
}
} else {
if (currentX + x >= Matrix.COLUMN) {
if (sharp[y][x] != 0) {
return false;
}
} else if (currentY + y >= Matrix.ROW) {
if (sharp[y][x] != 0) {
return false;
}
}
}
}
}
return true;
}

private synchronized void moveTo(int x, int y) {
for (int i = 0; i < Matrix.ROW; i++) {
for (int j = 0; j < Matrix.COLUMN; j++) {
state[i][j] = model[i][j];
}
}
for (int c = 0; c < 4; c++) {
for (int r = 0; r < 4; r++) {
if (currentX + c > -1 && currentX + c < Matrix.COLUMN
&& currentY + r < Matrix.ROW) {
state[currentY + r][currentX + c] |= sharp[r][c];
}
}
}
myParent.displayState(state);
}

public void over() {
System.out.println("game over");
runThread.end();
}

public synchronized void moveLeft() {
if (isMoveAvaliable(currentX - 1, currentY)) {
currentX -= 1;
moveTo(currentX, currentY);
}
}

public synchronized void moveRight() {
if (isMoveAvaliable(currentX + 1, currentY)) {
currentX += 1;
moveTo(currentX, currentY);
}
}

private boolean isOver() {
if (currentY == 0) {
return false;
}
return false;
}

private synchronized void resetState() {
state = new int[Matrix.ROW][Matrix.COLUMN];
moveTo(currentX, currentY);
}

public synchronized void down() {
if (isMoveAvaliable(currentX, currentY + 1)) {
currentY += 1;
moveTo(currentX, currentY);
} else {
if (isOver()) {
over();
return;
}
model = state;
check();
resetState();
newSharp();
}
}

public synchronized void check() {
Vector clearVector = new Vector();
for (int i = Matrix.ROW - 1; i > 0; i--) {
int j = 0;
for (; j < Matrix.COLUMN; j++) {
if (model[i][j] == 0) {
break;
}
}
if (j == Matrix.COLUMN) {
clearVector.add(new Integer(i));
}
}
clearRows(clearVector);
}

private synchronized void clearRows(Vector clearVector) {

for (int i = clearVector.size(); i > 0; i--) {
int row = ((Integer) clearVector.get(i - 1)).intValue();
for (int j = 0; j < Matrix.COLUMN; j++) {
for (int k = row; k >= 0; k--) {
if (k == 0) {
model[k][j] = 0;
} else {
model[k][j] = model[k - 1][j];
}
}
}
}
}

public synchronized void rotateSharp() {
int tmpIndex = currentIndex++;
if (currentIndex % 4 == 0) {
currentIndex -= 4;
}
int[][] tmpSharp = MatrixSharp.sharp[currentIndex];
if (isRotateAble(tmpSharp)) {
sharp = MatrixSharp.sharp[currentIndex];
moveTo(currentX, currentY);
} else {
currentIndex = tmpIndex;
}
}

private synchronized boolean isRotateAble(int sharp[][]) {
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++) {
if ((currentX + x < 0 || currentX + x >= Matrix.COLUMN)
|| (currentY + y < 0 || currentY + y >= Matrix.ROW)) {
if (sharp[y][x] != 0) {
return false;
}
}
}
}
return isMoveAvaliable(currentX, currentY);
}

private synchronized void newSharp() {
currentX = Matrix.COLUMN / 2;
currentY = 0;
sharp = randomSharp();
}

class RunThread extends Thread {
boolean isRun = true;

public void end() {
isRun = false;
}

public void run() {
while (isRun) {
down();
try {
sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

interface MatrixSharp {
static int sharp[][][] = new int[][][] {
{ { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 1 }, { 0, 0, 0, 0 } },
{ { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
{ { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } },
{ { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 1 }, { 0, 0, 0, 0 } },
{ { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 1 }, { 0, 0, 0, 0 } },
{ { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 1, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } },
{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 } },
{ { 0, 1, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 0 } },
{ { 0, 0, 0, 0 }, { 0, 1, 1, 1 }, { 0, 1, 0, 0 }, { 0, 0, 0, 0 } } };
static int NUMBER = sharp.length;
}
...全文
457 39 打赏 收藏 转发到动态 举报
写回复
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
fxingJava 2010-02-27
  • 打赏
  • 举报
回复
顶上去


打个广告:
新群创立,群名为:Java 技術交流,群号为: 36115166
本群交流JAVA各方面技术.为职业人提供一个交流的平台,同样为新手提供一个解决疑问平台.
希望大家能够维护好群内的学习氛围
BadPattern 2010-02-27
  • 打赏
  • 举报
回复
我也想学swing,苦于没有时间......
24K純帥 2010-02-27
  • 打赏
  • 举报
回复
不错啊,看是看明白了,就是自己写不出来。。
小小都不懂 2010-02-26
  • 打赏
  • 举报
回复
写的很好 我要好好学习下了
chen09 2010-02-24
  • 打赏
  • 举报
回复
mark一下,改天用swt写一个,也来凑凑热闹。
借我那把枪吧 2010-02-24
  • 打赏
  • 举报
回复
先接分再学习先接分再学习先接分再学习先接分再学习
ladybirds2008 2010-02-24
  • 打赏
  • 举报
回复
JF
啦 怎么都搞这个 啊 看来 我 也得研究下
kyo19 2010-02-24
  • 打赏
  • 举报
回复
果然强力啊

接分了
JavaAlpha 2010-02-24
  • 打赏
  • 举报
回复
O(∩_∩)O谢谢 楼主分享

接分

研究一下
呼吸先生 2010-02-24
  • 打赏
  • 举报
回复
JF 我out了看来
lryxxh 2010-02-23
  • 打赏
  • 举报
回复
我也看了这个例子,感觉有些费解啊
fyjava1984 2010-02-23
  • 打赏
  • 举报
回复
接分了,哈哈
回复内容太短了!
wangshu3000 2010-02-23
  • 打赏
  • 举报
回复
接分来。。。
贴个我的90行 估计是最短的了
http://wireless.javaeye.com/blog/595321
zings 2010-02-23
  • 打赏
  • 举报
回复
可操作性,界面是不是太。。。。
zings 2010-02-23
  • 打赏
  • 举报
回复
玩游戏抢代码+拿分。。。
骑猪去东莞 2010-02-23
  • 打赏
  • 举报
回复
先复制下来再说,慢慢研究算法
落叶的葬礼 2010-02-23
  • 打赏
  • 举报
回复
mark,了解一下,
hailovebing 2010-02-23
  • 打赏
  • 举报
回复
复制 粘贴 回复 接分 走人
wky65007098 2010-02-23
  • 打赏
  • 举报
回复
复制过来 look 下
sc526332192 2010-02-22
  • 打赏
  • 举报
回复
NICE!!!!!!!!!!!
加载更多回复(19)
相关推荐

62,568

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告