java实现简易打字游戏
写个博客写的心累啊 原本写好的后来网断了,还忘了保存草稿,然后一顿操作下来。。。。现在在重写的路上。
咋也不多说啥了,直接上效果图吧
项目结构图如下。主要运行代码都在edu.cc.game包下,test包只是一些测试代码
具体代码如下
package edu.cc.game;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.management.loading.PrivateClassLoader;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.xml.stream.events.StartDocument;
import com.sun.org.apache.bcel.internal.classfile.Field;
import sun.rmi.runtime.NewThreadAction;
//主类代码主要有窗口和少数的功能实现
public class PlayGame extends JFrame {
private static Utils util = new Utils();
String srt = null;//监听时用来接收键盘输入的字符串
Container container;//contain用于把窗口覆盖为黑色
JTextField jTextField;//键盘输入框
BorderLayout bl ;//布局
static Word word;//接收getword传来的Word对象
static ArrayList<Word> listwords = new ArrayList<Word>();
public void initGame() {
this.setTitle("打字游戏");
//由于JFrame使用流失布局,所以直接给JFrame谁知的背景色会被布局管理器覆盖,因此使用一个容器覆盖窗体
container = this.getContentPane();
this.setSize(600, 900);
this.setLocation(200, 100);
bl = new BorderLayout();
this.setLayout(bl);
jTextField = new JTextField(12);
container.add(jTextField);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container.setBackground(Color.BLACK);
this.add(BorderLayout.SOUTH,jTextField);
this.setVisible(true);
wordlisten();//监听输入单词
//新建线程用来实现移动功能移动
new Thread() {
@Override
public void run() {
while(true) {
addword();
removeword();
repaint();
try {
//两次重绘间停顿1秒
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
//监听jTextField输入
private void wordlisten() {
jTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
srt = jTextField.getText();
//遍历listWords。如果输入的单词一样就把这个单词移除
for(int i = 0;i<listwords.size();i++) {
if(srt.equals(listwords.get(i).getWord())) {
listwords.remove(i);
jTextField.setText("");
break;
}
}
}
});
}
//移动单词。实现:通过改变单词的y 坐标实现
protected void removeword() {
for (int i=0;i<listwords.size();i++){
int y = listwords.get(i).getY_index();
//每次移动15 像素
y += 15;
listwords.get(i).setY_index(y);
}
}
//把单词对象添加到listwords
protected void addword() {
word = util.getWords();
listwords.add(word);
}
//线程调用repaint 方法时系统自动调用paint方法
@Override
public void paint (Graphics g) {
super.paint(g);
for(int i =0;i<listwords.size();i++) {
Word word = listwords.get(i);
g.setColor(word.getColor());
g.setFont(new Font("楷体", Font.BOLD, 20));
g.drawString(word.getWord(),word.getX_index(),word.getY_index());
}
}
public static void main(String[] args){
PlayGame playgame = new PlayGame();
playgame.initGame();
}
}
工具类代码,实现Word对象的组装与获取
package edu.cc.game;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
//工具类
public class Utils {
private Word word;
//从Word.txt返回所有单词集合
public static List readTxtFile(String filePath){
List linesTxt = new ArrayList<String>();
InputStreamReader read = null ;
BufferedReader bufferedReader = null;
try {//设置读取格式
String encoding="GBK";
//建立读取文件
File file=new File(filePath);
//建立读取通道
read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
//读取操作
bufferedReader = new BufferedReader(read);
String linetxt = null;
//每次读取一行
while((linetxt = bufferedReader.readLine()) != null){
linesTxt.add(linetxt);
}
read.close();
}catch (Exception e) {
System.out.println("读取weord.txt出错");
}
return linesTxt;
}
//从读取的Arraylist里取出Word对象
public Word getWords() {
Random r = new Random();
//利用System对象获取Word.TXT的路径
String path = System.getProperty("user.dir")+"\\word.txt";
//取出单词作为组装Word对象。x坐标和y坐标随机生成
Word word = new Word((String) readTxtFile(path).get(r.nextInt(160)),r.nextInt(500)+20,45);
return word;
}
}
Word对象
package edu.cc.game;
import java.awt.Color;
import java.util.Random;
//单词类,用于包装一些变化的数据
public class Word {
Random r = new Random();
private String word;
private int x_index;
private int y_index;
private Color color;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getX_index() {
return x_index;
}
public void setX_index(int x_index) {
this.x_index = x_index;
}
public int getY_index() {
return y_index;
}
public void setY_index(int y_index) {
this.y_index = y_index;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Word(String word, int x_index, int y_index) {
super();
this.word = word;
this.x_index = x_index;
this.y_index = y_index;
this.color = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
}
public Word() {
super();
}
@Override
public String toString() {
return "Word [word=" + word + ", x_index=" + x_index + ", y_index=" + y_index + ", color=" + color + "]";
}
}
这个小游戏主要用来巩固下基础,如果感觉自己基础不牢靠的朋友可以借鉴下
源码地址如下:
https://github.com/zzc-2180886679/playWordGame
可直接运行