Java swing 实现angrybird

[java]  view plain  copy
 print ?
  1. /* 
  2.  * To change this template, choose Tools | Templates 
  3.  * and open the template in the editor. 
  4.  */  
  5. package AngryBirdsApplication;  
  6.   
  7. import java.awt.event.KeyEvent;  
  8. import java.awt.event.MouseAdapter;  
  9. import java.awt.event.MouseEvent;  
  10. import java.awt.event.MouseMotionListener;  
  11. import java.io.IOException;  
  12. import java.util.logging.Level;  
  13. import java.util.logging.Logger;  
  14. import org.jbox2d.callbacks.ContactImpulse;  
  15. import org.jbox2d.callbacks.ContactListener;  
  16. import org.jbox2d.collision.Manifold;  
  17. import org.jbox2d.common.Vec2;  
  18. import org.jbox2d.dynamics.Fixture;  
  19. import org.jbox2d.dynamics.contacts.Contact;  
  20.   
  21. /** 
  22.  * 
  23.  * @author Sumomoxiao 
  24.  */  
  25. public class AngryBirdsController extends MouseAdapter implements Runnable, MouseMotionListener, ContactListener {  
  26.   
  27.     private AngryBirdsArea m_stage;  
  28.     public AngryBirdsPanel m_view;  
  29.     private final AngryBirdsDraw drawer;  
  30.    // private final MusicController music;  
  31.     Thread gamethread;  
  32.     boolean stop = true;  
  33.   
  34.     AngryBirdsController(AngryBirdsArea m, AngryBirdsPanel v){//, MusicController mc) {  
  35.         this.m_stage = m;  
  36.         m_view = v;  
  37.         drawer = v.getSDDraw();  
  38.         v.setStageController(this);  
  39.         m.initStage();  
  40.         //music = mc;  
  41.         drawer.setStage(m);  
  42.         this.addListener();  
  43.   
  44.     }  
  45.   
  46.     @Override  
  47.     public void run() {  
  48.         while (true) {  
  49.             try {  
  50.                 while (!stop) {  
  51.                     m_stage.update();  
  52.                     drawer.drawStage();  
  53.                     try {  
  54.                         Thread.sleep(5);  
  55.                     } catch (InterruptedException ex) {  
  56.                     }  
  57.                 }  
  58.                 drawer.drawStage();  
  59.                 Thread.sleep(12);  
  60.             } catch (InterruptedException ex) {  
  61.                 Logger.getLogger(AngryBirdsController.class.getName()).log(Level.SEVERE, null, ex);  
  62.             }  
  63.         }  
  64.     }  
  65.   
  66.     public void start() {  
  67.         if (gamethread == null) {  
  68.             stop = false;  
  69.             gamethread = new Thread(this);  
  70.             gamethread.start();  
  71.         }  
  72.     }  
  73.   
  74.     public void pause() {  
  75.         if (isPainting()) {  
  76.             stop = true;  
  77.         }  
  78.     }  
  79.   
  80.     public void resume() {  
  81.         if (!isPainting()) {  
  82.             stop = false;  
  83.         }  
  84.     }  
  85.   
  86.     public boolean isPainting() {  
  87.         return m_view.isPainting();  
  88.     }  
  89.   
  90.     /* 
  91.      * synchronized public void setRunthread(boolean runthread) { 
  92.      * if(this.stop!=runthread){ this.stop = runthread; } 
  93.      * 
  94.      * 
  95.      * } 
  96.      * 
  97.      */  
  98.     public void addListener() {  
  99.         m_view.addMouseListener(new MouseAdapter() {  
  100.   
  101.             @Override  
  102.             public void mousePressed(MouseEvent e) {  
  103.   
  104.                 if (m_stage != null) {  
  105.                     Vec2 pos = new Vec2(e.getX(), e.getY());  
  106.                     if (e.getButton() == MouseEvent.BUTTON1) {  
  107.                         drawer.getScreenToWorldToOut(pos, pos);  
  108.                         m_stage.queueMouseDown(pos);  
  109.   
  110.                     }  
  111.                 }  
  112.             }  
  113.   
  114.             @Override  
  115.             public void mouseReleased(MouseEvent e) {  
  116.                 if (m_stage != null) {  
  117.                     Vec2 pos = new Vec2(e.getX(), e.getY());  
  118.                     drawer.getScreenToWorldToOut(pos, pos);  
  119.                     m_stage.queueMouseUp(pos);  
  120.                 }  
  121.             }  
  122.         });  
  123.   
  124.         m_view.addMouseMotionListener(new MouseMotionListener() {  
  125.   
  126.             final Vec2 posDif = new Vec2();  
  127.             final Vec2 pos = new Vec2();  
  128.             final Vec2 pos2 = new Vec2();  
  129.   
  130.             public void mouseDragged(MouseEvent e) {  
  131.                 pos.set(e.getX(), e.getY());  
  132.   
  133.   
  134.                 if (m_stage != null) {  
  135.                     drawer.getScreenToWorldToOut(pos, pos);  
  136.                     m_stage.queueMouseMove(pos);  
  137.                 }  
  138.             }  
  139.   
  140.             @Override  
  141.             public void mouseMoved(MouseEvent e) {  
  142.                 pos2.set(e.getX(), e.getY());  
  143.                 if (m_stage != null) {  
  144.                     drawer.getScreenToWorldToOut(pos2, pos2);  
  145.                     m_stage.queueMouseMove(pos2);  
  146.                 }  
  147.             }  
  148.         });  
  149.     }  
  150.   
  151.     @Override  
  152.     public synchronized void beginContact(Contact contact) {  
  153.                  
  154.           //   drawer.drawContact();  
  155.           //   System.out.print("listener thread !!!\n");  
  156.     }  
  157.   
  158.     @Override  
  159.     public void endContact(Contact contact) {  
  160.     }  
  161.   
  162.     @Override  
  163.     public void preSolve(Contact contact, Manifold oldManifold) {  
  164.         // throw new UnsupportedOperationException("Not supported yet.");  
  165.     }  
  166.   
  167.     Fixture fix ;  
  168.     @Override  
  169.     public void postSolve(Contact contact, ContactImpulse impulse) {  
  170.   
  171.        if (contact.m_fixtureA.m_filter.groupIndex == -1 || contact.m_fixtureB.m_filter.groupIndex == -1) {  
  172.                       fix = contact.m_fixtureA.m_filter.groupIndex == -1?contact.m_fixtureA:contact.m_fixtureB;  
  173.                         
  174.             for (int i = 0; i < contact.getManifold().pointCount; i++) {  
  175.   
  176.                 if (impulse.normalImpulses[i] > 0.8) {  
  177.                     System.out.print("pushing point \n");  
  178.                     drawer.pushContactPoint(fix.m_body.getPosition());  
  179.                     /*try { 
  180.                         //music.birdScream(); 
  181.                     } catch (IOException ex) {                    }*/  
  182.                 }  
  183.             }  
  184.         }  
  185.         if (contact.m_fixtureA.m_filter.groupIndex == 1 || contact.m_fixtureB.m_filter.groupIndex == 1) {  
  186.                 // System.out.print(" impulse point count  : "+impulse.normalImpulses.length+"\n");  
  187.             for (int i = 0; i < contact.getManifold().pointCount; i++) {  
  188.                  //System.out.print("wood impulse : "+impulse.normalImpulses[i]+"\n");  
  189.                 if (impulse.normalImpulses[i] > 3.1f) {  
  190.                     //System.out.print("wood impulse : "+impulse.normalImpulses[i]+"\n");  
  191.                     /*try { 
  192.                         //music.woodCollision(); 
  193.                     } catch (IOException ex) { 
  194.                     }*/  
  195.                     return;  
  196.                 }  
  197.             }  
  198.         }  
  199.   
  200.     }  
  201. }  

[java]  view plain  copy
 print ?
  1. /* 
  2.  * To change this template, choose Tools | Templates 
  3.  * and open the template in the editor. 
  4.  */  
  5. package AngryBirdsCharacters;  
  6.   
  7.   
  8. import javax.swing.ImageIcon;  
  9. import org.jbox2d.collision.shapes.CircleShape;  
  10. import org.jbox2d.collision.shapes.Shape;  
  11. import org.jbox2d.common.Vec2;  
  12. import org.jbox2d.dynamics.Body;  
  13. import org.jbox2d.dynamics.BodyDef;  
  14. import org.jbox2d.dynamics.FixtureDef;  
  15. import org.jbox2d.dynamics.World;  
  16.   
  17. /** 
  18.  * 
  19.  * @author SONY 
  20.  */  
  21. public class AngryBirdsModel extends AngryBirdsCharacter{  
  22.       
  23.    public ImageIcon birds1=new ImageIcon("src/AngryBirdsImagePack/birds.png");  
  24.    public ImageIcon birds2=new ImageIcon();  
  25.    public ImageIcon birds3=new ImageIcon();  
  26.      
  27.   public  AngryBirdsModel() {  
  28.      super();  
  29.          }  
  30.         
  31.     
  32.    public Body createBirds(World mom,int birdtype,Vec2 pos) {  
  33.       this.getCharacterdef().position.set(pos);  
  34.       this.getCharacterdef().linearDamping=0.01f;  
  35.       this.getCharacterfixdef().filter.groupIndex=-1;  
  36.       this.charactershape=new CircleShape();  
  37.       Body bird=mom.createBody(characterdef);  
  38.       switch(birdtype) {  
  39.           case 1:  
  40.               this.charactershape.m_radius=0.5f;  
  41.               this.getCharacterfixdef().shape= this.charactershape;  
  42.               this.getCharacterinfo().setName("Lil Bird");  
  43.               this.getCharacterinfo().setHafheight(0.5f);  
  44.               this.getCharacterinfo().setHafwidth(0.5f);  
  45.               this.getCharacterinfo().setAppearance(birds1.getImage());  
  46.               bird.m_userData=this.getCharacterinfo();  
  47.               bird.createFixture(characterfixdef);  
  48.               break;  
  49.           case 2:  
  50.               this.charactershape.m_radius=0.7f;  
  51.               this.getCharacterfixdef().shape= this.charactershape;  
  52.               this.getCharacterinfo().setName("Strong Bird");  
  53.               this.getCharacterinfo().setHafheight(0.7f);  
  54.               this.getCharacterinfo().setHafwidth(0.7f);  
  55.               this.getCharacterinfo().setAppearance(birds2.getImage());  
  56.               bird.m_userData=this.getCharacterinfo();  
  57.               bird.createFixture(characterfixdef);  
  58.               break;     
  59.           case 3:  
  60.               this.charactershape.m_radius=1f;  
  61.               this.getCharacterfixdef().shape= this.charactershape;  
  62.               this.getCharacterinfo().setName("Angry Bird");  
  63.               this.getCharacterinfo().setHafheight(1f);  
  64.               this.getCharacterinfo().setHafwidth(1f);  
  65.               this.getCharacterinfo().setAppearance(birds3.getImage());  
  66.               bird.m_userData=this.getCharacterinfo();  
  67.               bird.createFixture(characterfixdef);  
  68.               break;  
  69.       }  
  70.       return bird;  
  71.   }  
  72.     
  73.   
  74. }  
  75.      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值