This lesson shows some of the basic elements of ipythonblocks, following the official demo and another take on it with an assignment.
Instructions: Create a new directory called FunWithBlocks
with a notebook called FunWithBlocksTour
. Give it a heading 1 cell title Fun with Blocks. Read this page, typing in the code in the code cells and executing them as you go.
Do not copy/paste.
Type the commands yourself to get the practice doing it. This will also slow you down so you can think about the commands and what they are doing as you type them.
Save your notebook when you are done, then try the accompanying exercises.
Note: This was done as an instructor guided tour with students typing along with me as I explained things, so it contains no background or narrative text.
from ipythonblocks import BlockGrid
grid = BlockGrid(8, 8, fill=(123, 234, 123))
grid
grid[0, 0]
grid[0, 0] = (0, 0, 0)
grid[0, 2] = (255, 0, 0)
grid[0, 4] = (255, 255, 255)
grid[0, 6] = (0, 150, 150)
grid.show()
for block in grid:
if block.row % 2 == 0 and block.col % 3 == 0:
block.red = 0
block.green = 0
block.blue = 0
grid
for r in range(grid.height):
for c in range(grid.width):
sq = grid[r, c]
sq.red = 100
if r % 2 == 0:
sq.green = 15
else:
sq.green = 255
if c % 2 == 0:
sq.blue = 15
else:
sq.blue = 255
grid.show()
from ipythonblocks import colors
colors
grid[1, 1] = colors['Teal']
grid[1, 2] = colors['Thistle']
grid[1, 3] = colors['Peru']
grid.show()
row = 3
for col in [0, 1, 2, 3, 4, 5, 6]:
grid[row, col] = colors['Chocolate']
grid.show()
grid.width
grid.height
row = 5
for col in range(grid.width):
grid[row, col] = colors['Violet']
grid.show()
for col in [4, 5, 6]:
for row in range(grid.height):
grid[row, col] = colors['Crimson']
grid.show()
grid[5,5].show()
sub_grid = grid[:, 3]
sub_grid.show()
for block in sub_grid:
block.red = 255
sub_grid.show()
for block in grid[2:6, 2:4]:
block.set_colors(245, 178, 34)
grid
grid = BlockGrid(50, 50, block_size=5)
grid
grid.block_size = 2
grid
grid.lines_on = False
grid
grid.lines_on = True
grid
#Back to the original size, in black
grid = BlockGrid(8, 8)
grid
def one_color(target_grid, color):
"""Restore the grid to a single color"""
for row in range(target_grid.height):
for col in range(target_grid.width):
grid[row, col] = color
one_color(grid, colors['LightGreen'])
grid.show()
import time
from IPython.display import clear_output
for color in [colors['Red'], colors['Green'], colors['Blue'], colors['White'], colors['Purple']]:
#Have them do it both ways, with and without clear_output()
clear_output()
one_color(grid, color)
grid.show()
time.sleep(1)
one_color(grid, colors['Black'])
for row in range(grid.height):
for col in range(grid.width):
if col % 2 == 0:
grid[row, col] = colors['Red']
grid.show()
one_color(grid, colors['Black'])
for row in range(grid.height):
for col in range(grid.width):
if (col + row) % 2 == 0:
grid[row, col] = colors['Red']
grid.show()
base_color = [50, 50, 50]
for i in range(200):
clear_output()
for row in range(grid.height):
for col in range(grid.width):
grid[row, col] = (base_color[0], base_color[1]+row*20, base_color[2]+col*20)
grid.show()
base_color[0] += 1
base_color[1] += 1
base_color[2] += 1
time.sleep(0.02)
All content is under a modified MIT License, and can be freely used and adapted. See the full license text here.