MATLAB Answers

0

How to create a matrix "on top of" an image matrix?

Asked by Devin Das on 10 Nov 2018 at 8:28
Latest activity Commented on by Devin Das on 10 Nov 2018 at 22:21
For my engineering class, I have to create a video game using MATLAB, so I decided to recreate Pokemon RBY. I'm trying to create a "map" where the player can move his/her character through a room (a matrix) and find their way to the exit, which then places into the player into another room (another matrix). To do this, I want to use the original room image as the "map."
I've manually created a matrix based on image 1 below that looks like this (and is stored in a text file that's loaded into the program):
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 2 0
0 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 0
0 1 1 1 0 1 1 1 1 0
0 0 1 1 0 1 1 0 1 0
0 0 99 1 1 1 1 0 1 0
0 0 0 0 0 0 0 0 0 0
and I've written code to control the player's position using WASD like this:
player = 99;
house2f = load('pallettown_playerhouse_2f.txt');
house2f(9,3) = player;
house2fpic = imread('pallettown_playerhouse_2f_pic.png');
figure('WindowStyle','docked')
imshow(house2fpic)
r = 9;
c = 3;
disp(house2f)
while house2f(3,9) == 2
move = input('','s');
if move == 'w';
if house2f(r-1,c) == 0
sound(wallbump,Fs);
continue
elseif house2f(r-1,c) ~= 0
if house2f(r-1,c) == 1
house2f(r,c)= 1;
r=r-1;
house2f(r,c)= player;
house2f
elseif house2f(r-1,c) == 2
break
end
end
elseif move == 'a';
if house2f(r,c-1) == 0
sound(wallbump,Fs);
continue
elseif house2f(r,c-1) ~= 0
if house2f(r,c-1) == 1
house2f(r,c)= 1;
c=c-1;
house2f(r,c)= player;
house2f
elseif house2f(r,c-1) == 2
break
end
end
elseif move == 's';
if house2f(r+1,c) == 0
sound(wallbump,Fs);
continue
elseif house2f(r+1,c) ~= 0
if house2f(r+1,c) == 1
house2f(r,c)= 1;
r=r+1;
house2f(r,c)= player;
house2f
elseif house2f(r+1,c) == 2
break
end
end
elseif move == 'd';
if house2f(r,c+1) == 0
sound(wallbump,Fs);
continue
elseif house2f(r,c+1) ~= 0
if house2f(r,c+1) == 1
house2f(r,c)= 1;
c=c+1;
house2f(r,c)= player;
house2f
elseif house2f(r,c+1) == 2
break
end
end
end
end
Essentially, I have it made so that the player cannot move through the 0's (hence the 0 border around the matrix to prevent the player from creating more rows/columns in the matrix) and once the player reaches the 2 the matrix is closed. Here's the situation: I have an image that's 128 x 128 pixels (image 1) and I want to create a less-complex matrix from this image (example is show in image 2) that corresponds with the matrix I've written above to allow for the code I wrote earlier to work. I also want to be able to show the player's character movements on the image as they correspond to how the character moves across the matrix. How do I do these tasks?

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 10 Nov 2018 at 20:05

Devin - so you have a 10x10 matrix and a 128x128 image. If we assume that the walls are the borders/edges of your image (and so are not represented on the image as a space the character can move to) then the all we really care about is the 8x8 interior matrix (so the original one less its outer rows and columns). This means that every element of your matrix maps to a specific 16x16 pixel square in your image. And since you have the mapping, whenever the character moves to a free adjacent element in the matrix, you can then move the character to the 16x16 pixel square of the image (and "undo" it previous position).

  1 Comment

Right, that's exactly what I'm trying to do. How do I set the code so that when the character moves to an adjacent free element in the matrix the player icon shifts positions on the image? I have the code that allows the shifting of the character's value in the 8x8 matrix, but I don't know how to connect that so the character's icon image moves as well. In fact, I don't even know how to load the character's icon onto the image of the room (the image above is something I made in photoshop to explain the 8x8 matrix I wanted to create from the room).

Sign in to comment.