java打字游戏
小记:老早之前写的程序,今天发现之前在 csdn上写的东西的图片不显示了,搞得人好郁闷,所以把之前零星的几篇文章搬个家
游戏运行截图:
字母实体类
- package com.git.game;
- import java.awt.Color;
- public class Letter {
- private String num;
- private Color color;
- private int x;
- private int y;
- private int speed = 1;
- public Letter() {
- }
- public Letter(String num, Color color, int x, int y) {
- super();
- this.num = num;
- this.color = color;
- this.x = x;
- this.y = y;
- }
- /**
- * 判断字母是否出界
- *
- * @time 2016年6月29日 下午12:27:38
- * @author Lichao
- * @return 出界为true,未出界为false
- */
- public boolean outOfBound() {
- return this.y > LetterGame.HEIGTH;
- }
- public void step() {
- this.y += speed;
- }
- /* getter setter方法 */
- public String getNum() {
- return num;
- }
- public void setNum(String num) {
- this.num = num;
- }
- public Color getColor() {
- return color;
- }
- public void setColor(Color color) {
- this.color = color;
- }
- 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;
- }
- }
游戏主运行类
- package com.git.game;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.image.BufferedImage;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.imageio.ImageIO;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class LetterGame extends JPanel {
- private static final long serialVersionUID = 1L;
- public static final int WIDTH = 654;// 界面宽度
- public static final int HEIGTH = 600;// 界面高度
- private Timer timer; // 定时器
- private int interVal = 1000 / 100;// 时间间隔,10毫秒
- private Letter[] letters = {}; // 存放的字母
- private int outOfBoundNumber;// 记录丢掉的字母个数
- private int hitNumbers = 0; // 按中的字母个数
- public static BufferedImage background; // 背景图
- public static BufferedImage gameover; // 背景图
- // 游戏状态
- private int state;
- public static final int RUNNING = 0;// 运行状态
- public static final int GAME_OVER = 1; // 结束状态
- static { // 加载静态资源
- try {
- background = ImageIO.read(LetterGame.class
- .getResource("background.bmp"));
- gameover = ImageIO.read(LetterGame.class
- .getResource("gameover.bmp"));
- } catch (Exception e) {
- System.err.println("图片加载失败!");
- e.printStackTrace();
- }
- }
- public LetterGame() {
- }
- // 进数索引
- int enterIndex = 0;
- /**
- * 字母进入面板的方法
- *
- * @time 2016年6月29日 上午10:38:51
- * @author Lichao
- */
- public void enterAction() {
- enterIndex++;
- if (enterIndex % 30 == 0) {
- Letter letter = nextOne();// 每300毫秒执行一次
- letters = Arrays.copyOf(letters, letters.length + 1);
- letters[letters.length - 1] = letter;
- }
- }
- /**
- * 步进方法
- *
- * @time 2016年6月29日 上午10:40:58
- * @author Lichao
- */
- public void stepAction() {
- for (int i = 0; i < letters.length; i++) {
- letters[i].step();
- }
- }
- /**
- * 定时运行方法
- *
- * @time 2016年6月29日 上午11:12:35
- * @author Lichao
- */
- public void action() {
- state = RUNNING;
- this.repaint();
- /**
- * 键盘监听事件
- */
- KeyAdapter keyAdapter = new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- int index = -1;
- String keyPressed = e.getKeyChar() + "";
- for (int i = 0; i < letters.length; i++) {
- Letter letter = letters[i];
- if (keyPressed.equalsIgnoreCase(letter.getNum())) {
- hitNumbers++;
- index = i;
- break;
- }
- }
- if (index != -1) {
- Letter temp = letters[index];
- letters[index] = letters[letters.length - 1];
- letters[letters.length - 1] = temp;
- letters = Arrays.copyOf(letters, letters.length - 1);
- }
- }
- };
- /** 添加鼠标事件 */
- this.addKeyListener(keyAdapter);
- // 这两句用来监听键盘
- this.setFocusable(true);
- this.requestFocus();
- mouseAction();
- timer = new Timer();
- timer.schedule(new TimerTask() {
- @Override
- public void run() {
- if (state == RUNNING) {
- enterAction();
- stepAction();
- outOfBoundAction();
- }
- checkGameOverAction();
- repaint();
- }
- }, interVal, interVal);
- }
- /**
- * 出界操作
- *
- * @time 2016年6月29日 下午12:30:17
- * @author Lichao
- */
- public void outOfBoundAction() {
- int index = 0;
- Letter[] lettersInPanel = new Letter[letters.length];
- for (int i = 0; i < letters.length; i++) {
- Letter letter = letters[i];
- if (!letter.outOfBound()) {
- lettersInPanel[index++] = letter;
- } else {
- outOfBoundNumber++;
- }
- }
- letters = Arrays.copyOf(lettersInPanel, index);
- }
- /**
- * 判断游戏是否结束
- *
- * @time 2016年6月29日 下午1:38:24
- * @author Lichao
- */
- private void checkGameOverAction() {
- if (isGameOver()) {
- state = GAME_OVER;
- }
- }
- /**
- * 随机生成字母
- *
- * @time 2016年6月29日 上午10:35:46
- * @author Lichao
- * @return
- */
- protected Letter nextOne() {
- Random random = new Random();
- Letter letter = new Letter();
- letter.setNum(String.valueOf((char) (random.nextInt(26) + 'A')));
- letter.setX(random.nextInt(WIDTH - 25));
- letter.setY(10);
- letter.setColor(getRandColor());
- return letter;
- }
- /**
- * 生成随机颜色
- *
- * @time 2016年6月29日 上午10:13:06
- * @author Lichao
- * @return
- */
- public static Color getRandColor() {
- Random random = new Random();
- Color color = new Color(random.nextInt(255), random.nextInt(255),
- random.nextInt(255));
- return color;
- }
- /**
- * 重写父类方法
- */
- @Override
- public void paint(Graphics g) {
- g.drawImage(background, 0, 0, null);
- paintNumber(g);
- paintState(g);
- paintScore(g);
- }
- /**
- * 画图形
- *
- * @time 2016年6月29日 上午9:10:08
- * @author Lichao
- * @param g
- */
- private void paintNumber(Graphics g) {
- g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));
- for (int i = 0; i < letters.length; i++) {
- Letter letter = letters[i];
- g.setColor(letter.getColor());
- g.drawString(letter.getNum(), letter.getX(), letter.getY());
- }
- }
- /**
- * 画状态
- *
- * @time 2016年6月29日 下午1:17:46
- * @author Lichao
- * @param g
- */
- private void paintState(Graphics g) {
- switch (state) {
- case GAME_OVER:
- g.drawImage(gameover, 0, 0, null);
- break;
- }
- }
- /**
- * 添加鼠标事件
- *
- * @time 2016年6月29日 下午2:05:00
- * @author Lichao
- */
- private void mouseAction() {
- /** 鼠标监听事件 */
- MouseAdapter mouse = new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent event) {
- // 单击右键退出
- if (event.getButton() == MouseEvent.BUTTON3
- && state == GAME_OVER) {
- System.exit(0);
- }
- // 单击左键重新开始
- if (event.getButton() == MouseEvent.BUTTON1
- && state == GAME_OVER) {
- init();
- }
- }
- };
- this.addMouseListener(mouse);
- }
- /**
- * 初始化游戏
- *
- * @time 2016年6月29日 下午2:09:10
- * @author Lichao
- */
- protected void init() {
- this.state = RUNNING;
- this.outOfBoundNumber = 0;
- this.letters = new Letter[] {};
- this.repaint();
- }
- /**
- * 判断游戏是否结束
- *
- * @time 2016年6月29日 下午1:19:07
- * @author Lichao
- * @return
- */
- private boolean isGameOver() {
- return this.outOfBoundNumber > 5;
- }
- private void paintScore(Graphics g) {
- g.setColor(new Color(0xFF0000)); // 设置颜色(0xFF0000为纯红)
- g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20)); // 设置字体(Font.SANS_SERIF为字体,Font.BOLD为字体样式,20为字号)
- g.drawString("SCORE: " + hitNumbers, 10, 25); // 画分
- g.drawString("MISS: " + outOfBoundNumber, 10, 45); // 画丢失数
- }
- /** 主方法 */
- public static void main(String[] args) {
- JFrame frame = new JFrame("傻逼打字游戏");
- LetterGame game = new LetterGame();
- frame.add(game);
- frame.setBackground(new Color(111, 168, 220));
- frame.setSize(WIDTH, HEIGTH); // 设置窗口的大小
- frame.setAlwaysOnTop(true); // 设置窗口总在最上面
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置默认关闭操作(窗口关闭时退出程序)
- frame.setLocationRelativeTo(null); // 设置窗口起始位置(居中)
- frame.setVisible(true); // 1.设置窗口可见 2.尽快调用paint()方法
- frame.setResizable(false);
- game.action();
- }
- }
背景图片
游戏结束图片
ps:这两张图是我用画图工具花的,太丑了,哈哈。程序可以直接运行的,如果你不知道怎么运行,那我就帮不了你了。由于比较简单,我就不附送源码了。
java打字游戏的更多相关文章
- java打字游戏-一款快速提升java程序员打字速度的游戏(附源码)
一.效果如图: 源码地址:https://gitee.com/hoosson/TYPER 纯干货,别忘了留个赞哦!
- Java之线程———GUI线程(包含打字游戏和计时器俩个GUI实列)
当java程序包含图形用户界面(GUI)时,Java虚拟机在运行应用程序时会自动启动更多的线程,其中有两个重要的线程:AWT-EventQuecue 和 AWT-Windows. AWT-EventQ ...
- 用JS写了一个打字游戏,反正我是通不了关
今天想写个简单的游戏, 打字游戏好像都没写过, 那么就写打字游戏吧, gamePad包含了关卡的信息, 可以用来调整给个关卡字符下落的速度: getRandom函数会返回一个字符对象, 这个对象包含了 ...
- java俄罗斯方块游戏代码
java俄罗斯方块游戏代码: package com; import java.awt.Color; import java.awt.Graphics; import java.awt.event.K ...
- java围棋游戏源代码
//李雨泽源代码,不可随意修改.//时间:2017年9月22号.//地点:北京周末约科技有限公司.//package com.bao; /*围棋*/ /*import java.awt.*; impo ...
- jQuery 写的简单打字游戏
var off_x; //横坐标 var count=0; //总分 var speed=5000; //速度,默认是5秒. var keyErro=0; //输入错误次数 var keyRight= ...
- Java 3D游戏引擎——JME(java Monkey Engine)
转自:http://bbs.gameres.com/forum.php?mod=viewthread&tid=180732 JME(java Monkey Engine),一个非常棒的Java ...
- [转] java开源游戏
收藏一下 triplea Triplea是一个开放源码的boardgame.它允许玩家选择各种各样的战略版图游戏(如:轴心国或同盟军).TripleA引擎支持联网对战,支持声音,支持使用XML文 ...
- 寒假答辩作品:Java小游戏
目录 java入门小游戏[test] 游戏界面 前言 (可直接跳到程序介绍) 前期入门小项目 前期收获 后期自创关卡 后续 java入门小游戏[test] 游戏界面 github地址: https:/ ...
随机推荐
- SQL Server 存储(8/8):理解数据文件结构
这段时间谈了很多页,现在我们可以看下这些页在数据文件里是如何组织的. 我们都已经知道,SQL Server把数据文件分成8k的页,页是IO的最小操作单位.SQL Server把数据文件里的第1页标记为 ...
- Tesseract-OCR引擎 入门
OCR(Optical Character Recognition):光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程. Tesseract:开源的OCR识别引擎,初期Tesseract引 ...
- .NET转JAVA之拼音组件
PS:做了4年,自我感觉.NET到瓶颈了,而且公司并没有深入运用.NET技术的项目,自我学习感觉也没太大动力(请骂我懒T_T).再加上技术年限越往上走,了解到的.NET职业提升环境就越来越艰难(个人理 ...
- C#中弹出文件选择窗体和判断是否下载提示窗体的源码
1.创建一个window窗体
- JPA学习(3)JPA API
在我们的jpa的helloworld中,我们看到了简单的一个jpa保存操作,下面就来好好学习一下,JPA最主要的几个类 1.基本的几个类: ①:Persistence 类是用于获取 EntityMan ...
- 利用Spring创建定时任务
啊Spring Task看似很简单的感觉,但是自己搞起来还是花了蛮大的精力的,因为以前没接触过这个东西,所有当任务交给我的时候,我是一头的雾水的.然后我就各种查资料.其中我印象最深的是版本的问题和架包 ...
- Python 3.X 实现定时器 Timer,制作抽象的Timer定时器基类
Python 在不依赖第三方库的前提下,对于定时器的实现并不是很完美,但是这不意味着我们无法实现. 阅读了网上的一些资料,得出一些结论,顺手写了一个基类的定时器(Python3) BaseTimer: ...
- Little Jumper---(三分)
Description Little frog Georgie likes to jump. Recently he have discovered the new playground that s ...
- 状态压缩DP--Mondriaan's Dream
题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110044#problem/A Description Squares and ...
- 【转载】delete table 和 truncate table 的区别
使用delete语句删除数据的一般语法格式: delete [from] {table_name.view_name} [where] 将XS表中的所有行数据删除 delete XS 执行完后,发现X ...