目录
一、效果
二、教程
三、代码
首先我们先看效果,左上角的分数是用来记录我们打对了多少字母。字母是从上面开始往下落。每打对一个字母,分数增加,增加到一定分数后,字母下落的速度也会增加。(效果是动态的)
这是不是一个简单的打字小游戏呢?
1、使用IDEA搭建一个项目,项目名称:Words(可根据自己的喜好)
具体搭建过程可看博文用IDEA构建一个简单的Java程序范例,这里就不详细说了。
2、Word.class
(1)导入包
import java.awt.Component;
import javax.swing.JFrame;
(2)主函数
在这里我的panel大小设置的是800*600,大家可以根据自己的喜好设置的更大一些。
public static void main(String[] args) {
JFrame frame = new JFrame("打字游戏");
WordPanel panel = new WordPanel();
frame.add(panel);
Thread t = new Thread(panel);
t.start();
panel.addKeyListener(panel);
panel.setFocusable(true);
frame.setSize(800, 600);
frame.setLocationRelativeTo((Component)null);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
}
3、WordPanel.class
(1)导入包
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
(2)类的继承,接口的实现
public class WordPanel extends JPanel implements Runnable, KeyListener{}
(3)数据类型的定义
int[] xx = new int[10];
int[] yy = new int[10];
char[] words = new char[10];
Color[] colors = new Color[10];
int score = 0;
int speed = 10;
(4)画布函数WordPanel()
public WordPanel() {
for(int i = 0; i < 10; ++i) {
this.xx[i] = (int)(Math.random() * 800);
this.yy[i] = (int)(Math.random() * 600);
this.colors[i] = this.randomColor();
this.words[i] = (char)((int)(Math.random() * 26 + 65));
}
}
(5)画笔函数paint()
public void paint(Graphics g) {
super.paint(g);
Font ft = new Font("微软雅黑", 1, 28);
g.setFont(ft);
g.drawString("分数" + this.score, 50, 50);
for(int i = 0; i < 10; ++i) {
g.setColor(this.colors[i]);
g.drawString(String.valueOf(this.words[i]), this.xx[i], this.yy[i]);
}
}
(6)线程函数run()
public void run() {
while(true) {
for(int i = 0; i < 10; ++i) {
this.yy[i]++;
if (this.yy[i] > 600) {
this.yy[i] = 0;
}
}
try {
Thread.sleep((long)this.speed);
} catch (InterruptedException var2) {
var2.printStackTrace();
}
this.repaint();
}
}
(7)按键函数KeyPressed()
public void keyPressed(KeyEvent e) {
for(int i = 0; i < 10; ++i) {
if (e.getKeyCode() == this.words[i]) {
this.xx[i] = (int)(Math.random() * 800);
this.yy[i] = 0;
this.words[i] = (char)((int)(Math.random() * 26 + 65));
++this.score;
break;
}
}
if (this.score > 5 && this.score < 10) {
this.speed = 5;
} else if (this.score > 10) {
this.speed = 1;
}
this.repaint();
}
(8)颜色函数randomColor()
public Color randomColor() {
int R = (int)(Math.random() * 255);
int G = (int)(Math.random() * 255);
int B = (int)(Math.random() * 255);
Color color = new Color(R, G, B);
return color;
}
1、Word.class
package Words;
import java.awt.Component;
import javax.swing.JFrame;
public class Word {
public static void main(String[] args) {
JFrame frame = new JFrame("打字游戏");
WordPanel panel = new WordPanel();
frame.add(panel);
Thread t = new Thread(panel);
t.start();
panel.addKeyListener(panel);
panel.setFocusable(true);
frame.setSize(800, 600);
frame.setLocationRelativeTo((Component)null);
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
}
}
2、WordPanel.class
package Words;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class WordPanel extends JPanel implements Runnable, KeyListener{
int[] xx = new int[10];
int[] yy = new int[10];
char[] words = new char[10];
Color[] colors = new Color[10];
int score = 0;
int speed = 10;
public WordPanel() {
for(int i = 0; i < 10; ++i) {
this.xx[i] = (int)(Math.random() * 800);
this.yy[i] = (int)(Math.random() * 600);
this.colors[i] = this.randomColor();
this.words[i] = (char)((int)(Math.random() * 26 + 65));
}
}
public Color randomColor() {
int R = (int)(Math.random() * 255);
int G = (int)(Math.random() * 255);
int B = (int)(Math.random() * 255);
Color color = new Color(R, G, B);
return color;
}
public void paint(Graphics g) {
super.paint(g);
Font ft = new Font("微软雅黑", 1, 28);
g.setFont(ft);
g.drawString("分数" + this.score, 50, 50);
for(int i = 0; i < 10; ++i) {
g.setColor(this.colors[i]);
g.drawString(String.valueOf(this.words[i]), this.xx[i], this.yy[i]);
}
}
public void run() {
while(true) {
for(int i = 0; i < 10; ++i) {
this.yy[i]++;
if (this.yy[i] > 600) {
this.yy[i] = 0;
}
}
try {
Thread.sleep((long)this.speed);
} catch (InterruptedException var2) {
var2.printStackTrace();
}
this.repaint();
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
for(int i = 0; i < 10; ++i) {
if (e.getKeyCode() == this.words[i]) {
this.xx[i] = (int)(Math.random() * 800.0D);
this.yy[i] = 0;
this.words[i] = (char)((int)(Math.random() * 26 + 65));
++this.score;
break;
}
}
if (this.score > 5 && this.score < 10) {
this.speed = 5;
} else if (this.score > 10) {
this.speed = 1;
}
this.repaint();
}
public void keyReleased(KeyEvent e) {
}
}