請教座標問題 |
答題得分者是:richtop
|
cashyy
高階會員 ![]() ![]() ![]() ![]() 發表:117 回覆:322 積分:212 註冊:2004-04-30 發送簡訊給我 |
int Dn; if(Maze_Map[My][Mx-1]==4) /* 代表左邊有路 */ { if(Status != 2) { SRect.Right = 34; SRect.Bottom = 34; DRect.top = (My/2)*37 3; DRect.left = (Mx/2)*37 3; DRect.right = DRect.left 34; DRect.Bottom = DRect.top 34; ImageBack->Canvas->CopyRect(DRect,ImageLeft->Canvas,SRect); Status = 2; /* 2表示已經往左 */ } else { /* 覆蓋坦克原座標圖 */ ImageBack->Canvas->CopyRect(DRect,ImageTrack->Canvas,SRect); /* 顯示新坦克座標圖 */ Mx -=2; SRect.Right = 34; SRect.Bottom = 34; DRect.top = (My/2)*37 3; DRect.left = (Mx/2)*37 3; DRect.right = DRect.left 34; DRect.Bottom = DRect.top 34; ImageBack->Canvas->CopyRect(DRect,ImageLeft->Canvas,SRect); My = My; } } else { if(Maze_Map[My-1][Mx]==5) /* 代表上方有路 */ { if(Status != 0) { SRect.Right = 34; SRect.Bottom = 34; DRect.top = (My/2)*37 3; DRect.left = (Mx/2)*37 3; DRect.right = DRect.left 34; DRect.Bottom = DRect.top 34; ImageBack->Canvas->CopyRect(DRect,ImageUp->Canvas,SRect); Status = 0; /* 0表示已經往上 */ } else { /* 覆蓋坦克原座標圖 */ ImageBack->Canvas->CopyRect(DRect,ImageTrack->Canvas,SRect); /* 顯示新坦克座標圖 */ My -=2; SRect.Right = 34; SRect.Bottom = 34; DRect.top = (My/2)*37 3; DRect.left = (Mx/2)*37 3; DRect.right = DRect.left 34; DRect.Bottom = DRect.top 34; ImageBack->Canvas->CopyRect(DRect,ImageUp->Canvas,SRect); My = My; } } else { if(Maze_Map[My][Mx 1]==4) /* 代表右方有路 */ { if(Status != 3) { SRect.Right = 34; SRect.Bottom = 34; DRect.top = (My/2)*37 3; DRect.left = (Mx/2)*37 3; DRect.right = DRect.left 34; DRect.Bottom = DRect.top 34; ImageBack->Canvas->CopyRect(DRect,ImageRight->Canvas,SRect); Status = 3; /* 3表示已經往右 */ } else { /* 覆蓋坦克原座標圖 */ ImageBack->Canvas->CopyRect(DRect,ImageTrack->Canvas,SRect); /* 顯示新坦克座標圖 */ Mx =2; SRect.Right = 34; SRect.Bottom = 34; DRect.top = (My/2)*37 3; DRect.left = (Mx/2)*37 3; DRect.right = DRect.left 34; DRect.Bottom = DRect.top 34; ImageBack->Canvas->CopyRect(DRect,ImageRight->Canvas,SRect); My = My; } } else { if(Maze_Map[My 1][Mx]==5) /* 代表下方有路 */ { if(Status != 1) { SRect.Right = 34; SRect.Bottom = 34; DRect.top = (My/2)*37 3; DRect.left = (Mx/2)*37 3; DRect.right = DRect.left 34; DRect.Bottom = DRect.top 34; ImageBack->Canvas->CopyRect(DRect,ImageDown->Canvas,SRect); Status = 1; /* 1表示已經往下 */ } else { /* 覆蓋坦克原座標圖 */ ImageBack->Canvas->CopyRect(DRect,ImageTrack->Canvas,SRect); /* 顯示新坦克座標圖 */ My =2; SRect.Right = 34; SRect.Bottom = 34; DRect.top = (My/2)*37 3; DRect.left = (Mx/2)*37 3; DRect.right = DRect.left 34; DRect.Bottom = DRect.top 34; ImageBack->Canvas->CopyRect(DRect,ImageDown->Canvas,SRect); My = My; } } } } } /* ====================================== 前 = (x,y-1) = 左 右 = (x-1,y) 目前(x,y) (x 1,y) = = (x,y 1) = 後 = ====================================== */ 請問各位大大,我的問題是:前方為(x,y-1),那麼如果我右轉後,我的前方應為(x 1,y),那程式該修改那裡? |
richtop
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
cashyy 您好: 底下以四個方向為例,分析轉向與目前方向的關係,請參考。
如果要設成八個方向,相信也不難修改。
//--------------------------------------------------------------------------- // <1> // ( 0,-1) // ^ // | // <2> (-1, 0) <-- --> ( 1, 0) <0> // | // v // ( 0, 1) // <3> int dx[] = { 1, 0,-1, 0}; int dy[] = { 0,-1, 0, 1}; // To turn right, direction adds -1; // To turn left, direction adds 1; // To keep forward, nothing needs be changed; // To go back, direction adds 2 or -2; int direction=0, // 目前方向 dirX=dx[direction], // 沿目前方向的x增量 dirY=dy[direction]; // 沿目前方向的y增量 int nextDir(int dir, int turnTo) { dir = turnTo; if ( turnTo<0 ) dir = 4; return ( dir % 4); } void __fastcall TForm1::Button1Click(TObject *Sender) { // to right, direction = nextDir(direction, -1); dirX = dx[direction]; dirY = dx[direction]; x = dirX; y = dirY; // to left, direction = nextDir(direction, 1); dirX = dx[direction]; dirY = dx[direction]; x = dirX; y = dirY; // keep forward, //direction = nextDir(direction, 0); //dirX = dx[direction]; //dirY = dx[direction]; x = dirX; y = dirY; // go back, direction = nextDir(direction, 2); dirX = dx[direction]; dirY = dx[direction]; x = dirX; y = dirY; } //---------------------------------------------------------------------------RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====##### |
ddy
站務副站長 ![]() ![]() ![]() ![]() ![]() ![]() 發表:262 回覆:2105 積分:1169 註冊:2002-07-13 發送簡訊給我 |
|
cashyy
高階會員 ![]() ![]() ![]() ![]() 發表:117 回覆:322 積分:212 註冊:2004-04-30 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |