作者:雷神
QQ:38929568
QQ群:28048051JAVA游戏编程(满) 28047782(满) 50761834(新开)

声明:本人发表的代码全部可以用来学习,如果需要作商业用途,请及时与作者联系。

本例为J2SE游戏开发的经典游戏--俄罗斯方块,

方向键 上下左右ASDW2468都可以游戏,0更换方块,1显示隐藏网格!game over 时5键重新开始,*键退出游戏

游戏图片: 

JAVA游戏编程之三----j2me 手机游戏入门开发--俄罗斯方块_2_j2me

工程文件如附件所示,地址如下:方块2

代码如下:

  1. package code;  
  2. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  3. /** *//**  
  4.  * 俄罗斯方块  
  5.  * 高雷  
  6.  * 2007年11月30日  
  7.  */ 
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  9. import javax.microedition.midlet.*;    //j2me MIDlet程序必须继承MIDlet类,所以要引入此包  
  10. import javax.microedition.lcdui.*;    //Display这个类所在包  
  11.  
  12. ///////////////////////////////////////////////////////////////////////////////////////////////////  
  13.  
  14. public class Tetris extends MIDlet   
  15. ...{  
  16.     static Tetris s_midlet;    //MIDlet类的静态对象,方便实用 MIDlet类方法  
  17.     static Display s_display = null;//用来显示 Canvas  
  18.     static cGame s_game = null;        //Canvas类对象,主要实现游戏的类  
  19.       
  20.     public Tetris()  
  21.     ...{  
  22.         s_midlet = this;  
  23.     }  
  24.       
  25.     /** *//**  
  26.      * 程序开始 系统会调用这个函数  
  27.      * 也有些手机 可以把程序初始化部分放到构造函数里,这连个地方应视手机的不同而定!  
  28.      */ 
  29.     public void startApp()               
  30.     ...{  
  31.         if (s_display == null)   
  32.         ...{  
  33.             s_display = Display.getDisplay(this);//创建Display对象,参数是MIDlet类对象,也就是我们当前写的这个Minesweeper类  
  34.         }  
  35.  
  36.         if (s_game == null)   
  37.         ...{  
  38.             s_game = new cGame();                //创建 Canvas对象  
  39.             s_display.setCurrent(s_game);        //把Canvas对象设置成当前显示  
  40.         }   
  41.         else   
  42.         ...{  
  43.             s_display.setCurrent(s_game);  
  44.         }  
  45.     }  
  46.  
  47.     /** *//**  
  48.      * 程序暂停 系统会自动调用这个函数,不是所有手机都支持,  
  49.      * 手机在接到中断,如 来电,来短信时候会调用这个函数,这个函数 通常是空的!  
  50.      */ 
  51.     public void pauseApp()           
  52.     ...{  
  53.           
  54.     }  
  55.  
  56.     /** *//**  
  57.      * 程序关闭 系统会调用这个函数,如果希望关闭程序的时候保存数据,可在这个函数里添加保存数据的方法  
  58.      * 比如游戏进行中,按了关机键,程序就会调用这个函数,也可以在程序中调用这个函数来结束游戏!  
  59.      */ 
  60.     public void destroyApp(boolean unconditional)   
  61.     ...{  
  62.         notifyDestroyed();  
  63.     }  

程序主要逻辑代码类

  1. package code;  
  2.  
  3. //import java.awt.*;  
  4. //import java.awt.Canvas;  
  5. //import java.awt.event.*;  
  6. //import javax.swing.*;  
  7. import java.util.Random;  
  8. import javax.microedition.lcdui.*;            //写界面所需要的包  
  9. /** *//**  
  10.  * 俄罗斯方块  
  11.  * 高雷  
  12.  * 2007年11月30日  
  13.  */ 
  14. public class cGame extends Canvas implements Runnable   
  15. ...{  
  16.     private Random rand;  
  17.     private Thread thread;  
  18.     private Graphics    gb;  
  19.     private Image        buffer;  
  20.     private Image         gameOverImg;                    //游戏结束  
  21.     private static final int s_width      = 240;  
  22.     private static final int s_height     = 320;  
  23.     private static final int s_box_w      = 16;  
  24.     private static final int s_box_h      = 16;  
  25.     private static final int s_box_w_sum     = 10;        //操作区域宽 格子数  
  26.     private static final int s_box_h_sum     = 20;        //操作区域高 格子数  
  27.     private static final int s_line_between_x = s_box_w * s_box_w_sum;//分割线x位置  
  28.  
  29.     public static final int  UP     = -1;  
  30.     public static final int  DOWN     = -2;  
  31.     public static final int  LEFT     = -3;  
  32.     public static final int  RIGHT     = -4;  
  33.       
  34.     public static final int  init_x         = 3;        //当前方块初始化坐标X  
  35.     public static final int  init_y         = 0;        //当前方块初始化坐标y  
  36.       
  37.     public static int     s_box_x                = init_x;    //当前方块坐标X  
  38.     public static int     s_box_y                = init_y;    //当前方块坐标Y  
  39.     private static int    level                = 1;        //等级  
  40.     private static int    success                = 0;        //得分  
  41.     private static long goDownDelayTime[]    = //1800;    //下降延迟时间  
  42.     ...{  
  43.         1000,    900,    800,    700,  
  44.         600,    500,    400,  
  45.         300,    200,    100 
  46.     };  
  47.     private static int    level_up            = (int)(goDownDelayTime[0]-goDownDelayTime[level]);        //升级成绩  
  48.     private static boolean isShowReseau        = true;        //是否现实网格  
  49.     private static short s_next_box            = 0;        //下一个方块编号  
  50.     private static short boxColor;                        //当前box的颜色  
  51. //    private static final Color gameBG        = new Color( 0x333333 );    //游戏区域背景颜色  
  52.     private static final int gameBG            = 0x333333;    //游戏区域背景颜色  
  53. //    private static final Color gameColor[]    = new Color[]  
  54.     private static final int gameColor[]    = new int[]  
  55.     ...{  
  56.         0x444444,    //new Color( 0x444444 ),    //网格颜色  
  57.         0xEEEEEE,    //new Color( 0xEEEEEE ),    //方块颜色  
  58.         0xEE0000,    //new Color( 0xEE0000 ),  
  59.         0x00EE00,    //new Color( 0x00EE00 ),  
  60.         0x0000EE,    //new Color( 0x0000EE ),  
  61.         0xEE00EE,    //new Color( 0xEE00EE ),  
  62.         0xEEEE00,    //new Color( 0xEEEE00 ),  
  63.         0x00EEEE     //new Color( 0x00EEEE )  
  64.     };  
  65.     private static final short box_sum[][] = new short[][]    //所有方块图形  
  66.     ...{  
  67.         ...{ 0x06600x06600x06600x0660 },  
  68.         ...{ 0x22220x00F00x22220x00F0 },  
  69.         ...{ 0x02640x06300x02640x0630 },  
  70.         ...{ 0x04620x03600x04620x0360 },  
  71.         ...{ 0x02E00x44600x07400x0622 },  
  72.         ...{ 0x0E200x22600x04700x0644 },  
  73.         ...{ 0x04640x00E40x04C40x04E0 }  
  74.     };  
  75.  
  76.     private static short next_box[] = new short[]...{ 0x06600x06600x06600x0660 };   
  77.     private static short box[]          = new short[]...{ 0x06600x06600x06600x0660 };  
  78.     private static short map[][];        //地图  
  79.     private static short box_state  = 0;//当前BOX的状态//旋转方向  
  80.     private static short matrix[][] =    //定义矩阵用来计算出box_sum的方块  
  81.     ...{  
  82.         ...{ 0x10000x01000x00100x0001 },  
  83.         ...{ 0x20000x02000x00200x0002 },  
  84.         ...{ 0x40000x04000x00400x0004 },  
  85.         ...{ (short)0x80000x08000x00800x0008 }  
  86.     };  
  87.  
  88.     public cGame()  
  89.     ...{  
  90.         setFullScreenMode(true);        //设置游戏为全屏幕模式,该函数只能在支持midp2.0的手机上使用  
  91. //        s_width = getWidth();            //得到屏幕尺寸     宽  
  92. //        s_height= getHeight();            //得到屏幕尺寸     高  
  93.         rand = new Random( System.currentTimeMillis() );  
  94.         try 
  95.         ...{  
  96.             //gameOverImg = Toolkit.getDefaultToolkit().getImage("src/pics/laser.png");  
  97.             gameOverImg = Image.createImage("/pics/laser.png");  
  98.         }catch(Exception e)...{}  
  99.         //setSize( s_width, s_height );    //设置画布  
  100.         initGame();                        //游戏初始化  
  101.         thread  = new Thread(this);  
  102.         thread.start();  
  103.     }  
  104.       
  105.     private void initGame()  
  106.     ...{  
  107.         level        = 1;                //等级  
  108.         success        = 0;                //得分  
  109.         map     = new short[s_box_h_sum][s_box_w_sum];  
  110.         setNextBox();                    //设置下一个BOX  
  111.         setBox();                        //将下一个BOX设置成当前BOX  
  112.         setGameOver( false );            //恢复游戏  
  113.     }  
  114.       
  115.     private void setBox()                //将next_box设置成当前可控制box  
  116.     ...{  
  117.         box_state         = 0;                                        //box 状态  
  118.         s_box_x            = init_x;                                    //当前方块坐标X  
  119.         s_box_y            = init_y;                                    //当前方块坐标Y  
  120.         boxColor        = s_next_box;                                //设置当前BOX颜色  
  121.         System.arraycopy( next_box, 0, box, 0, next_box.length );    //box = next_box  
  122.         goDownPreTime     = System.currentTimeMillis();                //设置好当前BOX后 计时  
  123.         setNextBox();                                                //设置下一个BOX  
  124.         if( !isCanMove() )  
  125.         ...{  
  126.             setGameOver( true );  
  127.         }  
  128.     }  
  129.  
  130.     public static boolean isGameOver = false;  
  131.     public static long updatas     = 0;  
  132.     public static long fps         = 0;  
  133.     private long     startTime, beginTime, endTime;  
  134.     private long     delay         = 25;  
  135.     private long     upTime         = 25;  
  136.     public void run()   
  137.     ...{  
  138.         while ( true )   
  139.         ...{  
  140.             try 
  141.             ...{  
  142.                 beginTime = System.currentTimeMillis();  
  143.                 updatas++;  
  144.                 updata( updatas );  
  145.                 repaint();  
  146.                 endTime = System.currentTimeMillis();  
  147.                 upTime  = endTime-beginTime;  
  148.                 if( upTime<delay )  
  149.                 ...{  
  150.                     fps = 1000/delay;  
  151.                     thread.sleep(delay-upTime);  
  152.                 }  
  153.                 else 
  154.                     fps = 1000/upTime;  
  155.             }catch(Exception e)...{ }  
  156.         }  
  157.     }  
  158.     void setGameOver( boolean _isGameOver )  
  159.     ...{  
  160.         isGameOver = _isGameOver;  
  161.     }  
  162.     public void updata( long updatas )  
  163.     ...{  
  164.  
  165.     }  
  166.     public void update(Graphics g)   
  167.     ...{  
  168.         paint(g);  
  169.     }  
  170.     public static int     offx     = 0;  
  171.     public static int     offy     = 0;  
  172.     public void paint(Graphics g)  
  173.     ...{  
  174.         try 
  175.         ...{  
  176.             if( buffer == null )  
  177.             ...{  
  178.                 buffer = Image.createImage( s_width, s_height );    //设置画布缓冲区  
  179.                 gb = buffer.getGraphics();                    //得到绘图设备  
  180.             }  
  181. //            gb.translate( offx, offy );  
  182. //            gb.setColor( new Color( 0x0 ) );                //初始化 画布颜色  
  183.             gb.setColor( 0x0 );                                //初始化 画布颜色  
  184.             gb.setClip ( 00, s_width, s_height);            //初始化 画布区域  
  185.             gb.fillRect( 00, s_width, s_height);            //初始化 画布填充  
  186.             paintReseau( gb );                                //绘制网格  
  187.             paintNextBox( gb );                                //绘制下一BOX  
  188.             paintMap( gb );                                    //绘制地图上不可以动BOX  
  189.             paintBox( gb, s_box_x, s_box_y );                //绘制当前可控制BOX  
  190. //            gb.setColor( new Color( 0xFF3333 ) );            //分割线颜色  
  191.             gb.setColor( 0xFF3333 );                        //分割线颜色  
  192.             gb.drawLine( s_line_between_x, 0, s_line_between_x, s_height );    //分割线  
  193. //            gb.drawString( "FPS:"+fps,             s_line_between_x+10,10 );    //祯数  
  194. //            gb.drawString( "等级:"+level,         s_line_between_x+10,30 );    //等级  
  195. //            gb.drawString( "得分:"+success,         s_line_between_x+10,50 );    //分数  
  196.             gb.drawString( "FPS:"+fps,             s_line_between_x+1010, g.TOP|g.LEFT );//祯数  
  197.             gb.drawString( "等级:"+level,         s_line_between_x+1030, g.TOP|g.LEFT );//等级  
  198.             gb.drawString( "得分:"+success,         s_line_between_x+1050, g.TOP|g.LEFT );//分数  
  199.             if( isGameOver )  
  200.             ...{  
  201. //                gb.drawImage( gameOverImg, (getWidth()-offx-gameOverImg.getWidth(null))/2, (getHeight()-gameOverImg.getHeight(null))/2 , null );  
  202.                 gb.drawImage( gameOverImg,    s_width>>1, s_height>>1, g.HCENTER|g.VCENTER );  
  203.             }  
  204. //            gb.translate( -offx, -offy );  
  205.         }  
  206.         catch(Exception e)  
  207.         ...{   
  208.             System.out.println("err at paint.e====="+e);  
  209.         }  
  210. //        g.drawImage( buffer, offx, offy, null);                //将画布缓冲区绘制到屏幕//偏移 (2,2)  
  211.         g.drawImage( buffer, offx, offy, 0);                //将画布缓冲区绘制到屏幕//偏移 (2,2)  
  212.     }  
  213.       
  214.     private void paintReseau( Graphics g )                    //绘制网格  
  215.     ...{  
  216.         g.setColor( gameBG );  
  217.         g.fillRect( 00, s_line_between_x, s_height );  
  218.         if( isShowReseau )  
  219.         ...{  
  220.             g.setColor( gameColor[0] );  
  221.             forint i=0; i<s_line_between_x/s_box_w; i++ )    // |  
  222.             ...{  
  223.                 g.drawLine( i*s_box_h, 0, i*s_box_h, s_height );  
  224.             }  
  225.             forint j=0; j<s_height/s_box_h; j++ )            // -  
  226.             ...{  
  227.                 g.drawLine( 0, j*s_box_w, s_line_between_x, j*s_box_w );  
  228.             }  
  229.         }  
  230.     }  
  231.     private void paintBox( Graphics g, int off_x, int off_y )  
  232.     ...{  
  233.         forint i=0; i<4; i++ )        //行  
  234.         ...{  
  235.             forint j=0; j<4; j++ )    //列  
  236.             ...{  
  237.                 if( (box[box_state] & matrix[i][j]) == matrix[i][j] )  
  238.                 ...{  
  239.                     g.setColor( gameColor[ boxColor ] );  
  240.                     g.fillRect( (off_x+j)*s_box_w, (off_y+i)*s_box_h, s_box_w, s_box_h );  
  241.  
  242.                     g.setColor( gameBG );  
  243.                     g.drawRect( (off_x+j)*s_box_w+1, (off_y+i)*s_box_h+1, s_box_w-2, s_box_h-2 );  
  244.                 }  
  245.             }  
  246.         }  
  247.         goDown();                        //BOX是否下降  
  248.     }  
  249.     private void paintNextBox( Graphics g )  
  250.     ...{  
  251.         int off_x = s_line_between_x+( s_width - s_line_between_x - 4*s_box_w )/2;  
  252.         int off_y = s_height/2;  
  253.           
  254.         g.translate( off_x, off_y );  
  255.         g.setColor( gameBG );  
  256.         g.fillRect( 004*s_box_w, 4*s_box_h );  
  257.           
  258.         if( isShowReseau )                //显示格式  
  259.         ...{  
  260.             g.setColor( gameColor[0] );  
  261.             forint i=0; i<5; i++ )    // |  
  262.             ...{  
  263.                 g.drawLine( i*s_box_h, 0, i*s_box_h, 4*s_box_h );  
  264.             }  
  265.             forint j=0; j<5; j++ )    // -  
  266.             ...{  
  267.                 g.drawLine( 0, j*s_box_w, 4*s_box_w, j*s_box_w );  
  268.             }  
  269.         }  
  270.         forint i=0; i<4; i++ )        //行  
  271.         ...{  
  272.             forint j=0; j<4; j++ )    //列  
  273.             ...{  
  274.                 if( (next_box[0] & matrix[i][j]) == matrix[i][j] )  
  275.                 ...{  
  276.                     g.setColor( gameColor[ s_next_box ] );  
  277.                     g.fillRect( j*s_box_w, i*s_box_h, s_box_w, s_box_h );  
  278.  
  279.                     g.setColor( gameBG );  
  280.                     g.drawRect( j*s_box_w+1, i*s_box_h+1, s_box_w-2, s_box_h-2 );  
  281.                 }  
  282.             }  
  283.         }  
  284.         g.translate( -off_x, -off_y );  
  285.     }  
  286.  
  287.     private long goDownPreTime     = 0;    //上次下降时间  
  288.     private long currTime         = 0;    //当前时间  
  289.     private void goDown()                //当前BOX下降  
  290.     ...{  
  291.         if( isGameOver )    //游戏结束  
  292.             return;  
  293.         //isKeyDown按了向下移动就需要检查 不需要时间  
  294.         if( isKeyDown==1 || System.currentTimeMillis() - goDownPreTime >= goDownDelayTime[level] )  
  295.         ...{  
  296.             s_box_y++;  
  297.             goDownPreTime = System.currentTimeMillis();  
  298.             if( !isCanMove() )  
  299.             ...{  
  300.                 isKeyDown = 0;    //没有按下  
  301.                 s_box_y--;  
  302.                 setMap();        //将BOX放进map   
  303.                 setBox();        //新的BOX  
  304.             }  
  305.         }  
  306.     }  
  307.       
  308.     private void setMap()  
  309.     ...{  
  310.         forint i=0; i<4; i++ )        //行  
  311.         ...{  
  312.             forint j=0; j<4; j++ )    //列  
  313.             ...{  
  314.                 if( ( box[box_state] & matrix[i][j] ) == matrix[i][j] )    //是格子  
  315.                 ...{  
  316.                     map[s_box_y+i][s_box_x+j] = boxColor;  
  317.                 }  
  318.             }  
  319.         }  
  320.         //检测是否可以消去一行  
  321.         int line_success = 0;  
  322.         forint i=0; i<s_box_h_sum; i++ )        //行  
  323.         ...{  
  324.             if( isFullLine( i ) )                //这行可以消去  
  325.             ...{  
  326.                 setNullLine( i );                //设置第i行为空  
  327.                 setGoDownMap( i );                //地图第i行以上的向下移动一行  
  328.                 line_success++;  
  329.             }  
  330.         }  
  331.         success += line_success*line_success;    //设置得分  
  332.         level_up = (int)(goDownDelayTime[0]-goDownDelayTime[level]);  
  333.         if( success >= level_up )                //设置升级  
  334.         ...{  
  335.             level %= goDownDelayTime.length;  
  336.             level ++;  
  337.         }  
  338.     }  
  339.  
  340.     private void paintMap( Graphics g )  
  341.     ...{  
  342.         forint i=0; i<s_box_h_sum; i++ )        //行  
  343.         ...{  
  344.             forint j=0; j<s_box_w_sum; j++ )    //列  
  345.             ...{  
  346.                 if( map[i][j] > 0 )                //是格子//绘制格子  
  347.                 ...{  
  348.                     g.setColor( gameColor[ map[i][j] ] );  
  349.                     g.fillRect( j*s_box_w, i*s_box_h, s_box_w, s_box_h );      
  350.                       
  351.                     g.setColor( gameBG );  
  352.                     g.drawRect( j*s_box_w+1, i*s_box_h+1, s_box_w-2, s_box_h-2 );  
  353.                 }  
  354.             }  
  355.         }  
  356.     }  
  357.       
  358.     private boolean isFullLine(int line)    //是否一行已经满了  
  359.     ...{  
  360.         forint j=0; j<s_box_w_sum; j++ )    //列  
  361.         ...{  
  362.             if( map[line][j] <= 0 )  
  363.             ...{  
  364.                 return false;  
  365.             }  
  366.         }  
  367.         return true;  
  368.     }  
  369.       
  370.     private void  setNullLine( int line )    //设置地图上的这一行 空  
  371.     ...{  
  372.         forint j=0; j<s_box_w_sum; j++ )    //列  
  373.         ...{  
  374.             map[line][j] = 0;  
  375.         }      
  376.     }  
  377.     private void setGoDownMap( int line )    //设置地图line以上的每行都向下移动一行  
  378.     ...{  
  379.         forint i=line; i>0; i-- )            //行  
  380.         ...{  
  381.             forint j=0; j<s_box_w_sum; j++ )    //列  
  382.             ...{  
  383.                 map[i][j] = map[i-1][j];     //向下移动一行  
  384.             }  
  385.         }  
  386.     }  
  387.       
  388.     private boolean isCanMove()  
  389.     ...{  
  390.         forint i=0; i<4; i++ )        //行  
  391.         ...{  
  392.             forint j=0; j<4; j++ )    //列  
  393.             ...{  
  394.                 if( ( box[box_state] & matrix[i][j] ) == matrix[i][j] )    //是格子  
  395.                 ...{  
  396.                     if( s_box_x+j < 0 )                //左边界检测  
  397.                     ...{  
  398.                         System.out.println( "left s_box_x="+s_box_x+" matrix["+i+"]["+j+"]="+matrix[i][j]);  
  399.                         return false;  
  400.                     }  
  401.                     if( s_box_x+j > s_box_w_sum-1 )    //右边界检测  
  402.                     ...{  
  403.                         System.out.println( "right s_box_x="+s_box_x+" matrix["+i+"]["+j+"]="+matrix[i][j]);  
  404.                         return false;  
  405.                     }  
  406.                     if( s_box_y+i > s_box_h_sum-1 )    //下边界检测  
  407.                     ...{  
  408.                         System.out.println( "down s_box_y="+s_box_y+" matrix["+i+"]["+j+"]="+matrix[i][j]);  
  409.                         return false;  
  410.                     }  
  411.                     //地图格子检测  
  412.                     if( map[s_box_y+i][s_box_x+j] > 0 )  
  413.                         return false;  
  414.                 }  
  415.             }  
  416.         }  
  417.         return true;  
  418.     }  
  419.     private short isKeyDown = 0;    //0没有按下,1按下,2抬起  
  420. //    public boolean keyDown(Event evt, int key)  
  421.     public void keyPressed( int key )  
  422.     ...{  
  423.         key = getKeyCode( key );  
  424.         switch( key )  
  425.         ...{  
  426.             case UP:                //顺时针旋转  
  427.                 isKeyDown = 0;        //0没有按下  
  428.                 box_state ++;  
  429.                 box_state %= 4;  
  430.                 if( !isCanMove() )  
  431.                 ...{  
  432.                     box_state --;  
  433.                     if( box_state<0 )  
  434.                         box_state = 3;  
  435.                 }  
  436.             break;  
  437.             case DOWN:                //向下移动  
  438.                 if( isKeyDown == 2 )  
  439.                     isKeyDown = 1;  
  440.                 if( isKeyDown == 1 )  
  441.                 ...{  
  442.                     s_box_y ++;   
  443.                     if( !isCanMove() )  
  444.                         s_box_y --;  
  445.                 }  
  446.             break;  
  447.             case LEFT:                //向左移动BOX  
  448.                 isKeyDown = 0;        //0没有按下  
  449.                 s_box_x --;  
  450.                 if( !isCanMove() )  
  451.                     s_box_x ++;      
  452.             break;  
  453.             case RIGHT:                //向右移动BOX  
  454.                 isKeyDown = 0;        //0没有按下  
  455.                 s_box_x ++;  
  456.                 if( !isCanMove() )  
  457.                     s_box_x --;  
  458.             break;  
  459.             case 53:                //数字5键  
  460.                 if( isGameOver )    //游戏结束  
  461.                     initGame();        //重新游戏  
  462.             break;  
  463.             case 42:  
  464.                 if( isGameOver )    //游戏结束  
  465. //                    System.exit(0);    //退出游戏  
  466.                     Tetris.s_midlet.destroyApp(true);  
  467.             break;  
  468.             case 48:  
  469.                 setBox();            //新的BOX  
  470.             break;  
  471.             case 49:                //是否显示网格  
  472.                 isShowReseau = !isShowReseau;  
  473.             break;  
  474.         }  
  475.         repaint();                    //重新绘制屏幕  
  476. //        return true;  
  477.     }  
  478.     public void keyRepeated( int key )  
  479.     ...{  
  480.         keyPressed( key );  
  481.     }  
  482.     public void setNextBox()  
  483.     ...{  
  484.         s_next_box = (short)rand.nextInt( box_sum.length );  
  485.         System.arraycopy( box_sum[s_next_box], 0, next_box, 0, next_box.length );  
  486.         s_next_box++;  
  487.     }  
  488.  
  489.     public int getKeyCode( int key )  
  490.     ...{  
  491.         System.out.println( "key="+key );  
  492.         switch( key )  
  493.         ...{  
  494.             case 1004:     // up  
  495.             case 119:     // w  
  496.             case 87:     // W  
  497.             case 50:     // 2  
  498.             return UP;  
  499.  
  500.             case 1005:     // down  
  501.             case 115:     // s  
  502.             case 83:     // S  
  503.             case 56:     // 8  
  504.             return DOWN;  
  505.  
  506.             case 1006:     // left  
  507.             case 97:     // a  
  508.             case 65:     // A  
  509.             case 52:     // 4  
  510.             return LEFT;  
  511.  
  512.             case 1007:     // right  
  513.             case 100:     // d  
  514.             case 68:     // D  
  515.             case 54:     // 6  
  516.             return RIGHT;  
  517.             default:  
  518.             return key;  
  519.         }  
  520.     }  
  521. //    public boolean keyUp(Event evt, int key)   
  522.     public void keyReleased( int key )   
  523.     ...{  
  524.         isKeyDown = 2;    //释放按键  
  525. //        return true;  
  526.     }  
  527. //    public boolean mouseDown(Event evt, int x, int y)  
  528. //    {  
  529. //        try  
  530. //        {  
  531. ////            System.out.println( "x="+x+" y="+y );  
  532. //        }catch( Exception e){e.printStackTrace();}  
  533. ////        this.repaint();  
  534. //        return true;  
  535. //    }  
  536. //    public boolean mouseMove(Event evt, int x, int y)  
  537. //    {  
  538. //        try  
  539. //        {  
  540. //            //System.out.println( "x="+x+" y="+y );  
  541. //        }catch( Exception e){e.printStackTrace();}  
  542. //        return true;  
  543. //    }  
  544. //    public static void main(String[] args)   
  545. //    {  
  546. //        JFrame frame = new JFrame("俄罗斯方块 北京|雷神 QQ:38929568");  
  547. //        final cGame dc = new cGame();  
  548. //        frame.getContentPane().add(dc, BorderLayout.CENTER);  
  549. //  
  550. ////        JButton button = new JButton("刷新");  
  551. ////        button.addActionListener(new ActionListener()   
  552. ////        {  
  553. ////            public void actionPerformed(ActionEvent e)   
  554. ////            {  
  555. ////                dc.repaint();  
  556. ////            }  
  557. ////        });  
  558. ////        frame.getContentPane().add(button, BorderLayout.SOUTH);  
  559. //        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  560. //        frame.setSize(dc.s_width+10, dc.s_height+30);  
  561. //        frame.setVisible(true);  
  562. //    }