{ "metadata": { "name": "", "signature": "sha256:c0231219037ab017e41c630a7f39a375e3c7e5a6814af81c19d654133168b18e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ " Fun with Blocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This lesson shows some of the basic elements of ipythonblocks, following the official [demo](http://nbviewer.ipython.org/github/jiffyclub/ipythonblocks/blob/master/demos/ipythonblocks_demo.ipynb)\n", "and [another take on it](http://nbviewer.ipython.org/github/catherinedevlin/mpwfw_exercises/blob/master/blocks.ipynb) with an assignment." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "**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. \n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do not copy/paste. \n", "\n", "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.\n", "\n", "Save your notebook when you are done, then try the accompanying exercises.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Creating a grid" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import BlockGrid\n", "grid = BlockGrid(8, 8, fill=(123, 234, 123))\n", "grid" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid[0, 0]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid[0, 0] = (0, 0, 0)\n", "grid[0, 2] = (255, 0, 0)\n", "grid[0, 4] = (255, 255, 255)\n", "grid[0, 6] = (0, 150, 150)\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for block in grid:\n", " if block.row % 2 == 0 and block.col % 3 == 0:\n", " block.red = 0\n", " block.green = 0\n", " block.blue = 0\n", "grid" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for r in range(grid.height):\n", " for c in range(grid.width):\n", " sq = grid[r, c]\n", " sq.red = 100\n", " \n", " if r % 2 == 0:\n", " sq.green = 15\n", " else:\n", " sq.green = 255\n", " \n", " if c % 2 == 0:\n", " sq.blue = 15\n", " else:\n", " sq.blue = 255\n", " \n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Color options" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import colors\n", "colors" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid[1, 1] = colors['Teal']\n", "grid[1, 2] = colors['Thistle']\n", "grid[1, 3] = colors['Peru']\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "row = 3\n", "for col in [0, 1, 2, 3, 4, 5, 6]:\n", " grid[row, col] = colors['Chocolate']\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid.width" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid.height" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "row = 5\n", "for col in range(grid.width):\n", " grid[row, col] = colors['Violet']\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for col in [4, 5, 6]:\n", " for row in range(grid.height):\n", " grid[row, col] = colors['Crimson']\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid[5,5].show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Slicing the grid" ] }, { "cell_type": "code", "collapsed": false, "input": [ "sub_grid = grid[:, 3]\n", "sub_grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for block in sub_grid:\n", " block.red = 255\n", "sub_grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for block in grid[2:6, 2:4]:\n", " block.set_colors(245, 178, 34)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(50, 50, block_size=5)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid.block_size = 2\n", "grid" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid.lines_on = False\n", "grid" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "grid.lines_on = True\n", "grid" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "#Back to the original size, in black\n", "grid = BlockGrid(8, 8)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def one_color(target_grid, color):\n", " \"\"\"Restore the grid to a single color\"\"\"\n", " for row in range(target_grid.height):\n", " for col in range(target_grid.width):\n", " grid[row, col] = color" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "one_color(grid, colors['LightGreen'])\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Playing with the display" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "from IPython.display import clear_output\n", "for color in [colors['Red'], colors['Green'], colors['Blue'], colors['White'], colors['Purple']]:\n", " #Have them do it both ways, with and without clear_output()\n", " clear_output()\n", " one_color(grid, color)\n", " grid.show()\n", " time.sleep(1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "one_color(grid, colors['Black'])\n", "for row in range(grid.height):\n", " for col in range(grid.width):\n", " if col % 2 == 0:\n", " grid[row, col] = colors['Red']\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "one_color(grid, colors['Black'])\n", "for row in range(grid.height):\n", " for col in range(grid.width):\n", " if (col + row) % 2 == 0:\n", " grid[row, col] = colors['Red']\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "base_color = [50, 50, 50]\n", "for i in range(200):\n", " clear_output()\n", " for row in range(grid.height):\n", " for col in range(grid.width):\n", " grid[row, col] = (base_color[0], base_color[1]+row*20, base_color[2]+col*20)\n", " grid.show()\n", " base_color[0] += 1\n", " base_color[1] += 1\n", " base_color[2] += 1\n", " time.sleep(0.02)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " All content is under a modified MIT License, and can be freely used and adapted. See the full license text [here](../../LICENSE)." ] } ], "metadata": {} } ] }