java实现愤怒的小鸟游戏之Java swing图形界面开发游戏项目愤怒的小鸟源码及实现方式详解

来源:互联网 发布:解放军纪律知乎 编辑:程序博客网 时间:2024/05/16 06:31

今天给大家介绍一下Java swing实现游戏愤怒的小鸟的过程,完整源码下载地址在最下方,老规矩,先看运行结果:


下面我们来看看代码:

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package AngryBirdsApplication;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;import org.jbox2d.callbacks.ContactImpulse;import org.jbox2d.callbacks.ContactListener;import org.jbox2d.collision.Manifold;import org.jbox2d.common.Vec2;import org.jbox2d.dynamics.Fixture;import org.jbox2d.dynamics.contacts.Contact;/** * * @author Sumomoxiao */public class AngryBirdsController extends MouseAdapter implements Runnable, MouseMotionListener, ContactListener {    private AngryBirdsArea m_stage;    public AngryBirdsPanel m_view;    private final AngryBirdsDraw drawer;   // private final MusicController music;    Thread gamethread;    boolean stop = true;    AngryBirdsController(AngryBirdsArea m, AngryBirdsPanel v){//, MusicController mc) {        this.m_stage = m;        m_view = v;        drawer = v.getSDDraw();        v.setStageController(this);        m.initStage();        //music = mc;        drawer.setStage(m);        this.addListener();    }    @Override    public void run() {        while (true) {            try {                while (!stop) {                    m_stage.update();                    drawer.drawStage();                    try {                        Thread.sleep(5);                    } catch (InterruptedException ex) {                    }                }                drawer.drawStage();                Thread.sleep(12);            } catch (InterruptedException ex) {                Logger.getLogger(AngryBirdsController.class.getName()).log(Level.SEVERE, null, ex);            }        }    }    public void start() {        if (gamethread == null) {            stop = false;            gamethread = new Thread(this);            gamethread.start();        }    }    public void pause() {        if (isPainting()) {            stop = true;        }    }    public void resume() {        if (!isPainting()) {            stop = false;        }    }    public boolean isPainting() {        return m_view.isPainting();    }    /*     * synchronized public void setRunthread(boolean runthread) {     * if(this.stop!=runthread){ this.stop = runthread; }     *     *     * }     *     */    public void addListener() {        m_view.addMouseListener(new MouseAdapter() {            @Override            public void mousePressed(MouseEvent e) {                if (m_stage != null) {                    Vec2 pos = new Vec2(e.getX(), e.getY());                    if (e.getButton() == MouseEvent.BUTTON1) {                        drawer.getScreenToWorldToOut(pos, pos);                        m_stage.queueMouseDown(pos);                    }                }            }            @Override            public void mouseReleased(MouseEvent e) {                if (m_stage != null) {                    Vec2 pos = new Vec2(e.getX(), e.getY());                    drawer.getScreenToWorldToOut(pos, pos);                    m_stage.queueMouseUp(pos);                }            }        });        m_view.addMouseMotionListener(new MouseMotionListener() {            final Vec2 posDif = new Vec2();            final Vec2 pos = new Vec2();            final Vec2 pos2 = new Vec2();            public void mouseDragged(MouseEvent e) {                pos.set(e.getX(), e.getY());                if (m_stage != null) {                    drawer.getScreenToWorldToOut(pos, pos);                    m_stage.queueMouseMove(pos);                }            }            @Override            public void mouseMoved(MouseEvent e) {                pos2.set(e.getX(), e.getY());                if (m_stage != null) {                    drawer.getScreenToWorldToOut(pos2, pos2);                    m_stage.queueMouseMove(pos2);                }            }        });    }    @Override    public synchronized void beginContact(Contact contact) {                         //   drawer.drawContact();          //   System.out.print("listener thread !!!\n");    }    @Override    public void endContact(Contact contact) {    }    @Override    public void preSolve(Contact contact, Manifold oldManifold) {        // throw new UnsupportedOperationException("Not supported yet.");    }    Fixture fix ;    @Override    public void postSolve(Contact contact, ContactImpulse impulse) {       if (contact.m_fixtureA.m_filter.groupIndex == -1 || contact.m_fixtureB.m_filter.groupIndex == -1) {                      fix = contact.m_fixtureA.m_filter.groupIndex == -1?contact.m_fixtureA:contact.m_fixtureB;                                  for (int i = 0; i < contact.getManifold().pointCount; i++) {                if (impulse.normalImpulses[i] > 0.8) {                    System.out.print("pushing point \n");                    drawer.pushContactPoint(fix.m_body.getPosition());                    /*try {                        //music.birdScream();                    } catch (IOException ex) {                    }*/                }            }        }        if (contact.m_fixtureA.m_filter.groupIndex == 1 || contact.m_fixtureB.m_filter.groupIndex == 1) {                // System.out.print(" impulse point count  : "+impulse.normalImpulses.length+"\n");            for (int i = 0; i < contact.getManifold().pointCount; i++) {                 //System.out.print("wood impulse : "+impulse.normalImpulses[i]+"\n");                if (impulse.normalImpulses[i] > 3.1f) {                    //System.out.print("wood impulse : "+impulse.normalImpulses[i]+"\n");                    /*try {                        //music.woodCollision();                    } catch (IOException ex) {                    }*/                    return;                }            }        }    }}

/* * To change this template, choose Tools | Templates * and open the template in the editor. */package AngryBirdsCharacters;import javax.swing.ImageIcon;import org.jbox2d.collision.shapes.CircleShape;import org.jbox2d.collision.shapes.Shape;import org.jbox2d.common.Vec2;import org.jbox2d.dynamics.Body;import org.jbox2d.dynamics.BodyDef;import org.jbox2d.dynamics.FixtureDef;import org.jbox2d.dynamics.World;/** * * @author SONY */public class AngryBirdsModel extends AngryBirdsCharacter{       public ImageIcon birds1=new ImageIcon("src/AngryBirdsImagePack/birds.png");   public ImageIcon birds2=new ImageIcon();   public ImageIcon birds3=new ImageIcon();     public  AngryBirdsModel() {     super();         }           public Body createBirds(World mom,int birdtype,Vec2 pos) {      this.getCharacterdef().position.set(pos);      this.getCharacterdef().linearDamping=0.01f;      this.getCharacterfixdef().filter.groupIndex=-1;      this.charactershape=new CircleShape();      Body bird=mom.createBody(characterdef);      switch(birdtype) {          case 1:              this.charactershape.m_radius=0.5f;              this.getCharacterfixdef().shape= this.charactershape;              this.getCharacterinfo().setName("Lil Bird");              this.getCharacterinfo().setHafheight(0.5f);              this.getCharacterinfo().setHafwidth(0.5f);              this.getCharacterinfo().setAppearance(birds1.getImage());              bird.m_userData=this.getCharacterinfo();              bird.createFixture(characterfixdef);              break;          case 2:              this.charactershape.m_radius=0.7f;              this.getCharacterfixdef().shape= this.charactershape;              this.getCharacterinfo().setName("Strong Bird");              this.getCharacterinfo().setHafheight(0.7f);              this.getCharacterinfo().setHafwidth(0.7f);              this.getCharacterinfo().setAppearance(birds2.getImage());              bird.m_userData=this.getCharacterinfo();              bird.createFixture(characterfixdef);              break;             case 3:              this.charactershape.m_radius=1f;              this.getCharacterfixdef().shape= this.charactershape;              this.getCharacterinfo().setName("Angry Bird");              this.getCharacterinfo().setHafheight(1f);              this.getCharacterinfo().setHafwidth(1f);              this.getCharacterinfo().setAppearance(birds3.getImage());              bird.m_userData=this.getCharacterinfo();              bird.createFixture(characterfixdef);              break;      }      return bird;  }  }   

下载地址:点击下载(打开后第一个)

更多资源请点击:网盘搜索沐伊神器 咻盘网

1 0