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()
	{
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==Button1)
		{	
			
			GameWindow w1=new GameWindow("游戏界面");
			Button1.setEnabled(false);
			//游戏界面类 见GameWindow类
		}
		if(e.getSource()==Button2)
		{
			new HelpWindow("帮助菜单");
		}
	}
	
	
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(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 
		{
			//First
			FirstLine.setLayout(new FlowLayout());//顺序放入,从左往右
			Pgb.setMinimum(0);
			Pgb.setMaximum(10);
			Pgb.setValue(0);
			Pgb.setStringPainted(true);
			FirstLine.add(Label1);
			FirstLine.add(Text1);
			FirstLine.add(Pgb);
			
			//Second
			SecondLine.setLayout(new FlowLayout());
			SecondLine.add(Label2);
			SecondLine.add(Text2);
			SecondLine.add(Button1);
			
			//Third
			ThirdLine.setLayout(new FlowLayout());
			ThirdLine.add(Label3);
			ThirdLine.add(Text3);
			ThirdLine.add(Button2);
			ThirdLine.add(Button3);
			
			//Forth
			ForthLine.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();
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(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();//开始计时 清空文本框时重置计时
				}
			}
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
			}
			@Override
			public 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;//Text2
	static String String3;//Text3
	
	int 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&&currentlevel==0)
					Correct=count/text.length;
				else
					Correct=(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下,高版本会报错。

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值