溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java實(shí)現(xiàn)打字游戲的方法

發(fā)布時(shí)間:2020-08-04 12:07:00 來源:億速云 閱讀:269 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了Java實(shí)現(xiàn)打字游戲的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

新建一個(gè)項(xiàng)目,然后在src里面建一個(gè)MyGame.java文件,

把代碼粘到剛才新建的MyGame.java,
然后把兩張圖放到src下,就行了

一、代碼

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;

public class MyGame {
 static UIFrame uiFrame;//主界面
 static playGame playgame;//正式游戲開始界面

 public static void main(String[] args) {
  uiFrame = new UIFrame("打字游戲");
  playgame = new playGame();
 }
 /*游戲主界面*/
 static class UIFrame extends JFrame {
  int width = 500;
  int height = 700;
  Font X = new Font("方正舒體", Font.PLAIN, 30);
  JLabel playjb = new JLabel("開始游戲");
  JLabel rulejb = new JLabel("規(guī)則");
  JLabel exitjb = new JLabel("退出游戲");
  JFrame f1 = new JFrame("規(guī)則");
  /*主界面設(shè)置*/
  public UIFrame(String text) {
   super(text);
   this.setLayout(null);
   this.setSize(width, height);
   this.setLocationRelativeTo(null);
   this.setResizable(false);
   this.getLayeredPane().setLayout(null);
   JPanel imgPanel = (JPanel) this.getContentPane();
   imgPanel.setOpaque(false);
   imgPanel.setBounds(0, 0, width, height);
   imgPanel.setLayout(null);
   ImageIcon icon = new ImageIcon("src/bg.jpg");
   JLabel label = new JLabel(icon);
   label.setBounds(0, 0, this.getWidth(), this.getHeight());
   icon.setImage(icon.getImage().getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_DEFAULT));
   this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));

   Title title = new Title();//新建一個(gè)標(biāo)題對(duì)象
   this.add(title);//往窗口中加入標(biāo)題面板
   Thread t = new Thread(title);//將標(biāo)題面板加入一個(gè)線程
   t.start();//啟動(dòng)線程,實(shí)現(xiàn)標(biāo)題面板下落

   buildButton();
   add_JB_Listener();
   setruleJF();
   this.setVisible(true);
  }
  /*設(shè)置按鈕規(guī)格*/
  public void buildButton() {
   playjb.setForeground(Color.red);
   rulejb.setForeground(Color.red);
   exitjb.setForeground(Color.red);
   playjb.setFont(X);
   rulejb.setFont(X);
   exitjb.setFont(X);
   playjb.setBounds(width / 3, height * 2 / 6, width / 3, 50);
   rulejb.setBounds(width / 3, height * 3 / 6, width / 3, 50);
   exitjb.setBounds(width / 3, height * 4 / 6, width / 3, 50);
   playjb.setHorizontalAlignment(JLabel.CENTER);
   rulejb.setHorizontalAlignment(JLabel.CENTER);
   exitjb.setHorizontalAlignment(JLabel.CENTER);

   this.add(playjb);
   this.add(rulejb);
   this.add(exitjb);
  }
  /*設(shè)置規(guī)則窗口*/
  public void setruleJF(){
   JLabel text1 = new JLabel("<html><body>"+"基本規(guī)則:點(diǎn)擊開始游戲后可以選擇生命值,確認(rèn)后游戲正式開始游戲開始后會(huì)自動(dòng)下落四個(gè)三位"+"<br>"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"數(shù),在輸入框中輸入其中之一會(huì)自動(dòng)消除這個(gè)三位數(shù)," +
     "得分增加,并產(chǎn)生新數(shù)字,當(dāng)數(shù)字"+"<br>"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"掉落到屏幕底部時(shí)生命值減一,生命值為0游戲結(jié)束。(PS:在輸入框中輸入空格游戲暫"+"<br>"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+"停,輸入任意數(shù)字則繼續(xù))" +"<br>"+"<br>"+
     "難度介紹:游戲難度會(huì)隨著得分的增加而自動(dòng)增加,也可使用滑塊自己調(diào)整數(shù)字下落難度等級(jí)。"+"<br>"+"<br>"+
     "閃爍模式:游戲開始后可以點(diǎn)擊開始閃爍按鈕來開始閃爍模式,此時(shí)數(shù)字會(huì)隔一段時(shí)間消失再出現(xiàn)。"+"<br>"+"<br>"+"好好享受吧!"+"</body></html>");
   text1.setVerticalAlignment(JLabel.NORTH);//使其文本位于JLabel頂部
   text1.setFont(new Font("宋體", Font.PLAIN, 20));
   f1.add(text1);//f1為顯示規(guī)則的窗口

   f1.setResizable(false);
   f1.setSize(2 * width - 100, height / 2);
   f1.setLocationRelativeTo(null);
  }
  /*按鈕添加監(jiān)聽器*/
  public void add_JB_Listener() {
   playjb.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
     setVisible(false);
     Chooselife chooselife = new Chooselife();
    }

    @Override
    public void mouseEntered(MouseEvent e) {
     playjb.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.LIGHT_GRAY));
    }

    @Override
    public void mouseExited(MouseEvent e) {
     playjb.setBorder(null);
    }
   });

   rulejb.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
     f1.setVisible(true);
    }

    public void mouseEntered(MouseEvent e) {
     rulejb.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.LIGHT_GRAY));
    }

    @Override
    public void mouseExited(MouseEvent e) {
     rulejb.setBorder(null);
    }
   });

   exitjb.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent e) {
     System.exit(0);
    }

    public void mouseEntered(MouseEvent e) {
     exitjb.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.LIGHT_GRAY));
    }

    @Override
    public void mouseExited(MouseEvent e) {
     exitjb.setBorder(null);
    }
   });

  }
 }
 /*選擇生命界面*/
 static class Chooselife extends JFrame {
  static Boolean gamePlayflag = false;//第一次開始游戲則為false,否則為true,
  Chooselife() {
   setTitle("選擇生命值");
   setAlwaysOnTop(true);//置于頂部
   setLayout(null);
   setSize(300, 100);
   setLocationRelativeTo(null);
   setResizable(false);
   setBotton();//設(shè)按鈕
   setVisible(true);
  }
  /*設(shè)置按鈕*/
  void setBotton() {
   ButtonGroup lives = new ButtonGroup();//新建按鈕組實(shí)現(xiàn)互斥
   JRadioButton one = new JRadioButton("1", true);//按鈕默認(rèn)選擇1
   JRadioButton two = new JRadioButton("2", false);
   JRadioButton three = new JRadioButton("3", false);
   lives.add(one);//按鈕添加進(jìn)按鈕組
   lives.add(two);
   lives.add(three);
   JPanel chooselifejp = new JPanel();
   chooselifejp.setBounds(0, 0, getWidth(), getHeight() - 60);
   chooselifejp.add(one);//按鈕添加進(jìn)JPanel
   chooselifejp.add(two);
   chooselifejp.add(three);
   add(chooselifejp);//JPanel添加到JFrame

   JButton play = new JButton("開始");
   play.setBounds(getWidth() / 3 + 10, chooselifejp.getHeight(), 70, 25);
   add(play);
   /*給開始按鈕添加監(jiān)聽器,設(shè)置選中的生命值*/
   play.addActionListener(new ActionListener() {
    @Override

    public void actionPerformed(ActionEvent e) {
     if (one.isSelected()) {
      playgame.life = 1;//開始游戲后將生命值設(shè)成1
     } else if (two.isSelected()) {
      playgame.life = 2;
     } else {
      playgame.life = 3;
     }
     /*后面實(shí)現(xiàn)重玩再講*/
     playgame.templife =playgame.life;//為實(shí)現(xiàn)重玩功能而新建的臨時(shí)變量templife,用來保存當(dāng)前的生命值。
     if (!gamePlayflag) {//第一次游戲
      playgame.begin();//調(diào)用begin函數(shù)來新設(shè)置一些變量和線程、控件規(guī)格
      gamePlayflag = true;
     } else {
      playgame.injp.replay_func();//選擇完生命值之后,使用第一次游戲使用的那些變量、線程、控件,
             // 然后用重玩的函數(shù)功能重置一些變量即可實(shí)現(xiàn)開始新一輪游戲的效果
      playgame.PlayJF.setVisible(true);//顯示正式游戲界面
     }
     dispose();
    }
   });
  }
 }
 static class playGame {
  static int life=1;
  static int templife=1;
  static int width = 500;
  static int height = 700;
  static int N = 4;//數(shù)字列數(shù)
  static int x[] = new int[N];
  static int y[] = new int[N];
  static String[] num = new String[N];
  JFrame PlayJF = new JFrame("打字游戲");
  Image lifeicon = Toolkit.getDefaultToolkit().getImage("src/life.jpg");
  static int difficult_level = 1;//下落難度
  static int mindifficult_level = 1;//最小下落難度
  static int shanshuo_level = 1;//閃爍難度
  Boolean terminateflag = false;
  Boolean beginflag = false;
  Boolean shanshuoflag = false;
  Boolean zantingshanshuoflag = false;//閃爍且暫停
  Boolean Gameoverflag = false;
  JTextField in = new JTextField(3);
  JLabel showtext = new JLabel();
  int score;
  int count = 3;
  int allcount;
  int correctcount;
  int lianjicount;
  Boolean lianjiflag = false;
  MyPanel mp;
  messageJP injp;

  /*第一次開始游戲調(diào)用*/
  public void begin() {
   getrandnum();
   PlayJF.setSize(width, height);
   mp = new MyPanel();
   injp = new messageJP();
   PlayJF.setResizable(false);
   PlayJF.setLocationRelativeTo(null);
   PlayJF.getLayeredPane().setLayout(null);
   JPanel imgPanel = (JPanel) PlayJF.getContentPane();
   imgPanel.setOpaque(false);
   imgPanel.setBounds(0, 0, width, height);
   imgPanel.setLayout(null);
   ImageIcon icon = new ImageIcon("src/bg.jpg");
   JLabel label = new JLabel(icon);
   label.setBounds(0, 0, PlayJF.getWidth(), PlayJF.getHeight());
   icon.setImage(icon.getImage().getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_DEFAULT));

   PlayJF.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));
   PlayJF.getContentPane().add(mp);
   PlayJF.getContentPane().add(injp, Integer.valueOf(Integer.MAX_VALUE));
   Thread t = new Thread(mp);
   t.start();
   PlayJF.setVisible(true);
   in.requestFocus();
  }

  /*產(chǎn)生四個(gè)隨機(jī)三位數(shù)*/
  public static void getrandnum() {
   int i, j;
   for (i = 0; i < N; i++) {//生成數(shù)字的字符形式,設(shè)置初始橫縱坐標(biāo)
    num[i] = Integer.toString((int) (Math.random() * 900 + 100));//生成100到999之間隨機(jī)數(shù)
    x[i] = (int) (0.1 * width + i * 0.20 * width);
    y[i] = 50;
   }
   for (i = 0; i < N; i++) {
    for (j = i + 1; j < N; j++) {
     while (num[j].charAt(0) == num[i].charAt(0)) {//若數(shù)字與前面的數(shù)字首位相同,則重新生成該數(shù)字
      num[j] = Integer.toString((int) (Math.random() * 900 + 100));
     }
    }
   }
  }
  /*數(shù)字下落面板*/
  class MyPanel extends JPanel implements Runnable {
   int width = PlayJF.getWidth();
   int height = 500;
   long time;//記錄時(shí)間,用于閃爍功能的實(shí)現(xiàn)

   public MyPanel() {
    setOpaque(false);
    setBounds(0, 0, width, height);
    setBackground(Color.BLACK);
   }

   public void paint(Graphics g) {
    super.paint(g);

    if (Gameoverflag) {//游戲結(jié)束
     g.setColor(Color.RED);
     g.setFont(new Font("宋體", Font.BOLD, 35));
     g.drawString("游戲結(jié)束!", width / 3, height / 2);
     g.drawString("您的分?jǐn)?shù)為"+score,width / 3-15,height/2+35);
     gameoverwork();
    } else {
     if (!beginflag) {//倒計(jì)時(shí)
      g.setColor(Color.RED);
      g.setFont(new Font("宋體", Font.PLAIN, 50));
      if (count == 0) {
       g.drawString("Go!", width / 2, height / 2);
       in.setEditable(true);

      } else {
       in.requestFocus();
       g.drawString(String.valueOf(count), width / 2, height / 2);
      }
     } else {//數(shù)字開始掉落
      g.setFont(new Font("宋體", Font.PLAIN, 20));
      g.setColor(Color.WHITE);
      for (int i = 0; i < N; i++) {
       if (shanshuoflag) {//進(jìn)入閃爍模式
        if (zantingshanshuoflag == false) {//閃爍模式且不在暫停狀態(tài)
         if (time % 3000 < 500 * shanshuo_level == false) {//閃爍:若時(shí)間滿足條件,則繪出數(shù)字,否則不繪出數(shù)字
          g.drawString(num[i], x[i], y[i]);
         }
        } else {//閃爍模式且暫停,直接顯示數(shù)字
         g.drawString(num[i], x[i], y[i]);
        }
       } else {//不是閃爍則正常繪出數(shù)字
        g.drawString(num[i], x[i], y[i]);
       }

      }

      if (terminateflag) {//畫出暫停字樣
       g.setColor(Color.BLUE);
       g.setFont(new Font("宋體", Font.BOLD, 30));
       g.drawString("暫停中...", width / 4, height / 2);
       g.setFont(new Font("宋體", Font.BOLD, 20));
       g.drawString("輸入任意數(shù)字繼續(xù)...", width / 4, height / 2 + 30);

       if (shanshuoflag) {
        zantingshanshuoflag = true;//如果是暫停而且是閃爍狀態(tài),
       }

      } else {//不暫停,每次數(shù)字縱坐標(biāo)加一,掉落到底部生命值減一,重置所有數(shù)字縱坐標(biāo)。
       for (int i = 0; i < N; i++) {
        y[i] = y[i] + 1;
        if (y[i] > getHeight()&&templife>0) {
         templife--;
         for(int j=0;j<N;j++){
          y[j]=50;
         }
        }
       }
       if(templife==0){//游戲結(jié)束
        Gameoverflag=true;
       }
      }

      g.setColor(Color.WHITE);
      g.setFont(new Font("宋體", Font.PLAIN, 20));
      g.drawString("得分:" + score, 2, 20);
      g.drawString("生命值:", 270, 20);
      for (int i = 0; i < templife; i++) {
       g.drawImage(lifeicon, 350 + i * 21, 1, 20, 20, this);//在指定位置根據(jù)生命值繪出愛心桃
      }
     }
    }
   }
   /*清空輸入框和無法輸入*/
   public void gameoverwork(){
    in.setText("");
    in.setEditable(false);
   }

   @Override
   public void run() {
    long startTime = System.currentTimeMillis();//記錄游戲開始時(shí)間
    while (true) {
     /*倒計(jì)時(shí)*/
     if (!beginflag) {
      in.setEditable(false);
      repaint();
      try {
       Thread.sleep(1000);
       count--;
       if (count == -1) {
        beginflag = true;
       }
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     } else {//繪出數(shù)字
      repaint();
      try {
       Thread.sleep(40 - difficult_level * 5);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
      long endTime = System.currentTimeMillis();
      time = endTime - startTime;//記錄從開始到執(zhí)行這次重繪函數(shù)后總共經(jīng)歷的時(shí)間

     }
    }
   }
  }

  /*功能面板類*/
  class messageJP extends JPanel {
   JSlider difficultJS = new JSlider(1, 5, 1);//游戲難度滑塊
   JLabel difficultJL = new JLabel();//顯示“當(dāng)前游戲難度為多少”的字樣
   JSlider shanshuo_levelJS = new JSlider(1, 3, 1);//閃爍難度的滑塊
   JLabel shanshuo_levelJL = new JLabel();//顯示“閃爍難度等級(jí)”的字樣
   JLabel termiJL = new JLabel();//顯示“輸入空格暫?!弊謽?
   JButton replay = new JButton("重玩");
   JButton gotomain = new JButton("返回主界面");
   JButton shanshuoJB = new JButton("開啟閃爍");
   String input;

   messageJP() {
    setLayout(null);
    setBounds(0, mp.getHeight(), PlayJF.getWidth(), PlayJF.getHeight() - mp.getHeight());
    set_difficultJS();
    set_replay();
    set_gotomain();
    set_shanshuoJB();
    set_in();
    set_termiJL();
    set_showtext();

   }
   /*設(shè)置輸入框的一些功能*/
   void set_in() {
    in.setCaretPosition(in.getText().length());
    in.setBounds(width / 4, getHeight() - 70, width / 3, 30);
    in.setFont(new Font("宋體", Font.PLAIN, 15));
    in.addKeyListener(new KeyAdapter() {
     public void keyTyped(KeyEvent e) {
      System.out.println("KeyTyped:"+in.getText());
     }

     public void keyPressed(KeyEvent e) {

      super.keyPressed(e);
      System.out.println("KeyPressed:"+in.getText());
      if (e.getKeyChar() == KeyEvent.VK_SPACE) {//判斷輸入是否為空格
       if (terminateflag) {
        terminateflag = false;
       } else {
        terminateflag = true;
       }
      }
     }

     public void keyReleased(KeyEvent e) {
      System.out.println("KeyReleased:"+in.getText());
      String s = in.getText().replaceAll(" ", "");
      in.setText(s);
      if(terminateflag==true&&e.getKeyChar()!=KeyEvent.VK_SPACE){
       terminateflag = false;
      }
      if (in.getText().length() >= 3) {
       allcount++;
       input = in.getText();
       in.setText("");
       lianjiflag = false;
       for (int i = 0; i < N; i++) {
        if (input.equals(num[i])) {
         y[i] = 50;
         score += 10 * difficult_level;
         correctcount++;
         lianjiflag = true;

         if (mindifficult_level < 5 && score > 200) {
          mindifficult_level = score / 100;
          difficultJS.setMinimum(mindifficult_level);//設(shè)置滑塊的最小難度
          if (difficult_level < mindifficult_level) {
           difficult_level = mindifficult_level;//如果當(dāng)前難度比最小難度低,調(diào)整最小難度
          }

          difficultJS.setValue(difficult_level);
         }

         difficultJL.setText("下落等級(jí):" + difficult_level);

         num[i] = Integer.toString((int) (Math.random() * 900 + 100));
         while (true) {
          for (int j = 0; j < N; j++) {
           if (num[i].charAt(0) == num[j].charAt(0) && i != j) {
            num[i] = Integer.toString((int) (Math.random() * 900 + 100));
            j = -1;
           }
          }
          break;
         }
        }
       }
       if (lianjiflag) {
        lianjicount++;
       } else {
        lianjicount = 0;
       }
      }
      showtext.setText("<html>輸入總次數(shù):" + allcount + "<br/>正確次數(shù):" + correctcount + "<br/>當(dāng)前連擊數(shù):" + lianjicount + " <br/></html>");
     }
    });
    add(in);
   }
   /*輸入空格暫停字樣*/
   void set_termiJL() {
    termiJL.setText("輸入空格暫停");
    termiJL.setFont(new Font("宋體", Font.PLAIN, 15));
    termiJL.setForeground(Color.RED);
    termiJL.setBounds(width / 4, getHeight() - 95, width / 3, 30);
    add(termiJL);
   }
   /*難度等級(jí)滑塊*/
   void set_difficultJS() {
    difficultJS.setBounds(10, getHeight() - 110, 80, 40);
    difficultJS.setMajorTickSpacing(1);
    difficultJS.setSnapToTicks(true);
    difficultJS.setPaintTicks(true);
    difficultJS.setPaintLabels(true);
    difficultJS.addChangeListener(new ChangeListener() {
     @Override
     public void stateChanged(ChangeEvent e) {
      difficult_level = difficultJS.getValue();
      difficultJL.setText("下落等級(jí):" + difficult_level);
     }

    });
    difficultJL.setBounds(10, getHeight() - 85, 100, 50);
    difficultJL.setFont(new Font("方正姚體", Font.PLAIN, 15));
    difficultJL.setText("下落等級(jí):" + difficult_level);
    add(difficultJL);
    add(difficultJS, Integer.valueOf(Integer.MIN_VALUE));
   }
   /*重玩按鈕的功能*/
   void set_replay() {
    replay.setBounds(width * 3 / 4 + 10, 0, 100, 50);
    add(replay);
    replay.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent e) {
      replay_func();
     }
    });
   }
   /*返回主界面功能*/
   void set_gotomain() {
    gotomain.setBounds(width * 3 / 4 + 10, 55, 100, 50);
    add(gotomain);
    gotomain.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent e) {
      PlayJF.setVisible(false);
      uiFrame.setVisible(true);
     }
    });
   }
   /*閃爍按鈕及其功能*/
   void set_shanshuoJB() {
    shanshuoJB.setBounds(width * 3 / 4 + 10, 110, 100, 50);
    shanshuo_levelJS.setBounds(10, 5, 80, 40);
    shanshuo_levelJS.setSnapToTicks(true);
    shanshuo_levelJS.setPaintTicks(true);
    shanshuo_levelJS.setPaintLabels(true);
    shanshuo_levelJS.setMajorTickSpacing(1);

    shanshuo_levelJL.setFont(new Font("方正姚體", Font.PLAIN, 15));
    shanshuo_levelJL.setText("閃爍等級(jí):" + shanshuo_level);
    shanshuo_levelJL.setBounds(10, 30, 80, 70);
    shanshuo_levelJS.setVisible(false);
    shanshuo_levelJL.setVisible(false);
    add(shanshuoJB);
    add(shanshuo_levelJS);
    add(shanshuo_levelJL);
    shanshuoJB.addActionListener(new ActionListener() {
     @Override
     public void actionPerformed(ActionEvent e) {
      if (shanshuoflag) {//當(dāng)前模式是閃爍模式
       shanshuoflag = false;
       shanshuo_levelJS.setVisible(false);//隱藏閃爍難度調(diào)節(jié)滑塊
       shanshuo_levelJL.setVisible(false);
       shanshuoJB.setText("開啟閃爍");
       shanshuo_level = 1;
       shanshuo_levelJS.setValue(1);
      } else {
       shanshuoflag = true;
       shanshuo_levelJS.setVisible(true);
       shanshuo_levelJL.setVisible(true);
       shanshuoJB.setText("關(guān)閉閃爍");
      }

     }
    });
    shanshuo_levelJS.addChangeListener(new ChangeListener() {
     @Override
     public void stateChanged(ChangeEvent e) {
      shanshuo_level = shanshuo_levelJS.getValue();
      shanshuo_levelJL.setText("閃爍等級(jí):" + shanshuo_level);
     }
    });
   }
   /*顯示一些統(tǒng)計(jì)信息*/
   void set_showtext() {
    showtext.setFont(new Font("方正姚體", Font.PLAIN, 15));
    showtext.setBounds(width / 4, 10, getWidth() / 3, getHeight() / 2);
    showtext.setText("<html>輸入總次數(shù):" + allcount + "<br/>正確次數(shù):" + correctcount + "<br/>當(dāng)前連擊數(shù):" + lianjicount + " <br/></html>");
    showtext.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    add(showtext);

   }
   /*實(shí)現(xiàn)重玩功能的函數(shù)*/
   void replay_func() {//就是重置一些變量
    in.setEditable(true);
    difficult_level = 1;
    difficultJS.setMinimum(1);
    difficultJS.setMaximum(5);
    difficultJS.setValue(1);
    templife=life;
    shanshuo_level = 1;
    shanshuo_levelJS.setValue(1);
    shanshuo_levelJS.setVisible(false);
    shanshuo_levelJL.setVisible(false);
    terminateflag=false;
    getrandnum();
    score = 0;
    count = 3;
    lianjicount = 0;
    allcount = 0;
    correctcount = 0;
    in.setText("");
    showtext.setText("<html>輸入總次數(shù):" + allcount + "<br/>正確次數(shù):" + correctcount + "<br/>當(dāng)前連擊數(shù):" + lianjicount + " <br/></html>");
    lianjiflag = false;
    shanshuoflag = false;
    shanshuoJB.setText("開啟閃爍");
    beginflag = false;
    Gameoverflag = false;

    in.requestFocus();
   }
  }
 }

}

/*在一個(gè)面板上實(shí)現(xiàn)標(biāo)題自動(dòng)下落*/
class Title extends JPanel implements Runnable {
 int width = 500;
 int height = 250;
 int N = 4;
 int[] x = new int[N];//存儲(chǔ)標(biāo)題中的每個(gè)字的橫坐標(biāo)
 int[] y = new int[N];//存儲(chǔ)標(biāo)題中的每個(gè)字的縱坐標(biāo)
 String[] strs = new String[]{"打", "字", "游", "戲"};

 Title() {

  setBounds(0, 0, width, height);//設(shè)置面板大小
  setOpaque(false);//透明
  setplace();//設(shè)置標(biāo)題每個(gè)字初始的橫縱坐標(biāo)
 }

 void setplace() {
  for (int i = 0; i < N; i++) {
   x[i] = (int) (width * 0.15 + i * 0.2 * width);
   y[i] = 10;
  }
 }

 @Override
 public void paint(Graphics g) {
  super.paint(g);
  g.setColor(Color.RED);//設(shè)置畫筆顏色為紅
  g.setFont(new Font("方正姚體", Font.PLAIN, 50));//設(shè)置畫筆字體
  for (int i = 0; i < N; i++) {
   g.drawString(strs[i], x[i], y[i]);//在指定位置畫出標(biāo)題的字
   y[i]++;//標(biāo)題的字縱坐標(biāo)下移一像素
   if (y[i] > height - 50) {//如果到達(dá)height-50,則保持在那個(gè)位置
    y[i] = height - 50;
   }
  }

 }

 @Override
 public void run() {
  while (true) {
   try {
    Thread.sleep(10);//實(shí)現(xiàn)每10毫秒重繪一次
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   repaint();//調(diào)用重繪函數(shù)
  }
 }
}

二、圖片

bg.jpg

Java實(shí)現(xiàn)打字游戲的方法

life.jpg

Java實(shí)現(xiàn)打字游戲的方法

看完上述內(nèi)容,是不是對(duì)Java實(shí)現(xiàn)打字游戲的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI