{ "cells": [ { "cell_type": "markdown", "id": "efb95878-ff4a-4500-8b90-333ff5441752", "metadata": {}, "source": [ "# Module Mode py5 Examples" ] }, { "cell_type": "code", "execution_count": 1, "id": "0fd64185-8d40-4211-8681-fc7d1779b660", "metadata": {}, "outputs": [], "source": [ "import py5" ] }, { "cell_type": "markdown", "id": "f9b0f755-5c6a-4cf0-b3c0-d0ce73977d8a", "metadata": {}, "source": [ "## setup\n", "\n", "First, define a `setup()` function to create the drawing surface. This will be called once when the Sketch starts." ] }, { "cell_type": "code", "execution_count": 2, "id": "ee80b347-1a63-4db5-a735-50cf11788f2a", "metadata": {}, "outputs": [], "source": [ "def setup():\n", " py5.size(600, 400)\n", " py5.background(128)" ] }, { "cell_type": "markdown", "id": "12d373dc-e299-4362-af63-cac922aa870b", "metadata": {}, "source": [ "## draw\n", "\n", "Second, define a `draw()` function. This will be called repeatedly to create the Sketch animation." ] }, { "cell_type": "code", "execution_count": 3, "id": "5e1f985c-3f79-4d86-81a7-250ea13949e5", "metadata": {}, "outputs": [], "source": [ "def draw():\n", " py5.rect(py5.random(py5.width), py5.random(py5.height), 10, 10)" ] }, { "cell_type": "markdown", "id": "c3c177d6-20b7-40ee-b975-b13010ab7120", "metadata": {}, "source": [ "## Run the Sketch!" ] }, { "cell_type": "code", "execution_count": 4, "id": "2b87daf0-45f9-4774-8172-4af4a02f99f5", "metadata": {}, "outputs": [], "source": [ "py5.run_sketch()" ] }, { "cell_type": "markdown", "id": "86ff2e48-92f2-4e0c-83de-84b7aa8c8ce4", "metadata": {}, "source": [ "## new draw function\n", "\n", "This new `draw()` function will replace the `draw()` function defined earlier." ] }, { "cell_type": "code", "execution_count": 5, "id": "205b40d1-d9cf-43bc-be46-29d8b9f7baf1", "metadata": {}, "outputs": [], "source": [ "def draw():\n", " py5.rect(py5.mouse_x, py5.mouse_y, 10, 10)" ] }, { "cell_type": "markdown", "id": "adc3cfb1-c7d3-4190-8b64-80d07fc37b51", "metadata": {}, "source": [ "## Run the Sketch!\n", "\n", "The second Sketch will use the new `draw()` function and the `settings()` and `setup()` functions from before." ] }, { "cell_type": "code", "execution_count": 6, "id": "32802698-a3fd-4952-9303-6d24d9467fd0", "metadata": {}, "outputs": [], "source": [ "py5.run_sketch()" ] }, { "cell_type": "code", "execution_count": null, "id": "3eb84b01-e1a4-44fe-8d9f-a458da4ba25d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }