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;}@Overridepublic 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;}@Overridepublic 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 {@Overridepublic 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 >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') {buffer.append(c);} else if (c == '.') {buffer.append(c);} else if (code == KeyEvent.VK_BACK_SPACE) {if (buffer.length() > 0) {buffer.deleteCharAt(buffer.length() - 1);}}}}}public void startAction() {this.currentCourse = (Course) JOptionPane.showInputDialog(this, "选择", "选择框",JOptionPane.PLAIN_MESSAGE, new ImageIcon(logo),this.courses,this.currentCourse);if (this.currentCourse == null) {System.exit(0);}state = RUNNING;//添加定时器Timer timer = new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {switch (state) {case RUNNING:enterAction();moveAction();//改变单词状态changeState();bulletMove();remove();gameOver();break;}repaint();}}, 5, 25);}int count=0;public void gameOver(){for (Word word : words) {if (word.getY() >= HEIGHT) {count++;if (count >= 3) {state=GAMEOVER;}}}}//碰撞public int score = 0;public void remove() {Iterator<Bullet> iterator = bullets.iterator();while (iterator.hasNext()) {Bullet next = iterator.next();boolean flag = wordAction(next);if (flag) {iterator.remove();score += 10;}}}private boolean wordAction(Bullet next) {Iterator<Word> it = words.iterator();while (it.hasNext()) {Word word = it.next();if (word.isMatch()) {if (word.getX() == next.getX() && word.getY() <= next.getY()&& word.getY() + word.getHeight() >= next.getY()) {it.remove();return true;}}}return false;}//计算分数public void bulletMove() {for (Bullet bullet : bullets) {bullet.step();}}int moveIndex = 0;//改变单词状态public void changeState() {for (Word word : words) {if (word.getEnglish().equals(buffer.toString())) {word.setMatch(true);buffer = new StringBuffer();bullets.add(new Bullet(word.getX()));}}}public void moveAction() {if (moveIndex++ % 2 == 0) {for (Word w : words) {w.move();}}}int enterIndex = 0;public void enterAction() {if (enterIndex++ % 20 == 0) {words.add(this.currentWord.remove(0));}}@Overridepublic void paint(Graphics g) {//画背景图g.drawImage(background, 0, 0, null);switch (state) {case RUNNING:for (Word word1 : words) {if (word1.isMatch()) {g.setColor(Color.YELLOW);g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, FONT_SIZE));g.drawString(word1.getChinese(), word1.getX(), word1.getY());g.drawString(word1.getEnglish(), word1.getX(), word1.getY() + FONT_SIZE);} else {g.setColor(Color.red);g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, FONT_SIZE));g.drawString(word1.getChinese(), word1.getX(), word1.getY());g.drawString(word1.getEnglish(), word1.getX(), word1.getY() + FONT_SIZE);if (word1.getEnglish().startsWith(buffer.toString())) {g.setColor(Color.WHITE);g.drawString(buffer.toString(), word1.getX(), word1.getY() + FONT_SIZE);}}}//        g.drawString(str,270,475);g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));g.setColor(Color.BLUE);g.drawString(buffer.toString(), 270, 475);//给word添加状态,匹配过还是未匹配过for (Bullet bullet : bullets) {g.drawImage(Bullet.image, bullet.getX(), bullet.getY(), null);}g.setFont((new Font(Font.DIALOG, Font.BOLD, 20)));g.setColor((Color.CYAN));g.drawString("score", 10, 25);g.drawString(String.valueOf(score), 80, 25);break;case PAUSE:g.drawImage(pause, 0, 0, null);break;case GAMEOVER:g.drawImage(gameover, 0, 0, null);g.setFont((new Font(Font.DIALOG, Font.BOLD, 40)));g.setColor((Color.BLUE));g.drawString("游戏结束",320,200);g.drawString("score", 340, 240);g.drawString(String.valueOf(score), 340, 280);break;}}
}

Java JUI打字小游戏项目相关推荐

  1. JAVA实现打字小游戏

    目录 一.效果 二.教程 三.代码 一.效果 首先我们先看效果,左上角的分数是用来记录我们打对了多少字母.字母是从上面开始往下落.每打对一个字母,分数增加,增加到一定分数后,字母下落的速度也会增加.( ...

  2. java打字小游戏_java实现打字游戏小程序

    本文实例为大家分享了java实现打字游戏小程序的具体代码,供大家参考,具体内容如下 一.设计思路 1.创建一个窗体 2.在窗体上放置一个面板,用paint方法画出英文字母,随机放置字母位置,并随时间自 ...

  3. Java小游戏项目之坦克世界

    之前在网上冲浪的时候,发现了个用java写的坦克世界,所以自己有时间也跟着敲了一下 刚刚敲完,在最后给attack方法添加了alive条件之后,在单人模式时没办法射击,但是不影响双人模式,如果要玩单人 ...

  4. java实验2总结心得,打字小游戏JAVA实验总结及心得体会

    篇一:扫雷游戏实验报告 课程设计 班 级: 姓 名: 学 号: 指导教师: 成 绩: 电子与信息工程学院信息与通信工程系 目录 1.任务概述------------------------------ ...

  5. JAVA图形化打字小游戏

    不多废话,直接上代码 程序实际运行图片如下,可供参考. Main.java package 打字游戏;public class Main {public static void main(String ...

  6. Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇)

    Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇) 代码实现 窗口类 小车类 玩家类 电脑类 赛道类 小树类 打字类 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) ...

  7. Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇)

    Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇) 资源下载 完整代码 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) Python 打字小游戏开发,来体验不一样的 ...

  8. Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇)

    Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) 前言 游戏素材准备 游戏项目结构 项目里面的类说明 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇) Python ...

  9. java d打字游戏_java实现快速打字游戏

    本文实例为大家分享了java实现打字游戏的具体代码,供大家参考,具体内容如下 import java.util.Random; import java.util.Scanner; public cla ...

  10. JAVA课程设计——“小羊吃蓝莓”小游戏

    JAVA课程设计--"小羊吃蓝莓"小游戏 1. 团队课程设计博客链接 http://www.cnblogs.com/HXY071/p/7061216.html 2. 个人负责模块或 ...

最新文章

  1. python设置画布背景图_如何使画布中的背景图像随窗口展开?
  2. xCode自定义快捷键
  3. 数据库死锁查询及处理
  4. Winform控件扩展
  5. HBase数据模型和读写原理
  6. ServletContext的用法
  7. 易语言linux支持多线程,详解易语言启动多线程
  8. sketch up rbs/rbz/rb插件安装方法
  9. 音频播放AVAudioPlayer
  10. 微信小程序——map用法
  11. 轻量级程序编辑器的选择:EmEditor、Editplus等---Web开发系列之工具篇
  12. Linux 用户无法使用 crontab 命令
  13. vb读取文本文件某行的内容
  14. 企业的性质:诺奖得主科斯经典原文翻译及解读1
  15. 我所学到的EC-1(个人学习总结,不能保证正确,欢迎大佬指正)
  16. notepad++设置网络代理
  17. XenServer中本地磁盘管理
  18. 项目经理通过甘特图编制项目计划的方法
  19. Python学习——Linux命令——wget命令
  20. 阿里P8大佬的860页分布式微服务笔记,改变你对架构的认知

热门文章

  1. 十分钟学懂Python入门基础3(中)
  2. 自然辩证法与计算机科学与技术,自然辩证法与计算机科学技术.docx
  3. 人脸识别、活体检测、人脸识别面临的挑战
  4. yolov5的head修改为decouple head
  5. 14Python爬虫---爬虫伪装浏览器
  6. Fiverr 攻略:跨境自由职业通过 Fiverr 盈利
  7. Python处理二进制流(一)
  8. java手机游戏主角技能上剑魂,DNF高手来回答
  9. 常用论文检查语法错误软件和网址
  10. Matlab中不定积分和定积分的实现