java打字游戏

java打字游戏,第1张

java打字游戏 打字游戏 内部代码 DisplayCharacter 接口
package 打字游戏;

public interface DisplayCharacter {
	
	
	//随机生成字符的范围
	public static final char displayCharacter [] = {
			'1','4','6','9','@',
			'(',')','=','+','*',
			'{','}','2','3','0',
			'7','5','8','G','H',
			'Y',']','/','#','^'
			
	};
	

	//各级别每行输入最大时长[秒_s]
	int levelMaxDuration [] = {10,15,13,12};
	
	
	//各级别最小正确率
	double levelMinCorrect [] = {0.80,0.70,0.65,0.60};
	
		
}

Game类
package 打字游戏;
import java.util.Random;
public class Game implements DisplayCharacter{

	//随机生成字符串
	String Display(int level,char testCharacter []) {
		
		
		int tempSum =0,i=0;
		switch (level)
		{
			case 1 :tempSum = 3;break;
			case 2 :tempSum = 5;break;
			case 3 :tempSum = 7;break;
			case 4 :tempSum = 10;break;
		}
		
		Random random = new Random();
		int  tempRandom= random.nextInt(25)  ;//此处生成随机数0-24
		
		  String dis = "";//显示窗口的字符串
		
		while(i 
Player类 
package 打字游戏;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.*;


public class Player implements DisplayCharacter{
//结束判定条件
	static boolean startOrEnd= true;
	static boolean sub = true;
	
	
	
//随机数生成空间大小
	static int MAXSIZE = 25;
	
//游戏窗口界面信息
	static char inputCharacter [] ;
	
	static String  inputString = "";	//text1中数据--正确率-一轮时间--总时间等
	static String  dis = "";			//text2 --待输入字符
	static String  information = "";	//用户输入字符
	
	static double correctRate = 1 ;//正确率

//等级
	static int level = 1;		
	static int Templevel = 0;

	
//计时
	static double timeOne;
	static double timeSecond;
	static int InputRate = 0;//输入速率
	static int SInputRate = 0;	
	
//进度条
	static int PROJB=0;
	
	

//游戏规则
		static 	String Rule = 	
				"----------------------------------游戏规则--------------------------------n"+
				"在相应的时间内保持正确率输入显示的字符n"    +
				"每关两轮n"    +		
				"关卡t最大时间t最低正确率n" + 
				"第一关:t10st80%tn" +
				"第二关:t15st70%tn " +		
				"第三关:t13st65%tn " +
				"第四关:t12st60%tnn " + 
				"----------------------------------n" + 
				"--点击游戏开始--n" + 
				"--光标移入'输入栏'开始计时--n" + 
				"--在输入中一栏中输入上一栏显示字符--n" + 
				"--回车或点击提交,计时结束进入下一轮--n" + 

				"------------------------------------------------------------------------n";
				

//存放随机生成字符
	char testCharacter [] = new char [MAXSIZE];
	Scanner sc = new Scanner(System.in);

	//1.生成随机数组
	void TestInput1()
	{
		Game G = new Game();
		dis = G.Display(level, testCharacter);//根据当前级别生成一行随机字符

	}
	
	//2.计时开始
	void StartTiming() {
		
		Date date0  = new Date ();
		 timeOne = date0.getTime()/1000.0;//改成秒数
	}
	
	//3.计时结束
	void EndTiming()
	{
		Date date1  = new Date ();
		 timeSecond = date1.getTime()/1000.0;//改成秒数
		//计算时差
		InputRate =(int)(timeSecond-timeOne);// double -> int 
		SInputRate =SInputRate +  InputRate;
	}
	
	
	
	
	//4.向游戏界面输入当前判断结果
	void TestInput ()
	{
			if(sub) 
			{
				//计算正确率
				correctRate = StartOrEnd(information, dis);//调用计算正确率函数 -- 返回计算结果
				
				int Cor = (int)(correctRate*100);	//将获取的double 类型正确率  *100 转化成 int 类型
				
				//将获取的玩家数据整合
				inputString = "当前级别" + level + "  正确率:" + Cor + "%" 
							+ "  当前耗时:" + InputRate + "  总耗时" + SInputRate;
				
				//数据写入第一个文本框
				GameWindow.text1.setText(inputString);
			}else {
				EndWindow endw = new EndWindow("游戏结束");//d出结束窗口
			}
	
	}
	
	
	//5.是否结束--正确率--速率[时长]
	double StartOrEnd(String information,String dis) 
	{
		if(information.length()==dis.length())
		
		{
			char inputCharacter1[] = information.toCharArray();
			char testCh[] = dis.toCharArray();

			//根据当前输入判断总正确率
			int i = 0 ;
			double con=0;
			while(i=InputRate)&&(levelMinCorrect[level-1]<=correctRate))
			{
				
						Templevel++;//当前轮+1
						PROJB ++;//进度条状态
						GameWindow.progb.setValue(PROJB);//设置进度条状态
						
						if(Templevel%2==0)
						{
							
							level++;//级别升级
							Templevel = 0;//当前轮清空--2轮1关
							
							//通关
							if(level==5)
							{
								GameWindow.progb.setValue(10);
								EndWindow endw3 = new EndWindow("恭喜玩家通关");
								endw3.ENDTEXT.setForeground(Color.blue);	
							}
						}
				
			}
			else
			{
						if(levelMaxDuration[level-1]correctRate||Double.isNaN(correctRate))//NaN 
						{
							EndWindow endw2 = new EndWindow("非常抱歉正确率过低,您的闯关失败,请您再接再厉");
							endw2.ENDTEXT.setForeground(Color.red);
							
							sub = false;
						}

			}
		}else
		{
			//长度判断
			EndWindow endw4 = new EndWindow("长度不符,请重新输入!");
			endw4.ENDTEXT.setForeground(Color.red);
		}
		
		return  correctRate;
	}

}

图形化界面 StartWindow
package 打字游戏;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;


public class StartWindow extends Jframe implements ActionListener{
		//按钮
		JButton btn1 ;
		JButton btn2 ;
	
		 public StartWindow(String title)
		{

			//创建容器
		
			
			setBounds(400, 200, 300, 200);
			//initialwindow.setBackground(Col);
			setTitle(title);
			//更改默认布局管理器
			setLayout(new FlowLayout(FlowLayout.CENTER,100,20));
			
		
			btn1 = new JButton("开始游戏");
			btn2 = new JButton("详细规则");
			
			
			add(btn1);add(btn2);
			
			//给按钮加监听
			btn1.addActionListener(this);
			btn2.addActionListener(this);
		
			//关闭窗口按键
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			
			this.setVisible(true);
			
		}
		 
		 
	  
		 @Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(e.getSource()==btn1)
				{
					//更改数据 -- 简单
					new GameWindow("Game");
					//this.gameWindow("游戏");
				}
				if(e.getSource()==btn2)
				{
					//游戏规则
					RuleWindow endw = new RuleWindow("游戏规则");
				}
				
		
			}
			
}

GameWindow
package 打字游戏;

import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.Jframe;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

public class GameWindow extends Jframe implements ActionListener {
	
			 JButton gameStart = new JButton("游戏开始");
			 JButton submit = new JButton("提交");
			 JButton gameEnd   = new JButton("游戏结束");

			 JLabel lab1 = new JLabel("信息 	");;
			 JLabel lab2 = new JLabel("待输入");;
			 JLabel lab3 = new JLabel("输入中");;
			 
			 static JTextField text1 = new JTextField(30);//创建文本框,指定可见列数为12列
			 static JTextField text2 = new JTextField(30);
			 static JTextField text3 = new JTextField(30);
			 
			 static JProgressBar progb = new JProgressBar();//进度条
			 static final int MIN_PROGRESS = 0;//最小进度值
			 static final int MAX_PROGRESS = 10;//最大进度值
		
			 

			 
			 //容器组件
			 JPanel firstLine  = new JPanel();
			 JPanel secondLine = new JPanel();
			 JPanel thirdLine  = new JPanel();
			 JPanel FourtLine  = new JPanel();
		
			 
			  public  GameWindow(String title) {
				//创建容器
					super(title);
					setBounds(400, 200, 600, 400);
					//gamewindow.setSize(500,500);
					
					
					//更改默认布局管理器
					this.setLayout(new GridLayout(4,1));
		
					
					//第一行
					firstLine.setLayout(new FlowLayout(FlowLayout.LEADING,5,10));
					progb.setMinimum(MIN_PROGRESS);
					progb.setMaximum(MAX_PROGRESS);
					progb.setValue(0);progb.setStringPainted(true);
					firstLine.add(lab1);
					firstLine.add(text1);
					firstLine.add(progb);
					
					//第二行
					secondLine.setLayout(new FlowLayout(FlowLayout.LEADING,5,10));
					secondLine.add(lab2);
					secondLine.add(text2);
					secondLine.add(gameStart);
					
					//第三行
					thirdLine.setLayout(new FlowLayout(FlowLayout.LEADING,5,10));
					thirdLine.add(lab3);
					thirdLine.add(text3);
					thirdLine.add(submit);
				
					//第四行
					FourtLine.add(gameEnd);
					this.setLayout(new FlowLayout(FlowLayout.LEADING,5,10));
					this.add(firstLine);
					this.add(secondLine);
					this.add(thirdLine);
					this.add(FourtLine);
					
					
					//设置文本框文字颜色
					text1.setForeground(Color.black);
					text2.setForeground(Color.red);
					text3.setForeground(Color.blue);
					//增加事件监听
					gameStart.addActionListener(this);
					gameEnd.addActionListener(this);
					thirdLine.add(text3);
					submit.addActionListener(this);
					text3.addActionListener(this);

					//	this.pack();
					//关闭窗口按键
					setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
					//可视化
					this.setVisible(true);
			 }

			  //玩家
				static Player p1 = new Player();
				
			  
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				//游戏开始
				if(e.getSource()==gameStart)
				{
				
					p1.TestInput1();//生成
					text2.setText(Player.dis);//显示
					p1.StartTiming();//开始计时
					
				}
				
				//提交
				if(e.getSource()==submit)
				{
					p1.EndTiming();//停止计时
					
					Player.information = text3.getText();//获取输入信息

					p1.TestInput();
					
				
					text3.setText("");//清空输入栏
				
							
					p1.TestInput1();//生成随机数
				text2.setText(Player.dis);//显示
				}
				
				
				//游戏结束
				if(e.getSource()==gameEnd) {
					System.exit(0);
				}
				

				//回车触发--判断
				if(e.getSource()==text3)
				{//回车触发
					p1.EndTiming();//停止计时
					Player.information = text3.getText();//获取输入信息
					p1.TestInput();
					text3.setText("");//清空输入栏
					p1.TestInput1();//生成随机数
					text2.setText(Player.dis);//显示
					p1.StartTiming();//开始计时
				}
				
				
				//鼠标进入文本框,按时文本框为空,开始计时
				text3.addMouseListener(new MouseListener() {
					@Override
					public void mousePressed(MouseEvent e) {// 鼠标按下
						if(text3.getText().equals("")) {
							//System.out.println("-->" + text3.getText() + "<--" + "文本框空,鼠标按下");
							p1.StartTiming();//开始计时
						}
						
					
					}
					@Override// 鼠标进入组件
					public void mouseEntered(MouseEvent e) {}
					@Override// 鼠标单击事件
					public void mouseClicked(MouseEvent e) {}
					@Override// 鼠标松开
					public void mouseReleased(MouseEvent e) {}
					@Override// 鼠标退出组件
					public void mouseExited(MouseEvent e) {}
				});
			}
}

EndWindow
package 打字游戏;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Jframe;
import javax.swing.JTextField;

public class EndWindow extends Jframe implements ActionListener{

	//生成d出窗口界面
	JTextField ENDTEXT = new JTextField();
	  public  EndWindow(String title) {
			//创建容器
				super(title);
				setBounds(400, 200, 300, 100);
				//gamewindow.setSize(500,500);

				//更改默认布局管理器
				this.setLayout(new FlowLayout());
	
				ENDTEXT.setText(title);//设置文本框内容
				add(ENDTEXT);
				

				//关闭窗口按键
				setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
				//可视化
				this.setVisible(true);
		 }

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
	}
}
RuleWindow
package 打字游戏;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Jframe;
import javax.swing.Jtextarea;
import javax.swing.JTextField;

public class RuleWindow extends Jframe implements ActionListener{
	 public  RuleWindow(String title) {
			//创建容器
				super(title);
				setBounds(400, 200, 600, 400);
				//gamewindow.setSize(500,500);
				
				
				//更改默认布局管理器
				this.setLayout(new FlowLayout());
				
				Jtextarea JText = new Jtextarea(Player.Rule,15,40);
				JText.setFont(getFont());
				JText.setForeground(Color.RED);
				add(JText);
				
				
				//关闭窗口按键
				setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
				//可视化
				this.setVisible(true);
		 }
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
Test
package 打字游戏;
import java.util.Scanner;
public class Test {
	public static void main (String args[]) {
		
		 StartWindow T1 = new StartWindow("初始窗口");//打开初始窗口
	}
}
运行界面






欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5637825.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存