我用java编的俄罗斯方块游戏(完全游戏版本)

henryevol 2004-11-30 08:56:01
游戏下载地址为http://www.shengzhen.com.cn/javadiamonds/
欢迎大家使用,交流
...全文
1413 61 打赏 收藏 转发到动态 举报
写回复
用AI写文章
61 条回复
切换为时间正序
请发表友善的回复…
发表回复
oldmoon 2005-05-07
  • 打赏
  • 举报
回复
绿原,真好!楼主不厚道!
waterlss 2005-05-07
  • 打赏
  • 举报
回复
好地方,感谢007remember
najzni 2005-05-07
  • 打赏
  • 举报
回复
up
liuquanyi 2005-05-06
  • 打赏
  • 举报
回复
学习,感谢 007remember(绿原) !
the_son_of_java 2005-05-06
  • 打赏
  • 举报
回复
帮顶
007remember 2005-04-10
  • 打赏
  • 举报
回复
给大家个网站
http://www.javaresearch.org/
http://218.108.41.12/vchome/code/allcode.htm
这2个上有n多源代码
其中就包括java版的俄罗斯方块

希望对大家有帮助
dszch 2005-04-09
  • 打赏
  • 举报
回复
hao
007remember 2005-04-07
  • 打赏
  • 举报
回复
我这里有源代码
谁要
俄罗斯方块
感觉还可以
javame_sukeen 2005-04-07
  • 打赏
  • 举报
回复
zhanghanke_2004@163.com,楼主有责任培养后来人,谢谢楼主
007remember 2005-04-07
  • 打赏
  • 举报
回复
/**
* File: ErsBlock.java
* User: Administrator
* Date: Jan 15, 2003
* Describe: 俄罗斯方块的 Java 实现
*/

/**
* 块类,继承自线程类(Thread)
* 由 4 * 4 个方格(ErsBox)构成一个块,
* 控制块的移动、下落、变形等
*/
class ErsBlock extends Thread {
/**
* 一个块占的行数是4行
*/
public final static int BOXES_ROWS = 4;
/**
* 一个块占的列数是4列
*/
public final static int BOXES_COLS = 4;
/**
* 让升级变化平滑的因子,避免最后几级之间的速度相差近一倍
*/
public final static int LEVEL_FLATNESS_GENE = 3;
/**
* 相近的两级之间,块每下落一行的时间差别为多少(毫秒)
*/
public final static int BETWEEN_LEVELS_DEGRESS_TIME = 50;
/**
* 方块的样式数目为7
*/
private final static int BLOCK_KIND_NUMBER = 7;
/**
* 每一个样式的方块的反转状态种类为4
*/
private final static int BLOCK_STATUS_NUMBER = 4;
/**
* 分别对应对7种模型的28种状态
*/
public final static int[][] STYLES = {// 共28种状态
{0x0f00, 0x4444, 0x0f00, 0x4444}, // 长条型的四种状态
{0x04e0, 0x0464, 0x00e4, 0x04c4}, // 'T'型的四种状态
{0x4620, 0x6c00, 0x4620, 0x6c00}, // 反'Z'型的四种状态
{0x2640, 0xc600, 0x2640, 0xc600}, // 'Z'型的四种状态
{0x6220, 0x1700, 0x2230, 0x0740}, // '7'型的四种状态
{0x6440, 0x0e20, 0x44c0, 0x8e00}, // 反'7'型的四种状态
{0x0660, 0x0660, 0x0660, 0x0660}, // 方块的四种状态
};

private GameCanvas canvas;
private ErsBox[][] boxes = new ErsBox[BOXES_ROWS][BOXES_COLS];
private int style, y, x, level;
private boolean pausing = false, moving = true;

/**
* 构造函数,产生一个特定的块
* @param style 块的样式,对应STYLES的28个值中的一个
* @param y 起始位置,左上角在canvas中的坐标行
* @param x 起始位置,左上角在canvas中的坐标列
* @param level 游戏等级,控制块的下落速度
* @param canvas 画板
*/
public ErsBlock(int style, int y, int x, int level, GameCanvas canvas) {
this.style = style;
this.y = y;
this.x = x;
this.level = level;
this.canvas = canvas;

int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boolean isColor = ((style & key) != 0);
boxes[i][j] = new ErsBox(isColor);
key >>= 1;
}
}

display();
}

/**
* 线程类的run()函数覆盖,下落块,直到块不能再下落
*/
public void run() {
while (moving) {
try {
sleep(BETWEEN_LEVELS_DEGRESS_TIME
* (ErsBlocksGame.MAX_LEVEL - level + LEVEL_FLATNESS_GENE));
} catch (InterruptedException ie) {
ie.printStackTrace();
}
//后边的moving是表示在等待的100毫秒间,moving没被改变
if (!pausing) moving = (moveTo(y + 1, x) && moving);
}
}

/**
* 块向左移动一格
*/
public void moveLeft() {
moveTo(y, x - 1);
}

/**
* 块向右移动一格
*/
public void moveRight() {
moveTo(y, x + 1);
}

/**
* 块向下落一格
*/
public void moveDown() {
moveTo(y + 1, x);
}

/**
* 块变型
*/
public void turnNext() {
for (int i = 0; i < BLOCK_KIND_NUMBER; i++) {
for (int j = 0; j < BLOCK_STATUS_NUMBER; j++) {
if (STYLES[i][j] == style) {
int newStyle = STYLES[i][(j + 1) % BLOCK_STATUS_NUMBER];
turnTo(newStyle);
return;
}
}
}
}

/**
* 暂停块的下落,对应游戏暂停
*/
public void pauseMove() {
pausing = true;
}

/**
* 继续块的下落,对应游戏继续
*/
public void resumeMove() {
pausing = false;
}

/**
* 停止块的下落,对应游戏停止
*/
public void stopMove() {
moving = false;
}

/**
* 将当前块从画布的对应位置移除,要等到下次重画画布时才能反映出来
*/
private void earse() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(i + y, j + x);
if (box == null) continue;
box.setColor(false);
}
}
}
}

/**
* 让当前块放置在画布的对应位置上,要等到下次重画画布时才能看见
*/
private void display() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(y + i, x + j);
if (box == null) continue;
box.setColor(true);
}
}
}
}

/**
* 当前块能否移动到newRow/newCol所指定的位置
* @param newRow int, 目的地所在行
* @param newCol int, 目的地所在列
* @return boolean, true-能移动,false-不能
*/
private boolean isMoveAble(int newRow, int newCol) {
earse();
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(newRow + i, newCol + j);
if (box == null || (box.isColorBox())) {
display();
return false;
}
}
}
}
display();
return true;
}

/**
* 将当前画移动到newRow/newCol所指定的位置
* @param newRow int, 目的地所在行
* @param newCol int, 目的地所在列
* @return boolean, true-移动成功,false-移动失败
*/
private synchronized boolean moveTo(int newRow, int newCol) {
if (!isMoveAble(newRow, newCol) || !moving) return false;

earse();
y = newRow;
x = newCol;

display();
canvas.repaint();

return true;
}

/**
* 当前块能否变成newStyle所指定的块样式,主要是要考虑
* 边界以及被其它块挡住、不能移动的情况
* @param newStyle int,希望改变的块样式,对应STYLES的28个值中的一个
* @return boolean,true-能改变,false-不能改变
*/
private boolean isTurnAble(int newStyle) {
int key = 0x8000;
earse();
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if ((newStyle & key) != 0) {
ErsBox box = canvas.getBox(y + i, x + j);
if (box == null || box.isColorBox()) {
display();
return false;
}
}
key >>= 1;
}
}
display();
return true;
}

/**
* 将当前块变成newStyle所指定的块样式
* @param newStyle int,将要改变成的块样式,对应STYLES的28个值中的一个
* @return boolean,true-改变成功,false-改变失败
*/
private boolean turnTo(int newStyle) {
if (!isTurnAble(newStyle) || !moving) return false;

earse();
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boolean isColor = ((newStyle & key) != 0);
boxes[i][j].setColor(isColor);
key >>= 1;
}
}
style = newStyle;

display();
canvas.repaint();

return true;
}
}
007remember 2005-04-07
  • 打赏
  • 举报
回复
/**
* File: ControlPanel.java
* User: Administrator
* Date: Jan 15, 2003
* Describe: 俄罗斯方块的 Java 实现
*/

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;

/**
* 控制面板类,继承自JPanel.
* 上边安放预显窗口、等级、得分、控制按钮
* 主要用来控制游戏进程。
*/
class ControlPanel extends JPanel {
private JTextField
tfLevel = new JTextField("" + ErsBlocksGame.DEFAULT_LEVEL),
tfScore = new JTextField("0");

private JButton
btPlay = new JButton("Play"),
btPause = new JButton("Pause"),
btStop = new JButton("Stop"),
btTurnLevelUp = new JButton("Turn hard"),
btTurnLevelDown = new JButton("Turn easy");

private JPanel plTip = new JPanel(new BorderLayout());
private TipPanel plTipBlock = new TipPanel();
private JPanel plInfo = new JPanel(new GridLayout(4, 1));
private JPanel plButton = new JPanel(new GridLayout(5, 1));

private Timer timer;
private ErsBlocksGame game;

private Border border = new EtchedBorder(
EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));

/**
* 控制面板类的构造函数
* @param game ErsBlocksGame, ErsBoxesGame类的一个实例引用,
* 方便直接控制ErsBoxesGame类的行为。
*/
public ControlPanel(final ErsBlocksGame game) {
setLayout(new GridLayout(3, 1, 0, 4));
this.game = game;

plTip.add(new JLabel("Next block"), BorderLayout.NORTH);
plTip.add(plTipBlock);
plTip.setBorder(border);

plInfo.add(new JLabel("Level"));
plInfo.add(tfLevel);
plInfo.add(new JLabel("Score"));
plInfo.add(tfScore);
plInfo.setBorder(border);

tfLevel.setEditable(false);
tfScore.setEditable(false);

plButton.add(btPlay);
plButton.add(btPause);
plButton.add(btStop);
plButton.add(btTurnLevelUp);
plButton.add(btTurnLevelDown);
plButton.setBorder(border);

add(plTip);
add(plInfo);
add(plButton);

addKeyListener(new ControlKeyListener());

btPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.playGame();
}
});
btPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (btPause.getText().equals(new String("Pause"))) {
game.pauseGame();
} else {
game.resumeGame();
}
}
});
btStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
game.stopGame();
}
});
btTurnLevelUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level < ErsBlocksGame.MAX_LEVEL)
tfLevel.setText("" + (level + 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});
btTurnLevelDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level > 1)
tfLevel.setText("" + (level - 1));
} catch (NumberFormatException e) {
}
requestFocus();
}
});

addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
plTipBlock.fanning();
}
});

timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
tfScore.setText("" + game.getScore());
int scoreForLevelUpdate =
game.getScoreForLevelUpdate();
if (scoreForLevelUpdate >= ErsBlocksGame.PER_LEVEL_SCORE
&& scoreForLevelUpdate > 0)
game.levelUpdate();
}
});
timer.start();
}

/**
* 设置预显窗口的样式,
* @param style int,对应ErsBlock类的STYLES中的28个值
*/
public void setTipStyle(int style) {
plTipBlock.setStyle(style);
}

/**
* 取得用户设置的游戏等级。
* @return int, 难度等级,1 - ErsBlocksGame.MAX_LEVEL
*/
public int getLevel() {
int level = 0;
try {
level = Integer.parseInt(tfLevel.getText());
} catch (NumberFormatException e) {
}
return level;
}

/**
* 让用户修改游戏难度等级。
* @param level 修改后的游戏难度等级
*/
public void setLevel(int level) {
if (level > 0 && level < 11) tfLevel.setText("" + level);
}

/**
* 设置"开始"按钮的状态。
*/
public void setPlayButtonEnable(boolean enable) {
btPlay.setEnabled(enable);
}

public void setPauseButtonLabel(boolean pause) {
btPause.setText(pause ? "Pause" : "Continue");
}

/**
* 重置控制面板
*/
public void reset() {
tfScore.setText("0");
plTipBlock.setStyle(0);
}

/**
* 重新计算TipPanel里的boxes[][]里的小框的大小
*/
public void fanning() {
plTipBlock.fanning();
}

/**
* 预显窗口的实现细节类
*/
private class TipPanel extends JPanel {
private Color backColor = Color.darkGray, frontColor = Color.lightGray;
private ErsBox[][] boxes =
new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];

private int style, boxWidth, boxHeight;
private boolean isTiled = false;

/**
* 预显窗口类构造函数
*/
public TipPanel() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++)
boxes[i][j] = new ErsBox(false);
}
}

/**
* 预显窗口类构造函数
* @param backColor Color, 窗口的背景色
* @param frontColor Color, 窗口的前景色
*/
public TipPanel(Color backColor, Color frontColor) {
this();
this.backColor = backColor;
this.frontColor = frontColor;
}

/**
* 设置预显窗口的方块样式
* @param style int,对应ErsBlock类的STYLES中的28个值
*/
public void setStyle(int style) {
this.style = style;
repaint();
}

/**
* 覆盖JComponent类的函数,画组件。
* @param g 图形设备环境
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);

if (!isTiled) fanning();

int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
Color color = (((key & style) != 0) ? frontColor : backColor);
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
key >>= 1;
}
}
}

/**
* 根据窗口的大小,自动调整方格的尺寸
*/
public void fanning() {
boxWidth = getSize().width / ErsBlock.BOXES_COLS;
boxHeight = getSize().height / ErsBlock.BOXES_ROWS;
isTiled = true;
}
}

private class ControlKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
if (!game.isPlaying()) return;

ErsBlock block = game.getCurBlock();
switch (ke.getKeyCode()) {
case KeyEvent.VK_DOWN:
block.moveDown();
break;
case KeyEvent.VK_LEFT:
block.moveLeft();
break;
case KeyEvent.VK_RIGHT:
block.moveRight();
break;
case KeyEvent.VK_UP:
block.turnNext();
break;
default:
break;
}
}
}
}
sanhongmen 2005-01-13
  • 打赏
  • 举报
回复
楼主可以给小妹发一分源码吗? sanhongmen1981@hotmail.com先谢过楼主拉
xiongoo 2005-01-12
  • 打赏
  • 举报
回复
楼主可以给小弟发一分源码吗? _xiong_oo@163.com 先谢过楼主拉
zible 2005-01-12
  • 打赏
  • 举报
回复
ztw81@163.com
wj8384 2005-01-09
  • 打赏
  • 举报
回复
wj8384@126.com
谢谢
jxncligang 2005-01-08
  • 打赏
  • 举报
回复
我也要一份源码学习,谢谢!
jxncligang@163.com
jb303 2005-01-01
  • 打赏
  • 举报
回复
我也要一份
jiangbei1@sina.com
xinbill 2004-12-29
  • 打赏
  • 举报
回复
不错,比我厉害!顶!
SoulOfEdge 2004-12-28
  • 打赏
  • 举报
回复
我也要一份源码学习,谢谢!
yourknight@sohu.com
gougou606 2004-12-25
  • 打赏
  • 举报
回复
我想要源码 谢谢

wl_cuit@163.com
加载更多回复(40)

51,410

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧