小游戏俄罗斯方块代码(JAVA)
- 格式:doc
- 大小:138.00 KB
- 文档页数:20
东西绝对不多说,直接看!看了拷贝就懂了!!,直接可以拷贝下面的东西,然后记得把那个BLOCK 的名字改成你自己的类名,这个很关键哦,不然是错的可别怪我,呵呵~~
import java.awt.*;
import
import javax.swing.*;
import java.applet.*;
import.*;
import ng.*;
import java.io.*;
publicclass Block extends JPanel implements ActionListener,KeyListener//应该是继承JPanel {
static Button but[]=new Button[6];
static Button noStop=new Button("取消暂停");
static Label scoreLab=new Label("分数:");
static Label infoLab=new Label("提示:");
static Label speedLab=new Label("级数:");
static Label scoreT ex=new Label("0");
static Label infoT ex=new Label("");
static Label speedT ex=new Label("1");
static JFrame jf=new JFrame();
static MyTimer timer;
static ImageIcon icon=new ImageIcon("resource/Block.jpg");
static JMenuBar mb=new JMenuBar();
static JMenu menu0=new JMenu("游戏");
static JMenu menu1=new JMenu("帮助");
static JMenuItem mi0=new JMenuItem("新游戏");
static JMenuItem mi1=new JMenuItem("退出");
static JMenuItem mi1_0=new JMenuItem("关于");
static JDialog dlg_1;
static JT extArea dlg_1_text=new JTextArea();
staticint startSign=0;//游戏开始标志0未开始1开始2暂停
static String butLab[]={"开始游戏","重新开始","降低级数","提高级数","游戏暂停","退出游戏"}; staticint game_body[][]=newint[19][10];
staticint game_sign_x[]=newint[4];//用于记录4个方格的水平位置
staticint game_sign_y[]=newint[4];//用于记录4个方格的垂直位置
staticboolean downSign=false;//是否落下
staticint blockNumber=1;//砖块的编号
staticint gameScore=0;//游戏分数
staticint speedMark=1;
publicstaticvoid main(Stringargs[])
{
BlockmyBlock=new Block();
mb.add(menu0);
mb.add(menu1);
menu0.add(mi0);
menu0.add(mi1);
menu1.add(mi1_0);
jf.setJMenuBar(mb);
myBlock.init();
jf.add(myBlock);
jf.setSize(565,501);
jf.setResizable(false);
jf.setTitle("俄罗斯方块");
jf.setIconImage(icon.getImage());
jf.setLocation(200,100);
jf.show();
timer=new MyTimer(myBlock);//启动线程
timer.setDaemon(true);
timer.start();
timer.suspend();
}
publicvoid init()
{
setLayout(null);
for(int i=0;i<6;i++)
{
but[i]=new Button(butLab[i]);
add(but[i]);
but[i].addKeyListener(this);
but[i].setBounds(360,(240+30*i),160,25);
}
add(scoreLab);
add(scoreT ex);
add(speedLab);
add(speedT ex);
add(infoLab);
add(infoT ex);
add(scoreLab);
scoreLab.setBounds(320,15,30,20);
scoreT ex.setBounds(360,15,160,20);
scoreT ex.setBackground(Color.white);
speedLab.setBounds(320,45,30,20);
speedT ex.setBounds(360,45,160,20);
speedT ex.setBackground(Color.white);
but[1].setEnabled(false);
but[4].setEnabled(false);
infoLab.setBounds(320,75,30,20);
infoTex.setBounds(360,75,160,20);
infoTex.setBackground(Color.white);
noStop.setBounds(360,360,160,25);
noStop.addActionListener(this);
noStop.addKeyListener(this);
mi0.addActionListener(this);
mi1.addActionListener(this);
mi1_0.addActionListener(this);
num_csh_game();
rand_block();
}
publicvoid actionPerformed(ActionEvente)
{
if(e.getSource()==but[0])//开始游戏
{
startSign=1;
infoTex.setT ext("游戏已经开始!");
but[0].setEnabled(false);
but[1].setEnabled(true);
but[4].setEnabled(true);
timer.resume();
}
if(e.getSource()==but[1]||e.getSource()==mi0)//重新开始游戏