开始正题,现在是俄罗斯方块时间,这个是在打印字的基础上制作的!
2014-05-22 10:42
190 查看
首先,话不多说,发现视图很好做,难道是我自己的幻觉,上视图的代码:
然后该有的注释都在源代码中了,下面是control的代码:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package view; import control.Control; /** * * @author Janeness */ public class View extends javax.swing.JFrame { Control c; //如果构造器不是public修饰,那么就会出错,View就无法访问Control的构造器 /** * Creates new form View */ public View() { //初始化创建的control对象,并启动线程开始运行 initComponents(); c = new Control(this); c.start(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); jButton5 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20); jTextArea1.setRows(5); jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(java.awt.event.KeyEvent evt) { jTextArea1KeyPressed(evt); } }); jScrollPane1.setViewportView(jTextArea1); jButton1.setText("向左"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { goLeft(evt); } }); jButton2.setText("向下"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { goDown(evt); } }); jButton3.setText("向右"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { goRight(evt); } }); jButton4.setText("变形"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Change(evt); } }); jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT); jTextField1.setText("0"); jButton5.setText("暂停"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pause(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 600, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE) .addComponent(jButton5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE) .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE) .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE) .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(35, 35, 35) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton4) .addGap(116, 116, 116) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(53, 53, 53) .addComponent(jButton5)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE)) .addContainerGap()) ); pack(); }// </editor-fold> private void goLeft(java.awt.event.ActionEvent evt) { // TODO add your handling code here: c.goLeft(); //向左走,对应视图中的左按钮 } private void goDown(java.awt.event.ActionEvent evt) { // TODO add your handling code here: c.goDown(); //向下走,对应视图中的下按钮 } private void goRight(java.awt.event.ActionEvent evt) { // TODO add your handling code here: c.goRight(); //向右走,对应视图中的右按钮 } private void Change(java.awt.event.ActionEvent evt) { // TODO add your handling code here: c.change(); //改变方向,对应视图中的改变按钮 } private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt) { // TODO add your handling code here: // System.out.println(evt.getKeyCode()); //创建的键盘事件 switch(evt.getKeyCode()) { //键盘上下左右对应的keycode case 37: c.goLeft();break; case 38: c.change();break; case 39: c.goRight();break; case 40: c.goDown();break; } } private void pause(java.awt.event.ActionEvent evt) { // TODO add your handling code here: c.changeState(); //对应暂停按钮,控制游戏的进度 } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new View().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; public javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; public javax.swing.JButton jButton5; public javax.swing.JScrollPane jScrollPane1; public javax.swing.JTextArea jTextArea1; public javax.swing.JTextField jTextField1; // End of variables declaration }
然后该有的注释都在源代码中了,下面是control的代码:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package control; import model.Model; import view.View; /** * * @author Janeness */ public class Control extends Thread { View v; //创建指定到view的对象 Model m = new Model(); //创建model对象 String count = ""; //创建字符串count用来统计分数 public boolean isSleep = true; //创建boolean对象来实现暂停和继续 public Control(View view) {//把view作为形参传给control,来实现赋值 v = view; //把v对象指定到当前的view v.jTextArea1.setText(m.Out()); //在view中打印出整个框架结构 } //上面的操作可以让control类调用model的功能和控制view传过来的控件 // 指示线程是否修改标签上的内容 public void changeState() { //用来实现暂停和继续的互相转换 isSleep = !isSleep; //当为true的时候,就给isSleep对象赋值为false,反之则相反 v.jButton5.setText("继续"); //当为false的时候,把暂停按钮的显示改为继续 } @Override public void run() { while (m.flag) { //当model中的flag为true的时候 try { if (isSleep) { //当isSleep对象为true的时候 m.goDownOne(); //向下走一步 v.jButton5.setText("暂停"); //这个时候暂停按钮显示为暂停 count=m.count+""; //把model中的count的值转换成String类型 v.jTextArea1.setText(m.Out()); //在view中打印出整个框架结构 v.jTextField1.setText(count); //在view显示出获得的分数 Thread.sleep(250); //每走一步就休息0.25秒 } } catch (InterruptedException ex) { } } } public boolean gameOver() { return m.flag; //返回model中的flag的值 } public void goDown() { //向下走的方法,走之后再打印出来 m.goDownOne(); v.jTextArea1.setText(m.Out()); } public void goLeft() { //向左走的方法,同上 m.goLeftOne(); v.jTextArea1.setText(m.Out()); } public void goRight() { //向右走的方法,同上 m.goRightOne(); v.jTextArea1.setText(m.Out()); } public void change() { //变形的方法,每点一次,就逆时针转90度,同上 m.changeOne(); v.jTextArea1.setText(m.Out()); } }接下来是model的代码:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package model; import java.util.Random; import jimu.*; /** * * @author Janeness */ public class Model { // int x1 = 1; // // int y1 = 1; public final String s1 = "□"; //这里是定义你需要的字符串,一个为空心方,一个是实心方 public final String s2 = "■"; public final int x = 30; //这里定义的是行数和列数,用来实现对应的字符的排列 public final int y = 30; public String a[][] = new String[x][y]; //这里就是正式定义数组,数组的大小就通过上面的x和y决定 Random r = new Random(); //创建随机数对象 int r1; //定义一个整形来获取上面随机产生的数值 public boolean flag; //创建boolean类型的flag来控制游戏 Jimu jimu; //创建积木对象,可以父类引用子类对象,所以只创建一个 DeleteJimu DJ; //创建删除积木对象 public int count=0; //创建分数对象 public Model() { //利用model对游戏进行初始化 setE(); setB(); setJiMu(); DJ = new DeleteJimu(a); flag = true; //默认为true,才能开始跑起来 } private void setE() { //这个是用来初始化所有的数组元素为空心方 for (int j = 0; j < y; j++) { for (int i = 0; i < x; i++) { a[i][j] = s1; //和上面输出一样的循环,只不过这个是循环设值 } } } private void setB() { //这个是用来设置边框,把四条边都变成实心方 for (int i = 0; i < y; i++) { a[0][i] = s2; //所以下面的四条边都是设置成s2也就是实心方 a[y - 1][i] = s2; a[i][y - 1] = s2; a[i][0] = s2; } } public void deleteJimu() { if(DJ.panduan()){ //如果在删除的判断有满行,就进行删除 count=DJ.count; //删除一行就进行分数就进行累加 System.out.println("正在删除满行!"); }else { System.out.println("没有满行,继续游戏!"); } } private void setJiMu() { switch (r1 = r.nextInt(6)) { //随机出0-5六个数,来随机出现一种方块砖 //下面的每种方块都在jimu包中有单独进行配置,就减少了model的累赘,显得比较的整洁 //这里是父类调用子类对象 case 0: jimu = new One(a); break; case 1: jimu = new Tian(a); break; case 2: jimu = new L(a); break; case 3: jimu = new Z(a); break; case 4: jimu = new FZ(a); break; case 5: jimu = new FL(a); break; } } public void changeOne() { //这里的改变,向下,向左,向右都是boolean类型返回值的方法 //只要是为了输出不同的状态而做的,如果不需要,就乐意改成void类型的 //控制改变方块的形状,如果积木的change方法,如果返回为true就改变,并显示积木改变 if (jimu.change()) { System.out.println("积木改变了"); } else { System.out.println("积木没法改变了"); } } public void goDownOne() { //向下走 if (jimu.goDown()) { // System.out.println("积木改变了"); } else { if (jimu.getY() <= 4) { //判断游戏是否结束 System.out.println("游戏结束!!!"); flag = false; } else { setJiMu(); deleteJimu(); // System.out.println("积木没法改变了"); } } } public void goLeftOne() { //向左走 if (jimu.goLeft()) { // System.out.println("积木改变了"); } else { goDownOne(); // System.out.println("积木没法改变了"); } } public void goRightOne() { //向右走 if (jimu.goRight()) { // System.out.println("积木改变了"); } else { goDownOne(); // System.out.println("积木没法改变了"); } } public String Out() { String str = ""; //这里是输出这个数组,因为是一行一行的输出,所以x的循环再里面,先循环 for (int j = 0; j < y; j++) { //这个是y的循环,在x循环完一次之后,y加1 for (int i = 0; i < x; i++) { //在y每加1的时候,循环一整个x的元素 str += a[i][j]; //这是输出语句 } str += "\n"; //这是换行,用于输出完一行之后换行再输出下一行 } return str; } }还有model需要用到积木包中的各个方块的类在下一篇博文中展示,因为实在太多了。。。
相关文章推荐
- 俄罗斯方块(flash游戏制作)
- 第 8 章 哭泣吧!现在才开始讲基础问题
- GTD---时间管理 从现在开始
- (这个是根据例题改的)6 试写函数:1 打印当前时间年 2 打印当前时候月 3打印当前时间日 4 打印当前时间星期几 5 打印当前时间小时 6 打印当前时间分 7 打印当前时间秒
- 现在开始废止这个博客空间,转用新浪的博客!
- 申请这个BLOG好长时间了,现在开始进行使用,呵呵!
- 开始时间小于结束时间 WdatePicker这个控件是如何处理的
- 制作单机俄罗斯方块游戏心得(二)
- 制作单机俄罗斯方块游戏总结(一)
- 黑马程序员之C#编程基础学习笔记:不断要求用户输入一个数字,然后打印这个数字的二倍,当用户输入q的时候程序退出。
- 第 8 章 哭泣吧!现在才开始讲基础问题
- 通过俄罗斯方块浅谈小游戏制作(序)
- 现在开始看《把时间当做朋友这本书》
- 单机俄罗斯方块游戏制作心得(四)
- vc项目开发:俄罗斯方块制作日志 [上]
- 我儿子现在读一年级,到现在读了3个月,这个是今天老婆叫我给他打印的英语卡片,晕啊
- 从现在开始,格外珍惜时间,让生活充实起来
- 设计俄罗斯方块游戏之时间——计算机达人成长之路(14)连载
- shell脚本制作俄罗斯方块游戏
- <Unity3D>使用Unity来制作俄罗斯方块游戏