📄 tetris.java
字号:
/**
*俄罗斯方块v1.0
*
*我的第一个Java程序,类写得很烂,望不吝赐教。
*
*本人近来对Java颇感兴趣,希望能和众Java爱好者多多交流。
*
* By csdn [nothing]
* Email: yychaoban@china.com
* 2001.8.16
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Tetris extends Applet {
public Game game;
public int
Offset_x = 40,
Offset_y = 30;
public void init() {
this.setLayout(null);
this.setBackground(Color.white);
game = new Game(this, Offset_x, Offset_y);
game.init();
}
public void start() {
}
public void paint(Graphics g) {
if(game.getHasInit()) game.repaint();
}
public static void main(String[] args) {
Tetris applet = new Tetris();
Frame aFrame = new Frame("俄罗斯方块v1.0 By[csdn_nothing]");
aFrame.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(250, 400);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
//游戏控制入口
class Game extends ActionThread {
private boolean hasInit = false;
private boolean hasPause = false;
private boolean hasStart = false;
private Label labScore, labLevel;
private Label labConstTextScore, labConstTextLevel, labConstTextNextObject;
private Button btnStart, btnReset;
private int nextFigure;
private String msg;
private Color msgColor = Color.red; //显示的消息的颜色
private Color constTextColor = Color.blue; //标题颜色
private Color infoBgColor = Color.lightGray;
private Color infoColor = Color.red;
public Game(Tetris mainForm, int x, int y) {
super(mainForm, x, y);
}
public int getCtrlPanelX() { return getOffset_x()+getWorkPiexWidth()+10;}
public int getCtrlPanelY() { return getOffset_y();}
public boolean getHasInit() { return hasInit;}
public void init() {
if(hasInit) return;
hasInit = true;
hasPause = false;
nextFigure = (int)(Math.random()*tBlock.getMaxFigureNumber()*10) % tBlock.getMaxFigureNumber();
labConstTextScore = new Label("得分:");
labConstTextScore.setBounds(getCtrlPanelX(), getCtrlPanelY()+15, 55, 20);
labConstTextScore.setForeground(constTextColor);
labScore = new Label("0");
labScore.setBackground(infoBgColor);
labScore.setBounds(getCtrlPanelX(), getCtrlPanelY()+35, 55, 20);
labScore.setForeground(infoColor);
labConstTextLevel = new Label("级别:");
labConstTextLevel.setBounds(getCtrlPanelX(), getCtrlPanelY()+65, 55, 20);
labConstTextLevel.setForeground(constTextColor);
labLevel = new Label("1");
labLevel.setBackground(infoBgColor);
labLevel.setBounds(getCtrlPanelX(), getCtrlPanelY()+85, 55, 20);
labLevel.setForeground(infoColor);
labConstTextNextObject = new Label("下一个:");
labConstTextNextObject.setBounds(getCtrlPanelX(), getCtrlPanelY()+140, 55, 20);
labConstTextNextObject.setForeground(constTextColor);
btnStart = new Button("开始");
btnStart.setBounds(getCtrlPanelX(), getCtrlPanelY()+getWorkPiexHeight()-30, 55, 25);
btnReset = new Button("重来");
btnReset.setBounds(getCtrlPanelX(), getCtrlPanelY()+getWorkPiexHeight()-60, 55, 25);
btnReset.setEnabled(false);
btnReset.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
handleKeyEvent(e);
}
});
btnReset.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
gamereset();
}
});
btnStart.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
handleKeyEvent(e);
}
});
btnStart.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(!hasStart) {
play();
btnStart.setLabel("暂停");
} else
if(hasPause) {
play();
btnStart.setLabel("暂停");
} else {
pause();
btnStart.setLabel("开始");
}
btnReset.setEnabled(hasStart);
}
});
mainForm.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
handleKeyEvent(e);
}
});
mainForm.add(labScore);
mainForm.add(labLevel);
mainForm.add(btnReset);
mainForm.add(btnStart);
mainForm.add(labConstTextScore);
mainForm.add(labConstTextLevel);
mainForm.add(labConstTextNextObject);
}
public void play() {
if(!hasStart) start();
hasPause = false;
hasStart = true;
}
public void pause() {
hasPause = true;
}
public void gamereset() {
super.reset();
hasPause = true;
nextFigure = (int)(Math.random()*tBlock.getMaxFigureNumber()*10) % tBlock.getMaxFigureNumber();
newObject(objectColor, nextFigure);
nextFigure = (int)(Math.random()*tBlock.getMaxFigureNumber()*10) % tBlock.getMaxFigureNumber();
repaint();
score = 0;
level = 1;
showlevel(level);
showscore(score);
play();
}
public void run() {
showmsg("开始新游戏...");
System.out.print("Game started!\n");
mySleep(1000);
clearAndrepaint();
score = 0;
level = 1;
//hasStart = true;
//hasPause = false;
if(!newObject(objectColor, nextFigure)) return;
nextFigure = (int)(Math.random()*tBlock.getMaxFigureNumber()*10) % tBlock.getMaxFigureNumber();
showNextObject(nextFigure);
while(true) {
if(hasPause) continue;
mySleep(downSpeed);
if(!moveObject(mdDown)) { //can't down
writeGlassWorkSheet(Sheet_x, Sheet_y, tBlock.getFigureWorkSheet());
int[] Lines = getLines();
int maxLine = 0;
int onetimeDelLines = 0; //一次所消行数
for(int i = 0; i < Lines.length; i++) {
if(Lines[i] != 0) {
delLine(Lines[i]);
if(maxLine < Lines[i]) maxLine = Lines[i];
onetimeDelLines++;
}
}
if(onetimeDelLines > tBlock.getMaxCornerNumber())
onetimeDelLines = tBlock.getMaxCornerNumber();
score += onetimeToScore[onetimeDelLines];
showscore(score);
if(score >= levelSpeed[0][level]) {
level++;
showlevel(level);
showmsg("第 " + Integer.toString(level) + " 关!");
mySleep(1000);
clearAndrepaint();
downSpeed = levelSpeed[1][level];
}
if(level > maxLevel) {
showmsg("恭喜,您玩通关了!");
return;
}
if(maxLine > 0) repaint(maxLine);
if(!newObject(objectColor, nextFigure)) break;
nextFigure = (int)(Math.random()*tBlock.getMaxFigureNumber()*10) % tBlock.getMaxFigureNumber();
showNextObject(nextFigure);
}
}
}
public void showscore(int theScore) {
labScore.setText(Integer.toString(theScore));
}
public void showlevel(int theLevel) {
labLevel.setText(Integer.toString(theLevel));
}
public void showNextObject(int Figure) {
Graphics g = mainForm.getGraphics();
g.setColor(Color.blue);
tBlock.drawMyObject(getCtrlPanelX(), getCtrlPanelY()+165, g, Figure);
}
public void showmsg(String s) {
msg = s;
Graphics g = mainForm.getGraphics();
g.setColor(msgColor);
g.drawString(msg, getOffset_x(), getOffset_y()+10);
}
public void clearAndrepaint() {
mainForm.getGraphics().clearRect(getOffset_x(), getOffset_y(),
getWorkPiexWidth(), getWorkPiexHeight());
repaint();
}
public void mySleep(int t) {
try {
sleep(t);
} catch(InterruptedException e) {}
}
public void repaint() {
super.repaint();
if(hasStart) showNextObject(nextFigure);
}
public void handleKeyEvent(KeyEvent e) {
if(!hasInit || hasPause)
return;
if(e.getKeyCode() == e.VK_LEFT)
moveObject(mdLeft);
else if(e.getKeyCode() == e.VK_DOWN)
moveObject(mdDown);
else if(e.getKeyCode() == e.VK_RIGHT)
moveObject(mdRight);
else if(e.getKeyCode() == e.VK_UP)
turn();
}
}
//动作线程
class ActionThread extends Thread {
protected int
mdLeft = 0,
mdDown = 1,
mdRight = 2;
private int
GlassHeight = 28, //背景高(n方块)
GlassWidth = 10; //背景宽(n方块)
private int
Offset_x = 0, //背景位置(x像素)
Offset_y = 0; //背景位置(y像素)
protected int downSpeed = 500; //下落速度(越大越慢)
private boolean lockObject = false;
protected Color backgroundColor = Color.lightGray;
protected Color objectColor = Color.blue;
private byte[][] GlassWorkSheet = new byte[GlassHeight][GlassWidth];
protected int Sheet_x, Sheet_y;
private int currFigure;
protected int score;
protected int level;
protected int[][] levelSpeed = new int[][] {
{ 0, 1000, 2000, 3000, 4000, 5000, 6000, 0 }, //score
{ 0, 500, 400, 300, 200, 100, 50, 0 } //speed
};
protected int maxLevel = levelSpeed[0].length - 2;
protected int[] onetimeToScore = new int[] {
0, 100, 300, 500, 1000 //同时消0-4行所得分数
};
protected TetrisBlock tBlock;
protected Tetris mainForm;
public ActionThread(Tetris mainForm, int x, int y) {
this.mainForm = mainForm;
Offset_x = x;
Offset_y = y;
tBlock = new TetrisBlock(mainForm);
tBlock.setBackgroundColor(backgroundColor);
reset();
System.out.print("Thread creating...\n");
}
public int getScore() { return score;}
public int getLevel() { return level;}
public int getWorkPiexWidth() { return GlassWidth*tBlock.getBlockWidth();}
public int getWorkPiexHeight() { return GlassHeight*tBlock.getBlockHeight();}
public int getOffset_x() { return Offset_x;}
public int getOffset_y() { return Offset_y;}
public void reset() {
for(int y = 0; y < GlassHeight; y++)
for(int x = 0; x < GlassWidth; x++)
GlassWorkSheet[y][x] = 0;
}
public void repaint(int pos) {
if((pos < 0) || (pos >= GlassHeight)) return;
Color oldColor;
Graphics g = mainForm.getGraphics();
int w = tBlock.getBlockWidth();
int h = tBlock.getBlockHeight();
//g.clearRect(Offset_x, Offset_y, GlassWidth*w, GlassHeight*h);
for(int y = 0; (y <= pos) || (y < GlassHeight); y++)
for(int x = 0; x < GlassWidth; x++) {
oldColor = g.getColor();
if(GlassWorkSheet[y][x] == 1) {
g.setColor(objectColor);
g.fill3DRect(Offset_x+x*w, Offset_y+y*h, w-1, h-1, true);
}
else {
g.setColor(backgroundColor);
g.fillRect(Offset_x+x*w, Offset_y+y*h, w-1, h-1);
}
g.setColor(oldColor);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -