[java]代码库
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Tetris extends JFrame {
private JPanel contentPane;
static JLabel lblNewLabel_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Tetris frame = new Tetris();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Tetris() {
setTitle("反应力大考验");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("游戏");
menuBar.add(mnNewMenu);
JMenuItem mntmNewMenuItem_3 = new JMenuItem("退出");
mntmNewMenuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mnNewMenu.add(mntmNewMenuItem_3);
JMenu mnNewMenu_1 = new JMenu("帮助");
menuBar.add(mnNewMenu_1);
JMenuItem mntmNewMenuItem = new JMenuItem("游戏玩法");
mntmNewMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "空格键控制横条的摆放,正确对齐位置才能更上一层,成功达到顶层可获得游戏胜利!", "游戏玩法",
JOptionPane.INFORMATION_MESSAGE);
}
});
mnNewMenu_1.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_4 = new JMenuItem("关于作者");
mntmNewMenuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "网络1004班:陈雍,董振宇,高宝成,何其", "关于作者",
JOptionPane.INFORMATION_MESSAGE);
}
});
mnNewMenu_1.add(mntmNewMenuItem_4);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("您现在分数:");
lblNewLabel.setBounds(282, 169, 70, 15);
contentPane.add(lblNewLabel);
MyJPanel panel = new MyJPanel();
panel.setBounds(0, 0, 300, 300);
contentPane.add(panel);
addKeyListener(panel);
getContentPane().add(panel);
lblNewLabel_1 = new JLabel("[0]");
lblNewLabel_1.setBounds(311, 199, 70, 15);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("游戏说明");
lblNewLabel_2.setBounds(282, 12, 70, 15);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("1.按F1可开始游戏");
lblNewLabel_3.setBounds(264, 39, 117, 15);
contentPane.add(lblNewLabel_3);
JLabel lblf = new JLabel("2.按F2可暂停游戏");
lblf.setBounds(264, 66, 117, 15);
contentPane.add(lblf);
JLabel lblf_1 = new JLabel("3.按F3可恢复游戏");
lblf_1.setBounds(264, 93, 117, 15);
contentPane.add(lblf_1);
JLabel label_2 = new JLabel("4.按空格键控制横条");
label_2.setBounds(264, 120, 117, 15);
contentPane.add(label_2);
JLabel lblNewLabel_4 = new JLabel("5.对齐横条可胜利");
lblNewLabel_4.setBounds(264, 142, 117, 15);
contentPane.add(lblNewLabel_4);
}
public class MyJPanel extends JPanel implements KeyListener {
private int blockFlag = 5;
private int floor = 1;
private boolean startFlag;
private boolean outFlag;
private int ctrlBlock;
private int speed;
private int checkBlock;
private int blockSaver;
private boolean clearFlag=true;
private int grade;
private boolean ctrlFlag=false;
blockThread t;
public void paintComponent(Graphics g) {
super.paintComponents(g);
if(clearFlag==false){
g.clearRect(18,0,234, 234);
clearFlag=true;
}
g.clearRect(18, 234 - floor * 18, 180, 18);
for (int i = 0; i < 14; i++) {
g.drawRect(0, i * 18, 18, 18);
g.drawRect(198, i * 18, 18, 18);
if (i < 11) {
g.drawRect((i + 1) * 18, 234, 18, 18);
}
}
for (int i = 0; i < blockFlag; i++) {
g.fillRect((++ctrlBlock) * 18, 234 - floor * 18, 18, 18);
checkBlock = ctrlBlock;
}
ctrlBlock = 0;
}
public void start() {
ctrlFlag=true;
t = new blockThread();
t.start();
}
public void init(){
blockFlag = 5;
floor = 1;
startFlag = true;
outFlag = true;
ctrlBlock = 0;
speed = 500;
checkBlock = 0;
blockSaver = 0;
grade=0;
}
public void flushBlock() {
init();
int tmp = ctrlBlock;
while (startFlag) {
if (outFlag) {
for (int i = 0; i < (11 - blockFlag); i++) {
ctrlBlock = tmp;
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
tmp++;
}
outFlag = false;
} else {
for (int i = 0; i < (11 - blockFlag); i++) {
ctrlBlock = tmp - 1;
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
tmp--;
}
outFlag = true;
}
}
}
public void suspend() {
if(ctrlFlag){
t.suspend();
}else{
JOptionPane.showMessageDialog(null, "请先按F1开始游戏!", "提示",
JOptionPane.ERROR_MESSAGE);
}
}
public void resume() {
if(ctrlFlag){
t.resume();
}else{
JOptionPane.showMessageDialog(null, "请先按F1开始游戏!", "提示",
JOptionPane.ERROR_MESSAGE);
}
}
public void ctrl(){
if(ctrlFlag){
newline();
}else{
JOptionPane.showMessageDialog(null, "请先按F1开始游戏后再按空格键控制", "提示",
JOptionPane.ERROR_MESSAGE);
}
}
public void newline() {
if(floor>1&&blockSaver!=checkBlock){
JOptionPane.showMessageDialog(null, "Game Over!您的分数:"+grade, "提示",
JOptionPane.ERROR_MESSAGE);
t.stop();
init();
clearFlag=false;
ctrlFlag=false;
repaint();
Tetris.lblNewLabel_1.setText("["+grade+"]");
return;
}
if(floor>13){
JOptionPane.showMessageDialog(null, "您已经成功过关!", "恭喜",
JOptionPane.INFORMATION_MESSAGE);
init();
clearFlag=false;
ctrlFlag=false;
repaint();
Tetris.lblNewLabel_1.setText("["+grade+"]");
return;
}
blockSaver=checkBlock;
grade=grade+(floor*100);
floor++;
speed = speed - 38;
Tetris.lblNewLabel_1.setText("["+grade+"]");
}
class blockThread extends Thread {
public void run() {
flushBlock();
}
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_SPACE:
ctrl();
break;
case KeyEvent.VK_F1:
start();
break;
case KeyEvent.VK_F2:
suspend();
break;
case KeyEvent.VK_F3:
resume();
break;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
}
//源代码片段来自云代码http://yuncode.net