I made a minesweeper game and I need to know how to get an image of a bomb to display on the grid.

6 views (last 30 days)
I made a game where horizontal and vertical lines are drawn to make a grid. This acts as the playing field for the game and when clicked on a bomb the square of that area becomes red. Now I want to add an extra feature where an image of a bomb is displayed in the square that contains a bomb when it is clicked on. I'm a beginner at MATLAB and so don't really know where to begin with this. I tried looking up ways to get an image to display but it closed my grid and opened a separate window with the picture of the bomb in it.

Answers (3)

Image Analyst
Image Analyst on 15 Nov 2015
Another way is to study the way that the 7 mine sweeper File Exchange Submissions do it: http://www.mathworks.com/matlabcentral/fileexchange/?search_submit=fileexchange&query=mine&term=mine

Image Analyst
Image Analyst on 14 Nov 2015
Using GUIDE make a grid of axes. Fill them with gray uniform arrays using imshow(). Set up a callback for the image so that when the user clicks on it, you again call imshow() to display the actual item that is supposed to lie at that location.

Geoff Hayes
Geoff Hayes on 15 Nov 2015
Ali - an alternative may be to create a single image (mxnx3) and manipulate the data within it whenever the user "presses" on a certain position within the single axes of that image. For example, suppose that we have a function that generates a single blank tile of a fixed dimension (i.e. vxv pixels) as
function [tile] = createEmptyTile()
Now this function could either create a tile image (vxv3) or it could load the image from file (say a jpg of similar dimension vxvx3). We can replicate this image to create a grid of tiles as
tile = createEmptyTile();
playingArea = repmat(tile,numTilesY,numTilesX);
where numTilesY and numTileX indicates the number of tiles that we want to replicate in the y (rows) and x (columns) dimensions respectively. So now we have our mxnx3 image of tiles that we can "draw" using image as
hImage = image(playingArea);
set(gcf,'WindowButtonDownFcn',@tilePressCallback);
axis off;
axis equal;
We have assigned to the current figure (whose handle is obtained using gcf) a button down function callback which, when called, will colour the tile a certain colour (or it could load an image like your bomb jpg if it is of the same dimension as the tile that you want to replace). For example,
function tilePressCallback(~, ~)
currentPos = get(gca,'CurrentPoint');
tileX = ceil(currentPos(1,1)/numPixelsX)
tileY = ceil(currentPos(1,2)/numPixelsY);
y = (tileY-1)*numPixelsY + 1;
x = (tileX-1)*numPixelsX + 1;
playingArea(y:(y+numPixelsY-1),x:(x+numPixelsX-1),:) = colourTileRed();
set(hImage,'CData',playingArea);
end
In the above, we get the current axes position (the (x,y) coordinates of the mouse button down event) and determine which tile was pressed, tileX and tileY indicate this. We then calculate the x and y top left pixel of the tile to replace with the image returned from colourTileRed which we do so, and then update the image with the new playingArea.
See attached for an example of the above code.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!