首页 > 代码库 > swing简单的打字游戏源码

swing简单的打字游戏源码

代码如下

import java.awt.*;import java.awt.event.*;public class Main{        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("swing简单的打字游戏");                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{        private static final long serialVersionUID = 1L;        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",Main.f.getSize().width/2,Main.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()*(Main.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) {                                e.printStackTrace();                        }                        this.repaint();                }        }        public void mouseClicked(MouseEvent e) {                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()*(Main.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) {                        }        public void keyTyped(KeyEvent arg0) {                        }        public void mouseEntered(MouseEvent arg0) {                        }        public void mouseExited(MouseEvent arg0) {                        }        public void mousePressed(MouseEvent arg0) {                        }        public void mouseReleased(MouseEvent arg0) {                        }}

  效果图
<ignore_js_op> 

详细说明:http://java.662p.com/thread-3684-1-1.html

swing简单的打字游戏源码