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

📄 mainframe.java

📁 snake-games1(贪食蛇游戏)java开发的游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }

        else if (rows[0] < 0) {
          throughwall--;
          jLabel6.setText(Integer.toString(throughwall));
          rows[0] = (rows[0] + ROWS) % ROWS;
        }

        else if (columes[0] >= COLS) {
          throughwall--;
          jLabel6.setText(Integer.toString(throughwall));
          columes[0] = COLS - columes[0];
        }

        else if (columes[0] < 0) {
          throughwall--;
          jLabel6.setText(Integer.toString(throughwall));
          columes[0] = (columes[0] + COLS) % COLS;
        }

      }
      //当没有穿墙宝物,并且蛇头碰到墙时,游戏结束
      if (rows[0] >= ROWS || rows[0] < 0 || columes[0] >= COLS ||
          columes[0] < 0 && throughwall == 0) {

        isEnd = true;
      }
      //蛇头碰到蛇身时的处理操作
      if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.green)) {
        if (throughbody != 0) {
          throughbody--;
          jLabel4.setText(Integer.toString(throughbody));

        }
        else {
          isEnd = true;
        }
      }
      //吃食物时的操作
      if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.yellow)) {
        score += 100;
        jLabel2.setText(Integer.toString(score));
        if (score % 2000 == 0 && speed > 100) {

          JOptionPane.showMessageDialog(jPanel1,
                                        "真是了不起,准备进入下一关\nREADY? GO !   !   !");
          speed -= 100;
        }


      }
      //获得穿墙宝物时的操作
      if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.blue)) {
        score += 100;
        throughbody++;
        jLabel2.setText(Integer.toString(score));
        jLabel4.setText(Integer.toString(throughbody));
        if (score % 2000 == 0 && speed > 100) {

          JOptionPane.showMessageDialog(jPanel1,
                                        "真是了不起,准备进入下一关\nREADY? GO !   !   !");
          speed -= 100;
        }

      }
      //获得穿身宝物时的操作
      if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.red)) {
        score += 100;
        throughwall++;

        jLabel2.setText(Integer.toString(score));
        jLabel6.setText(Integer.toString(throughwall));
        if (score % 2000 == 0 && speed > 100) {

          JOptionPane.showMessageDialog(jPanel1,
                                        "真是了不起,准备进入下一关\nREADY? GO !   !   !");
          speed -= 100;
        }

      }

      //蛇头吃完食物后,蛇身加长,并随机显示下一个食物或宝物
      if (playBlocks[rows[0]][columes[0]].getBackground().equals(Color.yellow)
          ||playBlocks[rows[0]][columes[0]].getBackground().equals(Color.blue)
          ||playBlocks[rows[0]][columes[0]].getBackground().equals(Color.red)) {

        length++;

        int x, y;

        int random = (int) (Math.random() * 10);

        if (random < 7) {

          x = (int) (Math.random() * ROWS);
          y = (int) (Math.random() * COLS);

          while (playBlocks[x][y].isVisible()) {
            x = (int) (Math.random() * ROWS);
            y = (int) (Math.random() * COLS);
          }

          playBlocks[x][y].setBackground(Color.yellow);
          playBlocks[x][y].setVisible(true);
        }

        if (random >=7 && random < 9) {

          x = (int) (Math.random() * ROWS);
          y = (int) (Math.random() * COLS);

          while (playBlocks[x][y].isVisible()) {
            x = (int) (Math.random() * ROWS);
            y = (int) (Math.random() * COLS);
          }

          playBlocks[x][y].setBackground(Color.blue);
          playBlocks[x][y].setVisible(true);
        }
        if (random >= 9) {

          x = (int) (Math.random() * ROWS);
          y = (int) (Math.random() * COLS);

          while (playBlocks[x][y].isVisible()) {
            x = (int) (Math.random() * ROWS);
            y = (int) (Math.random() * COLS);
          }
          playBlocks[x][y].setBackground(Color.red);
          playBlocks[x][y].setVisible(true);
        }
      }
      //显示蛇头
      playBlocks[rows[0]][columes[0]].setBackground(Color.green);
      playBlocks[rows[0]][columes[0]].setVisible(true);

    }

  }

  class SnakeThread      extends Thread {

    public void run() {
      while (true) {
        try {
          //停顿
          Thread.sleep(snake.speed);
          //当游戏处于正常运行状态,则移动蛇身
          if (!isEnd && !isPause) {
            snake.move();

          }
        }
        catch (Exception ex){}

      }
    }
  }

  void jButton1_actionPerformed(ActionEvent e) {
    start();
  }

  void this_keyPressed(KeyEvent e) {
    //判断游戏状态
    if (!isEnd && !isPause) {
      //根据用户按键,设置蛇运动方向
      if (e.getKeyCode() == KeyEvent.VK_UP) {
        snake.direction = UP;
      }

      if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        snake.direction = DOWN;
      }

      if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        snake.direction = LEFT;
      }

      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        snake.direction = RIGHT;
      }

    }
  }

  class MainFrame_jButton1_actionAdapter
      implements java.awt.event.ActionListener {
    MainFrame adaptee;

    MainFrame_jButton1_actionAdapter(MainFrame adaptee) {
      this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
      adaptee.jButton1_actionPerformed(e);
    }
  }

  class MainFrame_this_keyAdapter
      extends java.awt.event.KeyAdapter {
    MainFrame adaptee;

    MainFrame_this_keyAdapter(MainFrame adaptee) {
      this.adaptee = adaptee;
    }

    public void keyPressed(KeyEvent e) {
      adaptee.this_keyPressed(e);
    }
  }

  void jButton3_actionPerformed(ActionEvent e) {
    System.exit(0);
  }

  void jButton2_actionPerformed(ActionEvent e) {
    if (isPause == true )
    {jButton2.setText("暂停");}
    if (isPause == false)
    {jButton2.setText("继续");}
    isPause = !isPause;

  }

  void jRadioButton1_actionPerformed(ActionEvent e) {
    level = BEGINNER;
  }

  void jRadioButton2_actionPerformed(ActionEvent e) {
    level = MIDDLE;
  }

  void jRadioButton3_actionPerformed(ActionEvent e) {
    level = EXPERT;
  }

  void jButton4_actionPerformed(ActionEvent e) {
   Help_Dialog dlg = new Help_Dialog();
   Dimension dlgSize = dlg.getPreferredSize();
   Dimension frmSize = getSize();
   Point loc = getLocation();
   dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
   dlg.setModal(true);
   dlg.pack();
   dlg.show();

  }
}

class MainFrame_jButton3_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jButton3_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

class MainFrame_jButton2_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jButton2_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class MainFrame_jRadioButton1_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jRadioButton1_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jRadioButton1_actionPerformed(e);
  }
}

class MainFrame_jRadioButton2_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jRadioButton2_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jRadioButton2_actionPerformed(e);
  }
}

class MainFrame_jRadioButton3_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jRadioButton3_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jRadioButton3_actionPerformed(e);
  }
}

class MainFrame_jButton4_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jButton4_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton4_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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