#! /usr/bin/env python3 # def mario_fill ( ): #*****************************************************************************80 # ## mario_fill() creates an image of Mario using colored squares. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 October 2026 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'mario_fill():' ) print ( ' numpy version: ' + np.version.version ) print ( ' python version: ' + platform.python_version ( ) ) print ( ' Create a plot of Mario, using colored squares.' ) m = 16 n = 13 # # RGB color values are # 'white', 'black', 'red', 'blue', 'yellow', 'bisque', 'brown' # rgb = np.array ( [ \ [ 1.0, 1.0, 1.0 ], \ [ 0.0, 0.0, 0.0 ], \ [ 1.0, 0.0, 0.0 ], \ [ 0.0, 0.0, 1.0 ], \ [ 1.0, 1.0, 0.0 ], \ [ 1.0, 0.8, 0.6 ], \ [ 0.8, 0.4, 0.0 ] ] ) # # For each cell of the grid, list a color index in the RGB array. # color_index = np.array ( [ \ [ 0, 0, 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0 ], \ [ 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0 ], \ [ 0, 0, 6, 6, 6, 5, 5, 5, 1, 5, 0, 0, 0 ], \ [ 0, 6, 5, 6, 5, 5, 5, 5, 1, 5, 5, 5, 0 ], \ [ 0, 6, 5, 6, 6, 5, 5, 5, 5, 1, 5, 5, 5 ], \ [ 0, 6, 6, 5, 5, 5, 5, 5, 1, 1, 1, 1, 0 ], \ [ 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0 ], \ [ 0, 0, 2, 2, 3, 2, 2, 2, 2, 0, 0, 0, 0 ], \ [ 0, 2, 2, 2, 3, 2, 2, 3, 2, 2, 2, 0, 0 ], \ [ 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 0 ], \ [ 5, 5, 2, 3, 4, 3, 3, 4, 3, 2, 5, 5, 0 ], \ [ 5, 5, 5, 3, 3, 3, 3, 3, 3, 5, 5, 5, 0 ], \ [ 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 0 ], \ [ 0, 0, 3, 3, 3, 0, 0, 3, 3, 3, 0, 0, 0 ], \ [ 0, 6, 6, 6, 0, 0, 0, 0, 6, 6, 6, 0, 0 ], \ [ 6, 6, 6, 6, 0, 0, 0, 0, 6, 6, 6, 6, 0 ] ] ) # # The "(i,j)" box is surrounded by matrix indices where # i represents a row (0 = top, m = bottom ), # j represents a column (0=left, n = right) # # In (x,y) coordinates, # x represents the column, 0 = left, n = right # y represents the row, 0 = bottom, m = top) # # So (i,j) ==> (x,y) where x = j and y = m - 1 - i # (x,y) ==> (i,j) where j = x and i = m - 1 - y # # (i,j) <---------- (i,j+1) (x,y+1) <------ (x+1,y+1) # | ^ | ^ # | | | | # | Color K | | Color K | # | | | | # V | V | # (i+1,j) --------> (i+1,j+1) (x,y) --------> (x+1,y) # command_filename = 'mario_commands.txt' command_unit = open ( command_filename, 'wt' ) command_unit.write ( '# ' + command_filename + '\n' ) command_unit.write ( '#\n' ) command_unit.write ( '# Usage:\n' ) command_unit.write ( '# gnuplot < ' + command_filename + '\n' ) command_unit.write ( '#\n' ) command_unit.write ( 'set term png\n' ) command_unit.write ( 'set output "mario_fill.png"\n' ) command_unit.write ( 'set title "MARIO"\n' ) command_unit.write ( 'set timestamp\n' ) command_unit.write ( 'set xrange [ -1.5 : 14.5 ]\n' ) command_unit.write ( 'set yrange [ 0 : 16 ]\n' ) command_unit.write ( 'set size square\n' ) command_unit.write ( 'unset border\n' ) command_unit.write ( 'unset tics\n' ) command_unit.write ( 'set nokey\n' ) k = 0 for i in range ( 0, m ): for j in range ( 0, n ): k = k + 1 if ( color_index[i,j] == 0 ): color = "white" elif ( color_index[i,j] == 1 ): color = "black" elif ( color_index[i,j] == 2 ): color = "red" elif ( color_index[i,j] == 3 ): color = "blue" elif ( color_index[i,j] == 4 ): color = "yellow" elif ( color_index[i,j] == 5 ): color = "#FFEBCD" elif ( color_index[i,j] == 6 ): color = "brown" i_reverse = m - 1 - i command_unit.write ( \ 'set object %d rect from %d, %d to %d, %d back\n' \ % ( k, j, i_reverse, j + 1, i_reverse + 1 ) ) command_unit.write ( \ 'set object %d rect fc rgb "%s" fillstyle solid 1.0\n' % ( k, color ) ) # # If you don't have some bogus PLOT command here, all the previous work # results in no plot all. Way to go, gnuplot! # Here, we plot the function y = -1, which is out of range and won't show up. # command_unit.write ( 'plot -1 with lines\n' ) command_unit.write ( 'quit\n' ) command_unit.close ( ) print ( ' Created command file "' + command_filename + '"' ) # # Terminate. # print ( '' ) print ( 'mario_fill():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) mario_fill ( ) timestamp ( )