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

: 이제 플롯에서 사물 및 제거 물건을 이동하는 것은 또 다른 문제 ...

일부 링크입니다