今天为大家分享打地鼠游戏的开发与制作,目前是单机版游戏,后续时间空了,会进一步完善。整个系统界面漂亮,有完整的源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步

开发环境

开发语言为Java,开发环境Eclipse或者IDEA都可以。运行主程序,或者执行打开JAR文件即可以运行本程序。

系统框架

利用JDK自带的SWING框架开发,不需要安装第三方JAR包。纯窗体模式,直接运行DaDiS文件即可以。同时带有详细得设计文档。

主要功能

本程序主要用来真实模仿路口的交通灯的运行机制,和现实版的交通灯运行原理是一样的。

启动方法

对DaDiS.java点右键,run as Application,启动打地鼠游戏。

一 . 游戏说明:

1. 游戏操作简单,用锤子击打(鼠标点击)地鼠即可。

2. 您的最高得分将被系统保存,若在所有玩家中排前三

您将登上高分榜,可返回大厅查看.(有待后续完成)

二 . 版权声明:

1. 游戏中的图片均由网络图片经本人整合、修改而成

2. 背景音乐来源网络

3. 如需源代码,评论或者私信

三. 若您在游戏中发现影响游戏的bug请务必联系本人改进

主要功能点

1 游戏最长时间为60秒,击打陈工一个地鼠,分数+1

2窗口现实当前的最新分数和剩余时间

3 窗口最上面有4个按钮,重新开始、暂停、说明、退出游戏

游戏运行效果

主要代码

public class DaDiS extends JFrame implements Runnable, ActionListener {

  private static boolean isContinue = true;
  private static Thread t1;
  private boolean interrupt = false;
  private boolean isPause = false;
  private boolean oneceAgrin = true;
  private JFrame jf;
  private JLabel back;
  private ImageIcon imgMouse;
  private ImageIcon imgMouse1;
  private JLabel[] mouses;
  private JLabel scoreL;

  private JMenuBar bar = new JMenuBar();
  private JButton[] bts = new JButton[4];
  private Font font = new Font("楷体", 1, 18);
  private int score = 0;
  private int time;
  private Sound qiao = new Sound("jida.WMA");
  private Sound bgm = new Sound("LoveYourself.wav");

  public DaDiS() {
    
    jf = new JFrame("小僵尸的派对之--打地鼠");
    jf.setResizable(false);
    jf.getContentPane().setLayout(null);

    Container con = jf.getContentPane();

    back = new JLabel();
    back.setBounds(0, 0, 620, 420);

    jf.setBounds(300, 100, 620, 475);
    Toolkit kit = Toolkit.getDefaultToolkit();
    Image img = new ImageIcon(this.getClass().getResource("1.png")).getImage();
    Image img2 = new ImageIcon(this.getClass().getResource("11.png")).getImage();
    final Cursor myCursor = kit.createCustomCursor(img, new Point(3, 3), "光标名字");
    final Cursor myCursor2 = kit.createCustomCursor(img2, new Point(3, 3), "光标名字");
    // 创建一个自定义光标对象。 若要隐藏光标,可将热点Point设为0,0
    // 参数Point 功能未知
    jf.setCursor(myCursor);// setCursor()设置鼠标样式
    // 注意,多帧图像是无效的,可能造成此方法被挂起。
    ImageIcon icon = new ImageIcon(this.getClass().getResource("3.jpg"));
    // this.getClass()返回运行时的类,此处为DaDiS
    back.setIcon(icon);
    imgMouse = new ImageIcon(this.getClass().getResource("2.png"));
    // 类名.getResource("名字")查找带有给定名字的资源package_name/name
    // 返回一个URL对象,否则null
    imgMouse1 = new ImageIcon(this.getClass().getResource("22.png"));
    
    mouses = new JLabel[9];
    for (int i = 0; i < 9; i++) {
      mouses[i] = new JLabel();
      mouses[i].setSize(imgMouse.getIconWidth(), imgMouse.getIconHeight());
      // mouses[i].setIcon(imgMouse);//位置测试
      mouses[i].addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
          Object ob = e.getSource();
          if (ob instanceof JLabel) {// ob属于JLabel的实例
            JLabel label = (JLabel) ob;
            if (label.getIcon() != null && label.getIcon() != imgMouse1) {
              qiao.play();// 添加击打音效
              label.setIcon(imgMouse1);
              score += 10;
              scoreL.setText("您的得分:" + score + "分      时间剩余:" + time + "s");
            }
          }
        }

        public void mousePressed(MouseEvent e) {
          jf.setCursor(myCursor2);
        }

        public void mouseReleased(MouseEvent e) {
          jf.setCursor(myCursor);
        }
      });
      con.add(mouses[i]);
    }
    scoreL = new JLabel();
    scoreL.setBounds(20, 0, 430, 50);
    scoreL.setFont(new Font("楷体", 1, 20));
    scoreL.setForeground(new Color(250, 120, 10));
    scoreL.setText("您的得分:" + score + "分      时间剩余:" + time + "s");
    con.add(scoreL);

    mouses[0].setLocation(105, 85);
    mouses[1].setLocation(255, 92);
    mouses[2].setLocation(409, 93);
    mouses[3].setLocation(80, 172);
    mouses[4].setLocation(252, 172);
    mouses[5].setLocation(408, 171);
    mouses[6].setLocation(75, 257);
    mouses[7].setLocation(257, 261);
    mouses[8].setLocation(425, 261);

    String[] name = { " 重 新 开 始  ", " 暂 停/继 续 ", " 说    明  ", " 退 出 游 戏  " };
    for (int i = 0; i < 4; i++) {
      bts[i] = new JButton(name[i]);
      // bts[i].setSize(150,21);
      bts[i].setFont(font);
      bts[i].setForeground(new Color(10, 120, 250));
      bts[i].addActionListener(this);
      /*
       * bts[i].addMouseMotionListener(new MouseMotionAdapter() { public void
        mouseMoveed(MouseEvent e) {
      **/bts[i].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
       
      bar.add(bts[i]);
    }
    jf.setJMenuBar(bar);
    con.add(back);
    con.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        jf.setCursor(myCursor2);
      }

      public void mouseReleased(MouseEvent e) {
        jf.setCursor(myCursor);
      }
    });
    con.setLayout(null);
    jf.setResizable(false);// 窗口大小不可变
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true);
    
    
  }

  public static void main(String[] args) {
    while (isContinue) {
      isContinue = false;
      DaDiS d1 = new DaDiS();
      t1 = new Thread(d1);
      t1.start();
    }
  }

  public void winMessage(String str) {// 提示窗口,有多个地方调用
    JOptionPane.showMessageDialog(null, str, "友情提示", 1);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.bts[0]) {// 重新开始
      isPause = true;
      winMessage("新局已开!\n来吧!加油!");
      bgm.loopPlay();
      isPause = false;
      oneceAgrin = true;
    } else if (e.getSource() == this.bts[1]) {// 暂停

      if (!isPause) {
        isPause = true;
        bgm.stop();
        winMessage("游戏已暂停!");
      } else {
        bgm.loopPlay();
        isPause = false;
      }
    } else if (e.getSource() == this.bts[2]) {// 说明应包括暂停功能
      isPause = true;
      winMessage("一 . 游戏说明:\n\n"
          + "  1. 游戏操作简单,用锤子击打(鼠标点击)地鼠即可。\n\n"
          + "  2. 您的最高得分将被系统保存,若在所有玩家中排前三\n\n"
          + "     您将登上高分榜,可返回大厅查看.(有待后续完成)\n\n"
          + "二 . 版权声明:\n\n"
          + "  1. 游戏中的图片均由网络图片经本人整合、修改而成;\n\n"
          + "  2. 背景音乐来源网络;\n\n"
          + "  3. 如需源代码,可联系2497737951@qq.com\n\n"
          + "三. 若您在游戏中发现影响游戏的bug请务必联系本人改进");
      isPause = false;
    } else {// 退出
      System.exit(0);
    }

  }

  @Override
  public void run() {
    do {
      bgm.loopPlay();
      oneceAgrin = false;
      this.time = 60;// 60s限时
      //TODO
      interrupt = false;
      this.score = 0;
      scoreL.setText("您的得分:" + score + "分      时间剩余:" + time + "s");
      int i, j, k = 0;
      i = (int) (Math.random() * 9);
      j = (int) (Math.random() * 9);
      while (true) {
        if (oneceAgrin)
          break;
        if (isPause) {
          System.out.println(isPause);
          if (oneceAgrin)
            break;
          continue;
        }

        try {
          Thread.sleep(200);
          if (mouses[j].getIcon() == null)
            mouses[j].setIcon(imgMouse);
          if (mouses[i].getIcon() == null && k % 3 == 0)
            mouses[i].setIcon(imgMouse);
          Thread.sleep(800);
          if (mouses[j].isShowing()) {
            mouses[j].setIcon(null);
            j = (int) (Math.random() * 9);
          }
          if (mouses[i].isShowing()) {
            mouses[i].setIcon(null);
            i = (int) (Math.random() * 9);
          }
        } catch (InterruptedException e) {// interrupted被打断
          e.printStackTrace();
        }
        time--;
        scoreL.setText("您的得分:" + score + "分      时间剩余:" + time + "s");
        k++;
        if (time <= 0)
          break;
        if (interrupt) {
          Thread.interrupted();
        }
      }
      if (mouses[j].isShowing())
        mouses[j].setIcon(null);
      if (mouses[i].isShowing())
        mouses[i].setIcon(null);
      bgm.stop();
      String judge;
      if (score <= 300)
        judge = "\n还要多加练习消灭地鼠哦!\n\n";
      else if (score <= 350)
        judge = "\n真不懒!地鼠都怕你了!\n\n";
      else if (score <= 400)
        judge = "\n地鼠:兄弟们!快逃啊!!!~\n\n";
      else if (score <= 450)
        judge = "\n什么情况!\n地鼠家族都要覆灭了!\n\n";
      else
        judge = "\n地鼠族长:苍天呐~\n给我们留个后代吧~(仰天大哭)\n\n";
      if (!oneceAgrin) {
        winMessage("您的时间已经用完了!\n您的得分为" + score + "\n" + judge);
      }
      while (!oneceAgrin) {
        //System.out.println(oneceAgrin);
      }
    } while (oneceAgrin);
  }
}

// 背景音乐
class Sound {
  private URL sound;
  private AudioClip music;

  public Sound(String ad) {
    sound = this.getClass().getResource(ad);
    music = JApplet.newAudioClip(sound);
  }

  public void play() {
    music.play();
  }

  public void loopPlay() {
    music.loop();
  }

  public void stop() {
    music.stop();
  }
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐