目录

一、效果

二、教程

三、代码


一、效果

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

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

二、教程

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

JAVA实现打字小游戏相关推荐

  1. java实验2总结心得,打字小游戏JAVA实验总结及心得体会

    篇一:扫雷游戏实验报告 课程设计 班 级: 姓 名: 学 号: 指导教师: 成 绩: 电子与信息工程学院信息与通信工程系 目录 1.任务概述------------------------------ ...

  2. Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇)

    Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇) 代码实现 窗口类 小车类 玩家类 电脑类 赛道类 小树类 打字类 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) ...

  3. Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇)

    Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇) 资源下载 完整代码 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) Python 打字小游戏开发,来体验不一样的 ...

  4. 不愧是大厂牛人!用Java实现象棋小游戏(附超详细,超长究极无敌代码)

    本文实例为大家分享了java实现象棋小游戏的具体代码,供大家参考,具体内容如下 用Eclipse编写 java环境1.8jdk 代码如下 package xiangqi象棋; /***中国象棋Java ...

  5. Java练手小游戏---黄金矿工

    你玩过4399曾经最火的黄金矿工小游戏吗,黄金矿工小游戏是一款休闲娱乐的一个挖宝游戏,在游戏中地下会有许多的宝藏,你得想办法把它们都挖出来,作为矿工的你,对你来说是轻车熟路的,能不能挖到宝,看你抓取的 ...

  6. JAVA实现2048小游戏

    2048小游戏也算是一款好玩的益智休闲小游戏,下面本博主用 java 语言将该游戏复现,感兴趣的小伙伴点击 关注 哦! 同时博主还用 python 语言复现了该游戏,可点击以下链接浏览博主的另一篇文章 ...

  7. JavaScript-事件和事件对象、实现键盘打字小游戏

    JavaScript-事件和事件对象 一.事件介绍 事件一般是用于浏览器和用户操作进行交互.最早是IE和Netscape Navigator中出现,作为分担服务器端运算负载的一种手段.直到几乎所有的浏 ...

  8. Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇)

    Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) 前言 游戏素材准备 游戏项目结构 项目里面的类说明 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇) Python ...

  9. java文字类小游戏2.0版本

    java文字类小游戏 用javaFx面板显示文字类小游戏,目前正已完成基本打斗和打怪爆出武器的开发,后续会不断更新示例图如下: 运行这个类开始代码我已上传至码云,有需要的小伙伴自行拉取代码,git项目 ...

最新文章

  1. [SQL]死锁处理语句
  2. linux 虚拟机大量udp请求失败_理解 Linux 网络栈:Linux 网络协议栈简单总结分析...
  3. Boost:程序选项program options实例
  4. GNOME Shell Extensions开发介绍
  5. JqueryCookie
  6. 第三章 中间件,3.1 万亿级数据洪峰下的分布式消息引擎(作者:冯嘉、誓嘉、尘央、牟羽)...
  7. 插件一:JAVA微信砍价活动源码分享[商品帮砍到0元,免费领取奖品]
  8. sql子查询示例_学习SQL:SQL查询示例
  9. 同软件多个线程设置不同ip_IP数量不够该如何解决,快试试掘金网ip代理
  10. 《深度学习》花书-读书笔记汇总贴(汇总19/19)
  11. 写给15岁的女儿-乐嘉
  12. 2021年黄石二中高考成绩查询,【黄石二中2018高考金榜】黄石二中2004届高考总结...
  13. 面部表情识别3:Android实现表情识别(含源码,可实时检测)
  14. MySQL的SQL解析器是干什么的?底层原理是什么?
  15. 感谢谦哥的家族为中国相声事业做出了贡献。
  16. 题目 1026: [编程入门]数字逆序输出
  17. konfig:采用ConfigMap实现线上配置热更新
  18. 跟着b站大学学习C语言--哔哩大学计算机学院
  19. 婴幼儿呼吸道感染和发烧
  20. [ZUCC 英语周测]Quiz B-3-7

热门文章

  1. 步进电机五根线怎么接_第一讲:老冯三分钟教会你步进电机接线
  2. bootstrap专栏 03.图文处理 02.图文排版
  3. 稍微好看点的登录页面html
  4. oracle 18c ad登陆,Oracle 18c PSU
  5. vue点击把某个区域变成图片数据
  6. 二极管与、或门,三极管非门电路原理
  7. 永远不能懈怠,要记住,黎明之前,最为黑暗
  8. 工程师必备硬件EMC设计规范
  9. 定时器 java qua_【spring-boot】 springboot整合quartz实现定时任务
  10. android studio编辑环境变量,Android studio gradle环境变量配置教程