JAVA实现打字小游戏

目录

一、效果

二、教程

三、代码


一、效果

首先我们先看效果,左上角的分数是用来记录我们打对了多少字母。字母是从上面开始往下落。每打对一个字母,分数增加,增加到一定分数后,字母下落的速度也会增加。(效果是动态的)

这是不是一个简单的打字小游戏呢?

JAVA实现打字小游戏_第1张图片

 

JAVA实现打字小游戏_第2张图片

二、教程

1、使用IDEA搭建一个项目,项目名称:Words(可根据自己的喜好)

具体搭建过程可看博文用IDEA构建一个简单的Java程序范例,这里就不详细说了。

JAVA实现打字小游戏_第3张图片

2、Word.class

(1)导入包

import java.awt.Component;
import javax.swing.JFrame;

(2)主函数

在这里我的panel大小设置的是800*600,大家可以根据自己的喜好设置的更大一些。

public static void main(String[] args) {
        JFrame frame = new JFrame("打字游戏");
        WordPanel panel = new WordPanel();
        frame.add(panel);
        Thread t = new Thread(panel);
        t.start();
        panel.addKeyListener(panel);
        panel.setFocusable(true);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo((Component)null);
        frame.setDefaultCloseOperation(3);
        frame.setVisible(true);
    }

3、WordPanel.class

(1)导入包

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;

(2)类的继承,接口的实现

  • JPanel类:面板组件,非顶层容器
  • KeyListener:键盘监听接口
public class WordPanel extends JPanel implements Runnable, KeyListener{}

(3)数据类型的定义

  • xx, yy:int,为字母的坐标
  • words:char
  • color:Color
  • score:int,为打字的分数
  • speed:int,为字母的下落速度
int[] xx = new int[10];
int[] yy = new int[10];
char[] words = new char[10];
Color[] colors = new Color[10];
int score = 0;
int speed = 10;

(4)画布函数WordPanel()

  • for循环,每次10个字母,字母出现位置随机,字母的颜色也随机,出现的字母也随机,总之,都是随机。
public WordPanel() {
        for(int i = 0; i < 10; ++i) {
            this.xx[i] = (int)(Math.random() * 800);
            this.yy[i] = (int)(Math.random() * 600);
            this.colors[i] = this.randomColor();
            this.words[i] = (char)((int)(Math.random() * 26 + 65));
        }
    }

(5)画笔函数paint()

  • 画笔名称g
  • 字母的字体,微软雅黑,大小为28
public void paint(Graphics g) {
        super.paint(g);
        Font ft = new Font("微软雅黑", 1, 28);
        g.setFont(ft);
        g.drawString("分数" + this.score, 50, 50);

        for(int i = 0; i < 10; ++i) {
            g.setColor(this.colors[i]);
            g.drawString(String.valueOf(this.words[i]), this.xx[i], this.yy[i]);
        }
    }

(6)线程函数run()

  • for循环,如果超出下边界,使yy = 0,即重新生成新的字母 
public void run() {
        while(true) {
            for(int i = 0; i < 10; ++i) {
                this.yy[i]++;
                if (this.yy[i] > 600) {
                    this.yy[i] = 0;
                }
            }

            try {
                Thread.sleep((long)this.speed);
            } catch (InterruptedException var2) {
                var2.printStackTrace();
            }

            this.repaint();
        }
    }

(7)按键函数KeyPressed()

  • 如果我们按键的字母和画布上的字母匹配,则该字母“消失”:xx = (int)(Math.random() * 26 + 65); yy = 0
  • 分数score + 1
  • 如果我们的分数在 5 - 10:speed = 5
  • 如果分数大于10:speed = 1 
 public void keyPressed(KeyEvent e) {
        for(int i = 0; i < 10; ++i) {
            if (e.getKeyCode() == this.words[i]) {
                this.xx[i] = (int)(Math.random() * 800);
                this.yy[i] = 0;
                this.words[i] = (char)((int)(Math.random() * 26 + 65));
                ++this.score;
                break;
            }
        }

        if (this.score > 5 && this.score < 10) {
            this.speed = 5;
        } else if (this.score > 10) {
            this.speed = 1;
        }

        this.repaint();
    }

(8)颜色函数randomColor()

public Color randomColor() {
        int R = (int)(Math.random() * 255);
        int G = (int)(Math.random() * 255);
        int B = (int)(Math.random() * 255);
        Color color = new Color(R, G, B);
        return color;
    }

三、代码

1、Word.class

package Words;

import java.awt.Component;
import javax.swing.JFrame;

public class Word {
    public static void main(String[] args) {
        JFrame frame = new JFrame("打字游戏");
        WordPanel panel = new WordPanel();
        frame.add(panel);
        Thread t = new Thread(panel);
        t.start();
        panel.addKeyListener(panel);
        panel.setFocusable(true);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo((Component)null);
        frame.setDefaultCloseOperation(3);
        frame.setVisible(true);
    }
}

2、WordPanel.class

 

package Words;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;

public class WordPanel extends JPanel implements Runnable, KeyListener{
    int[] xx = new int[10];
    int[] yy = new int[10];
    char[] words = new char[10];
    Color[] colors = new Color[10];
    int score = 0;
    int speed = 10;

    public WordPanel() {
        for(int i = 0; i < 10; ++i) {
            this.xx[i] = (int)(Math.random() * 800);
            this.yy[i] = (int)(Math.random() * 600);
            this.colors[i] = this.randomColor();
            this.words[i] = (char)((int)(Math.random() * 26 + 65));
        }
    }

    public Color randomColor() {
        int R = (int)(Math.random() * 255);
        int G = (int)(Math.random() * 255);
        int B = (int)(Math.random() * 255);
        Color color = new Color(R, G, B);
        return color;
    }

    public void paint(Graphics g) {
        super.paint(g);
        Font ft = new Font("微软雅黑", 1, 28);
        g.setFont(ft);
        g.drawString("分数" + this.score, 50, 50);

        for(int i = 0; i < 10; ++i) {
            g.setColor(this.colors[i]);
            g.drawString(String.valueOf(this.words[i]), this.xx[i], this.yy[i]);
        }

    }

    public void run() {
        while(true) {
            for(int i = 0; i < 10; ++i) {
                this.yy[i]++;
                if (this.yy[i] > 600) {
                    this.yy[i] = 0;
                }
            }

            try {
                Thread.sleep((long)this.speed);
            } catch (InterruptedException var2) {
                var2.printStackTrace();
            }

            this.repaint();
        }
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        for(int i = 0; i < 10; ++i) {
            if (e.getKeyCode() == this.words[i]) {
                this.xx[i] = (int)(Math.random() * 800.0D);
                this.yy[i] = 0;
                this.words[i] = (char)((int)(Math.random() * 26 + 65));
                ++this.score;
                break;
            }
        }

        if (this.score > 5 && this.score < 10) {
            this.speed = 5;
        } else if (this.score > 10) {
            this.speed = 1;
        }

        this.repaint();
    }

    public void keyReleased(KeyEvent e) {
    }
}

 

你可能感兴趣的:(Java,java,swing)