用JAVA 编写小游戏

来源:转载

import java.awt.*;

import java.awt.event.*;

public class My1111{

public static void main(String arg[]){



Frame w= new Frame();
w.setSize(300,400);



MyPanel mp= new MyPanel();
w.add(mp);


w.addKeyListener(mp);
mp.addKeyListener(mp);



Thread t= new Thread(mp);
t.start();


w.show();

}
}



class MyPanel extends Panel implements Runnable, KeyListener{
int x[]= new int[10];
int y[]=new int [10];
char c[]= new char[10];
int sum =99999999;


public MyPanel(){
for (int i=0; i<10;i++){
x=(int)(Math.random()*300);
y=(int)( Math.random()*300);
c=(char)(Math.random()*26+97);
}




}


public void paint(Graphics g){
for (int i=0; i<10;i++){
g.drawString(""+c,x,y);



}

g.setColor(Color.red);
g.drawString("成绩为:"+sum,10,10);

}
public void run(){
while(true){
for (int i=0; i<10;i++){
y++;
if(y>400){



y=0;
x=(int)(Math.random()*300);
c=(char)(Math.random()*26+97);
sum-=100;


}
}
try {
Thread.sleep(40) ;
}
catch (Exception ex) {
}
repaint() ;
}
}


public void keyTyped(KeyEvent e) {
char cc=e.getKeyChar() ;
boolean b= false;
for (int i=0;i<10;i++){
if(cc==c){
y=0;
x=(int)(Math.random()*300);
c=(char)(Math.random()*26+97);

b=true;
break;
}
}

if(b){
sum+=10;
}else{
sum-=100;
}


}


public void keyPressed(KeyEvent e) {
// TODO: Add your code here
}


public void keyReleased(KeyEvent e) {
// TODO: Add your code here
}


}


分享给朋友:
您可能感兴趣的文章:
随机阅读: