Skip to content

Instantly share code, notes, and snippets.

@tjvr
Created May 10, 2015 00:52
Show Gist options
  • Save tjvr/0cccd5f8d97770f784bf to your computer and use it in GitHub Desktop.
Save tjvr/0cccd5f8d97770f784bf to your computer and use it in GitHub Desktop.
kurt-define-block.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import kurt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"p = kurt.Project()\n",
"cat = kurt.Sprite(p, \"cat\")\n",
"p.sprites.append(cat)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"spec = \"circle radius %n around %m.sprite color %c\"\n",
"parameters = [\"x\", \"sprite\", \"color\"]\n",
"defaults = [12, \"Sprite1\", 0]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['circle radius ',\n",
" kurt.Insert('number', default=12),\n",
" ' around ',\n",
" kurt.Insert('readonly-menu', 'sprite', default='Sprite1'),\n",
" ' color ',\n",
" kurt.Insert('color')]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"parts = list(kurt.scratch20.blocks.parse_spec(spec, list(defaults)))\n",
"parts"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<kurt.CustomBlockType('circle radius 12 around Sprite1 color #ff0000' shape='stack')>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cbt = kurt.CustomBlockType(\"stack\", parts)\n",
"cbt"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[kurt.Insert('number', default=12, name='x'),\n",
" kurt.Insert('readonly-menu', 'sprite', default='Sprite1', name='sprite'),\n",
" kurt.Insert('color', default=0, name='color')]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"for i, name in zip(cbt.inserts, parameters):\n",
" i.name = name\n",
"for i, value in zip(cbt.inserts, defaults):\n",
" i.default = value\n",
"cbt.inserts"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[12, 'Sprite1', 0]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"defaults"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cat.scripts.append(kurt.Script([\n",
" kurt.Block(\"define\", cbt),\n",
"]))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'hacked.sb2'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p.save(\"hacked.sb2\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"!open hacked.sb2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment