2010年1月31日 星期日

QT4.6 2D戰略遊戲開發框架








































計算角色移動力公式:
























/////////////////滑鼠控制,計算在框框內,產生移動路徑//////////////////////////////////
void QGame_move_line_rpg::set_move_rpg_to_location(QPoint p)
{

this->clear_rpg_move_label();//先清除label
int x=this->get_move_obj_x();
int y=this->get_move_obj_y();
int disx=p.x()-x;
int disy=p.y()-y;
int stepx=disx/50;
int stepxx=stepx;
if(stepx<0)stepx=-stepx;//+
if(stepx>rpg.move_ability){stepx=rpg.move_ability;} //最大只能到角色移動力x
int stepy=disy/50;
int stepyy=stepy;
if(stepy<0)stepy=-stepy+1;//+
if(stepy>rpg.move_ability-stepx ){stepy=rpg.move_ability+1-stepx;}//最大只能到角色移動力y
// if(move_label_list->size()==0)
// {
for(int i=1;i<stepx+1;i++)
{
int st=i; //正x方向
QPixmap image1;
if(disx<0){st=-i;}//負x方向

QPoint sp(x+st*50,y);
image1=QPixmap(":/new/prefix1/icon/linev.png").scaled(50,50);//線
/////正x軸
if(i==stepx and stepy<2 and st > 0)
{
image1=QPixmap(":/new/prefix1/icon/liner.png").scaled(50,50);//右鍵頭
}
if(stepx!=rpg.move_ability){//若最大就不要顯示轉角
if(i==stepx and stepyy>=2 and st > 0)
{
image1=QPixmap(":/new/prefix1/icon/linerd.png").scaled(50,50);//右下轉彎
}
if(i==stepx and stepyy<=-1 and st > 0){
image1=QPixmap(":/new/prefix1/icon/lineru.png").scaled(50,50);//右上轉彎
}
}
/////負x軸
if(i==stepx and stepy<2 and st < 0){
image1=QPixmap(":/new/prefix1/icon/linel.png").scaled(50,50);//左箭頭
}
if(stepx!=rpg.move_ability){//若最大就不要顯示轉角
if(i==stepx and stepyy<=-1 and st < 0){
image1=QPixmap(":/new/prefix1/icon/linelu.png").scaled(50,50);//左上
}
if(i==stepx and stepyy>=2 and st < 0){
image1=QPixmap(":/new/prefix1/icon/lineld.png").scaled(50,50);//左下
}
}


QGame_move_line *a1=new QGame_move_line(sp,sp,1,500,"KEEPWHENDSTOP",image1,parentt);
move_label_list->append(a1);
if(i==stepx)rpg_move_point=sp;//RPG移動點

}

for(int j=1;j<stepy;j++)
{
int st=j;
QPixmap image1;
if(disy<0){st=-j;}//負y方向
QPoint sp(x+stepxx*50,y+st*50);
image1=QPixmap(":/new/prefix1/icon/lineh.png").scaled(50,50);
if(j==stepy-1 and stepyy>0){ image1=QPixmap(":/new/prefix1/icon/lined.png").scaled(50,50);}
if(j==stepy-1 and stepyy<0){ image1=QPixmap(":/new/prefix1/icon/lineu.png").scaled(50,50);}




QGame_move_line *a1=new QGame_move_line(sp,sp,1,500,"KEEPWHENDSTOP",image1,parentt);
move_label_list->append(a1);
if(j==stepy-1)rpg_move_point=sp;//RPG移動點
}

// }
}
//移動RPG到指定位置
void QGame_move_line_rpg::move_rpg_to_location()
{
this->move_obj->setEndValue(rpg_move_point);
this->move_obj->start();
}

void QGame_move_line_rpg::clear_rpg_move_label()
{
for(int i=0;i<move_label_list->size();i++)
{
move_label_list->at(i)->clear_move_obj();
move_label_list->removeAt(i);;
}
}

A.目前根據滑鼠所在位置,顯示移動路徑,並將改角色移動到箭頭位置
原理:加入滑鼠事件,計算當前到mose的x到移動物要放多少圖片(當然這是用QGame_move_line來產生),在計算y軸的將他全加到QList,每次mouse清聽到移動事件,先清空QList.

B.碰撞敵人產生攻擊選項
只要一偵測到碰撞敵人,就在上下左右個產生一個QLabel(當然這是用QGame_move_line來產生)

C.戰略遊戲,要有AI計算(從沒試過),這部份有些難,努力中,還不曉得能寫出來AI這部份

D.定義角色的能力,使用typedef struct簡單明瞭

typedef struct
{
int move_ability; //移動力
int life; //生命
int attack; //攻擊力
int defense; //防禦力
}rpg_ability;

rpg_ability rpg;
E.來看看使用這個框架,如何簡單就產生一隻會移動的角色,只要一行
//攻擊10,防禦8,生命20,移動力4
body=new QGame_move_line_rpg(10,8,20,4,pstart,pend,1,10,"KEEPWHENDSTOP",birdimg,this);

沒有留言: