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.</font>
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.
In [ ]:
from ipythonblocks import BlockGrid
grid = BlockGrid(8, 8, fill=(123, 234, 123))
grid
In [ ]:
grid[0, 0]
In [ ]:
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()
In [ ]:
for block in grid:
if block.row % 2 == 0 and block.col % 3 == 0:
block.red = 0
block.green = 0
block.blue = 0
grid
In [ ]:
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()
In [ ]:
from ipythonblocks import colors
colors
In [ ]:
grid[1, 1] = colors['Teal']
grid[1, 2] = colors['Thistle']
grid[1, 3] = colors['Peru']
grid.show()
In [ ]:
row = 3
for col in [0, 1, 2, 3, 4, 5, 6]:
grid[row, col] = colors['Chocolate']
grid.show()
In [ ]:
grid.width
In [ ]:
grid.height
In [ ]:
row = 5
for col in range(grid.width):
grid[row, col] = colors['Violet']
grid.show()
In [ ]:
for col in [4, 5, 6]:
for row in range(grid.height):
grid[row, col] = colors['Crimson']
grid.show()
In [ ]:
grid[5,5].show()
In [ ]:
sub_grid = grid[:, 3]
sub_grid.show()
In [ ]:
for block in sub_grid:
block.red = 255
sub_grid.show()
In [ ]:
for block in grid[2:6, 2:4]:
block.set_colors(245, 178, 34)
grid
In [ ]:
grid = BlockGrid(50, 50, block_size=5)
grid
In [ ]:
grid.block_size = 2
grid
In [ ]:
grid.lines_on = False
grid
In [ ]:
grid.lines_on = True
grid
In [ ]:
#Back to the original size, in black
grid = BlockGrid(8, 8)
grid
In [1]:
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
In [ ]:
one_color(grid, colors['LightGreen'])
grid.show()
In [ ]:
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)
In [ ]:
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()
In [ ]:
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()
In [ ]:
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)