#! /usr/bin/env python3 # def mario ( ): #*****************************************************************************80 # ## mario() creates an image of Mario using colored squares. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 July 2022 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'mario():' ) print ( ' Draw a picture of Mario, using colored squares.' ) # # Shape of the grid. # 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) # plt.axis ( 'equal' ) plt.axis ( 'off' ) # # Use Cartesian coordinates to describe the boxes. # for y in range ( 0, m ): i = m - 1 - y for x in range ( 0, n ): j = x k = color_index[i,j] plt.fill ( [ x, x+1, x+1, x ], [ y, y, y+1, y+1 ], color = rgb[k] ) plt.plot ( [ x, x+1, x+1, x, x ], [ y, y, y+1, y+1, y ], 'k-' ) filename = 'mario.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( ) plt.close ( ) # # Terminate. # print ( '' ) print ( 'mario():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) mario ( ) timestamp ( )