求JAVA编写打字游戏源代码!

要求:记时一分钟,键盘输入字母,屏幕上对应字母消失,并计数!能改变字母速率!... 要求:记时一分钟,键盘输入字母,屏幕上对应字母消失,并计数!能改变字母速率! 展开
 我来答
chenlong6619
2010-01-08 · 超过12用户采纳过TA的回答
知道答主
回答量:44
采纳率:0%
帮助的人:0
展开全部
package chen;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.JFrame;

public class keyTest extends Window implements KeyListener, Runnable {

int width, height;
BufferedImage buf;
Graphics gc;
boolean play = true;
Vector<oneChar> keys = new Vector<oneChar>();
int ok = 0, fail = 0, error = 0, sum = 0;
Font small = new Font("宋体", 0, 30);
Font big = new Font("宋体", 0, 50);
long time = System.currentTimeMillis();

keyTest(Frame f) {
super(f);
Dimension s = getToolkit().getScreenSize();
width = (int) s.getWidth();
height = (int) s.getHeight();
this.setBounds(0, 0, width, height);
this.buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
gc = buf.getGraphics();
this.setVisible(true);
this.setAlwaysOnTop(true);
this.buf = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
gc = buf.getGraphics();
new Thread(this).start();

}

public static void main(String s[]) {
JFrame help = new JFrame("打字练习");
help.setVisible(true);
help.setDefaultCloseOperation(3);
help.addKeyListener(new keyTest(help));

}

public void keyTyped(KeyEvent e) {
}

public synchronized void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 27) {
play = false;
this.dispose();
System.exit(0);
}
char s = e.getKeyChar();
if (s >= 'a' && s <= 'z' || (s >= 'A' && s <= 'Z')) {
String l = "" + s;
for (int i = 0; i < keys.size(); i++) {
if (l.equals(((oneChar) keys.elementAt(i)).s)) {
keys.removeElementAt(i);
ok++;
return;
}
}
error++;
}
}

public void keyReleased(KeyEvent e) {
}

@Override
public void update(Graphics g) {

gc.setColor(Color.BLACK);
gc.fillRect(0, 0, width, height);
gc.setColor(Color.red);
int l = (ok + error) > 0 ? (ok * 100 / (ok + error)) : 100;
gc.setFont(small);
gc.drawString("成功:" + ok + " 错误:" + error + " 失败:" + fail + " 正确率:" + l + "% 时间:" + (System.currentTimeMillis() - time) / 1000, 10, height - 30);
gc.setFont(big);
oneChar o;
for (int i = 0; i < keys.size(); i++) {
o = keys.elementAt(i);
gc.setColor(o.c);
gc.drawString(o.s, o.x, o.y += 6);
if (o.y > height - 10) {
fail++;
keys.removeElementAt(i);
}
}
g.drawImage(buf, 0, 0, null);
}

public void run() {
while (play) {
try {
sum++;
if (sum % 5 == 0) {
newchar();
}
Thread.sleep(80);
repaint();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}

private void newchar() {
keys.add(oneChar.getinstance(width));
}
}
package chen;

import java.awt.Color;

public class oneChar {

static java.util.Random r = new java.util.Random();

public static oneChar getinstance(int maxX) {
oneChar a = new oneChar();
int b = r.nextInt(26);
a.s = "" + (char) (b + (r.nextInt(4) > 1 ? 'a' : 'A'));
a.x = r.nextInt(maxX - 30);
a.c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
return a;
}
int x, y;
Color c;
String s;
}

参考资料: 如果您的回答是从其他地方引用,请表明出处

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
数篷科技
2025-03-20 广告
源代码安全对于数篷科技(深圳)有限公司至关重要。我们采用多重安全措施保护源代码,包括访问控制、加密存储以及定期审计。所有开发人员需经过严格身份验证,仅能访问其职责范围内的代码。源代码存储于安全服务器,实施高级加密标准,确保数据在传输和存储过... 点击进入详情页
本回答由数篷科技提供
吸殻
2010-01-08 · TA获得超过217个赞
知道小有建树答主
回答量:379
采纳率:100%
帮助的人:216万
展开全部
分太少了。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式