2012-12-06 91 views
2

好吧,現在我正嘗試通過Matlab編碼創建連接四個遊戲;現在遊戲仍然是嬰兒,但我的問題是我無法在每個網格廣場上繪製圖形,或者我無法獲得「圓形」圖形進行繪製。請以任何可能的方式提供幫助。如果有人知道任何連接四個matlab教程,它將不勝感激。連接四個matlab

function [] = Kinect4(nrRows, nrCols) 
    board = zeros(nrRows, nrCols); 

    nrMoves = 0; 

     set(gca, 'xlim', [0 nrCols]); 
     set(gca, 'ylim', [0 nrRows]); 

     for r = 1 : 1 : nrRows - 1 
     line([0, nrCols], [r, r], ... 
     'LineWidth', 4, 'Color', [0 0 1]); 
     end 

     for c = 1 : 1 : nrCols - 1 
     line([c, c], [0, nrRows], ... 
     'LineWidth', 4, 'Color', [0 0 1]); 
     end 

    DrawBoard(nrRows, nrCols) 
    hold on; 

    while nrMoves < nrRows * nrCols     %Computes ability to move polygon 
     [x, y] = ginput(1); 
     r = ceil(y); % convert to row index 
     c = ceil(x); % convert to column index 

angles = 0 : 1 : 360; 
     x = cx + r .* cosd(angles); 
     y = cy + r .* sind(angles); 

     plot(x, y, 'Color', [1 1 1], 'LineWidth', 3); 
     axis square; 
    end 
    end 

回答

2

下面是對代碼的一些修復。

  1. 刪除該行DrawBoard(nrRows, nrCols)。不確定是否將它作爲評論放在那裏,因爲您已經繪製了該板或者如果DrawBoard是一個單獨的功能。
  2. 更改了rc的計算結果,以使您所在單元格的中心處於插入狀態。這是通過從每個中減去0.5來完成的。
  3. 將行x = cx + r .* cosd(angles);更改爲x = c + 0.5*cosd(angles);。在前面的變量中,變量cx未定義,而代替r作爲掛鉤的半徑,我使用0.5,您可以用適當的變量替換它。但是我們的想法是繪製一個半徑爲0.5的圓(以便它適合一個單元格),中心沿着x軸偏移cy的類似變化可抵消沿y軸的掛鉤。
  4. plot命令的顏色更改爲[0 0 0],該顏色爲黑色。 [1 1 1]是白色的,不可能在白色背景上看到:)。我建議使用黑色的'k',藍色的'b',等等。查看基本顏色規範的matlab文檔。
  5. 我猜你還沒有實現重力,因此掛釘下移。你也需要檢查一個單元格是否已經填滿。所有這些和其他改進(如刪除不必要的for循環,更好的方法來繪製掛鉤,等等)只要你有一個工作代碼就留下。

這裏的 「工作」 代碼:

function [] = Kinect4(nrRows, nrCols) 
    board = zeros(nrRows, nrCols); 

    nrMoves = 0; 

     set(gca, 'xlim', [0 nrCols]); 
     set(gca, 'ylim', [0 nrRows]); 

     for r = 1 : 1 : nrRows - 1 
      line([0, nrCols], [r, r], ... 
      'LineWidth', 4, 'Color', [0 0 1]); 
     end 

     for c = 1 : 1 : nrCols - 1 
      line([c, c], [0, nrRows], ... 
      'LineWidth', 4, 'Color', [0 0 1]); 
     end 

     axis square; 
     hold on; 

    while nrMoves < nrRows * nrCols  %Computes ability to move polygon 
     [x, y] = ginput(1); 
     r = ceil(y) - 0.5; 
     c = ceil(x) - 0.5; 

     angles = 0 : 1 : 360; 
     x = c + 0.5*cosd(angles); 
     y = r + 0.5*sind(angles); 

     plot(x, y, 'Color', [0 0 0], 'LineWidth', 3); 
    end 
end 
+0

非常感謝你的幫助。我的下一個問題是,你將如何實現引力並檢查單元格是否已經被填充?我不需要確切的編碼,但如何去做的方向將非常感激。 – FunnyBro777

+0

@ FunnyBro777有很多方法可以做到這一點。一種可視化的方式是使用「board」martix來跟蹤已填充的單元格。所以如果一個單元被佔用,相應的元素將不爲零。當玩家進入下一步棋子時,'ginput'只關心列('x')的位置,並替換該棋盤矩陣列中最後一個零元素。您可以將其翻轉爲1或2以跟蹤哪個球員已經進行了移動。 – mythealias

+0

另一種選擇是使用一個行向量,並且每次玩家移動時只增加一個相應的列。您不需要立即吸引玩家的動作,因爲無法跟蹤所做的動作。 – mythealias