java汉字打字游戏_java简单打字游戏

importjava.awt.Color;importjava.awt.Container;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Toolkit;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.util.ArrayList;importjava.util.List;importjava.util.Random;importjavax.swing.JFrame;importjavax.swing.JLabel;/***

*@author拢唔北伊

**/

public classMyTypeGame {

JFrame jf;final int screenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;//屏幕高度

final int screenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;//屏幕宽度

int x,y;//窗口距左距右的距离

int winWidth,winHeight;//窗口的宽度和高度

int back=-50;//使字符在窗口的上方多长的距离 使其不会一开始就下来了

Container cp;

Random random;char[]allChar=new char[94];//要打的字符数组

List chars;//在屏幕中显示的字符的集合

int perfect=0;//击中字符计数器

int miss=0;//错过字符计数器

int welcomeY=200;//欢迎界面的Y坐标

publicMyTypeGame()

{

jf= new JFrame("TypeGame-Version1.0");

winWidth=screenWidth/2;

winHeight=screenHeight/2;

x=(screenWidth-winWidth)/2;

y=(screenHeight-winHeight)/2;

jf.setSize(winWidth,winHeight);

jf.setLocation(x, y);

jf.setResizable(false);

cp=jf.getContentPane();

DrawStr drawstr= new DrawStr();//描绘字符的面板

cp.setBackground(Color.black);

drawstr.setSize(winWidth, winHeight);

cp.add(drawstr);

jf.setVisible(true);

Thread thread= newThread(drawstr);

thread.start();for(int i=0;i<94;i++)

{

allChar[i]=(char) (i+33);

}

chars= new ArrayList();

jf.addKeyListener(newKeyAdapter()

{public voidkeyTyped(KeyEvent g) {//TODO Auto-generated method stub//int escape = g.getID();

char c =g.getKeyChar();for(int i = 0;i

{if(chars.get(i).getC()==c)

{

chars.remove(i);

perfect++;

}

}

}

});

}class DrawStr extends JLabel implementsRunnable{public synchronized voidpaint(Graphics g) {//TODO Auto-generated method stub

super.paint(g);

g.setFont(new Font("times new roman",Font.BOLD,28));

g.setColor(new Color(150,150,150));

random= newRandom();

onChar temp= new onChar();//一个新的字符

if(back

back=-130;

temp.setC(allChar[random.nextInt(26)]);

temp.setX(random.nextInt(winWidth-50));

temp.setY(back-=50);if(chars.size()<12)//每次最多画12个字符

{if(chars.size()==0)//如果里面没有字符 直接加入

chars.add(temp);else//避免加入的字符重复

{int i=0;int size =chars.size();for(;i

{if(chars.get(i).getC()==temp.getC())break;

}if(i==size)

chars.add(temp);

}

}if(chars.size()!=0)

{for(int i=0;i

{

onChar charq=chars.get(i);if(charq.getY()+30>winHeight-100)

{

chars.remove(charq);

miss++;

}else{

g.drawString(String.valueOf(charq.getC()), charq.getX(), charq.getY()+30);

charq.setY(charq.getY()+2);

}

}

}if(welcomeY>winHeight-100);elseg.drawString("Thank You For Play! Ok Let's Go",winWidth/2-250,welcomeY+=2);

g.drawLine(0, winHeight-100, winWidth, winHeight-100);

g.drawString("PERFECT: "+perfect, 0, winHeight-75);

g.drawString("MISS: "+miss,0,winHeight-50);

}

@Overridepublic voidrun() {//TODO Auto-generated method stub

while(true)

{try{

Thread.sleep(30);

}catch(InterruptedException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

repaint();//刷新画面

}

}

}public static void main(String[] args) {newMyTypeGame();}

}classonChar

{charc;intx;inty;public void setXY(int x,inty)

{this.x=x;this.y=y;

}public chargetC() {returnc;

}public void setC(charc) {this.c =c;

}public intgetX() {returnx;

}public void setX(intx) {this.x =x;

}public intgetY() {returny;

}public void setY(inty) {this.y =y;

}

@OverridepublicString toString() {//TODO Auto-generated method stub

return "Char: "+this.c+" x: "+this.x+" y: "+this.y;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值