import java.awt.*; import java.awt.event.*; public class MyGame{ public static Frame f =new Frame(); public static void main(String[] agrs){ f.setSize(500, 400); f.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-f.getSize().width)/2, (Toolkit.getDefaultToolkit().getScreenSize().height-f.getSize().height)/2); f.setTitle("打字Game"); f.setBackground(Color.black); Mypanel mp =new Mypanel(); f.add(mp); Thread t =new Thread(mp); t.start(); f.addKeyListener(mp); mp.addKeyListener(mp); mp.addMouseListener(mp); f.setVisible(true); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } } class Mypanel extends Panel implements Runnable,KeyListener,MouseListener{ int x[]=new int[20]; int y[]=new int[20]; char c[]=new char[20]; int score=1000; int level=1; boolean flag=true; public Mypanel(){ for(int i=0;i<20;i++){ x[i]=(int)(Math.random()*1020); y[i]=(int)(Math.random()*768)-700; c[i]=(char)(Math.random()*26+65); } } public void paint(Graphics g){ g.setColor(Color.BLUE); g.setFont(new Font("宋体",Font.BOLD,30)); if(flag==false){ g.setColor(Color.GREEN); g.setFont(new Font("隶书",Font.BOLD,50)); g.drawString("Over",MyGame.f.getSize().width/2,MyGame.f.getSize().height/2); } for(int i=0;i<20;i++){ g.drawString(c[i]+"",x[i],y[i]); } g.drawString("得分:"+score,20,50); g.drawString("等级:"+level,850,50); } public void run(){ while(flag){ for(int i=0;i<20;i++){ y[i]++; if(y[i]>768){ y[i]=0; x[i]=(int)(Math.random()*(MyGame.f.getSize().width)); c[i]=(char)(Math.random()*26+65); score-=100; if(score<=0){ score=0; flag=false; c=new char[20]; } } } try { Thread.sleep(20-3*level); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.repaint(); } } public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub System.out.println(e.getX()); System.out.println(e.getY()); } public void keyPressed(KeyEvent e) { int maxY=-1000; int index=-1; if(e.getKeyCode()>=e.VK_A&&e.getKeyCode()<=e.VK_Z){ for(int i=0;i<20;i++){ if(e.getKeyCode()==c[i]){ if(y[i]>maxY){ maxY=y[i]; index=i; } } } if(index!=-1){ y[index]=0; x[index]=(int)(Math.random()*(MyGame.f.getSize().width)); c[index]=(char)(Math.random()*26+65); score +=50; if((score-500)/500>level){ level++; } } else{ score-=100; if(score<=0){ score=0; flag=false; c=new char[20]; } repaint(); } } } public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } }
最近下载更多
baigeiky LV1
2023年12月21日
TLGT15 LV1
2023年12月20日
Neljukse Gabriel LV1
2023年12月3日
fyhfyhfyh LV1
2023年8月21日
22111303117 LV1
2023年8月21日
shiinazhen LV1
2023年8月21日
3043253518 LV1
2023年6月28日
AppleBannana LV1
2023年6月26日
ChangingY LV1
2023年6月3日
luo110012 LV9
2023年5月15日
最近浏览更多
求学的熊猫 LV6
2023年12月26日
lshlsh
2023年12月25日
暂无贡献等级
baigeiky LV1
2023年12月21日
TLGT15 LV1
2023年12月20日
shiaomon
2023年12月14日
暂无贡献等级
Neljukse Gabriel LV1
2023年12月3日
吉萨VUK
2023年11月1日
暂无贡献等级
fyhfyhfyh LV1
2023年8月21日
22111303117 LV1
2023年8月21日
你们的代码都是我的了 LV16
2023年8月21日