纯Java开发的游戏引擎V0.4--DEMO2 -- 物理引擎


项目命名: JPhysicalEngine
项目目的: 自己爱好/毕业设计
项目人员: http://blog.csdn.net/kakashi8841
运行机器: Intel P8600 2.4GHz、2G内存、Intel GMA X4500 HD
开发环境: Linux Ubuntu 10.10
开发语言: Java
开发工具: Eclipse

项目描述: 使用Java开发的2D游戏物理引擎,可以使得以后开发类似愤怒的小鸟、雷电等物理、碰撞、动作类游戏可以更快速、更方便。

项目进度:

    【已实现】
        版本  完成日期       实现功能
        V0.1 [2011-04-07]  大致框架
        V0.2 [2011-04-11]  基本动画
        V0.3 [2011-04-15]  恒力和AABB碰撞检测
        V0.4 [2011-04-22]  框架优化、简单粒子系统

    【待实现】
        版本  计划完成日期    实现功能
        V0.5 [2011-05-05]  更精确的OBB碰撞检测
        V0.6 [2011-05-10]  变力
        V0.7 [2011-05-12]  更完善的粒子系统
        V0.8 [2011-05-17]  弹力、引力
        V0.9 [2011-05-20]  框架优化、简单的输入输出操作

目前引用自制引擎做的DEMO2:

代码只需200+行:

  1.  * org.ubird.demo.Demo2.java   
  2. package org.ubird.demo;   
  3.   
  4.   
  5. import java.awt.Image;   
  6. import java.awt.event.KeyEvent;   
  7. import java.awt.event.KeyListener;   
  8. import java.io.IOException;   
  9.   
  10. import javax.imageio.ImageIO;   
  11.   
  12.   
  13. import org.ubird.app.SimpleGame;   
  14. import org.ubird.scene.ImageNode;   
  15. import org.ubird.scene.Node;   
  16. import org.ubird.scene.particle.ParticleNode;   
  17. import org.ubird.scene.particle.ParticleSystem;   
  18.   
  19. /**  
  20.  * 测试类2  
  21.  * @author junhong.c  
  22.  * @version 1.0  
  23.  * @date 2011-4-23  
  24.  */  
  25. public class Demo2 extends SimpleGame{   
  26.   
  27.     private Node player;   
  28.        
  29.     private ParticleNode bomb = ParticleSystem.getParticles(500,15,15,10);   
  30.        
  31.     private Node[] bullets = new ImageNode[40];   
  32.     private int currentBullet = 0;   
  33.        
  34.     private long delay;     //子弹延时   
  35.     private boolean fire;   
  36.     private boolean fire2;   
  37.     private boolean fire3;   
  38.     private boolean down;   
  39.     private boolean up;   
  40.     private boolean right;   
  41.     private boolean left;   
  42.        
  43.     public Demo2() {   
  44.         super("Java2D游戏引擎 V0.4.0 -- By John.Cha");   
  45.     }   
  46.   
  47.     public static void main(String[] args) throws IOException {   
  48.         SimpleGame game = new Demo2();   
  49.         game.setFPS(60);   
  50.         game.setStageBackground("resources/bg.png");   
  51.         game.setStageSize(1000,600);   
  52.         game.start();   
  53.     }   
  54.   
  55.   
  56.     @Override  
  57.     public void initWorld() {   
  58.         initScene();   
  59.         initNode();   
  60.         initEvent();   
  61.     }   
  62.        
  63.     /**  
  64.      * 事件  
  65.      */  
  66.     private void initEvent() {   
  67.         addKeyListener(new MyKeyListener());   
  68.     }   
  69.   
  70.     /**  
  71.      * 定义自己的事件监听  
  72.      * @author junhong.c  
  73.      * @version 1.0  
  74.      * @date 2011-4-22  
  75.      */  
  76.     private class MyKeyListener implements KeyListener{   
  77.   
  78.         @Override  
  79.         public void keyTyped(KeyEvent e) {}   
  80.   
  81.         @Override  
  82.         public void keyReleased(KeyEvent e) {   
  83.             switch(e.getKeyCode()){   
  84.                 case KeyEvent.VK_UP :   
  85.                     up=false;   
  86.                     break;   
  87.                 case KeyEvent.VK_DOWN:   
  88.                     down=false;   
  89.                     break;   
  90.                 case KeyEvent.VK_RIGHT:   
  91.                     right=false;   
  92.                     break;   
  93.                 case KeyEvent.VK_LEFT:   
  94.                     left=false;   
  95.                     break;   
  96.                 case KeyEvent.VK_A:   
  97.                     fire=false;   
  98.                     break;   
  99.                 case KeyEvent.VK_S:   
  100.                     fire2=false;   
  101.                     break;   
  102.                 case KeyEvent.VK_D:   
  103.                     fire3=false;   
  104.                     break;   
  105.             }   
  106.         }   
  107.   
  108.         @Override  
  109.         public void keyPressed(KeyEvent e) {   
  110.             if(fire && e.getKeyCode()==KeyEvent.VK_A) return;   
  111.             if(fire2 && e.getKeyCode()==KeyEvent.VK_S) return;   
  112.             if(fire3 && e.getKeyCode()==KeyEvent.VK_D) return;   
  113.             if(up && e.getKeyCode()==KeyEvent.VK_UP) return;   
  114.             if(down && e.getKeyCode()==KeyEvent.VK_DOWN) return;   
  115.             if(left && e.getKeyCode()==KeyEvent.VK_LEFT) return;   
  116.             if(right && e.getKeyCode()==KeyEvent.VK_RIGHT) return;   
  117.                
  118.             switch(e.getKeyCode()){   
  119.                 case KeyEvent.VK_UP :   
  120.                     up=true;   
  121.                     break;   
  122.                 case KeyEvent.VK_DOWN:   
  123.                     down=true;   
  124.                     break;   
  125.                 case KeyEvent.VK_RIGHT:   
  126.                     right=true;   
  127.                     break;   
  128.                 case KeyEvent.VK_LEFT:   
  129.                     left=true;   
  130.                     break;   
  131.                 case KeyEvent.VK_B:   
  132.                     bomb.start(player.getLocation().getIntX()-player.getWidth()/2-3,player.getLocation().getIntY()-player.getHeight());   
  133.                     break;   
  134.                 case KeyEvent.VK_A:   
  135.                     fire=true;   
  136.                     break;   
  137.                 case KeyEvent.VK_S:   
  138.                     fire2=true;   
  139.                     break;   
  140.                 case KeyEvent.VK_D:   
  141.                     fire3=true;   
  142.                     break;   
  143.             }   
  144.                
  145.             getProcessThread().start();   
  146.         }   
  147.     }   
  148.        
  149.        
  150.   
  151.     /**  
  152.      * 场景   
  153.      */  
  154.     private void initScene() {   
  155.         add(bomb);   
  156.     }   
  157.        
  158.     /**  
  159.      * 处理按键的线程  
  160.      * @return  
  161.      */  
  162.     public Thread getProcessThread(){   
  163.         return new Thread(){   
  164.             public void run(){   
  165.                 try {   
  166.                     while(up||down||left||right||fire||fire2||fire3){   
  167.                         Thread.sleep(1000/60);   
  168.                         float v = 0.2f;   
  169.                         if(up)   
  170.                             player.getVelocity().setY(-v);   
  171.                         else if(down)   
  172.                             player.getVelocity().setY(v);   
  173.                            
  174.                         if(left)   
  175.                             player.getVelocity().setX(-v);   
  176.                         else if(right)   
  177.                             player.getVelocity().setX(v);   
  178.                            
  179.                         if(!(up||down))   
  180.                             player.getVelocity().setY(0);   
  181.                            
  182.                         if(!(left||right))   
  183.                             player.getVelocity().setX(0);   
  184.                            
  185.                         if( fire ){   
  186.                             if(delay <= System.currentTimeMillis()){   
  187.                                 for(int i=0; i<2; i++){   
  188.                                     bullets[currentBullet].getVelocity().setY(-v*2f);   
  189.                                     bullets[currentBullet].getVelocity().setX(0);   
  190.                                     int fix = i == 0 ? -15 : 15;   
  191.                                        
  192.                                     float x = player.getLocation().getX()+(player.getWidth() - bullets[currentBullet].getWidth())*0.5f;   
  193.                                     bullets[currentBullet].setLocation(fix+x, player.getLocation().getY()-bullets[currentBullet].getHeight());   
  194.                                     currentBullet = ++currentBullet%bullets.length;   
  195.                                 }   
  196.                                 delay = System.currentTimeMillis()+80;   
  197.                             }   
  198.                         }   
  199.                            
  200.                         if(fire2){   
  201.                             if(delay <= System.currentTimeMillis()){   
  202.                                 for(int i=0; i<3; i++){   
  203.                                     bullets[currentBullet].getVelocity().setY(-v*2f);   
  204.                                     int fix = i == 0 ? -15 : i==2 ? 15 : 0;   
  205.                                     float vx = i==0 ? -v/4 : i==1 ? 0 : v/4;   
  206.                                     bullets[currentBullet].getVelocity().setX(vx);   
  207.                                     float x = player.getLocation().getX()+(player.getWidth() - bullets[currentBullet].getWidth())*0.5f;   
  208.                                     bullets[currentBullet].setLocation(fix+x, player.getLocation().getY()-bullets[currentBullet].getHeight());   
  209.                                        
  210.                                     currentBullet = ++currentBullet%bullets.length;   
  211.                                 }   
  212.                                 delay = System.currentTimeMillis()+100;   
  213.                             }   
  214.                         }   
  215.                            
  216.                         if(fire3){   
  217.                             for(int i=0; i<bullets.length; i++){   
  218.                                 bullets[currentBullet].getVelocity().setY(-v*2f);   
  219.                                 int fix = i == 0 ? -15 : i==2 ? 15 : 0;   
  220.                                    
  221.                                 float vx = (float) (v*Math.cos(6.28*i/bullets.length));   
  222.                                 float vy = (float) (v*Math.sin(6.28*i/bullets.length));   
  223.                                    
  224.                                 bullets[currentBullet].getVelocity().setX(vx);   
  225.                                 bullets[currentBullet].getVelocity().setY(vy);   
  226.                                 bullets[currentBullet].setLocation(fix+player.getLocation().getX()-player.getWidth()/2+2,player.getLocation().getY()-player.getHeight());   
  227.                                 bullets[currentBullet].setDelay(currentBullet);   
  228.                                 currentBullet = ++currentBullet%bullets.length;   
  229.                             }   
  230.                             fire3=false;   
  231.                         }   
  232.                     }   
  233.                 } catch (InterruptedException e) {   
  234.                     e.printStackTrace();   
  235.                 }   
  236.             }   
  237.         };   
  238.     }   
  239.   
  240.     /**  
  241.      *   
  242.      */  
  243.     private void initNode() {   
  244.            
  245.         /**  
  246.          * 初始化子弹  
  247.          */  
  248.         try {   
  249.             Image image = ImageIO.read(getClass().getClassLoader().getResource("resources/bullet.png"));   
  250.                
  251.             for(int i=0; i<bullets.length; i++){   
  252.                 bullets[i] = new ImageNode(image,i%6*35,0,35,35);   
  253.                 bullets[i].setSize(30,30);   
  254.                 bullets[i].setLocation(-500,-500);   
  255.                 add(bullets[i]);   
  256.             }   
  257.                
  258.             player = new ImageNode(image,70,35,35,35);  //平抛--图像结点   
  259.             player.setSize(35,35);   
  260.             player.setLocation((getStageWidth()-player.getWidth())/2,getStageHeight()-player.getHeight());   
  261.             player.setMass(10);   
  262.             add(player);   
  263.         } catch (IOException e1) {   
  264.             e1.printStackTrace();   
  265.         }   
  266.     }   
  267.   
  268.   
  269.     @Override  
  270.     public void updateWorld(long time) {   
  271.         player.update(time);   
  272.            
  273.         bomb.update(time);   
  274.            
  275.         for(int i=0; i<bullets.length; i++){   
  276.             if(bullets[i]!=null){   
  277.                 bullets[i].update(time);   
  278.             }   
  279.         }   
  280.     }   
  281. }  

相关内容