2017-12-02 2 views
0

現在、MATLABでPACMANを作成中ですが、メイン図のマップの生成を開始する方法がわかりません。クラスuint8 RGBの背景を持つ.pngファイルを使用することもできますが、この場合はPACMANとゴーストのパスを妨げる壁を登録することはできません。私が考えているもう一つの方法は、黒い空のピクセル、青い壁(塗りつぶした)、および点(黄色)の位置を表す0、1、および2のマップをそれぞれ作成することです。しかし、後者の方法を試してみると、switch-case-otherwiseメソッドで300 x 300マトリックスの各特定のインデックスに色を割り当てる問題があります。どのように続行するかについての任意の提案?任意の応答が大幅に感謝し、以下の私がこれまでに作成しようとしているサンプルコードで次のようになります。PACMANを作成するMATLABのバックグラウンドマップ

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

関連する問題