用JAVA写一个完整的游戏程序
导入Java . awt . *;
导入javax . swing . *;
导入Java . awt . event . *;
导入Java . util . random;
类WordPanel扩展JPanel实现Runnable{
私有线程thread = null
private int level = 1;
私有字体font =新字体(“ì”,字体。斜体+字体。粗体,24);
私彩color =颜色。蓝色;
公共静态final int x = 10;
private int y = 0;
私有char字;//落款字母
私有静态随机rand = new Random();
公共空集合(int y){
this.y = y
}
public void setWord(char word){
this.word = word
}
public char getWord(){
返回this.word
}
public static char newChar(){
return(char)(97+rand . nextint(26));
}
public WordPanel(){
word = new char();
thread =新线程(this);
thread . start();
}
public void paint component(Graphics g){
super . paint component(g);
g.setFont(字体);
g.setColor(颜色);
g . drawstring(string . value of(word),x,y);
}
公共无效运行(){
while (true){
尝试{
thread . sleep(1000);
this . repaint();
如果(y & gt=this.getHeight()){
y = 0;
word = this . new char();
}否则
y+= 20;
}
catch(例外){
ex . printstacktrace();
}
}
}
}
公共类文字游戏扩展JFrame{
private word panel[]words = new word panel[10];
类侦听器扩展KeyAdapter{
公共void键入的(KeyEvent e) {
char input = e . getkey char();
for(int I = 0;我& ltwords.lengthi++){
if ( input==words[i]。getWord() ){
词[我]。set word(word panel . new char());
词[我]。setY(0);
词[我]。repaint();
打破;
}
}
}
}
公共文字游戏(字符串标题){
超级(标题);
容器c = this . getcontentpane();
c . set layout(new GridLayout(1,words . length));
this . addkey Listener(new Listener());
for(int I = 0;我& ltwords.lengthi++){
words[I]= new word panel();
c.add(单词[I]);
}
this.setSize(新维度(400,400));
this . set visible(true);
}
公共静态void main(String[] args){
WordGame游戏=新WordGame(“简单打字游戏”);
}
}