資源簡介
用swing寫的一個人物行走實例,只是實現(xiàn)了人物行走,對于地圖之類的就沒有實現(xiàn)了,菜鳥寫的菜鳥程序拿出來給大家學(xué)習(xí)學(xué)習(xí),呵呵!

代碼片段和文件信息
/**
?*?@(#)baseSprite.java
?*
?*
?*?@author?saint
?*?@version?1.00?2010/1/2
?*/
package?org;
import?javax.swing.*;
import?java.awt.*;
import?java.awt.event.*;
public?class?baseSprite?extends?JComponent?implements?Runnable?{
private?Image?SpriteImg; //精靈圖片
private?int?Direct; //8(上)2(下)4(左)6(右)
private?int?Spriteframe; //精靈當(dāng)前所在幀
private?int?SpriteWidth; //一幀的寬度
private?int?SpriteHeight; //一幀的高度
private?int?moveSpeed; //移動速度
private?int?sleepMillis; //線程休眠時間(秒)
private?int?sleepNanos; //線程休眠時間(納秒)
private?int?moveLevel; //跟速度等級有關(guān)
private?int?xy; //精靈坐標(biāo)
private?int?pxpy; //精靈偏移坐標(biāo)
private?boolean?stop; //是否停止移動
????public?baseSprite(String?img?int?w?int?h)?{
???? this.setSpriteImg(img?w?h);
???? this.initialize();
???? new?Thread(this).start();
????}
????
????//初始化
????private?void?initialize()?{
???? this.setMoveLevel(3);
???? this.Spriteframe?=?1;
???? this.x?=?0;
???? this.y?=?0;
???? this.px?=?(SpriteWidth-32)/2;
???? this.py?=?(SpriteHeight-32);
???? this.setPreferredSize(new?Dimension(SpriteWidthSpriteHeight));
???? this.setBounds(x-px?y-py?SpriteWidth?SpriteHeight);
???? this.stop?=?true;
????}
????
????//設(shè)置移動速度等級
????public?void?setMoveLevel(int?c)?{
???? if(c==1)?{
???? this.moveSpeed?=?1;
???? this.sleepMillis?=?10;
???? this.sleepNanos?=?0;
???? this.moveLevel?=?16;
???? }else?if(c==2)?{
???? this.moveSpeed?=?1;
???? this.sleepMillis?=?9;
???? this.sleepNanos?=?500;
???? this.moveLevel?=?16;
???? }else?if(c==3)?{
???? this.moveSpeed?=?2;
???? this.sleepMillis?=?10;
???? this.sleepNanos?=?500;
???? this.moveLevel?=?8;
???? }
????}
????
????//加載圖片
????private?Image?LoadImg(String?str)?{
???? return?(Toolkit.getDefaultToolkit()).createImage(str);
????}
?????
????//設(shè)置/獲取精靈圖片及其屬性
????public?void?setSpriteImg(String?str?int?w?int?h)?{
???? this.SpriteImg?=?this.LoadImg(str);
???? this.SpriteWidth?=?w/4;
???? this.SpriteHeight?=?h/4;
????}
????public?Image?getSpriteImg()?{
???? return?this.SpriteImg;
????}
????public?int?getSpriteWidth()?{
???? return?this.SpriteWidth;
????}
????public?int?getSpriteHeight()?{
???? return?this.SpriteHeight;
????}
????
????//獲取人物坐標(biāo)
????public?int?getSpriteX()?{
???? return?this.x;
????}
????public?int?getSpriteY()?{
???? return?this.y;
????}
????
????//人物行走
????public?void?moveStart(int?c)?{
???? this.Direct?=?c?;
???? if(stop)
???? stop?=?false;
????}
????private?void?moveSprite(int?c)?{
???? int?tmpx?=?x;
????int?tmpy?=?y;
????switch(c)?{
???? case?2:
???? tmpy+=moveSpeed;
??? break;
??? case?8:
??? tmpy-=moveSpeed;
???? break;
??? case?4:
??? tmpx-=moveSpeed;
??? break;
??? case?6:
??? tmpx+=moveSpeed;
??? }
??? if((tmpx>=0?&&?tmpx<=608)?&&?(tmpy>=0?&&?tmpy<=416))?{
??? x?=?tmpx;
??? y?=?tmpy;
??? this.setBounds(x-px?y-py?SpriteWidth?SpriteHeight);
??? }
????}
????//人物停止
????public?void?moveSto
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4556??2010-01-03?01:17??人物行走\ba
?????文件????????843??2010-01-02?16:31??人物行走\JImagePanel.java
?????文件???????2560??2010-01-03?01:23??人物行走\mainApp.java
?????文件???????3887??2010-01-03?01:24??人物行走\org\ba
?????文件???????5067??2004-06-25?00:00??人物行走\org\img\Actor.png
?????文件???????1949??2010-01-03?00:46??人物行走\org\img\map.gif
?????文件???????4028??2009-12-12?12:19??人物行走\org\img\snt32.png
?????文件???????1069??2010-01-02?16:31??人物行走\org\JImagePanel.class
?????文件???????1123??2010-01-02?15:47??人物行走\org\mainApp$1.class
?????文件???????2988??2010-01-03?01:23??人物行走\org\mainApp.class
?????文件????????571??2010-01-03?01:24??人物行走\org\winfr
?????文件???????1119??2010-01-03?01:24??人物行走\org\winfr
?????文件????????300??2010-01-03?01:34??人物行走\readme.txt
?????文件?????????17??2010-01-03?01:27??人物行走\Run.bat
?????文件????????760??2010-01-02?16:31??人物行走\winfr
?????目錄??????????0??2010-01-03?00:46??人物行走\org\img
?????目錄??????????0??2010-01-02?17:54??人物行走\org
?????目錄??????????0??2010-01-03?01:27??人物行走
-----------?---------??----------?-----??----
????????????????30837????????????????????18
評論
共有 條評論