2017-12-02 45 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