JAVA图形化打字小游戏
不多废话,直接上代码
程序实际运行图片如下,可供参考。
Main.java
package 打字游戏;public class Main {public static void main(String[] args) {Windows w1=new Windows("欢迎");}}
Windows.java
package 打字游戏;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;import javax.swing.*;public class Windows extends JFrame implements ActionListener,Runnable{//这里是开始游戏窗口JButton Button1;//开始按钮JButton Button2;JLabel CurrentTimeLabel=new JLabel();//JPanel Panel1=new JPanel();//Thread T;//Windows(String Title){setBounds(400,200,300,200);//距左边有多少像素,距上边有多少像素,宽,高
// 宽
// ——————————————————————————
// |
// |
// 高 |
// |
// |setTitle(Title);setLayout(new FlowLayout(FlowLayout.CENTER,20,50));//整数参数是水平间隙和垂直间隙Button1=new JButton("开始游戏");Button2=new JButton("帮助菜单");add(Button1);add(Button2);Button1.addActionListener(this);//this 指的是当前类的这个对象 (这里看不到这个类的对象,会在Main中创建)Button2.addActionListener(this);setDefaultCloseOperation(EXIT_ON_CLOSE);//不继承JFrame类这个常量会报错 这里写成3也是可以的setVisible(true);Panel1.add(CurrentTimeLabel);//add(Panel1);//T=new Thread(this);//T.start();//}Windows(){}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()==Button1){ GameWindow w1=new GameWindow("游戏界面");Button1.setEnabled(false);//游戏界面类 见GameWindow类}if(e.getSource()==Button2){new HelpWindow("帮助菜单");}}@Overridepublic void run() {// TODO Auto-generated method stubwhile(true){Calendar C=Calendar.getInstance();int mHour=C.get(Calendar.HOUR);int mMin=C.get(Calendar.MINUTE);int mSec=C.get(Calendar.SECOND);int mTotalTime=mHour*3600+mMin*60+mSec;CurrentTimeLabel.setText("现在时间:"+(mHour+12)+":"+mMin+":"+mSec);}}}
HelpWindow.java
package 打字游戏;
import java.awt.*;
import javax.swing.*;public class HelpWindow extends Windows{HelpWindow(String Title){setTitle(Title);setBounds(400,300,1000,300);JTextArea Area=new JTextArea();String Help=new String("游戏规则\n"+ " 1.游戏分为5个等级,每个等级分为两关,正确率高于当前关卡,时间未超出当前关卡限制且输入字符长度一致可以进入下一关\n"+ " 2.每个等级所需正确率为80% 70% 60% 50% 40%,等级越高容错率越高\n"+ " 3.每个等级最长允许时间为10s,15s,18s,20s,25s\n"+ " 4.当全部5个等级通过之后即可通关\n"+ "\n游戏操作指南\n"+ " 点击开始游戏按钮以开始游戏,鼠标点击答案文本框后开始计时,游戏结束后点击结束游戏以退出游戏。\n");Area.setText(Help);Area.setFont(new Font("楷体",Font.PLAIN,17));Area.setForeground(Color.blue);add(Area);setVisible(true);setDefaultCloseOperation(HIDE_ON_CLOSE);}
}
GameWindow.java
package 打字游戏;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;public class GameWindow extends Windows implements ActionListener{//界面设计如下:
// 标签1(按一次输出一次)
// 标签2(题目)
// 标签3(答案)
// 按钮1(开始)
// 按钮2(提交答案)
// 按钮3(结束)
// 文本框1(显示信息)
// 文本框2(显示要输入的字符)
// 文本框3(获取用户输入的字符串)//文本框4 (反馈出错误信息)static JButton Button1,Button2,Button3;JLabel Label1,Label2,Label3,Label4,Label5;static JTextField Text1,Text2,Text3,Text4;static JProgressBar Pgb;JPanel FirstLine,SecondLine,ThirdLine,ForthLine,FifthLine;//这里的对象都没有初始化,直接使用会报错,必须经过初始化才能使用public GameWindow(String Title){//对象初始化Button1=new JButton("开始游戏");Button2=new JButton("提交答案");Button3=new JButton("结束游戏");Label1=new JLabel("提示");Label2=new JLabel("文本栏");Label3=new JLabel("输入栏");Label4=new JLabel("Tips:不用担心时间,只有当你开始打字才计时");Label5=new JLabel("错误反馈");Text1=new JTextField(40);Text2=new JTextField(20);Text3=new JTextField(20);Text4=new JTextField(40);Text1.setText("欢迎游玩,如有不足之处请多多包涵");Text1.setFont(new Font("楷体",Font.BOLD, 16));//Font类构造函数 调整字体的 BLOD粗体 PLAIN不加粗 ITALIC斜体// public Font(String name,// int style,// int size)Text2.setText(" 题 目 区 域 ");Text2.setFont(new Font("楷体",Font.BOLD, 22));Text3.setText(" 答 题 区 域 ");Text3.setFont(new Font("楷体",Font.BOLD, 22));Text4.setText("错误信息在这里");Text4.setFont(new Font("楷体",Font.BOLD, 18));Pgb=new JProgressBar();FirstLine=new JPanel();SecondLine=new JPanel();ThirdLine=new JPanel();ForthLine=new JPanel();FifthLine=new JPanel();Label4.setFont(new Font("楷体",Font.ITALIC,18));setTitle(Title);setBounds(400,250,550,400);setLayout(new GridLayout(5,1));//五行一列//初始化部分结束try {//FirstFirstLine.setLayout(new FlowLayout());//顺序放入,从左往右Pgb.setMinimum(0);Pgb.setMaximum(10);Pgb.setValue(0);Pgb.setStringPainted(true);FirstLine.add(Label1);FirstLine.add(Text1);FirstLine.add(Pgb);//SecondSecondLine.setLayout(new FlowLayout());SecondLine.add(Label2);SecondLine.add(Text2);SecondLine.add(Button1);//ThirdThirdLine.setLayout(new FlowLayout());ThirdLine.add(Label3);ThirdLine.add(Text3);ThirdLine.add(Button2);ThirdLine.add(Button3);//ForthForthLine.add(Label4);//就是一个提示栏 不用给格式//FIfth 实际放在Forth上面 起到错误反馈作用FifthLine.add(Label5);FifthLine.add(Text4);add(FirstLine);Text1.setForeground(Color.black);add(SecondLine);Text2.setForeground(Color.blue);add(ThirdLine);Text3.setForeground(Color.black);add(FifthLine);add(ForthLine);//向窗口加入组件,设置文字颜色Button2.setEnabled(false);Button1.addActionListener(this);Button2.addActionListener(this);Button3.addActionListener(this);Text3.addActionListener(this);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}catch(NullPointerException e){new EndWindows("对象未创建成功,请关闭窗口检查之后重新运行");}}Player player1=new Player();@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()==Button1){Button2.setEnabled(true);player1.TestTextInput();//题目创建Text2.setText(player1.String2);Text3.setText("");//清空初始文字,便于输入Text4.setText("");//反馈player1.StartTime();Text2.setEditable(false);}if(e.getSource()==Button2){player1.EndTime();player1.String3=Text3.getText();//读取输入player1.TextInput();Text3.setText("");player1.TestTextInput();Text2.setText(player1.String2);}if(e.getSource()==Button3){System.exit(0);//结束游戏按钮}if(e.getSource()==Text3)//回车的输入支持{player1.EndTime();player1.String3=Text3.getText();player1.TextInput();Text3.setText("");player1.TestTextInput();Text2.setText(player1.String2);player1.StartTime();}Text3.addMouseListener(new MouseListener() {public void mousePressed(MouseEvent e) {// 鼠标按下if(Text3.getText().equals("")) {player1.StartTime();//开始计时 清空文本框时重置计时}}@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}});}}
Game.java
package 打字游戏;
import java.util.*;public class Game implements Character{String String_Text(int level){int Length=0;int temp=0;//存随机数,以它为下标找字符库的字母String Text="";if(level==1){Length=3;}if(level==2){Length=5;}if(level==3){Length=7;}if(level==4){Length=10;}if(level==5){Length=12;}Random random=new Random();for(int i=0;i<Length;i++){temp=random.nextInt(25);//帮助文档 返回一个伪随机数,它是从此随机数生成器的序列中取出的、在 0(包括)和指定值(不包括)之间均匀分布的 int值。Text+=Character[temp];}
// Text+="\0";return Text;}
}
Player.java
package 打字游戏;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;public class Player implements Character{char Input[];static String String1;//给Text1的信息static String String2;//Text2static String String3;//Text3int level=1;int currentlevel=0;Boolean A=true;//能够继续游戏的条件double Correct=0d;//正确率double TimeStart=0d;double TimeEnd=0d;double TimeDifference=0d;double TimeAll=0d;int Pgb=0;//进度条void TestTextInput(){Game G=new Game();String2=G.String_Text(level);}void StartTime(){Date Date_Start=new Date();TimeStart=Date_Start.getTime()/1000d;}void EndTime(){Date Date_End=new Date();TimeEnd=Date_End.getTime()/1000d;TimeDifference=TimeEnd-TimeStart;TimeAll+=TimeDifference;}String Feedback(String StrQ,String StrA){String Back=new String("输入有误,输入为"+ StrA+" 答案为"+ StrQ);return Back;}void TextInput() {if(A){Correct=Game_Continue(String2,String3);int Correctint=(int)(Correct*100);int TimeDifferenceint=(int)(TimeDifference);int TimeAllint=(int)TimeAll;String1="级别 "+level+"正确率 "+Correctint+"% 目前耗时"+TimeDifferenceint+"s 总耗时"+TimeAllint+"s";GameWindow.Text1.setText(String1);}else{EndWindows End=new EndWindows("游戏结束");GameWindow.Text2.setEditable(false);GameWindow.Text3.setEditable(false);GameWindow.Text4.setEditable(false);GameWindow.Button1.setEnabled(false);GameWindow.Button2.setEnabled(false);End.EndLabel.setForeground(Color.black);}}double Game_Continue(String Str2,String Str3){try{if(Str2.length()==Str3.length())//判断字符串长度是否一致{char text[]=Str2.toCharArray();char input[]=Str3.toCharArray();double count=0d;for(int i=0;i<text.length;i++){if(text[i]==input[i]){count++;}}if(count!=text.length)//反馈错误{GameWindow.Text4.setText(Feedback(Str2,Str3));}if(level==1&¤tlevel==0)Correct=count/text.length;elseCorrect=(Correct+count/text.length)/2.0;if(LevelMaxTime[level-1]>=TimeDifference&&LevelMinCorrect[level-1]<=Correct){currentlevel++;Pgb++;GameWindow.Pgb.setValue(Pgb);if(currentlevel%2==0){level++;currentlevel=0;if(level==6){level--;GameWindow.Text3.setEditable(false);GameWindow.Text4.setEditable(false);GameWindow.Button1.setEnabled(false);GameWindow.Button2.setEnabled(false);GameWindow.Pgb.setValue(10);EndWindows End=new EndWindows("恭喜你通关了");End.EndLabel.setForeground(Color.black);}}}else{if(LevelMaxTime[level-1]<TimeDifference){EndWindows End=new EndWindows("当前轮用时过长,请再接再厉");GameWindow.Text2.setEditable(false);GameWindow.Text3.setEditable(false);GameWindow.Text4.setEditable(false);GameWindow.Button1.setEnabled(false);GameWindow.Button2.setEnabled(false);End.EndLabel.setForeground(Color.black);}if(LevelMinCorrect[level-1]>Correct){EndWindows End=new EndWindows("您的正确率过低,请再接再厉");GameWindow.Text2.setEditable(false);GameWindow.Text3.setEditable(false);GameWindow.Text4.setEditable(false);GameWindow.Button1.setEnabled(false);GameWindow.Button2.setEnabled(false);End.EndLabel.setForeground(Color.black);//debug//System.out.print(Str2.length()+" "+Str3.length()+" "+LevelMinCorrect[level-1]+" "+Correct);}A=false;}}else{EndWindows End=new EndWindows("输入长度有误,请重新开始游戏");GameWindow.Text2.setEditable(false);GameWindow.Text3.setEditable(false);GameWindow.Text4.setEditable(false);GameWindow.Button1.setEnabled(false);GameWindow.Button2.setEnabled(false);End.EndLabel.setForeground(Color.black);//debug//System.out.print(Str2.length()+" "+Str3.length());}}catch(NullPointerException e){GameWindow.Text2.setEditable(false);GameWindow.Text3.setEditable(false);GameWindow.Text4.setEditable(false);GameWindow.Button1.setEnabled(false);GameWindow.Button2.setEnabled(false);new EndWindows("Error 程序中有对象未初始化,请重新开始游戏");}catch(Exception e) {GameWindow.Text2.setEditable(false);GameWindow.Text3.setEditable(false);GameWindow.Text4.setEditable(false);GameWindow.Button1.setEnabled(false);GameWindow.Button2.setEnabled(false);new EndWindows("发生错误,请退出后重新进入");}return Correct;}}
EndWindows.java
package 打字游戏;import java.awt.*;
import java.awt.event.*;import javax.swing.*;public class EndWindows extends Windows{//结束画面JLabel EndLabel;EndWindows(String Title){setTitle(Title);setBounds(400,200,300,200);setLayout(new FlowLayout());EndLabel=new JLabel();EndLabel.setText(Title);add(EndLabel);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}}
Character.java
package 打字游戏;public interface Character {//接口提供字符库,游戏中采用的题目字符串为随机生成char Character []= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};//自动补上public static final//以后拓展这个地方,比如说读取文件,直接改成四六级背词宝典double LevelMinCorrect[]= {0.8,0.7,0.6,0.5,0.4};int LevelMaxTime[]= {10,15,18,20,25};}
运行于JavaSE-1.8下,高版本会报错。
JAVA图形化打字小游戏相关推荐
- 基于Ubuntu+Bochs模拟器实现的操作系统图形化的小游戏(2048、flappybird、)
目录 操作系统课程设计文档 1 项目设计与功能说明文档 1 一. 项目描述 2 项目目的 2 开发环境 2 项目完成指标 3 一. 功能说明 3 图形化界面 3 开机动画 4 图片显示 4 进程管理 ...
- JAVA实现打字小游戏
目录 一.效果 二.教程 三.代码 一.效果 首先我们先看效果,左上角的分数是用来记录我们打对了多少字母.字母是从上面开始往下落.每打对一个字母,分数增加,增加到一定分数后,字母下落的速度也会增加.( ...
- java实验2总结心得,打字小游戏JAVA实验总结及心得体会
篇一:扫雷游戏实验报告 课程设计 班 级: 姓 名: 学 号: 指导教师: 成 绩: 电子与信息工程学院信息与通信工程系 目录 1.任务概述------------------------------ ...
- java 2048游戏_JAVA2048游戏 本课程设计是基于java语言的2048小游戏设计 联合开发网 - pudn.com...
JAVA2048游戏 所属分类:游戏 开发工具:Java 文件大小:789KB 下载次数:4 上传日期:2020-11-23 10:57:11 上 传 者:滴滴滴大萌 说明: 本课程设计是基于jav ...
- java图形化界面(GUI)
java图形化界面(GUI) 一.AWT编程(抽象窗口工具集) 1.AWT继承体系 2.Container容器 2.1 Container继承体系 2.2 常见API 2.3 容器演示 2.3.1 w ...
- Java图形化界面设计——容器(JFrame)
Java图形化界面设计--容器(JFrame) 程序是为了方便用户使用的,因此实现图形化界面的程序编写是所有编程语言发展的必然趋势,在命令提示符下运行的程序可以让我们了解java程序的基本知识体系结构 ...
- Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇)
Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇) 代码实现 窗口类 小车类 玩家类 电脑类 赛道类 小树类 打字类 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) ...
- Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇)
Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇) 资源下载 完整代码 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) Python 打字小游戏开发,来体验不一样的 ...
- JAVA课程设计(小游戏贪吃蛇)完整源码附素材(二)
目录 JAVA课程设计(小游戏贪吃蛇)完整源码附素材(一) JAVA课程设计(小游戏贪吃蛇)完整源码附素材(二) JAVA课程设计(小游戏贪吃蛇)完整源码附素材(三) 前言 1. 任务描述 1.1 ...
最新文章
- 吸引:由《你的知识需要管理》编辑过程想到的
- linux 时钟同步 chrony 简介
- jenkins 配置使用
- java实现计算机图形学中点画线算法
- CloudStack部署运维手册V2 --- 新鲜出炉
- 为Qt Designer创建自定义Widgets小部件
- winform模拟鼠标点击
- python连接不上数据库_详解pycharm连接不上mysql数据库的解决办法
- 二维haar小波matlab_洪泽湖入湖水沙序列的多时间尺度小波分析
- Oracle 创建表空间与用户
- Eddy Cue称FBI可能会要求苹果激活iPhone摄像头并打开麦克风
- 23种设计模式(1):单例模式
- vmware station密钥
- 破解win7开机密码
- 《别做正常的傻瓜》的一些读书心得
- 26岁,2020 - 观《人生七年》
- 如何查看mysql技术文档_数据库
- 根据ID取拼多多商品详情 的测试过程
- 图表制作中需要注意哪些问题:认识图表、选择图表
- 如何修改Github仓库的description