Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 343|回复: 0

[Java游戏学习]Applets开发的扫雷游戏

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-12 23:50:57 | 显示全部楼层 |阅读模式
    Applets开发的扫雷游戏,可在线玩。来自:
    http://www.javaside.com/asp/mus.asp?page=/us/JVMine.sHTML
    这是启动游戏的html文件
    <html>
    <head><title>RBL - JVMine V 1.02 - Java</title>
      <applet code=JVMine.class width=450 height=300 ID="JMine" NAME="JMine">
    <param name=cabbase value="./cab/jvmine.cab">
    <param name=bImg value="./img/j">
    <param name=bgcolor value=12632256>
    <param name=_nbmine value=10>
    <param name=_nbcol value=8>
    <param name=_nbligne value=8>
    <param name=fontsize value=16>
    </applet></html>  
      
      
      
       
      
      
    游戏源码:
    1. //******************************************************************************
    2. // -----------------------------------------------------------
    3. // JVMine.java                                 
    4. // -----------------------------------------------------------
    5. //   Main Applet class for Demineur JVMine Applet
    6. // -----------------------------------------------------------
    7. // Author : R. BERTHOU
    8. // E-Mail : rbl@berthou.com
    9. // URL    : http://www.javaside.com
    10. // -----------------------------------------------------------
    11. // Ver  * Author     *  DATE    * Description
    12. // ....................DD/MM/YY...............................
    13. // 1.02 * R. BERTHOU * 28/01/99 * Change mail address
    14. // 1.01 * R. BERTHOU * 28/08/97 * Correct minor bug
    15. // 1.00 * R. BERTHOU * 31/07/97 * Creation
    16. //******************************************************************************
    17. import java.applet.Applet;
    18. import java.util.Random ;
    19. import java.awt.*;
    20. import java.net.URL;
    21. import java.net.MalformedURLException;
    22. //==============================================================================
    23. // Main Class
    24. //==============================================================================
    25. public class JVMine extends  java.applet.Applet
    26. {
    27.         //--------------------------------------------------------------------------
    28.         private int[]            bStr ;
    29.         private int                        iEtat ;
    30.         private int                        iCpt ;
    31.         private        Image                buffer;
    32.         private        Graphics        offScreen;
    33.         private long                startTick ;
    34.         private Image[]                img ;               
    35.         private int aWidth, aHeight;
    36.         private int                m_bgcolor = 0 ;
    37.         public  int                m_mine = 99 ;
    38.         public  int                m_ligne = 16 ;
    39.         public  int                m_col = 30 ;
    40.         private int     iMax ;
    41.         // Font control variables
    42.         private Font wFont;
    43.         private FontMetrics wMetrics;
    44.         private int         iPas, iX ;
    45.     //--------------------------------------------------------------------------
    46.         public String getAppletInfo()
    47.         {
    48.                 return "Name: JVMine V 1.02
    49. " +
    50.                        "Author: R. BERTHOU
    51. " +
    52.                            "E-Mail : rbl@berthou.com
    53. " +
    54.                            "URL : http://www.javaside.com/" ;
    55.         }
    56.     //--------------------------------------------------------------------------
    57.         public String[][] getParameterInfo()
    58.         {
    59.                 String[][] info =
    60.                 {
    61.                         { "bgImg",        "String",        "bgImage" },
    62.                         { "bgcolor", "int",                "bgColor" },
    63.                         { "nbmine",  "int",                "nb de mine" },
    64.                         { "nbcol",         "int",                "nb de col" },
    65.                         { "nbligne", "int",                "nb de ligne" },
    66.                         { "font",        "String",        "Font Name" },
    67.                         { "fontsize", "int",        "Font Size" },
    68.                 };
    69.                 return info;               
    70.         }
    71.     //--------------------------------------------------------------------------
    72.         // The init() method is called by the AWT when an applet is first loaded or
    73.         // reloaded.  Override this method to perform whatever initialization your
    74.         // applet needs, such as initializing data structures, loading images or
    75.         // fonts, creating frame windows, setting the layout manager, or adding UI
    76.         // components.
    77.     //--------------------------------------------------------------------------
    78.         public void init()
    79.         {
    80.                 String param;
    81.                 try {
    82.                         aWidth = size().width;
    83.                         aHeight = size().height;
    84.                         buffer = createImage (aWidth, aHeight);
    85.                         offScreen = buffer.getGraphics ();
    86.                 } catch (Exception e) {
    87.                         offScreen = null;
    88.                 }
    89.                 // nbMine : nombre de mines
    90.                 //----------------------------------------------------------------------
    91.                 param = getParameter("nbmine");
    92.                 if (param != null)
    93.                         m_mine = Integer.parseInt(param);
    94.                 // nbcol : nombre de colones
    95.                 //----------------------------------------------------------------------
    96.                 param = getParameter("nbcol");
    97.                 if (param != null)
    98.                         m_col = Integer.parseInt(param);
    99.                 // nbligne : nombre de colones
    100.                 //----------------------------------------------------------------------
    101.                 param = getParameter("nbligne");
    102.                 if (param != null)
    103.                         m_ligne = Integer.parseInt(param);
    104.                 // bgcolor: Couleur du fond
    105.                 //----------------------------------------------------------------------
    106.                 param = getParameter("bgcolor");
    107.                 if (param != null)
    108.                         m_bgcolor = Integer.parseInt(param);
    109.                 MediaTracker  trk = new MediaTracker(this) ;
    110.                 // bImg: Bouton Image
    111.                 //----------------------------------------------------------------------
    112.                 param = getParameter("bImg");
    113.                 img = new Image[13];
    114.                 if (param != null) {
    115.                         for (int i = 0 ; i < 13 ; i++) {
    116.                             img[i] = getImage(getCodeBase(), param + (i) + ".gif");
    117.                                 trk.addImage(img[i], 0) ;
    118.                         }
    119.                 }
    120.                 else
    121.                         for (int i = 0 ; i < 13 ; i++)
    122.                             img[i] = null ;
    123.                 try          { trk.waitForAll() ; }
    124.                 catch (InterruptedException e) {        }
    125.                 // Font
    126.                 //----------------------------------------------------------------------
    127.                 param = getParameter("fontsize");
    128.                 int iSize = 12 ;
    129.                 if (param != null)
    130.                         iSize = Integer.parseInt(param);
    131.                 param = getParameter("font");
    132.                 if (param == null)
    133.                         param = new String("Arial") ;
    134.                 wFont = new Font(param, Font.BOLD, iSize);
    135.                 if (wFont == null) wFont = getFont() ;
    136.                 wMetrics = getFontMetrics(wFont);
    137.                 iPas = wMetrics.getHeight() * 2 ;
    138.                 newGame() ;
    139.                        
    140.   }
    141.   //----------------------------------------------------------------------
    142.   
    143.   // start :                                                   random
    144.   //----------------------------------------------------------------------
    145.   public void newGame() {
    146.             Random a ;
    147.                 int i = 0 ;
    148.                 int j = 0 ;
    149.                 int iE ;
    150.                 a = new Random() ;
    151.                 iEtat = 0 ;
    152.                 iCpt = m_mine ;
    153.                 int k = 0 ;
    154.                 iMax = m_ligne*m_col ;
    155.                 bStr = new int[iMax] ;
    156.                 iX = (aWidth - (m_col * 15)) / 2 ;
    157.                 if ( (m_col * 15) > aWidth ) iX = 0 ;
    158.                 for (i = 0 ; i < iMax; i++) bStr[i] = 10 ;
    159.                 // calculate the tick to wait for
    160.                 startTick = -1 ;
    161.                 i = 0 ;
    162.                 while ( i < m_mine) {
    163.                         j = (int)(iMax * a.nextDouble()) ;
    164.                         if ( (j < iMax) && (bStr[j] != 19 ) ) {
    165.                                 iE = j % m_col ;
    166.                                 bStr[j] = 19 ;
    167.                                 i++ ;
    168.                                 if (iE > 0) { // pas la 1er colonne
    169.                                         k = j-1-m_col ;        // -31
    170.                                         if ( k >= 0 )  
    171.                                                 if ( bStr[k] != 19 ) bStr[k] += 1 ;
    172.                                         k = j - 1 ;                // -1
    173.                                         if ( k >= 0 )  
    174.                                                 if ( bStr[k] != 19 ) bStr[k] += 1 ;
    175.                                         k = j+m_col-1 ; // +29
    176.                                         if ( k < iMax )  
    177.                                                 if ( bStr[k] != 19 ) bStr[k] += 1 ;
    178.                                 }
    179.                                 k = j - m_col ;
    180.                                 if ( k >= 0 )  
    181.                                         if ( bStr[k] != 19 ) bStr[k] += 1 ;
    182.                                 k = j + m_col ;
    183.                                 if ( k < iMax )  
    184.                                         if ( bStr[k] != 19 ) bStr[k] += 1 ;
    185.                                 if (iE < (m_col-1)) {  // Pas la derniere
    186.                                         k = j-m_col+1 ;                // -29
    187.                                         if ( k >= 0 )  
    188.                                                 if ( bStr[k] != 19 ) bStr[k] += 1 ;
    189.                                         k = j + m_col + 1 ; // +31
    190.                                         if ( k < iMax )  
    191.                                                 if ( bStr[k] != 19 ) bStr[k] += 1 ;
    192.                                         k = j + 1 ;                        // +1
    193.                                         if ( k < iMax )  
    194.                                                 if ( bStr[k] != 19 ) bStr[k] += 1 ;
    195.                                 }
    196.                         }
    197.                 }
    198.                   repaint();
    199.                  
    200.   }
    201.   // start :
    202.   //----------------------------------------------------------------------
    203.   public void start() {
    204.                   repaint();
    205.   }
    206.   // mouseDown :
    207.   //----------------------------------------------------------------------
    208.   public boolean mouseDown(Event e, int x, int y){
    209.           int iC = (x-iX) / 15 ;
    210.           int iL = (y-iPas) / 15 ;
    211.           boolean rep = false ;
    212.           if ( (x > ((aWidth/2)-20)) && (x < ((aWidth/2)+40)) &&
    213.                    (y > (iPas/4))        && (y < (iPas*3/4) ) ) {
    214.                   URL clickDest ;
    215.                     try{
    216.                         clickDest = new URL("http://www.javaside.com/") ;
    217.                         getAppletContext().showDocument(clickDest, "rbl");
    218.                   }catch(MalformedURLException mal){
    219.                         System.out.println("Bad URL!");
    220.                   }
    221.                   return(true) ;
    222.           }
    223.           if (iEtat == 1) {
    224.                   newGame() ;
    225.               repaint() ;
    226.                   return(true);
    227.           }
    228.           if ( (y > iPas) && (x > iX) && (x < aWidth - iX))
    229.           if ( (e.modifiers & Event.META_MASK) == Event.META_MASK) {
    230.                   if ( bStr[(iL*m_col)+iC] > 9 ) {
    231.                         rep = true ;
    232.                         if ( bStr[(iL*m_col)+iC] < 20 ) {
    233.                                 if (iCpt > 0) {
    234.                                         bStr[(iL*m_col)+iC] += 10 ;
    235.                                         iCpt-- ;
    236.                                 }
    237.                                 else
    238.                                         showStatus("Erreur trop de mines...");
    239.                         }
    240.                         else {
    241.                                         bStr[(iL*m_col)+iC] -= 10 ;
    242.                                         iCpt++ ;
    243.                         }
    244.                 }
    245.                 else
    246.                         rep = testCase((iL*m_col)+iC ) ;
    247.                
    248.           }
    249.           else {
    250.                   if ( bStr[(iL*m_col)+iC] > 9) {
    251.                                 if (startTick == -1)
    252.                                           startTick = System.currentTimeMillis() ;
    253.                                 bStr[(iL*m_col)+iC] -= 10 ;
    254.                                 rep = true ;
    255.                                 if (bStr[(iL*m_col)+iC] == 9)
    256.                                         iEtat = 1 ;
    257.                                 if (bStr[(iL*m_col)+iC] == 0)
    258.                                         verifPos( (iL*m_col) + iC) ;
    259.                   }
    260.           }
    261.           if (rep)  repaint() ;
    262.           return(true);
    263.   }
    264.   //----------------------------------------------------------------------
    265.   public void verifPos(int j) {
    266.                 int iE = j % m_col ;
    267.                 int k ;
    268.                 if (iE > 0) {        // Pas la 1ere col
    269.                         k = j - m_col -1 ;        // -31
    270.                         if ( k >= 0 )  
    271.                                 if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
    272.                                         bStr[k] -= 10 ;
    273.                                         if (bStr[k] == 0) verifPos( k ) ;
    274.                             }
    275.                         k = j - 1 ;                        // -1
    276.                         if ( k >= 0 )  
    277.                                 if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
    278.                                         bStr[k] -= 10 ;
    279.                                         if (bStr[k] == 0) verifPos( k ) ;
    280.                             }
    281.                         k = j + m_col - 1 ; // +29
    282.                         if ( k < iMax )  
    283.                                 if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
    284.                                         bStr[k] -= 10 ;
    285.                                         if (bStr[k] == 0) verifPos( k ) ;
    286.                             }
    287.                 }
    288.                 k = j - m_col ;
    289.                 if ( k >= 0 )  
    290.                         if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
    291.                                 bStr[k] -= 10 ;
    292.                                 if (bStr[k] == 0) verifPos( k ) ;
    293.                     }
    294.                 k = j + m_col ;
    295.                 if ( k < iMax )  
    296.                                 if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
    297.                                         bStr[k] -= 10 ;
    298.                                         if (bStr[k] == 0) verifPos( k ) ;
    299.                             }
    300.                 if (iE < (m_col-1)) {        // Pas la derniere
    301.                         k = j - m_col+1 ;        // -29
    302.                         if ( k >= 0 )  
    303.                                 if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
    304.                                         bStr[k] -= 10 ;
    305.                                         if (bStr[k] == 0) verifPos( k ) ;
    306.                             }
    307.                         k = j + m_col + 1 ;        // +31
    308.                         if ( k < iMax )  
    309.                                 if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
    310.                                         bStr[k] -= 10 ;
    311.                                         if (bStr[k] == 0) verifPos( k ) ;
    312.                             }
    313.                         k = j + 1 ;                        // +1
    314.                         if ( k < iMax )  
    315.                                 if (( bStr[k] > 9 ) && ( bStr[k] < 20 ) )  {
    316.                                         bStr[k] -= 10 ;
    317.                                         if (bStr[k] == 0) verifPos( k ) ;
    318.                             }
    319.                 }
    320.   }
    321.   public boolean testCase(int j) {
    322.                 int iE = j % m_col ;
    323.                 int i = 0 ;
    324.                 int k ;
    325.                 if (iE > 0) {                // Pas la 1ere col
    326.                         k = j - m_col - 1 ;   // -31
    327.                         if ( k >= 0 )  
    328.                                 if ( bStr[k] > 19 ) i++ ;
    329.                         k = j - 1 ;                // -1
    330.                         if ( k >= 0 )  
    331.                                 if ( bStr[k] > 19 ) i++ ;
    332.                         k = j + m_col - 1 ;  // +29
    333.                         if ( k < iMax )  
    334.                                 if ( bStr[k] > 19 ) i++ ;
    335.                 }
    336.                 k = j - m_col ;
    337.                 if ( k >= 0 )  
    338.                                 if ( bStr[k] > 19 ) i++ ;
    339.                
    340.                 k = j + m_col ;
    341.                 if ( k < iMax )  
    342.                                 if ( bStr[k] > 19 ) i++ ;
    343.                 if (iE < (m_col-1) ) {
    344.                         k = j - m_col + 1 ;  // -29
    345.                         if ( k >= 0 )  
    346.                                 if ( bStr[k] > 19 ) i++ ;
    347.                         k = j + m_col + 1 ;  // +31
    348.                         if ( k < iMax )  
    349.                                 if ( bStr[k] > 19 ) i++ ;
    350.                         k = j + 1 ;                        // +1
    351.                         if ( k < iMax )  
    352.                                 if ( bStr[k] > 19 ) i++ ;
    353.                 }
    354.                 if (i == bStr[j])  {
    355.                         verifPos(j) ;
    356.                         return true ;
    357.                 }
    358.                 else
    359.                         return false ;
    360.   }
    361.   // paint :  Called when the applet needs to be painted
    362.   //                  calls the flicker free updating system
    363.   //----------------------------------------------------------------------
    364.   public void paint (Graphics g) {
    365.                 update(g);
    366.   } // end of paint
    367.   //----------------------------------------------------------------------
    368.   // update :  Draw the applet without flicker
    369.   //----------------------------------------------------------------------
    370.   public synchronized void update(Graphics g) {
    371.                 if (offScreen!=null) {
    372.                         paintApplet(offScreen);
    373.                         g.drawImage(buffer,0,0,this);
    374.                 } else
    375.                         paintApplet(g);
    376.   } // end of update
    377.   //----------------------------------------------------------------------
    378.   // paintApplet :  Paint the applet into whatever image
    379.   //----------------------------------------------------------------------
    380.   public void paintApplet(Graphics g) {
    381.           int i = 0 ;
    382.           int j = 0 ;
    383.           int a = 0 ;
    384.           int k = 0 ;
    385.           g.setColor( new Color(m_bgcolor) );
    386.           g.fillRect(0, 0, aWidth, aHeight) ;
    387.           for (i = 0 ; i < m_ligne; i++) {
    388.              for (j = 0 ; j < m_col; j++) {
    389.                          a = bStr[(i*m_col)+j] ;
    390.                          if ( (iEtat == 0) && (a == 9) ) iEtat = 1 ;
    391.                          if (iEtat == 1) {
    392.                                  if (a == 19) a = 9 ;
    393.                                  if ((a > 19) && (a != 29) ) {
    394.                                          iEtat = 1 ;
    395.                                          a = 41 ;
    396.                                  }
    397.                          }
    398.                                   
    399.                          if (a > 40) a = 12 ; else
    400.                          if (a > 19) a = 11 ; else
    401.                          if (a > 9) { a = 10 ;        k++; }
    402.                          if (img[a] != null)
    403.                                 g.drawImage(img[a], iX+(j*15), iPas + (i*15) , this) ;
    404.                  }
    405.           }
    406.           long thisTick ;
    407.                 // calculate the tick to wait for
    408.           if (startTick != -1)
    409.                 thisTick = (System.currentTimeMillis() - startTick ) / 1000;
    410.           else
    411.                 thisTick = 0 ;
    412.           g.setColor( new Color(0) );
    413.           g.fill3DRect(iPas,
    414.                                      iPas/4,
    415.                                    iPas,
    416.                                    iPas/2,
    417.                                    true ) ;
    418.           g.fill3DRect(aWidth-iPas-iPas,
    419.                                      iPas/4,
    420.                                    iPas,
    421.                                    iPas/2,
    422.                                    true ) ;
    423.           g.setFont( wFont );
    424.           g.setColor( Color.green );
    425.           g.drawString (" " + iCpt, iPas+5, iPas*2/3 ) ;
    426.           g.drawString (" " + thisTick, aWidth-iPas-iPas+5, iPas*2/3 ) ;
    427.           g.setColor( Color.black );
    428.           if (k == 0) {
    429.                   iEtat = 1 ;
    430.                   g.drawString (" " + iCpt, iPas+5, (iPas*2/3) ) ;
    431.                   g.drawString ("Win", (aWidth/2)-5, (iPas*2/3) ) ;
    432.           }
    433.           else
    434.                   if (iEtat == 1)
    435.                          g.drawString ("Lose", (aWidth/2)-5, (iPas*2/3) ) ;
    436.                   else {
    437.                          g.setColor( new Color(0xC0C0C0) );
    438.                          g.fill3DRect( (aWidth / 2)-20, iPas/4, 40, iPas/2, true) ;
    439.                          g.setColor( new Color(0) );
    440.                          g.drawString ("?", (aWidth/2)-3, (iPas*2/3) ) ;
    441.                   }
    442.   }
    443. }
    复制代码


      
      
       
       

         
       

         
       
      

      

      



                            function TempSave(ElementID)
                            {
                                    CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
                                    CommentsPersistDiv.save("CommentXMLStore");
                            }
                            function Restore(ElementID)
                            {
                                    CommentsPersistDiv.load("CommentXMLStore");
                                    document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
                   
                      











    源码下载:http://file.javaxxz.com/2014/10/12/235057578.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 07:16 , Processed in 0.533279 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表