⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 .#merg43021cvs

📁 java编写的打鸟游戏
💻 #MERG43021CVS
字号:
/* * GraphicsPanel.java * * Created on 2007年12月18日, 下午12:27 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package SBGameCore;import graphics.Bird;import graphics.Feather;import java.awt.Cursor;import java.awt.Font;import java.awt.MediaTracker;import java.awt.Point;import java.awt.Rectangle;import java.util.Vector;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JPanel;/** * * @author wusir */public class GraphicsPanel extends JPanel implements Runnable{    /*Toolkit toolkit = Toolkit.getDefaultToolkit();    Image image1 = toolkit.getImage("image/0.jpg");    int x=0;    int y=0;    boolean stop=false;         public GraphicsPanel(){        super();        //move();    }    public void paintComponent(Graphics g) {        super.paintComponent(g);        //Graphics2D g2d = (Graphics2D) g;        g.setColor(Color.WHITE);        g.fillRect(0, 0, this.getWidth(), this.getHeight());        g.drawImage(image1,x,y,this);    }    public void move(){        Thread t=new Thread(){            public void run(){                while(!stop){                    try {                        repaint();                        Thread.sleep(20);                        x+=1;                        y+=1;                        repaint();                        if(x>700||y>700)stop=true;                    } catch (InterruptedException ex) {                        ex.printStackTrace();                    }                }            }        };        t.start();    }}*/    public Toolkit toolkit = null;    public Image buffer = null ;	 // image buffer    public Image backdrop = null ;      // background    public Image bgimg  = null ;        // privouse background    public Image[] birds = new Image[7];//birds images    public Image feather = null;        //feather    public Image cursor = null;           //cursor    public Graphics  buf_g = null ;     // graphics buffer    public Graphics  bkd_g = null ;     // background graphics    public Dimension window_size = null;//windows size    public Font font ;    public Font font_s ;                //word fonts        MediaTracker tracker = null ;        public Thread game = null;          //game thread    public boolean game_over = false;    public Point mouse = null;           //mouse position    public Rectangle paint_area = new Rectangle() ; //object's current area    public Rectangle new_area   = new Rectangle() ; //object's new area        public Vector Vbird = new Vector();  //bird vector    public int Nbird = 1;                //count of bird    public Vector Vfeather = new Vector();//count of dropping-feather    public int score = 0;    Graphics g2d;        public GraphicsPanel(){        this.setSize(1000,700);        init();    }    public void init(){        tracker = new MediaTracker(this) ;        toolkit = Toolkit.getDefaultToolkit();        bgimg = toolkit.getImage("image/123.JPG");        tracker.addImage(bgimg,0) ;        for(int i=0;i<7;i++){            birds[i] = toolkit.getImage("image/"+i+".jpg");            tracker.addImage(birds[i],0) ;        }        // feather = toolkit.getImage("");        cursor = toolkit.getImage("image/aim.jpg");        tracker.addImage(cursor,0) ;        try {            tracker.waitForAll();        } catch (InterruptedException ex) {            ex.printStackTrace();        }        font   = new Font("Helvetica", Font.BOLD, 24) ;        font_s = new Font("Helvetica", Font.BOLD, 14) ;    }        public void paintComponent(Graphics g) {                //draw the panel with the graphics from buffer        super.paintComponent(g);        setCursor(toolkit.createCustomCursor(cursor,new Point(0,0),"sight")) ; //set the cursor here             g2d = g;        g2d.setColor(Color.WHITE);        g2d.fillRect(0, 0, this.getWidth(), this.getHeight());        g2d.drawImage(bgimg,0,0,this);        // this.run();                                window_size=this.size();//get the panel size             buffer = null;        buffer = createImage(window_size.width, window_size.height);             backdrop = null;        backdrop = createImage(window_size.width, window_size.height);             buf_g = buffer.getGraphics();        buf_g.fillRect(0, 0, window_size.width, window_size.height);             /*load sound here          */        game_over = true ;                                            }                public Graphics getGraphics(){        return  g2d;    }     /*   public void start(){             setCursor(toolkit.createCustomCursor(cursor,new Point(0,0),"sight")) ; //set the cursor here        window_size=this.size();//get the panel size             buffer = null;        buffer = createImage(window_size.width, window_size.height);             backdrop = null;        backdrop = createImage(window_size.width, window_size.height);             buf_g = buffer.getGraphics();        buf_g.fillRect(0, 0, window_size.width, window_size.height);             /*load sound here          */       /* game_over = true ;    }*/        public int stop() {        if(game!=null){            game.stop();            game = null;        }        setCursor(Cursor.getDefaultCursor()) ;        return score;    }            public void run() {        Bird bird;        Feather feather;                Graphics g = this.getGraphics() ;        g.setColor(Color.red) ;        g.drawString("shot bird", 500,350) ;                long time = 0;        long count = 0;        buf_g = backdrop.getGraphics();        buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;        buf_g = getGraphics();        buf_g.drawImage(backdrop,0,0,this) ;                        buf_g = buffer.getGraphics();        buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;                repaint() ;                display_score() ;                while(true){            time = System.currentTimeMillis() ;            if ((Vbird.size() < Nbird) && (Math.random() > (Vbird.size() == 0 ? 0.90 : 0.98))) {                bird = new Bird(this);                //if (score > 10 && Math.random() > 0.7) ;速度控制                Vbird.add(bird);            }            for (int j=Vfeather.size()-1; j>=0 ; --j) {                feather = (Feather) Vfeather.elementAt(j) ;                if (feather.active) {                                        //if one bird is shot,show the floating feather                    feather.draw() ;                } else {                                        // shotting end,remove the feather object                    feather.erase() ;                    Vfeather.removeElementAt(j) ;                }            }            for(int i=0;i<Vbird.size();i++){                bird = (Bird)Vbird.elementAt(i);                if(bird.active()){                    bird.move();                }                            }            for (int i=(Vbird.size()-1); i >=0 ; --i) {                bird = (Bird) Vbird.elementAt(i);             /*if(...){              击中得分代码               if(Nbird<5&&score/10==1)               ++Nbird;               bird.active(false);               bird.erase();               feather = new Feather(this,bird.px,bird.py);               Vfeather.addElement(feather);                }              */                if(bird.active())                    bird.draw();                else                    Vbird.removeElementAt(i);                            }            time = System.currentTimeMillis() - time ;            time = 20 - time;            time = time > 0 ?  10 + time : 10 ;            Thread.yield();            try {                Thread.sleep(time);            } catch (InterruptedException ex) {}            if((count = ++count % 100)==0)                repaint();        }    }        public void display_score() {        Graphics bkd_g = backdrop.getGraphics();        bkd_g.clipRect(window_size.width/2, 0, window_size.width/2, 40);        bkd_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;                bkd_g.setColor(Color.red) ;        bkd_g.setFont(font) ;        String aux = score > 9 ? "" : "0" ;        bkd_g.drawString(aux+score, window_size.width - 60,30) ;        bkd_g.dispose() ;                Graphics bg = buffer.getGraphics() ;        bg.clipRect(0, 0, window_size.width, 40);        bg.drawImage(backdrop,0,0,this) ;        bg.dispose() ;                Graphics g = getGraphics() ;        g.clipRect(0, 0, window_size.width, 40);        g.drawImage(buffer,0,0,this) ;        g.dispose() ;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -