JAVA实现打字小游戏

目录

一、效果

二、教程

三、代码


一、效果

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

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

 

二、教程

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

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

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) {
    }
}

 

  • 31
    点赞
  • 191
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
好的,以下是一个简单的Quickhit项目案例中的Java打字游戏代码,供您参考: 1. 在VSCode中创建一个新的Java文件,保存为“TypingGame.java”,并添加以下代码: ```java import java.util.Scanner; public class TypingGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = "Quickhit 是一个快速开发工具集,可用于构建 Web 应用程序、移动应用程序和后端服务。"; int timeLeft = 30; int score = 0; boolean gameStarted = false; System.out.println("欢迎来到 Quickhit 打字游戏!"); System.out.println("请在下方输入框中输入以下内容:"); System.out.println(text); while (timeLeft > 0) { if (!gameStarted) { System.out.println("游戏开始!"); gameStarted = true; } System.out.print("> "); String input = scanner.nextLine(); if (input.equals(text.substring(score))) { score++; int newTextLength = text.length() - score; String newText = text.substring(score, score + newTextLength); System.out.println(newText); } timeLeft--; } System.out.println("游戏结束!您的得分为 " + score + " 分!"); } } ``` 2. 在VSCode中打开终端,进入Java文件所在的目录,并编译运行该文件。可以使用以下命令: ```bash javac TypingGame.java java TypingGame ``` 3. 在控制台中运行该打字游戏,玩家需要在30秒内尽可能多地输入给定的文本,最终得分将在游戏结束后显示。您可以根据自己的需求和想法对代码进行修改和扩展。 以上代码实现了一个简单的Java打字游戏,玩家需要在30秒内尽可能多地输入给定的文本,最终得分将在游戏结束后显示。您可以根据自己的需求和想法对代码进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值