Java JUI打字小游戏项目
Posted 抹泪的知更鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java JUI打字小游戏项目相关的知识,希望对你有一定的参考价值。
JUI打字游戏
效果展示
游戏页面
暂停图
游戏结束页面
素材
链接: 单词素材
提取码: 95a8
链接:图片素材
提取码: 7s1u
代码实现
子弹类:
package www.git;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
/*
*@author Liu
*@Description 创建子弹类
*@data 2021/12/8
*/
public class Bullet
private int x;
private int y;
private int speed;
public static BufferedImage image;
static
try
image = ImageIO.read(Bullet.class.getResourceAsStream("1bullet.png"));
catch (IOException e)
e.printStackTrace();
public void step()
this.y -= this.speed;
public Bullet(int x)
this.x = x;
this.y = Typer.HEIGHT;
this.speed = 12;
public int getX()
return x;
public void setX(int x)
this.x = x;
public int getY()
return y;
public void setY(int y)
this.y = y;
public int getSpeed()
return speed;
public void setSpeed(int speed)
this.speed = speed;
public BufferedImage getImage()
return image;
public void setImage(BufferedImage image)
this.image = image;
单词课程类
```java
package www.git;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
/*
*@author Liu
*@Description
*@data 2021/12/7
*/
public class Course
private String name;//第一行的内容
private String content;//第二行的内容
private List<Word> list;
private int index;
public Course(File file, int index) throws IOException
if (!file.exists())
System.out.println("文件不存在!");
return;
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
BufferedReader br = new BufferedReader(isr);
this.name=br.readLine().trim();
this.content=br.readLine().trim();
this.list=new LinkedList<>();
String line=null;
while ((line=br.readLine()) != null)
line=line.trim();
if (line.length()!=0)
String[] s = line.trim().split("\\\\s+");
Word word = new Word(s[1], s[0]);
this.list.add(word);
this.index=index;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getContent()
return content;
public void setContent(String content)
this.content = content;
public List<Word> getList()
return list;
public void setList(List<Word> list)
this.list = list;
public int getIndex()
return index;
public void setIndex(int index)
this.index = index;
@Override
public String toString()
return index+"."+this.name;
单词类
package www.git;
import java.util.Random;
/*
*@author Liu
*@Description
*@data 2021/12/7
*/
public class Word
private int x;
private int y;
private int width;
private int height;
private int speed;
private String chinese;
private String english;
private boolean match;
public boolean isMatch()
return match;
public void setMatch(boolean match)
this.match = match;
public Word(String chinese, String english)
this.chinese = chinese;
this.english = english;
int max = Math.max(chinese.length() * 2, english.length());
this.width = max * 16;//字号*两者的较大值
this.height = 2 * 16 + 2;
Random random = new Random();
this.x = random.nextInt(800 - this.width);//窗口的宽度
this.y = -this.height;
this.speed = random.nextInt(3) + 1;
public void move()
this.y+=this.speed;
@Override
public String toString()
return "Word" +
"x=" + x +
", y=" + y +
", width=" + width +
", height=" + height +
", speed=" + speed +
", chinese='" + chinese + '\\'' +
", english='" + english + '\\'' +
'';
public int getX()
return x;
public void setX(int x)
this.x = x;
public int getY()
return y;
public void setY(int y)
this.y = y;
public int getWidth()
return width;
public void setWidth(int width)
this.width = width;
public int getHeight()
return height;
public void setHeight(int height)
this.height = height;
public int getSpeed()
return speed;
public void setSpeed(int speed)
this.speed = speed;
public String getChinese()
return chinese;
public void setChinese(String chinese)
this.chinese = chinese;
public String getEnglish()
return english;
public void setEnglish(String english)
this.english = english;
功能类
package www.git;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;
import java.util.Timer;
/*
*@author Liu
*@Description
*@data 2021/12/7
*/
public class Typer extends JPanel
public static final int WIDTH = 800;//宽
public static final int HEIGHT = 534;//高
public static final int FONT_SIZE = 16;//字号
private static final int START = 1;//游戏状态
private static final int PAUSE = 2;
private static final int RUNNING = 3;
private static final int GAMEOVER = 4;
//表示游戏状态
private int state = START;
public static BufferedImage logo;
public static BufferedImage background;
public static BufferedImage pause;
public static BufferedImage gameover;
private List<Bullet> bullets;//创建子弹的对象
private Course[] courses;
private Course currentCourse;
private List<Word> currentWord;
private List<Word> words;//掉落的对象
private StringBuffer buffer;//键盘键入的字符
//logo图片
static
try
logo = ImageIO.read(Typer.class.getResourceAsStream("1typer.png"));
background = ImageIO.read(Typer.class.getResourceAsStream("1bg.png"));
pause = ImageIO.read(Typer.class.getResourceAsStream("1pause.png"));
gameover = ImageIO.read(Typer.class.getResourceAsStream("1gameover.png"));
catch (IOException e)
e.printStackTrace();
public Typer() throws IOException
File file = new File("./dic");
File[] files = file.listFiles();
this.courses = new Course[files.length];
for (int i = 0; i < files.length; i++)
this.courses[i] = new Course(files[i], i + 1);
this.currentCourse = this.courses[0];
this.currentWord = this.currentCourse.getList();
this.words = new LinkedList<>();
this.buffer = new StringBuffer();
this.bullets = new LinkedList<>();
public static void main(String[] args)
JFrame frame = new JFrame();
frame.setSize(WIDTH, HEIGHT);
frame.setTitle("打字游戏");
frame.setIconImage(logo);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗口,关闭程序
frame.setLocationRelativeTo(null);//设置窗口位置
//创建画板对象
Typer typer = null;
try
typer = new Typer();
catch (IOException e)
e.printStackTrace();
frame.add(typer);
frame.setVisible(true);//设置窗口可见,并调用paint方法
typer.startAction();
//添加键盘监听
Typer.Key l = typer.new Key();
frame.addKeyListener(l);
typer.addKeyListener(l);
class Key extends KeyAdapter
@Override
public void keyPressed(KeyEvent e)
char c = e.getKeyChar();
System.out.println(c);
int code = e.getKeyCode();
if (code == KeyEvent.VK_F1 && state == RUNNING)
state = PAUSE;
else if (code == KeyEvent.VK_C && state == PAUSE)
state = RUNNING;
else if (code == KeyEvent.VK_ESCAPE)
state=GAMEOVER;
System.out.println("游戏结束!");
System.exit(0);
else
if (c markdown 打字稿...编码说明,提示,作弊,指南,代码片段和教程文章