2017-12-02 51 views
0

所以我目前正在製作PACMAN上的MATLAB程序,但似乎無法弄清楚如何開始在主圖上生成地圖。我可以使用背景爲uint8 RGB的.png文件,但這種情況不允許我註冊妨礙PACMAN和鬼魂路徑的牆壁。我認爲另一種方法是使用0,1和2分別代表黑色空像素,藍色牆(填充)和點(黃色)的位置創建地圖。但是,在嘗試執行後一種方法時,我遇到了一個問題,即在switch-case-other方法中爲300 x 300矩陣的每個特定索引分配顏色。有關如何繼續的建議?任何反應將是不勝感激及以下的是,我已嘗試到目前爲止創建示例代碼:在MATLAB上創建PACMAN背景圖

function level = LevelOne() 
% the functionality of this function is to generate the walls that 
% PACMAN and the ghosts cannot cross 

% Create color map 
color = [255 75 75; % red 1 
153 0 0; % dark red 2 
255 255 153; % light yellow 3 
255 102 178; % pink 4 
0 0 0; % black 5 
0 255 255; % light blue 6 
0 0 255; % blue 7 
255 255 153; % light yellow 8 
192 192 192; % silver 9 
255 255 255]/255; % white 10 

%create general map area 
level = zeros(300); 


%create location of filled in walls(represented by 1's) 
level(18:38,37:70) = 1; 
level(65:75,37:70) = 1; 
level(300-18:300-38,300-37:300-70) = 1; 
level(300-65:300-75,300-37:300-70) = 1; 


[x,y] = size(level); 

axis([0 x 0 y]) 
for ii = 1:x 
    for jj = 1:y 
     switch level(ii,jj) 
      case 1 %represents a blue wall 

      case 0 %represents an empty black space for PACMAN & Ghosts to move through 

      case 2 %represents the location of the fruit 

      case 3 %represents the location 

      otherwise 

     end 
    end 

回答

0

如果我理解問題正確,可以加載從圖像矩陣數據(其可以容易地得出在一個照片編輯應用程序)到一個新的矩陣,獲得兩全其美。 事情是這樣的:

image = imread('map.png'); 
grayLevel = image(row, column); ' Get the pixel like that if it is grayscale image. 
rgbColor = impixel(image, column, row); ' Get the pixel like that if it is colorful image. 

循環雖然圖像數據,並將其複製到矩陣(也許轉換顏色到您的0/1/2/3值的同時)是下一個步驟。

我沒有測試過這在所有的,但這裏是我的嘗試:

level = zeros(300); 
[x,y] = size(level); 

% Copy from image. 
for ii = 1:x 
    for jj = 1:y 
     level(ii,jj) = image(ii, jj); % Here maybe convert blue to 1, etc yourself. I only copy data here. 
    end 
end 

% Render it. 
axis([0 x 0 y]) 
for ii = 1:x 
    for jj = 1:y 
     switch level(ii,jj) 
      case 1 %represents a blue wall 
       rectangle('Position',[ii, jj, 1, 1],'FaceColor',[0 0 .5],'EdgeColor','b', 'LineWidth', 1) 
      case 0 %represents an empty black space for PACMAN & Ghosts to move through 
       rectangle('Position',[ii, jj, 1, 1],'FaceColor',[0 0 0],'EdgeColor','b', 'LineWidth', 1) 

      case 2 %represents the location of the fruit 

      case 3 %represents the location 

      otherwise 

     end 
    end 
end 

,這應該繪製矩形,如果這是你的要求:

rectangle('Position',[1,2,5,10],'FaceColor',[0 0 .5],'EdgeColor','b', 'LineWidth', 1) 

一般情況下,嘗試谷歌搜索,因爲這會給你更多的信息。 現在,從情節感動的事情和刪除的東西是另一個問題...

一些鏈接:

https://www.mathworks.com/help/matlab/ref/rectangle.html https://www.mathworks.com/matlabcentral/answers/151779-how-to-extract-the-value-pixel-valu-from-image