还只是大体实现了一下,没有去优化了!大家多多指点!!
----------------解决方案--------------------------------------------------------
/**
* 打字游戏,开始屏幕上最多10个字母,按下回车键,结束游戏,给出成绩!
* @author liaolijun
* @copyright 0.1
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class TypedGame extends JFrame implements Runnable,KeyListener
{
private Dimension locate[]; //保存10个字符的位置
private String str[]; //保存10个字符
private String letter=""; //保存26个字母
private final int WIDTH=400; //窗口的宽度
private final int HEIGHT=300; //窗口的高度
private final int SCREEN=10; //一屏所显示的最大字符数
private static int totalCounts=10; //字符的总数
private static int deleteCounts=0; //被击中的字符总数
boolean flag=true; //说明程序是否中止
//contructed
public TypedGame()
{
this.setTitle("打字游戏");
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
str=new String[SCREEN];
createAllPos();
createAllLetter();
this.addKeyListener(this);
this.setVisible(true);
}
//随机生成一个位置
public Dimension createPos()
{
int x=(int)(Math.random()*this.getWidth());
if(x<10)
x=10;
if(x>this.getWidth()-15)
x=this.getWidth()-15;
int y=-(int)((Math.random())*21);
return new Dimension(x,y);
}
//生成26个字符
private void createLetter()
{
for (int i=0;i<26;i++)
letter+=(char)((int)'A'+i);
}
//生成一个字符
//随机生成一个字母
public char createA()
{
createLetter();
return letter.charAt((int)(Math.random()*26));
}
//随机生成所有的位置
private void createAllPos()
{
locate=new Dimension[SCREEN];
for(int i=0;i<SCREEN;i++)
{
locate[i]=new Dimension();
Dimension temp=createPos();
if(!isSuperpose(temp))
{
locate[i]=temp;
}
else
{
i--;
}
}
}
//判断位置是否有重叠
private boolean isSuperpose(Dimension temp)
{
for(int i=0;i<SCREEN-1 && locate[i].width!=0 ;i++)
{
if(Math.abs(locate[i].height-temp.height)<10 && Math.abs(locate[i].width-temp.width)<10)
{
return true;
}
}
return false;
}
//随机生成所有当前要显示的字符
private void createAllLetter()
{
for(int i=0;i<SCREEN;i++)
{
str[i]=String.valueOf(createA());
}
}
//绘制字符
public void paint(Graphics g)
{
for(int i=0;i<SCREEN;i++)
{
g.drawString(str[i],locate[i].width,locate[i].height);
}
}
//更机绘制
public void update(Graphics g)
{
this.setForeground(this.getBackground());
for(int i=0;i<SCREEN;i++)
g.drawString(str[i],locate[i].width,locate[i].height);
this.setForeground(Color.black);
}
//坐标的改变
private void moveTO()
{
for(int i=0;i<SCREEN;i++)
{
if(locate[i].height>this.getHeight())
{
str[i]="";
}
locate[i].height+=1;
}
}
//实现KeyListener接口
public void keyTyped(KeyEvent e)
{
String str1=String.valueOf(e.getKeyChar());
for(int i=0;i<SCREEN;i++)
{
if(str1.equalsIgnoreCase(str[i]))
{
deleteCounts++;
totalCounts++;
str[i]="";
locate[i]=this.createPos();
str[i]=String.valueOf(this.createA());
break;
}
}
}
public void keyPressed(KeyEvent e)
{
NumberFormat nf=NumberFormat.getPercentInstance();
nf.setMaximumFractionDigits(2);
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
JOptionPane.showMessageDialog(null,"您的成绩为:"+nf.format((double)deleteCounts/totalCounts),"成绩",JOptionPane.INFORMATION_MESSAGE);
flag=false;
}
}
public void keyReleased(KeyEvent e){}
//重写Runnable接口中的run方法
public void run()
{
while(true && flag)
{
moveTO();
try
{
Thread.sleep(30);
}
catch(Exception ex)
{
ex.printStackTrace();
}
repaint();
}
if(flag==false)
this.getGraphics().clearRect(0, 0, this.getWidth(),this.getHeight());
}
public static void main(String[] args)
{
Thread t1=new Thread(new TypedGame());
t1.start();
}
}
[此贴子已经被作者于2007-6-10 19:06:54编辑过]
----------------解决方案--------------------------------------------------------
我想加个菜单控制开始,但是线程是在main里面建立的,不知怎样去加?
----------------解决方案--------------------------------------------------------
历害。顶
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------