俄罗斯方块带背景音乐
很简单,一个主类运行基本操作,一个音乐类播放背景音乐。
一、运行结果
二、程序结构
g)
三、Block类
package 俄罗斯方块; import java.awt.*; import java.awt.event.*; import javax.swing.*; import 俄罗斯方块.Music; import java.applet.*; import java.lang.String.*; import java.util.concurrent.TimeUnit; import java.lang.*; import java.io.*; public class 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 scoreTex = new Label("0"); static Label infoTex = new Label(" "); static Label speedTex = new Label("1"); //创建窗体对象 static JFrame jf = new JFrame(); static MyTimer timer; //静态定时 static ImageIcon icon=new ImageIcon("image/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 JTextArea dlg_1_text = new JTextArea();//创建多行文本容器,可实现对文本的自动换行 static int startSign = 0;//游戏开始标志 0 未开始 1 开始 2 暂停 static String butLab[] = {"开 始 游 戏","重 新 开 始","降 低 级 数","提 高 级 数","游 戏 暂 停","退 出 游 戏"}; static int game_body[][] = new int[19][10];//创建游戏主界面 static int game_sign_x[] = new int[4];//用于记录4个方格的水平位置 static int game_sign_y[] = new int[4];//用于记录4个方格的垂直位置 static boolean downSign = false;//是否落下 static int blockNumber = 1;//砖块的编号 static int gameScore = 0;//游戏分数 static int speedMark = 1;//速度标志 public static void main(String args[]) throws InterruptedException { Block myBlock = new Block(); //添加菜单栏监听项 mb.add(menu0);//:游戏 mb.add(menu1);//:帮助 //添加菜单栏监听对象下拉菜单 menu0.add(mi0);//:新游戏 menu0.add(mi1);//:退出 menu1.add(mi1_0);//:关于 jf.setJMenuBar(mb);//设置容器 mb.getComponent().setBackground(Color.orange);//设置菜单栏背景颜色为橘色 myBlock.init();//初始化方法 jf.add(myBlock); jf.setSize(565,501); jf.setResizable(false);//窗体大小不可被用户改变 jf.setTitle("俄罗斯方块"); jf.setIconImage(icon.getImage());//设置JFrame窗口标题图标 jf.setLocation(200,100);//窗体大小 jf.show(); timer = new MyTimer(myBlock); //启动线程 timer.setDaemon(true); timer.start(); timer.suspend();//定时睡眠 Music player = new Music("E:/file/debug/俄罗斯方块/src/image/俄罗斯方块游戏音乐.wav");//创建音乐播放器 player.start(true);//以开始以循环的形式播放,player(false)为不循环播放 TimeUnit.SECONDS.sleep(5); player.stop();//暂停播放音频 TimeUnit.SECONDS.sleep(4); player.continues();//继续开始播放音频 } //初始化数据 public void init() { setLayout(null); for(int i = 0;i < 6;i++) { but[i] = new Button(butLab[i]);//创建新按钮 add(but[i]); but[i].addActionListener(this);//鼠标监听器 but[i].addKeyListener(this);//键盘监听器 but[i].setBounds(360,(240 + 30 * i),160,25);//对组件进行自定义大小和位置设置 //修改组件x坐标,y坐标,宽度,长度 } //添加侧方标记 add(scoreLab);//分数 add(scoreTex); add(speedLab);//级数 add(speedTex); add(infoLab);//提示 add(infoTex); add(scoreLab); //设置标记位置 scoreLab.setBounds(320,15,30,20);//“分数标记”的x坐标,y坐标,宽度,长度 scoreTex.setBounds(360,15,160,20);//“分数标记文本框”的x坐标,y坐标,宽度,长度 scoreTex.setBackground(Color.CYAN);//设青色文本框 speedLab.setBounds(320,45,30,20);//“级数标记”的x坐标,y坐标,宽度,长度 speedTex.setBounds(360,45,160,20);//“级数标记文本框”的x坐标,y坐标,宽度,长度 speedTex.setBackground(Color.CYAN);//青色文本框 //冻结按钮1,4 but[1].setEnabled(false); but[4].setEnabled(false); infoLab.setBounds(320,75,30,20);//“提示标记”的x坐标,y坐标,宽度,长度 infoTex.setBounds(360,75,160,20);//“提示标记文本框”的x坐标,y坐标,宽度,长度 infoTex.setBackground(Color.CYAN);//青色文本框 noStop.setBounds(360,360,160,25);//游戏暂停按钮的x坐标,y坐标,宽度,长度 //添加按钮监听器 noStop.addActionListener(this); noStop.addKeyListener(this); //添加下拉菜单栏监听器 mi0.addActionListener(this); mi1.addActionListener(this); mi1_0.addActionListener(this); num_csh_game(); rand_block(); } public void actionPerformed(ActionEvent e)//游戏行为 { if(e.getSource() == but[0])//开始游戏 { startSign = 1;//开始标志1 infoTex.setText("游戏已经开始!"); but[0].setEnabled(false);//冻结开始游戏按钮 but[1].setEnabled(true);//激活重新开始按钮 but[4].setEnabled(true);//激活暂停按钮 timer.resume(); } if(e.getSource() == but[1]||e.getSource() == mi0)//重新开始游戏 { startSign = 0;//标志设0,游戏未开始 gameScore = 0;//游戏分数清零 timer.suspend();//定时睡眠 num_csh_restart(); repaint(); rand_block(); scoreTex.setText("0");//得分设0 infoTex.setText("新游戏!");//提示框显示:新游戏 but[0].setEnabled(true);//激活开始游戏按钮 but[1].setEnabled(false);//冻结重新开始按钮 but[4].setEnabled(false);//冻结暂停按钮 } if(e.getSource() == but[2])//降低级数 { infoTex.setText("降低级数!"); speedMark--;//级数减 if(speedMark <= 1) { speedMark = 1; infoTex.setText("已经是最低级数!");//提示框输出 } speedTex.setText(speedMark + "");//输出当前正确级数 } if(e.getSource() == but[3])//提高级数 { infoTex.setText("提高级数!");//提示框输出 speedMark++;//级数加 if(speedMark >= 9) { speedMark = 9; infoTex.setText("已经是最高级数!");//提示框输出 } speedTex.setText(speedMark + "");//输出当前级数 } if(e.getSource() == but[4])//游戏暂停 { this.add(noStop); this.remove(but[4]); infoTex.setText("游戏暂停!"); timer.suspend();//定时睡眠 } if(e.getSource() == noStop)//取消暂停 { this.remove(noStop); this.add(but[4]); infoTex.setText("继续游戏!"); timer.resume(); } if(e.getSource() == but[5]||e.getSource() == mi1)//退出游戏 { jf.dispose();//关闭游戏窗口 } if(e.getSource() == mi1_0)//退出游戏 { dlg_1 = new JDialog(jf,"关 于"); try{ FileInputStream io = new FileInputStream("E:\\file\\debug\\俄罗斯方块\\src\\image");//得到路径 byte a[] = new byte[io.available()]; io.read(a); io.close(); String str = new String(a); dlg_1_text.setText(str); } catch(Exception g){} dlg_1_text.setEditable(false); dlg_1.add(dlg_1_text); dlg_1.pack(); dlg_1.setResizable(false); dlg_1.setSize(200, 120); dlg_1.setLocation(400, 240); dlg_1.show(); } } public void rand_block()//随机产生砖块 { int num; num = (int)(Math.random() * 6) + 1;//产生0~6之间的随机数 blockNumber = num; switch(blockNumber)//砖块编号 { case 1: block1(); blockNumber = 1; break; case 2: block2(); blockNumber = 2; break; case 3: block3(); blockNumber = 3; break; case 4: block4(); blockNumber = 4; break; case 5: block5(); blockNumber = 5; break; case 6: block6(); blockNumber = 6; break; case 7: block7(); blockNumber = 7; break; } } public void change_body(int blockNumber)//改变砖块状态 { dingwei(); if(blockNumber == 1&&downSign == false)//变换长条2种情况 { if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 16)//长条是横着的,1和2方格垂直位置相同且4方格垂直位置小于等于16 { if(game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] + 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] = 1; infoTex.setText("游戏进行中!"); repaint();//重新绘图 } } if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] >= 1&&game_sign_x[3] <= 7)//长条是竖着的 { if(game_body[game_sign_y[0] + 1][game_sign_x[0]-1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]]=1; game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] = 1; infoTex.setText("游戏进行中!"); repaint(); } } } if(blockNumber == 3&&downSign == false)//变换转弯1有4种情况 { //第一种 if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] >= 1) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第二种 if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] <= 17) { if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第三种 if(game_sign_x[1] == game_sign_x[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[3] <= 8) { if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第四种 if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] == game_sign_x[3]) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1; infoTex.setText("游戏进行中!"); repaint(); } } } if(blockNumber == 4&&downSign == false)//变换转弯2有4种情况 { //第一种 if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[3] <= 7) { if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第二种 if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[1] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[2]) { if(game_body[game_sign_y[1]][game_sign_x[1] + 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0]][game_sign_x[0]] = 1; game_body[game_sign_y[1]][game_sign_x[1] + 2] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第三种 if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] >= 2) { if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第四种 if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[0] == game_sign_y[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] <= 16) { if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] - 2] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1; game_body[game_sign_y[2]][game_sign_x[2] - 2] = 1; game_body[game_sign_y[3]][game_sign_x[3]] = 1; infoTex.setText("游戏进行中!"); repaint(); } } } if(blockNumber == 5&&downSign == false)//变换转弯3有4种情况 { //第一种 if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[2] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[1] >= 2) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] - 2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第二种 if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[1]&&game_sign_y[0] <= 16) { if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第三种 if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[2] == game_sign_y[3]) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] + 2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2]][game_sign_x[2] + 2] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第四种 if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[2] == game_sign_x[3]) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1; infoTex.setText("游戏进行中!"); repaint(); } } } if(blockNumber == 6&&downSign == false)//变换两层砖块1的2种情况 { //第一种 if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] >= 2) { if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] -1 ] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1; game_body[game_sign_y[1]][game_sign_x[1]] = 1; game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第二种 if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 17) { if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1; game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] = 1; infoTex.setText("游戏进行中!"); repaint(); } } } if(blockNumber == 7&&downSign == false)//变换两层砖块2的2种情况 { //第一种 if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] <= 16) { if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1; game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] = 1; game_body[game_sign_y[2]][game_sign_x[2]] = 1; game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1; infoTex.setText("游戏进行中!"); repaint(); } } //第二种 if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[2] <= 17) { if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] - 2] != 2&&game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] != 2) { num_csh_game();//数组清零 game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1; game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1; game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] = 1; game_body[game_sign_y[3]][game_sign_x[3]] = 1; infoTex.setText("游戏进行中!"); repaint(); } } } } public void num_csh_game()//数组清零 { for(int i = 0;i < 19;i++) { for(int j = 0;j < 10;j++) { if(game_body[i][j] == 2) { game_body[i][j] = 2; } else { game_body[i][j] = 0;//界面清零 } } } } public void num_csh_restart()//重新开始时数组清零 { for(int i = 0;i < 19;i++) { for(int j = 0;j < 10;j++) { game_body[i][j] = 0;//界面清零 } } } public void keyTyped(KeyEvent e){}//敲击键盘,发生在按键按下后放开前 public void keyPressed(KeyEvent e)//按下按键时发生 { if(e.getKeyCode() == KeyEvent.VK_DOWN&&startSign == 1)//处理下键 { this.down(); } if(e.getKeyCode() == KeyEvent.VK_LEFT&&startSign == 1)//处理左键 { this.left(); } if(e.getKeyCode() == KeyEvent.VK_RIGHT&&startSign == 1)//处理右键 { this.right(); } if(e.getKeyCode() == KeyEvent.VK_UP&&startSign == 1)//处理上键转换 { this.change_body(blockNumber); } if(startSign == 0) { infoTex.setText("游戏未开始或已结束!"); } } public void keyReleased(KeyEvent e){}//松开按键时发生 public void paint(Graphics g)//绘图 { g.setColor(Color.PINK);//游戏界面粉色 g.fill3DRect(0,0,300,450,true);//画一个游戏主界面着色块 //x坐标,y坐标,宽度,长度,显示 for(int i = 0;i < 19;i++) { for(int j = 0;j < 10;j++) { if(game_body[i][j] == 1)//未落下 { g.setColor(Color.BLACK); g.fill3DRect(30*j,30*(i-4),30,30,true);//画一个黑色方块 } if(game_body[i][j] == 2)//已落下 { g.setColor(Color.GREEN); g.fill3DRect(30*j,30*(i-4),30,30,true);//画一个绿色方块 } } } } public void left()//向左移动 { int sign = 0; dingwei(); for(int k = 0;k < 4;k++)//循环遍历16个方格(4*4) { if(game_sign_x[k] == 0||game_body[game_sign_y[k]][game_sign_x[k] - 1] == 2)//如果左移一格没有方块时 { sign = 1;//左移 } } if(sign == 0&&downSign == false)//已经落下且占满 { num_csh_game();//数组清零 for(int k = 0;k < 4;k++) { game_body[game_sign_y[k]][game_sign_x[k] - 1] = 1; } infoTex.setText("向左移动!"); repaint(); } } public void right()//向右移动 { int sign = 0; dingwei(); for(int k = 0;k < 4;k++) { if(game_sign_x[k] == 9||game_body[game_sign_y[k]][game_sign_x[k] + 1] == 2) { sign = 1; } } if(sign == 0&&downSign == false) { num_csh_game();//数组清零 for(int k = 0;k < 4;k++) { game_body[game_sign_y[k]][game_sign_x[k] + 1] = 1; } infoTex.setText("向右移动!"); repaint(); } } public void down()//下落 { int sign = 0; dingwei(); for(int k = 0;k < 4;k++) { if(game_sign_y[k] == 18||game_body[game_sign_y[k] + 1][game_sign_x[k]] == 2) { sign = 1; downSign = true;//落下 changeColor(); cancelDW(); getScore(); if(game_over() == false)//游戏未结束 { rand_block(); repaint(); } } } if(sign == 0) { num_csh_game();//数组清零 for(int k = 0;k < 4;k++) { game_body[game_sign_y[k] + 1][game_sign_x[k]] = 1; } infoTex.setText("游戏进行中!"); repaint(); } } public boolean game_over()//判断游戏是否结束 { int sign=0; for(int i = 0;i < 10;i++) { if(game_body[4][i] == 2) { sign = 1; } } if(sign == 1) { infoTex.setText("游戏结束!"); changeColor(); repaint(); startSign = 0; timer.suspend();//定时睡眠 return true; } else return false; } public void getScore()//满行消除方法 { for(int i = 0;i < 19;i++) { int sign = 0; for(int j = 0;j < 10;j++) { if(game_body[i][j] == 2) { sign++; } } if(sign == 10) { gameScore += 100; scoreTex.setText(gameScore+""); infoTex.setText("恭喜得分!"); for(int j = i;j >= 1;j--) { for(int k = 0;k < 10;k++) { game_body[j][k] = game_body[j - 1][k]; } } } } } public void changeColor()//给已经落下的块换色 { downSign = false; for(int k = 0;k < 4;k++) { game_body[game_sign_y[k]][game_sign_x[k]] = 2; } } public void dingwei()//确定其位置 { int k = 0; cancelDW(); for(int i = 0;i < 19;i++) { for(int j = 0;j < 10;j++) { if(game_body[i][j] == 1) { game_sign_x[k] = j; game_sign_y[k] = i; k++; } } } } public void cancelDW()//将定位数组初始化 { for(int k = 0;k < 4;k++) { game_sign_x[k] = 0; game_sign_y[k] = 0; } } public void block1()//长条 { game_body[0][4] = 1;//█ game_body[1][4] = 1;//█ game_body[2][4] = 1;//█ game_body[3][4] = 1;//█ } public void block2()//正方形 { game_body[3][4] = 1;// game_body[2][4] = 1;//██ game_body[3][5] = 1;//██ game_body[2][5] = 1;// } public void block3()//3加1(下) { game_body[1][4] = 1;//█ game_body[2][4] = 1;//█ game_body[3][4] = 1;//██ game_body[3][5] = 1;// } public void block4()//3加1(中) { game_body[1][4] = 1;//█ game_body[2][4] = 1;//██ game_body[3][4] = 1;//█ game_body[2][5] = 1;// } public void block5()//3加1(上) { game_body[1][4] = 1;//██ game_body[2][4] = 1;//█ game_body[3][4] = 1;//█ game_body[1][5] = 1;// } public void block6()//转折1 { game_body[1][5] = 1;// █ game_body[2][5] = 1;// █ game_body[2][4] = 1;//█ game_body[3][4] = 1;//█ } public void block7()//转折2 { game_body[1][4] = 1;//█ game_body[2][4] = 1;//█ game_body[2][5] = 1;// █ game_body[3][5] = 1;// █ }//其余形状自行设计 } //定时线程 class MyTimer extends Thread { Block myBlock; public MyTimer(Block myBlock) { this.myBlock = myBlock; } public void run() { while(myBlock.startSign == 1) { try{ sleep((10-myBlock.speedMark + 1)*100); myBlock.down(); } catch(InterruptedException e){} } } }
四、Music类
背景音乐必须转为wav格式,且复制完代码必须修改背景音乐的地址和图片地址
package 俄罗斯方块; import javax.sound.sampled.*; import java.io.*; import java.util.concurrent.TimeUnit; public class Music { private String musicPath; //音频文件 private volatile boolean run = true; //记录音频是否播放 private Thread mainThread; //播放音频的任务线程 private AudioInputStream audioStream; private AudioFormat audioFormat; private SourceDataLine sourceDataLine; public Music(String musicPath) { this.musicPath = musicPath; prefetch(); } //数据准备 private void prefetch(){ try{ //获取音频输入流 audioStream = AudioSystem.getAudioInputStream(new File(musicPath)); //获取音频的编码对象 audioFormat = audioStream.getFormat(); //包装音频信息 DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat,AudioSystem.NOT_SPECIFIED); //使用包装音频信息后的Info类创建源数据行,充当混频器的源 sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo); sourceDataLine.open(audioFormat); sourceDataLine.start(); }catch(UnsupportedAudioFileException ex){ ex.printStackTrace(); }catch(LineUnavailableException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); } } //析构函数:关闭音频读取流和数据行 protected void finalize() throws Throwable{ super.finalize(); sourceDataLine.drain(); sourceDataLine.close(); audioStream.close(); } //播放音频:通过loop参数设置是否循环播放 private void playMusic(boolean loop)throws InterruptedException { try{ if(loop){ while(true){ playMusic(); } }else{ playMusic(); //清空数据行并关闭 sourceDataLine.drain(); sourceDataLine.close(); audioStream.close(); } }catch(IOException ex){ ex.printStackTrace(); } } private void playMusic(){ try{ synchronized(this){ run = true; } //通过数据行读取音频数据流,发送到混音器; //数据流传输过程:AudioInputStream -> SourceDataLine; audioStream = AudioSystem.getAudioInputStream(new File(musicPath)); int count; byte tempBuff[] = new byte[1024]; while((count = audioStream.read(tempBuff,0,tempBuff.length)) != -1){ synchronized(this){ while(!run) wait(); } sourceDataLine.write(tempBuff,0,count); } }catch(UnsupportedAudioFileException ex){ ex.printStackTrace(); }catch(IOException ex){ ex.printStackTrace(); }catch(InterruptedException ex){ ex.printStackTrace(); } } //暂停播放音频 private void stopMusic(){ synchronized(this){ run = false; notifyAll(); } } //继续播放音乐 private void continueMusic(){ synchronized(this){ run = true; notifyAll(); } } //外部调用控制方法:生成音频主线程; public void start(boolean loop){ mainThread = new Thread(new Runnable(){ public void run(){ try { playMusic(loop); } catch (InterruptedException e) { e.printStackTrace(); } } }); mainThread.start(); } //外部调用控制方法:暂停音频线程 public void stop(){ new Thread(new Runnable(){ public void run(){ stopMusic(); } }).start(); } //外部调用控制方法:继续音频线程 public void continues(){ new Thread(new Runnable(){ public void run(){ continueMusic(); } }).start(); } }
源码链接: https://pan.baidu.com/s/1eK86Anxv4t2khgilp0x0aA .
提取码:854T