版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、不多说,直接可以拷贝下面的东西,然后记得把那个BLOCK的名字改成你自己的类名,这个很关键哦,不然是错的可别怪我,呵呵import java.awt.*;import java.awt.event.*;import javax.swing.*; import java.applet.*;import .*;import java.lang.*;import java.io.*;public class Block extends JPanel implements ActionListener,KeyListener/应该是继承JPanelstatic Button but = new But
2、ton6;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("
3、 ");static Label speedTex = 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("
4、;帮助 ");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 S
5、tring butLab = "开 始 游 戏","重 新 开 始","降 低 级 数","提 高 级 数","游 戏 暂 停","退 出 游 戏"static int game_body = new int1910;static int game_sign_x = new int4;/用于记录4个方格的水平位置static int game_sign_y = new int4;/用于记录4个方格的垂直位置static boolean downSign = false;/是否
6、落下static int blockNumber = 1;/砖块的编号static int gameScore = 0;/游戏分数static int speedMark = 1;public static void main(String args) Block myBlock = 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,5
7、01);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();public void init() setLayout(null); for(int i = 0;i < 6;i+) buti = new Button(butLa
8、bi); add(buti); buti.addActionListener(this); buti.addKeyListener(this); buti.setBounds(360,(240 + 30 * i),160,25); add(scoreLab); add(scoreTex); add(speedLab); add(speedTex); add(infoLab); add(infoTex); add(scoreLab); scoreLab.setBounds(320,15,30,20); scoreTex.setBounds(360,15,160,20);scoreTex.setB
9、ackground(Color.white);speedLab.setBounds(320,45,30,20);speedTex.setBounds(360,45,160,20);speedTex.setBackground(Color.white);but1.setEnabled(false);but4.setEnabled(false);infoLab.setBounds(320,75,30,20);infoTex.setBounds(360,75,160,20);infoTex.setBackground(Color.white);noStop.setBounds(360,360,160
10、,25);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() = but0)/开始游戏 startSign = 1; infoTex.setText("游戏已经开始!&quo
11、t;); but0.setEnabled(false); but1.setEnabled(true); but4.setEnabled(true); timer.resume(); if(e.getSource() = but1|e.getSource() = mi0)/重新开始游戏 startSign = 0; gameScore = 0; timer.suspend(); num_csh_restart(); repaint(); rand_block(); scoreTex.setText("0"); infoTex.setText("新游戏!")
12、; but0.setEnabled(true); but1.setEnabled(false); but4.setEnabled(false); if(e.getSource() = but2)/降低级数 infoTex.setText("降低级数!"); speedMark-; if(speedMark <= 1) speedMark = 1; infoTex.setText("已经是最低级数!"); speedTex.setText(speedMark + ""); if(e.getSource() = but3)/提高级数
13、 infoTex.setText("提高级数!"); speedMark+; if(speedMark >= 9) speedMark = 9; infoTex.setText("已经是最高级数!"); speedTex.setText(speedMark + ""); if(e.getSource() = but4)/游戏暂停 this.add(noStop); this.remove(but4); infoTex.setText("游戏暂停!"); timer.suspend(); if(e.getSou
14、rce() = noStop)/取消暂停 this.remove(noStop); this.add(but4); infoTex.setText("继续游戏!"); timer.resume(); if(e.getSource() = but5|e.getSource() = mi1)/退出游戏 jf.dispose(); if(e.getSource() = mi1_0)/退出游戏 dlg_1 = new JDialog(jf,"关 于"); try FileInputStream io = new FileInputStream("res
15、ource/guanyu.txt");/得到路径 byte a = new byteio.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
16、, 240); dlg_1.show(); public void rand_block()/随机产生砖块 int num;num = (int)(Math.random() * 6) + 1;/产生06之间的随机数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(); blockNumbe
17、r = 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_y0 = game_sign_y1&&game_s
18、ign_y3 <= 16)/说明长条是横着的 if(game_bodygame_sign_y0 - 1game_sign_x0 + 1 != 2&&game_bodygame_sign_y3 + 2game_sign_x3 - 2 != 2) num_csh_game(); game_bodygame_sign_y0 - 1game_sign_x0 + 1 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 + 1game_sign_x2 - 1 = 1; game_bodygame_sign
19、_y3 + 2game_sign_x3 - 2 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_x0 = game_sign_x1&&game_sign_x0 >= 1&&game_sign_x3 <= 7)/说明长条是竖着的 if(game_bodygame_sign_y0 + 1game_sign_x0-1 != 2&&game_bodygame_sign_y3 - 2game_sign_x3 + 2 != 2) num_csh_game(); g
20、ame_bodygame_sign_y0 + 1game_sign_x0 - 1 = 1; game_bodygame_sign_y1game_sign_x1=1; game_bodygame_sign_y2 - 1game_sign_x2 + 1 = 1; game_bodygame_sign_y3 - 2game_sign_x3 + 2 = 1; infoTex.setText("游戏进行中!"); repaint(); if(blockNumber = 3&&downSign = false)/变换转弯1有4种情况 if(game_sign_x0 =
21、game_sign_x1&&game_sign_x0 = game_sign_x2&&game_sign_y2 = game_sign_y3&&game_sign_x0 >= 1) if(game_bodygame_sign_y0 + 1game_sign_x0 - 1 != 2&&game_bodygame_sign_y2 - 1game_sign_x2 + 1 != 2&&game_bodygame_sign_y3 - 2game_sign_x3 != 2) num_csh_game(); game_bo
22、dygame_sign_y0 + 1game_sign_x0 - 1 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2 + 1 = 1; game_bodygame_sign_y3 - 2game_sign_x3 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_y1 = game_sign_y2&&game_sign_y2 = game_sign_y3&&game_s
23、ign_x0 = game_sign_x3&&game_sign_y1 <= 17) if(game_bodygame_sign_y0game_sign_x0 - 2 != 2&&game_bodygame_sign_y1 + 1game_sign_x1 + 1 != 2&&game_bodygame_sign_y3 - 1game_sign_x3 - 1 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 - 2 = 1; game_bodygame_sign_y1 + 1gam
24、e_sign_x1 + 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 - 1game_sign_x3 - 1 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_x1 = game_sign_x2&&game_sign_x1 = game_sign_x3&&game_sign_y0 = game_sign_y1&&game_sign_x3 <= 8) if(game_bod
25、ygame_sign_y0 + 2game_sign_x0 != 2&&game_bodygame_sign_y1 + 1game_sign_x1 - 1 != 2&&game_bodygame_sign_y3 - 1game_sign_x3 + 1 != 2) num_csh_game(); game_bodygame_sign_y0 + 2game_sign_x0 = 1; game_bodygame_sign_y1 + 1game_sign_x1 - 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bo
26、dygame_sign_y3 - 1game_sign_x3 + 1 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_y0 = game_sign_y1&&game_sign_y1 = game_sign_y2&&game_sign_x0 = game_sign_x3) if(game_bodygame_sign_y0 + 1game_sign_x0 + 1 != 2&&game_bodygame_sign_y2 - 1game_sign_x2 - 1 != 2&
27、amp;&game_bodygame_sign_y3game_sign_x3 + 2 != 2) num_csh_game(); game_bodygame_sign_y0 + 1game_sign_x0 + 1 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2 - 1 = 1; game_bodygame_sign_y3game_sign_x3 + 2 = 1; infoTex.setText("游戏进行中!"); repaint(); if(bloc
28、kNumber = 4&&downSign = false)/变换转弯2有4种情况 if(game_sign_x0 = game_sign_x1&&game_sign_x0 = game_sign_x3&&game_sign_y1 = game_sign_y2&&game_sign_x3 <= 7) if(game_bodygame_sign_y0 + 2game_sign_x0 != 2&&game_bodygame_sign_y1 + 1game_sign_x1 + 1 != 2&&gam
29、e_bodygame_sign_y3game_sign_x3 + 2 != 2) num_csh_game(); game_bodygame_sign_y0 + 2game_sign_x0 = 1; game_bodygame_sign_y1 + 1game_sign_x1 + 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3game_sign_x3 + 2 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_y1 = game_
30、sign_y2&&game_sign_y1 = game_sign_y3&&game_sign_x0 = game_sign_x2) if(game_bodygame_sign_y1game_sign_x1 + 2 != 2&&game_bodygame_sign_y2 - 1game_sign_x2 + 1 != 2&&game_bodygame_sign_y3 - 2game_sign_x3 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 = 1; game_bo
31、dygame_sign_y1game_sign_x1 + 2 = 1; game_bodygame_sign_y2 - 1game_sign_x2 + 1 = 1; game_bodygame_sign_y3 - 2game_sign_x3 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_x0 = game_sign_x2&&game_sign_x0 = game_sign_x3&&game_sign_y1 = game_sign_y2&&game_sign_x0
32、 >= 2) if(game_bodygame_sign_y0game_sign_x0 - 2 != 2&&game_bodygame_sign_y2 - 1game_sign_x2 - 1 != 2&&game_bodygame_sign_y3 - 2game_sign_x3 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 - 2 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2
33、 - 1 = 1; game_bodygame_sign_y3 - 2game_sign_x3 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_y0 = game_sign_y1&&game_sign_y0 = game_sign_y2&&game_sign_x1 = game_sign_x3&&game_sign_y0 <= 16) if(game_bodygame_sign_y0 + 2game_sign_x0 != 2&&game_bo
34、dygame_sign_y1 + 1game_sign_x1 - 1 != 2&&game_bodygame_sign_y2game_sign_x2 - 2 != 2) num_csh_game(); game_bodygame_sign_y0 + 2game_sign_x0 = 1; game_bodygame_sign_y1 + 1game_sign_x1 - 1 = 1; game_bodygame_sign_y2game_sign_x2 - 2 = 1; game_bodygame_sign_y3game_sign_x3 = 1; infoTex.setText(&qu
35、ot;游戏进行中!"); repaint(); if(blockNumber = 5&&downSign = false)/变换转弯3有4种情况 if(game_sign_x0 = game_sign_x2&&game_sign_x2 = game_sign_x3&&game_sign_y0 = game_sign_y1&&game_sign_x1 >= 2) if(game_bodygame_sign_y0 + 1game_sign_x0 - 1 != 2&&game_bodygame_sign_
36、y1game_sign_x1 - 2 != 2&&game_bodygame_sign_y3 - 1game_sign_x3 + 1 != 2) num_csh_game(); game_bodygame_sign_y0 + 1game_sign_x0 - 1 = 1; game_bodygame_sign_y1game_sign_x1 - 2 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 - 1game_sign_x3 + 1 = 1; infoTex.setText("游戏进行中
37、!"); repaint(); if(game_sign_y1 = game_sign_y2&&game_sign_y2 = game_sign_y3&&game_sign_x0 = game_sign_x1&&game_sign_y0 <= 16) if(game_bodygame_sign_y0 + 2game_sign_x0 != 2&&game_bodygame_sign_y1 + 1game_sign_x1 + 1 != 2&&game_bodygame_sign_y3 - 1game_s
38、ign_x3 - 1 != 2) num_csh_game(); game_bodygame_sign_y0 + 2game_sign_x0 = 1; game_bodygame_sign_y1 + 1game_sign_x1 + 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 - 1game_sign_x3 - 1 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_x0 = game_sign_x1&&gam
39、e_sign_x1 = game_sign_x3&&game_sign_y2 = game_sign_y3) if(game_bodygame_sign_y0 + 1game_sign_x0 - 1 != 2&&game_bodygame_sign_y2game_sign_x2 + 2 != 2&&game_bodygame_sign_y3 - 1game_sign_x3 + 1 != 2) num_csh_game(); game_bodygame_sign_y0 + 1game_sign_x0 - 1 = 1; game_bodygame_s
40、ign_y1game_sign_x1 = 1; game_bodygame_sign_y2game_sign_x2 + 2 = 1; game_bodygame_sign_y3 - 1game_sign_x3 + 1 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_y0 = game_sign_y1&&game_sign_y1 = game_sign_y2&&game_sign_x2 = game_sign_x3) if(game_bodygame_sign_y0 + 1game
41、_sign_x0 + 1 != 2&&game_bodygame_sign_y2 - 1game_sign_x2 - 1 != 2&&game_bodygame_sign_y3 - 2game_sign_x3 != 2) num_csh_game(); game_bodygame_sign_y0 + 1game_sign_x0 + 1 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2 - 1 = 1; game_bodygame_sign_y3 -
42、2game_sign_x3 = 1; infoTex.setText("游戏进行中!"); repaint(); if(blockNumber = 6&&downSign = false)/变换两层砖块1的2种情况 if(game_sign_x0 = game_sign_x2&&game_sign_x0 >= 2) if(game_bodygame_sign_y0game_sign_x0 - 2 != 2&&game_bodygame_sign_y2 - 1game_sign_x2 -1 != 2&&ga
43、me_bodygame_sign_y3 - 1game_sign_x3 + 1 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 - 2 = 1; game_bodygame_sign_y1game_sign_x1 = 1; game_bodygame_sign_y2 - 1game_sign_x2 - 1 = 1; game_bodygame_sign_y3 - 1game_sign_x3 + 1 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_y
44、0 = game_sign_y1&&game_sign_y3 <= 17) if(game_bodygame_sign_y0game_sign_x0 + 2 != 2&&game_bodygame_sign_y1 + 1game_sign_x1 + 1 != 2&&game_bodygame_sign_y3 + 1game_sign_x3 - 1 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 + 2 = 1; game_bodygame_sign_y1 + 1game_sig
45、n_x1 + 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 + 1game_sign_x3 - 1 = 1; infoTex.setText("游戏进行中!"); repaint(); if(blockNumber = 7&&downSign = false)/变换两层砖块2的2种情况 if(game_sign_x0 = game_sign_x1&&game_sign_x0 <= 16) if(game_bodygame_sign_y0game_s
46、ign_x0 + 2 != 2&&game_bodygame_sign_y1 - 1game_sign_x1 + 1 != 2&&game_bodygame_sign_y3 - 1game_sign_x3 - 1 != 2) num_csh_game(); game_bodygame_sign_y0game_sign_x0 + 2 = 1; game_bodygame_sign_y1 - 1game_sign_x1 + 1 = 1; game_bodygame_sign_y2game_sign_x2 = 1; game_bodygame_sign_y3 - 1g
47、ame_sign_x3 - 1 = 1; infoTex.setText("游戏进行中!"); repaint(); if(game_sign_y0 = game_sign_y1&&game_sign_y2 <= 17) if(game_bodygame_sign_y0 + 1game_sign_x0 - 1 != 2&&game_bodygame_sign_y1game_sign_x1 - 2 != 2&&game_bodygame_sign_y2 + 1game_sign_x2 + 1 != 2) num_csh_g
48、ame(); game_bodygame_sign_y0 + 1game_sign_x0 - 1 = 1; game_bodygame_sign_y1game_sign_x1 - 2 = 1; game_bodygame_sign_y2 + 1game_sign_x2 + 1 = 1; game_bodygame_sign_y3game_sign_x3 = 1; infoTex.setText("游戏进行中!"); repaint(); public void num_csh_game()/数组清零 for(int i = 0;i < 19;i+) for(int j
49、 = 0;j < 10;j+) if(game_bodyij = 2) game_bodyij = 2; else game_bodyij = 0; public void num_csh_restart()/重新开始时数组清零 for(int i = 0;i < 19;i+) for(int j = 0;j < 10;j+) game_bodyij = 0; public void keyTyped(KeyEvent e) public void keyPressed(KeyEvent e) if(e.getKeyCode() = KeyEvent.VK_DOWN&
50、&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(st
51、artSign = 0) infoTex.setText("游戏未开始或已结束!"); public void keyReleased(KeyEvent e) public void paint(Graphics g)g.setColor(Color.black);g.fill3DRect(0,0,300,450,true);for(int i = 0;i < 19;i+) for(int j = 0;j < 10;j+) if(game_bodyij = 1) g.setColor(Color.blue); g.fill3DRect(30*j,30*(i-4),30,30,true); if(game_bodyij = 2) g.setColor(Color.magenta); g.fill3DRect(30*j,30*(i-4),30,30,true); public void left()/向左移动int sign = 0;dingwei();for(int k = 0;k < 4;k+) if(game_sign_xk = 0|game_bodygame_sign_ykgame_sign_xk - 1 = 2) sign = 1; if
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年中国化工专用泵行业市场前景预测及投资价值评估分析报告
- 2026福建水投集团招聘笔试考试参考题库及答案解析
- 2025重庆医科大学附属第二医院JCTH杂志社英文编辑(编外)招聘1人笔试考试备考题库及答案解析
- 2025年文化演出票务合同协议书
- 儿童脑瘫的康复训练指南
- 主播经营粉丝关系的系统化策略
- 高空作业承包合同范本
- 2026年防城港职业技术学院单招职业适应性考试必刷测试卷新版
- 2026年河南轻工职业学院单招职业适应性测试题库新版
- 2026年淮南职业技术学院单招职业技能测试题库及答案1套
- 安全生产考核巡查办法全文
- 第五讲-马克思的劳动价值论和剩余价值论课件
- 新媒体运营技术与应用PPT完整全套教学课件
- 信创实验室建设方案
- 横钩、竖钩写法
- 监理实施细则(天然气)
- 校本课程开发教学课件
- 《卷烟原料配方设计》配套教学课件
- 秉持责任担当厚植家国情怀-主题班会课件
- 水电站运行和管理课件
- 叶片泵的叶轮理论与性能
评论
0/150
提交评论