一些好用的方法,编写游戏的时候可能用的上!不断更新中_2009_04_23

简介:

1.Bresenham 直线画法 改进版: 

 
  1. /** *//**  
  2.      * Bresenham Line Algorithm  
  3.      * @param dashedMask 设置线型的虚线的间隔,为0则画实线。  
  4.      * @param lineWidth 设置线宽。  
  5.      * @param x1   
  6.      * @param y1  
  7.      * @param x2  
  8.      * @param y2  
  9.     */ 
  10.     public static void bresenhamLine(Graphics g, int dashedMask, int lineWidth, int x1, int y1, int x2, int y2)   
  11.     ...{  
  12.         g.setColor( 0xFFFFFF );  
  13.         int x, y;  
  14.         int dx, dy;  
  15.         int incx, incy;  
  16.         int balance;  
  17.         int i = 0;  
  18.         if (x2 >= x1)  
  19.         ...{  
  20.             dx = x2 - x1;  
  21.             incx = 1;  
  22.         } else   
  23.         ...{  
  24.             dx = x1 - x2;  
  25.             incx = -1;  
  26.         }  
  27.         if (y2 >= y1)   
  28.         ...{  
  29.             dy = y2 - y1;  
  30.             incy = 1;  
  31.         } else   
  32.         ...{  
  33.             dy = y1 - y2;  
  34.             incy = -1;  
  35.         }  
  36.         x = x1;  
  37.         y = y1;  
  38.         if (dx >= dy)   
  39.         ...{  
  40.             dy <<= 1;  
  41.             balance = dy - dx;  
  42.             dx <<= 1;  
  43.             while (x != x2)   
  44.             ...{  
  45.                 if ((i & dashedMask) == 0)  
  46.                     g.fillRect(x, y, lineWidth, lineWidth);  
  47.                 if (balance >= 0)   
  48.                 ...{  
  49.                     y += incy;  
  50.                     balance -= dx;  
  51.                 }  
  52.                 balance += dy;  
  53.                 x += incx;  
  54.                 i++;  
  55.             }  
  56.             if ((i & dashedMask) == 0)  
  57.                 g.fillRect(x, y, lineWidth, lineWidth);  
  58.         }   
  59.         else   
  60.         ...{  
  61.             dx <<= 1;  
  62.             balance = dx - dy;  
  63.             dy <<= 1;  
  64.             while (y != y2)   
  65.             ...{  
  66.                 if ((i & dashedMask) == 0)  
  67.                     g.fillRect(x, y, lineWidth, lineWidth);  
  68.                 if (balance >= 0)   
  69.                 ...{  
  70.                     x += incx;  
  71.                     balance -= dy;  
  72.                 }  
  73.                 balance += dx;  
  74.                 y += incy;  
  75.                 i++;  
  76.             }  
  77.             if ((i & dashedMask) == 0)  
  78.                 g.fillRect(x, y, lineWidth, lineWidth);  
  79.         }  
  80.     } 

2.绘制精灵飞行轨迹方法 
使用示例
isFlyEnd=paintPicFly( g, sprite[y][x], sprite[y][x].getX(), sprite[y][x].getY(), x*RECT_W, y*RECT_H, 4 );

 
  1. /** *//**  
  2.      * 绘制精灵飞行轨迹  
  3.      * @param g  
  4.      * @param sprite  
  5.      * @param x1    起点  
  6.      * @param y1  
  7.      * @param x2    终点  
  8.      * @param y2  
  9.      * @param speed 速度  
  10.      * @return        是否飞行结束  
  11.      */ 
  12.     public final boolean paintPicFly( Graphics g, Sprite sprite, int x1, int y1, int x2, int y2, int speed )  
  13.     ...{  
  14.         if( x1==x2 && y1==y2 )  
  15.         ...{  
  16.             sprite.paint(g);  
  17.             return true;  
  18.         }  
  19.         int x, y;  
  20.         int dx, dy;  
  21.         int incx, incy;  
  22.         int balance;  
  23.         int i = 0;  
  24.         if (x2 >= x1)  
  25.         ...{  
  26.             dx = x2 - x1;  
  27.             incx = 1;  
  28.         } else   
  29.         ...{  
  30.             dx = x1 - x2;  
  31.             incx = -1;  
  32.         }  
  33.         if (y2 >= y1)   
  34.         ...{  
  35.             dy = y2 - y1;  
  36.             incy = 1;  
  37.         } else   
  38.         ...{  
  39.             dy = y1 - y2;  
  40.             incy = -1;  
  41.         }  
  42.         x = x1;  
  43.         y = y1;  
  44.         if (dx >= dy)   
  45.         ...{  
  46.             dy <<= 1;  
  47.             balance = dy - dx;  
  48.             dx <<= 1;  
  49.             for( ; x!=x2; )    //while (x != x2)   
  50.             ...{  
  51.                 if (balance >= 0)   
  52.                 ...{  
  53.                     y += incy;  
  54.                     balance -= dx;  
  55.                 }  
  56.                 balance += dy;  
  57.                 x += incx;  
  58.                 i++;  
  59.                 if ((i & speed) == 0 || i == speed )  
  60.                 ...{  
  61.                     paintPic( g, sprite, x, y );  
  62.                     return false;  
  63.                 }  
  64.             }  
  65.             if ((i & speed) == 0 || i == speed )  
  66.             ...{  
  67.                 paintPic( g, sprite, x, y );  
  68.             }  
  69.         }   
  70.         else   
  71.         ...{  
  72.             dx <<= 1;  
  73.             balance = dx - dy;  
  74.             dy <<= 1;  
  75.             for( ; y!=y2; )    //while (y != y2)   
  76.             ...{  
  77.                 if (balance >= 0)   
  78.                 ...{  
  79.                     x += incx;  
  80.                     balance -= dy;  
  81.                 }  
  82.                 balance += dx;  
  83.                 y += incy;  
  84.                 i++;  
  85.                 if ((i & speed) == 0 || i == speed )  
  86.                 ...{  
  87.                     paintPic( g, sprite, x, y );  
  88.                     return false;  
  89.                 }  
  90.             }  
  91.             if ((i & speed) == 0 || i == speed )  
  92.             ...{  
  93.                 paintPic( g, sprite, x, y );  
  94.             }  
  95.         }  
  96.         return false;  
  97.     }  
  98.     void paintPic( Graphics g, Sprite sprite, int x, int y )  
  99.     ...{  
  100.         sprite.setPosition(x,y);  
  101.         sprite.paint(g);  
  102. //        g.setColor( 0xFF0000 );  
  103. //        g.fillRect(x, y, 1, 1);  
  104.     } 

 3.矩形碰撞检测

 
  1.  /** *//**  
  2.     * 矩形碰撞检测  
  3.     * @param x1    矩形1左上角X坐标  
  4.     * @param y1    矩形1左上角y坐标  
  5.     * @param w1    矩形1宽  
  6.     * @param h1    矩形1高  
  7.     * @param x2    矩形2左上角y坐标  
  8.     * @param y2    矩形2左上角y坐标  
  9.     * @param w2    矩形2宽  
  10.     * @param h2    矩形2高  
  11.     * @return    是否碰撞  
  12.     */ 
  13.    boolean isIntersect(int x1,int y1, int w1, int h1, int x2, int y2, int w2, int h2)  
  14.    ...{  
  15.        if( Math.abs(x2-x1) < (w1+w2)/2 && Math.abs(y2-y1) < (h1+h2)/2 )  
  16.        ...{  
  17.            return true;  
  18.        }  
  19.        else 
  20.            return false;  
  21.    }  
  22. /以上方法是有问题的!请参看以下方法  
  23. /**  
  24.     * 矩形碰撞检测  
  25.     * @param x1    矩形1左上角X坐标  
  26.     * @param y1    矩形1左上角y坐标  
  27.     * @param w1    矩形1宽  
  28.     * @param h1    矩形1高  
  29.     * @param x2    矩形2左上角x坐标  
  30.     * @param y2    矩形2左上角y坐标  
  31.     * @param w2    矩形2宽  
  32.     * @param h2    矩形2高  
  33.     * @return     是否碰撞  
  34.     */ 
  35. public static boolean  isIntersect(int x1,int y1, int w1, int h1, int x2, int y2, int w2, int h2)  
  36.    {  
  37.        if( isIntersect(x1,y1,w1,h1,x2   ,y2) )  
  38.            return true;  
  39.        if( isIntersect(x1,y1,w1,h1,x2+w2,y2) )  
  40.            return true;  
  41.        if( isIntersect(x1,y1,w1,h1,x2+w2,y2+h2) )  
  42.            return true;  
  43.        if( isIntersect(x1,y1,w1,h1,x2   ,y2+h2) )  
  44.            return true;  
  45.        return false;  
  46.    }  
  47. //判断一个点是否在一个矩形内部或者在这个矩形的边上  
  48. private static boolean isIntersect(int x,int y, int w, int h, int px, int py)  
  49. {  
  50.  if( px>=x&&px<=x+w && py>=y&&py<=y+h )  
  51.   return true;  
  52.  else 
  53.   return false;  

4.单Player播放MIDI

 
  1. static boolean                            m_isSoundOn;  
  2.     static boolean                            m_isMusicOn;  
  3.     static int                                m_currentSound;  
  4.     static int                                m_currentMIDIID;  
  5.     static javax.microedition.media.Player    m_sound;  
  6.     static byte[][]                         s_soundData;  
  7.     static boolean                             s_isPlayMid = false;  
  8.       
  9.     void LoadSound()  
  10.     ...{  
  11.         try 
  12.         ...{  
  13.             SetCurrentInputStream( new DataInputStream("".getClass().getResourceAsStream("/sou") ) );  
  14.             ReadOffsetTable();  
  15.             s_soundData = new byte[m_nDataBlocksCount][];  
  16.               
  17.             byte[] buffer = new byte[ m_dataBlocksOffset[ m_nDataBlocksCount ] ];  
  18.             int[]  header = m_dataBlocksOffset;  
  19.             ReadBytes(buffer);  
  20.             SetCurrentInputStream( null );  
  21.             System.gc();  
  22.  
  23.             for (int i=0; i<m_nDataBlocksCount; i++)  
  24.             ...{  
  25.                 int length = header[ i + 1 ] - header[ i ];  
  26.                 if ( length > 0 )  
  27.                 ...{  
  28.                     s_soundData[i] = new byte[length];  
  29.                     System.arraycopy( buffer, header[i], s_soundData[i], 0, length );  
  30.                 }  
  31.             }  
  32.             header = null;       
  33.             buffer = null;  
  34.             System.gc();         
  35.             m_currentSound = -1;  
  36.             m_currentMIDIID = -1;  
  37.         }  
  38.         catch ( Exception e ) ...{ }  
  39.     }  
  40.       
  41.     static void PlaySound(int id)  
  42.     ...{  
  43.            PlaySoundLoop(id,1);  
  44.     }  
  45.  
  46.     static void PlaySoundLoop( int id, int loop )  
  47.     ...{  
  48.         if (id <= dSoundID.Sound_ID_TITLE)  // midi  
  49.         ...{  
  50.             if (!m_isMusicOn)  
  51.                 return;  
  52.         }  
  53.         else      // wav  
  54.         ...{  
  55.             if (!m_isSoundOn && id != dSoundID.Sound_ID_MENUCONFIRM)  
  56.                 return;  
  57.         }  
  58.         if (id == m_currentSound && m_sound!=null&& (m_sound.getState() == javax.microedition.media.Player.STARTED))  
  59.             return;  
  60.         else 
  61.             StopSound(id);  
  62.         try   
  63.         ...{  
  64.             InputStream is = new ByteArrayInputStream( s_soundData[id] );  
  65.             m_sound = javax.microedition.media.Manager.createPlayer(is, "audio/midi");  
  66.             m_sound.realize();  
  67.             m_sound.setLoopCount( loop );  
  68.             m_sound.start();  
  69.             m_currentSound = id;  
  70.             s_isPlayMid = false;  
  71.         }  
  72.         catch (Exception e) ...{ e.printStackTrace(); }  
  73.     }  
  74.  
  75.     static void StopSound(int id)  
  76.     ...{  
  77.         try 
  78.         ...{  
  79.             if( m_sound == null )  
  80.                 return;  
  81.             else 
  82.             ...{  
  83.                 m_sound.deallocate();  
  84.                 m_sound.close();  
  85.                 m_sound = null;  
  86.             }  
  87.         }  
  88.         catch ( Exception e ) ...{ e.printStackTrace(); }  
  89.     }  
  90.  
  91.     static void ResumeSound()  
  92.     ...{  
  93.         PlaySound( m_currentSound );  
  94.     } 

5.从一张大图分割成小图

 
  1. try 
  2.       ...{  
  3.           img        = Image.createImage("/pics/"+imgNum+".png");//将大图创建到临时Image对象中  
  4.           Graphics gn;                                        //创建临时绘图设备  
  5.           forint i=0; i<y_tile_sum; i++ )  
  6.           ...{  
  7.               forint j=0; j<x_tile_sum; j++ )  
  8.               ...{  
  9.                   img_tile[i][j] = Image.createImage(RECT_W, RECT_H);            //创建小图的大小  
  10.                   gn = img_tile[i][j].getGraphics();                            //创建小图的大小的临时绘图设备      
  11.                   //在该设备上绘制大图temp,但 绘图设备比较小,只有小图那么大,大图多余部分不会被绘制出来  
  12.                   gn.drawImage(img, -j*RECT_W, -i*RECT_H, gn.LEFT|gn.TOP);    //绘制大图时候的起点位置  
  13.               }  
  14.           }  
  15.           gn     = null;  
  16.           System.gc();    //通知垃圾回收机制,在需要时候会进行垃圾回收  
  17.       }  
  18.       catch(Exception e)...{ e.printStackTrace(); } 

6.j2me 保存记录

 
  1. final static String RECORD_FILENAME = "/assassin.sav";  
  2.     final static int RECORD_SIZE = 160;  
  3.     static byte s_recordData[] = new byte[RECORD_SIZE];  
  4.     public static void RecordStore(boolean save)  
  5.     ...{  
  6.         try 
  7.         ...{  
  8.             RecordStore rs = RecordStore.openRecordStore(RECORD_FILENAME, true);  
  9.             if (rs.getNumRecords() <= 0)  
  10.             ...{  
  11.                 if (save)  
  12.                 ...{  
  13.                     rs.addRecord(s_recordData, 0, RECORD_SIZE);  
  14.                 }  
  15.                 else 
  16.                 ...{  
  17.                     rs.closeRecordStore();  
  18.                     return;  
  19.                 }  
  20.             }  
  21.             else if (save)  
  22.             ...{  
  23.                 rs.setRecord(1, s_recordData, 0, RECORD_SIZE);  
  24.             }  
  25.             else 
  26.             ...{  
  27.                 rs.getRecord(1, s_recordData, 0);  
  28.             }  
  29.             rs.closeRecordStore();  
  30.             rs = null;  
  31.         }  
  32.         catch (Exception e)  
  33.         ...{  
  34.  
  35.         }  
  36.     } 

 

本文转自 kome2000 51CTO博客,原文链接:http://blog.51cto.com/kome2000/578525



相关文章
|
2月前
游戏辅助 -- 获取人物属性代码编写
游戏辅助 -- 获取人物属性代码编写
31 0
|
5月前
|
缓存 网络协议 5G
剖析KCP以及KCP在游戏中是如何使用的
剖析KCP以及KCP在游戏中是如何使用的
|
3月前
|
移动开发 JavaScript C#
分享31个游戏源代码总有一个是你想要的
分享31个游戏源代码总有一个是你想要的
84 0
|
4月前
|
人工智能 移动开发 数据可视化
推荐几个不用写代码也能做游戏的工具
推荐几个不用写代码也能做游戏的工具
137 0
|
9月前
|
数据采集 安全 小程序
如何用100行Python代码做出魔性声控游戏“八分音符酱”
有了这两样东西,其他就没什么特别的了。如果你用 cocos2d 开发过小游戏,剩下的就是一些常规工作。
|
11月前
|
Windows 容器
游戏开发零基础入门教程(13):整合到一起,做出第一个游戏
终于到了真正动手做游戏的时刻,在这一节里,我会带你从头开始将我们的“太空保卫者”按照设计方案制作出来。这一节里的内容会非常的多,一遍消化不了,可以多读几遍。别着急,慢慢来。
86 0
|
11月前
|
人工智能 移动开发 数据可视化
不会写代码,想要做游戏
嗨!大家好,我是小蚂蚁。 “我不会写代码,还能做游戏吗?”不少想做游戏的朋友可能都会有这个疑问,答案当然是“能”。 即使不会代码,也并不影响你做游戏。会不会写代码,并不是做游戏的必要条件。 感谢工具的力量,是先进的工具让做游戏这件事对很多人来说成为了可能,下面我就为大家介绍几个不需要写代码,也能够做游戏的工具。
130 0
|
存储 设计模式 JavaScript
从零开始做一款Unity3D游戏<三>——编写游戏机制(二)
从零开始做一款Unity3D游戏<三>——编写游戏机制
从零开始做一款Unity3D游戏<三>——编写游戏机制(二)
|
存储 JavaScript C#
从零开始做一款Unity3D游戏<三>——编写游戏机制(一)
从零开始做一款Unity3D游戏<三>——编写游戏机制
从零开始做一款Unity3D游戏<三>——编写游戏机制(一)
|
数据安全/隐私保护
【自然框架】——重开在线演示
  以前的那个在线演示的空间和域名过期了(感谢“云淡风清”和“恭敬”提供域名、空间和数据库),想想还是自己弄个空间来的稳定一些。所以买了个空间,交了一年的钱,所以至少一年内是稳定的。另外还想做一个专门介绍“自然框架”的网站,现在还在构思和完善中,预计一周的时间可以基本成型。
867 0