⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maze1.java

📁 这是本人自主开发的一个JAVA小游戏
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

class InfoDialog extends Dialog implements ActionListener
{
   Button B;                     //建立一个Button类的实例按钮B

   public InfoDialog(Frame Owner,String Title,boolean isModal)
   {
      super(Owner,Title,isModal);       

      setLayout(new GridLayout(4,1));  //得用GridLayout布局将容器分为1行4列  
      //加入4个组件
      add(new Label("迷路天使中文版")); 
      add(new Label("LILY毕设作品"));
      add(new Label("2006/5/15"));
      add(B = new Button("确定"));

      B.addActionListener(this);   //注册按钮的事件处理方法     

      pack();       //包装对话框                          
   }

 
   public void actionPerformed(ActionEvent e)
   {
      setVisible(false);    //隐藏窗口                           
   }
}

class GameFrame2 extends Frame 
      implements WindowListener,ActionListener
{
   Panel    main,center;           //main将包含center
   Menu     MU;                          //主菜单选项    
   MenuBar  MB;                        //菜单栏 
   MenuItem MI1,MI2,MI3;           //菜单选项
   Maze1 Game;

   InfoDialog   DW;                  //对话框
   CloseDialog  CD;
   public GameFrame2(String Title,int AppletWidth,int AppletHeight,
                    Maze1 Game1)
   {
      super(Title);             //建立窗口
      Game = Game1;         
      main      = new Panel();    //建立 Panel
      center    = new Panel();
      CD = Game.CD;
      DW        = new InfoDialog(this,"基本信息",true);

      addWindowListener(this);

      main.setLayout(new BorderLayout()); //main使用BorderLayout
      center.setLayout(new CardLayout()); //center使用 CardLayout

  
      main.add(center,BorderLayout.CENTER); //将center放入main中央

      center.add(Game,"main");       //在 center中放入Applet        
      add(main);                      //在窗口中放入main                 

       //建立菜单
      MB = new MenuBar();
      MU = new Menu("选项");
      MU.add(MI1 = new MenuItem("基本信息"));
      MU.add(MI2 = new MenuItem("离开游戏"));
      MU.add(MI3 = new MenuItem("再来一次"));
      MB.add(MU);
      setMenuBar(MB);                        //将菜单加入到窗口中                

      MI1.addActionListener(this);       //注册选项的事件处理方法     
      MI2.addActionListener(this); 
      MI3.addActionListener(this);
      
      setResizable(false);           //设置不可改变窗口大小               
      setSize(AppletWidth,AppletHeight + 100);  //设置窗口大小
      setVisible(true);            //显示窗口                          
   }

   public void windowClosing(WindowEvent e)
   { 
      dispose();                //销毁窗口
   }

   public void windowOpened(WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}
   public void windowDeiconified(WindowEvent e){}

   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource() == MI1)     //当按下选项1时 
      {
         DW.setVisible(true);        //显示对话框  
      }   
      else if(e.getSource() == MI2){ //当按下选项2时
         CD.setVisible(true);       //显示对话框
      } 
      else if (e.getSource() == MI3){ //当按下选项3时
         Game.startGame();      //游戏重新开始
      }       
   }
}

class CloseDialog extends Dialog implements ActionListener
{
   Panel        P1,P2;
   Button       B1,B2;
   Maze1 Game;

  
   public CloseDialog(Maze1 Game,Frame owner)
   {
      super(owner,"离开游戏...",true);    

      this.Game = Game;

      setLayout(new GridLayout(2,1));    
      P1 = new Panel();                   
      P2 = new Panel();
      
      P1.add(new Label("真的要离开吗???"));
      P2.add(B1 = new Button("确定"));     
      P2.add(B2 = new Button("取消"));
      B1.addActionListener(this);          
      B2.addActionListener(this);

      add(P1);                          
      add(P2);
      pack();                                      
   }

   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource() == B1){ 
     
         Game.endGame(true,false);
      }
      else if(e.getSource() == B2){ 

         Game.endGame(false,true);
      }

      setVisible(false);                            
   }
}

class StartScreen{            //开始画面类
//=====================数据成员=========================
  int width,height,StringWidth,StringHeight,Ascent,Descent,X,Y;
  int ImageLeftBound,ImageRightBound,ImageX,ImageY,ImageWidth,ImageHeight,VX;
  Font F1,F2,F3;
  Image Normal,bkImage,Hit,currentImage;
  String ChineseTitle,EnglishTitle,PressEnter;
  Maze1 Game;
  Random R;
  boolean showPressEnter;
  FontMetrics FM;
//=====================函数成员=========================
  public StartScreen(int AppletWidth,int AppletHeight,Maze1 Game,Image normal,Image hit,Image bk,String chineseTitle,String englishTitle,String press){
    R = new Random();     //用来取随机数的类
    //绘制字符串需要用到的三种字体 
    F1 = new Font("TimesRoman",Font.BOLD,72);
    F2 = new Font("TimesRoman",Font.BOLD + Font.ITALIC,36);
    F3 = new Font("TimesRoman",Font.BOLD,20);

  // ChineseTitle = "迷路天使";                 //使用F1
  // EnglishTitle = "lost angle";    //使用F2
   // PressEnter = "<<<==请按键开始游戏==>>>";  //使用F3
  
    width = AppletWidth;                  //Appelt的宽度
    height = AppletHeight;               //Applet的高度

    ChineseTitle = chineseTitle;
    EnglishTitle = englishTitle;
    PressEnter = press;

    Normal = normal;             //心形图像
    Hit = hit;                   //星形图像
    bkImage = bk;                //背景蓝天白云图像

    ImageWidth = Normal.getWidth(Game);  //心形图像的宽度
    ImageHeight = Normal.getHeight(Game); //心形图像的高度
    ImageLeftBound = 25;              //心形和星形图像移动的左边界
    ImageRightBound = AppletWidth-300;//心形和星形图像移动的右边界
    ImageX = ImageRightBound;     //心形图像的起始位置

    VX = -10;                //图像移动的速度
    this.Game = Game;  
    currentImage = Normal;   //指定当前移动的图像为心形图像
    showPressEnter = true;   //显示"<<<==请按Enter键开始游戏==>>>"
  }

  public void UpdateStatus(){      //更新动画的函数
    ImageX = ImageX +VX;                //指定图像的新位置
    if(ImageX<=ImageLeftBound){       //如果碰到左边界
      currentImage = Hit;       //指定目前移动的图像为星形图像
      ImageX = ImageLeftBound;      //设置图像的新位置
      VX = -VX;                     //改变图像移动的方向
    }
    if(ImageX>=ImageRightBound){     //如果碰到右边界
      currentImage = Normal;        //指定目前移动的图像为心形图像
      ImageX = ImageRightBound;       //设置图像的新位置
      VX = -VX;                    //改变图像移动方向
    }
//利用随机数来让字符串闪动
    if(showPressEnter == true){
      if((R.nextInt(5)+1)%5 == 0) showPressEnter = false;
    }
    else{
      if((R.nextInt(5)+1)%5 ==0)  showPressEnter = true;
    }
  }
  
  public void paintScreen(Graphics g){           //绘制动画的函数
    g.clearRect(0,0,width,height);                   //清除次画面

    g.setFont(F1);                                  //设置字体F1
    FM = g.getFontMetrics();          //取得FontMetrics类的实例
    
    Ascent = FM.getAscent();      //取得字符串ChineseTitle的Ascent
    Descent = FM.getDescent();  //取得字符串ChineseTitle的Descent
    StringWidth = FM.stringWidth(ChineseTitle);//取得字符串ChineseTitle的Width
    StringHeight = Ascent + Descent;//取得字符串ChineseTitle的Height

    X = (width - StringWidth)/2;    //设置ChineseTitle开始处的X值
    Y = Ascent;                    //设置ChineseTitle开始处的Y值

    g.drawImage(bkImage,0,0,width,height,Game);//绘制背景的蓝天白云图像
    g.setColor(Color.red);                  //设置前景色为白色
    g.drawString(ChineseTitle,X,Y);         //绘制中文标题

    Y = StringHeight;                    //设置线段1起点处的Y值
    g.drawLine(X,Y,X+StringWidth,Y);        //绘制线段1

    X = X+30;                            //设置线段2起点处的X值
    Y = Y + 5;                          //设置线段2起点处的Y值
    g.drawLine(X,Y,X+StringWidth-60,Y);    //绘制线段2

    g.setFont(F2);                        //设置字体F2
    FM = g.getFontMetrics();            //取得FontMetrics类的实例

    Ascent = FM.getAscent();      //取得字符串EnglishTitle的Ascent
    Descent = FM.getDescent();  //取得字符串EnglishTitle的Descent

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -