Code covered by the BSD License  

Highlights from
Draw 8-bit Mario

image thumbnail

Draw 8-bit Mario

by

 

15 Feb 2013 (Updated )

Simple script to draw an 8-bit Mario on a figure using patches.

mario( min_x, max_x, min_y, max_y )
function [  ] = mario( min_x, max_x, min_y, max_y )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MARIO This will draw an 8-bit version of mario in 12 x 16 resolution on the current
% figure.  If no figure exists, will create a figure.
% 
% Inputs: min_x, max_x, min_y, max_y
% These coordinates specify the start and end coordinates to draw mario.  Divides the vertical space into 
% 16 chunks to determine the pixel size
%
% mario array - a 16 x 12 array containing pixel colors as a function of pixel location
% 1 = bg, 2 = hat, 3 = hair, 4 = skin, 5 = eyes, 6 = mustache, 7 = undershirt, 8 = overalls, 9 = buttons, 0 = shoes
% we end up making minor transfomations to this matrix to make it useable, but it is entered in this way bc it's the 
% easiest way to do it (and it's fun to try to visualize mario just from the numbers)
color_by_numbers_mario = [ 1 1 1 2 2 2 2 2 1 1 1 1; ...
						   1 1 2 2 2 2 2 2 2 2 2 1; ...
						   1 1 3 3 3 4 4 5 4 1 1 1; ...
						   1 3 4 3 4 4 4 5 4 4 4 1; ...
						   1 3 4 3 3 4 4 4 6 4 4 4; ...
						   1 3 3 4 4 4 4 6 6 6 6 1; ...
						   1 1 1 4 4 4 4 4 4 4 1 1; ...
						   1 1 7 7 8 7 7 7 1 1 1 1; ...
						   1 7 7 7 8 7 7 8 7 7 7 1; ...
						   7 7 7 7 8 8 8 8 7 7 7 7; ...
						   4 4 7 8 9 8 8 9 8 7 4 4; ...
						   4 4 4 8 8 8 8 8 8 4 4 4; ...
						   4 4 8 8 8 8 8 8 8 8 4 4; ...
						   1 1 8 8 8 1 1 8 8 8 1 1; ...
						   1 0 0 0 1 1 1 1 0 0 0 1; ...
						   0 0 0 0 1 1 1 1 0 0 0 0];

% required transformations to mario array
% flip up and down because our numbering has the top left corner as (1,1) but matlab plotting uses (1,16) as top left corner
color_by_numbers_mario = flipud(color_by_numbers_mario);
% replace 0 values with 10 so we can index into our color arrays, simply used 0 so that the alignment doesn't get screwed up
% b/c 10 has 2 digits and 0 only has 1.
color_by_numbers_mario( color_by_numbers_mario == 0) = 10;

% NES mario colors - change these colors here if you want a different style of mario
% 1 = bg, 2 = hat, 3 = hair, 4 = skin, 5 = eyes, 6 = mustache, 7 = undershirt, 8 = overalls, 9 = buttons, 10 = shoes
colors = {[1 1 1 ] [1 0 0 ] [0.64453 0.16406 0.16406 ] [0.95417 0.64062 0.375 ] [0.64453 0.16406 0.16406 ] [0.64453 0.16406 0.16406 ] [0.64453 0.16406 0.16406 ] [1 0 0 ] [1 0.83984 0 ] [0.64453 0.16406 0.16406 ] };
% Above I've explicitly included the colors for mario (so that you don't have to download an extra matlab file exchange script),
% but for my own use I end up using the rgb Matlab file exchange function and below are the colors I would use for that.
% I highly recommend using that file which can be downloaded from here:
% http://www.mathworks.com/matlabcentral/fileexchange/24497-rgb-triple-of-color-name-version-2
% colors = {rgb('White'),rgb('Red'),rgb('Brown'),rgb('SandyBrown'),rgb('Brown'),rgb('Brown'),rgb('Brown'),rgb('Red'),rgb('Gold'),rgb('Brown')};

gcf;

% calculate "pixel" size
pxl_size = (max_y - min_y) / 16;

% just a friendly check/warning message in case the user specified x/y values that don't make sense.
if 12 * pxl_size + min_x > max_x
	display('Warning: Given the pxl_size value, as calculated by pxl_size = max_y - min_y / 16, it seems that the width of the drawing, 12 * pxl_size, exceeds the value specified by x_max - x_min. We''re gunna draw Mario anyway, but if things get funky that''s a *you* problem, not a me problem');
end

for x = 1 : 12 
	x1 = (x-1) * pxl_size + min_x;
	x2 = (x) * pxl_size + min_x;
	for y = 1 : 16
		y1 = (y-1) * pxl_size + min_y;
		y2 = (y ) * pxl_size + min_y;
		% draw a pixel and fill it in with the color as specified by our color_by_numbers_mario array
		patch([x1 x1 x2 x2], [y1 y2 y2 y1], colors{color_by_numbers_mario(y,x) },'EdgeColor','none');
	end
end

end

Contact us