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

📄 maze1.java

📁 这是本人自主开发的一个JAVA小游戏
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    try{
      showStatus("Loading Image...");
      MT.waitForAll();
    }
    catch(InterruptedException E){}
  
    sky2 = createImage(sky.getSource());      //复制图片
    ImageWidth = 900;

    X1 = 0;
    X2 = -ImageWidth;    

    OffScreen = createImage(AppletWidth,AppletHeight);
    drawOffScreen = OffScreen.getGraphics();
    floorW = block1[1].getWidth(this);
    ImageWidthb = character.getWidth(this)/3;
    ImageHeightb = character.getHeight(this)/4;
    ImageWidthg = chong1[0].getWidth(this);
    ImageHeightg = chong2[0].getHeight(this);
    ImageX = floorW;
    ImageY = floorW;
    S_Screen = new StartScreen(AppletWidth,AppletHeight,this,block1[2],block2[0],bkImage,ChineseTitle,EnglishTitle,press1);
    E_Screen = new StartScreen(AppletWidth,AppletHeight,this,block1[2],block2[0],bkImage,E_ChineseTitle,E_EnglishTitle,press2);
    W_Screen = new StartScreen(AppletWidth,AppletHeight,this,block1[2],block2[0],bkImage,W_ChineseTitle,W_EnglishTitle,press2);
    s1 = new Brother(0,ImageY,floorW,floorW,maze,1,0);//幻幻的实例
    s = new Brother(ImageX,ImageY,floorW,floorW,maze,1,1);//灵灵的实例
    gs[0] = new Guaishou(ImageX,ImageY*4,ImageWidthg,ImageHeightg,maze,4,1);
    gs[1] = new Guaishou(ImageX*5,ImageY*10,ImageWidthg,ImageHeightg,maze,10,5);
    gs[2] = new Guaishou(ImageX*7,ImageY*6,ImageWidthg,ImageHeightg,maze,6,7);
    gs[3] = new Guaishou(ImageX*2,ImageY*2,ImageWidthg,ImageHeightg,maze,2,2);
   
  }
  public void start(){
    newThread = new Thread(this);
    newThread.start();
  }
  public void stop(){
    newThread = null;
  }
  public void update(Graphics g){   
    paint(g); 
  }
  public void paint(Graphics g){
            AppletWidth = getSize().width;
    AppletHeight = getSize().height;  

    if((startnow == false)&&(gameover == false)) //如果游戏处于并未进行也并结束中
      S_Screen.paintScreen(drawOffScreen); //则显示开始界面
    
    if((gameover == true)&&(startnow == false)){ //如果游戏处于并未进行而且已经结束
      E_Screen.paintScreen(drawOffScreen); //则显示结束界面
      A1.stop();                   //游戏进行音乐停止
      
    }
    if((scores>=100)&&(s.indexI==12)&&(s.indexJ==17)&&(Gamesecond<=60)){  //如果游戏胜利
      gameover = startnow = true;
      W_Screen.paintScreen(drawOffScreen);//则显示胜利界面
      A1.stop();              //游戏进行音乐停止
      A5.loop();               //游戏胜利音乐循环播放
    }
    
    currentImageblock1 = (++currentImageblock1)%3;
    currentImageblock2 = (++currentImageblock2)%4;
    currentImagechong1 = (++currentImagechong1)%2;
    currentImagechong2 = (++currentImagechong2)%2;
    currentImagefly = (++currentImagefly)%3;
    g.drawImage(OffScreen,0,0,AppletWidth,AppletHeight,this); 
    g.setFont(F1);
    FM = g.getFontMetrics();
    g.drawString("时间:"+Gamesecond+" 秒",750,50);  //显示游戏时间
    g.drawString("分数:"+scores+"分",750,20);      //显示灵力值
  }
  public void run(){ 

    while(newThread !=null){
      
      if(startnow == true && gameover == false){  //如果游戏正在进行而且并未结束
        time = new GregorianCalendar();  //取得当前系统时间和日期
        second = time.get(Calendar.SECOND);//取出目前时间的秒数
        if(currentsecond != second){     //如果当前游戏的的秒数不等于取出的目前时间秒数
          currentsecond = time.get(Calendar.SECOND);//则使得当前游戏的秒数等于当前时间的秒数
          Gamesecond++;      //游戏进行时间的秒数+1

        }
        
        if(Gamesecond >= 60) {Gamesecond = 0;gameover = true;startnow = false;scores = 0;} //当游戏大于60秒时,游戏结束,各种参数恢复原始值
        drawOffScreen.drawImage(sky,X1,0,AppletWidth,AppletHeight,this);
        drawOffScreen.drawImage(sky2,X2,0,AppletWidth,AppletHeight,this);
        for(int i=0;i<14;i++)
          for(int j=0;j<18;j++)
            if(maze[i][j]==2)          //贴心形图案
                drawOffScreen.drawImage(block1[currentImageblock1],j*floorW,i*floorW,(j+1)*floorW,(i+1)*floorW,0,0,floorW,floorW,this);
            else if(maze[i][j]==1)    //贴星形图案
                drawOffScreen.drawImage(block2[currentImageblock2],j*floorW,i*floorW,(j+1)*floorW,(i+1)*floorW,0,0,floorW,floorW,this);
            else if(maze[i][j]==3)     //贴胡萝卜
                drawOffScreen.drawImage(blocks1,j*floorW,i*floorW,(j+1)*floorW,(i+1)*floorW,0,0,floorW,floorW,this);
            else if(maze[i][j]==4)    //贴蘑菇
                drawOffScreen.drawImage(blocks2,j*floorW,i*floorW,(j+1)*floorW,(i+1)*floorW,0,0,floorW,floorW,this);
            
        switch(direction){
          case 'U':   //若向上移动
            drawOffScreen.drawImage(character,s.LcX,s.LcY,s.LcX+s.SizeW,s.LcY+s.SizeH,sx,0,sx+ImageWidthb,ImageHeightb,this);
            drawOffScreen.drawImage(fly[currentImagefly],s.LcX,s.LcY+s.SizeH,s.LcX+s.SizeW,s.LcY+2*s.SizeH,0,0,48,36,this);
            break;
          case 'D':   //若向下移动
            drawOffScreen.drawImage(character,s.LcX,s.LcY,s.LcX+s.SizeW,s.LcY+s.SizeH,sx,ImageHeightb*2,sx+ImageWidthb,ImageHeightb*3,this);
            drawOffScreen.drawImage(fly[currentImagefly],s.LcX,s.LcY-s.SizeH,s.LcX+s.SizeW,s.LcY,0,0,48,36,this);
            break;
          case 'L':    //若向左移动
            drawOffScreen.drawImage(character,s.LcX,s.LcY,s.LcX+s.SizeW,s.LcY+s.SizeH,sx,ImageHeightb*3,sx+ImageWidthb,ImageHeightb*4,this);
            drawOffScreen.drawImage(fly[currentImagefly],s.LcX+s.SizeW,s.LcY,s.LcX+2*s.SizeW,s.LcY+s.SizeH,0,0,48,36,this);
            break;
          case 'R':     //若向右移动
            drawOffScreen.drawImage(character,s.LcX,s.LcY,s.LcX+s.SizeW,s.LcY+s.SizeH,sx,ImageHeightb,sx+ImageWidthb,ImageHeightb*2,this);
            drawOffScreen.drawImage(fly[currentImagefly],s.LcX-s.SizeW,s.LcY,s.LcX,s.LcY+s.SizeH,0,0,48,36,this);
            break;
        }  

        for(int i=0;i<14;i++) 
          for(int j=0;j<18;j++)
            if((maze[i][j]==3)||(maze[i][j]==4)){     //如果判断当前迷宫处有灵力果子
              I = i;   //记下此时的X轴索引值
              J = j;  //记下此时的Y轴索引值
              if((s.indexI == I)&&(s.indexJ == J)){   //如果角色与灵力果子的X轴和Y轴索引值相同
                maze[I][J]=0;              //则当前灵力果子被吃掉,迷宫中此位置为空
                scores=scores+10;    //吃到一个灵力果子分灵力值+10
                A4.play();      //播放吃到灵力果子的音效
              } 
            }
            
      
        if(X1 == AppletWidth)         //若sky图片的最左到达Applet程序的右边界
          X1 = -ImageWidth+1;   //则sky图片的最右为Applet程序的左边界
        else X1++;            //否则sky图片向右移
        if(X2 == AppletWidth) //若sky2图片的最最左到达Applet程序的右边界
          X2 = -ImageWidth+1;   //则sky2图片的最右为Applet	程序的左边界
        else X2++;             //否则sky2图片向右移
        for(int i=0;i<4;i++){   //表示一共有4个怪物虫
          if(gs[i].mr == true){ //如果第i个怪物虫可以向右移动
            drawOffScreen.drawImage(chong2[currentImagechong2],gs[i].LcX,gs[i].LcY,gs[i].LcX+gs[i].SizeW,gs[i].LcY+gs[i].SizeH,0,0,gs[i].SizeW,gs[i].SizeH,this);
            gs[i].moveRight(); //调用Sprite类的向右移的函数使怪物虫向右移
          }
          else if(gs[i].ml == true){    //如果第i个怪物虫可以向左移动
            drawOffScreen.drawImage(chong1[currentImagechong1],gs[i].LcX,gs[i].LcY,gs[i].LcX+gs[i].SizeW,gs[i].LcY+gs[i].SizeH,0,0,gs[i].SizeW,gs[i].SizeH,this);
            gs[i].moveLeft();
          }
          else if(gs[i].ml == false || gs[i].mr ==false) {gs[i].mr = true;gs[i].ml=true;} //如果怪物左右两边中的任何一边不能移动了,则重新设置它为左右都能移动
           //如果人物和怪物虫发  生碰撞,则使游戏的各项属性回复初始值,游戏结束
          if((s.indexI==gs[i].indexI)&&(s.indexJ==gs[i].indexJ)) {Gamesecond = 0;gameover = true;startnow = false;scores =0;}
        }
        repaint();

        sx += ImageWidthb;
        if(sx>ImageWidthb*2)
          sx = 0;
        try{
         Thread.sleep(200);
        }
        catch(InterruptedException E){}
      }
    else if(startnow == false){
      repaint();
      try{
        Thread.sleep(80);
      }
      catch(InterruptedException E){}

      S_Screen.UpdateStatus();
    }
    }
  }

  public void actionPerformed(ActionEvent a)
   {
 
   }

   public void endGame(boolean isEndGame,boolean isStartGame)
   {
      gameover = isEndGame;
      startnow = isStartGame;
      if(isEndGame == true & isStartGame == false) A1.stop();
   }

  public void startGame(){
    gameover = false;
    startnow = true;
    A1.loop();
    A5.stop();
    startnow = true;
    gameover = false;
    for(int i=0;i<14;i++)
     for(int j=0;j<18;j++)
       maze[i][j] = maze1[i][j];
    s.indexI = s.indexJ = 1;
    s.LcX = s.LcY = floorW;
    s1.indexI = 1;
    s1.indexJ = 0;
    s1.LcX = 0;
    s1.LcY = floorW;
    Gamesecond = 0;
    scores = 0;
  }
    
  public void mouseExited(MouseEvent e){}
  public void mouseReleased(MouseEvent e){}
  public void mousePressed(MouseEvent e){}
  public void mouseClicked(MouseEvent e){}
  public void mouseMoved(MouseEvent e){
    showStatus("(" + e.getX() + "," + e.getY() + ")");
  }
  public void mouseDragged(MouseEvent e){}
  public void mouseEntered(MouseEvent e){
    
  }
  
  public void keyTyped(KeyEvent e){}
  public void keyPressed(KeyEvent e){     //当按下键盘时 
    key = e.getKeyCode();             //取得当前所按键的值
    if(key == KeyEvent.VK_RIGHT){       //如果按下的是向右的方向键
      direction = 'R';                 //角色的方向为右
      s.moveRight();               //调用Sprite类中角色向右走的函数
    }
    else if(key == KeyEvent.VK_LEFT){    //如果按下的是向左的方向键
      direction = 'L';
      s.moveLeft();
    }
    else if(key == KeyEvent.VK_UP){   //如果按下的是向上的方向键
      direction = 'U';
      s.moveUp();
    }
    else if(key == KeyEvent.VK_DOWN){ //如果按下的是向下的方向键
      direction = 'D';
      s.moveDown();
    }
    else if(key == KeyEvent.VK_ENTER){ //如果说按的是Enter键
      if(startnow == false && gameover == false){
        A1.loop();                      //动画进行中的音乐循环播放
        startnow = true;                   //设置游戏开始的布尔值为真
        gameover = false;                //设置游戏结束的布尔值为假
      }
    }
    for(int i=0;i<4;i++)
       //判断如果角色与怪物虫发生碰撞则游戏中的各属性回到最初状态,游戏结束
      if((s.indexI==gs[i].indexI)&&(s.indexJ==gs[i].indexJ)) {Gamesecond = 0;gameover = true;startnow = false;scores = 0;}
  
  }

  public void keyReleased(KeyEvent e){}
}

⌨️ 快捷键说明

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