From bebc0fa99e0024bd7cb495496d02a3ac9747efc7 Mon Sep 17 00:00:00 2001 From: David Leoni Date: Tue, 27 Aug 2024 06:51:40 +0200 Subject: [PATCH] event loop prototype #8 --- css/turtleps.css | 4 + main.py | 190 ++++------------------------------------------- test_main.py | 179 ++++++++++++++++++++++++++++++++++++++++++++ tests.py | 10 +++ turtleps.py | 89 +++++++++++++++++++++- 5 files changed, 294 insertions(+), 178 deletions(-) create mode 100644 test_main.py diff --git a/css/turtleps.css b/css/turtleps.css index b1202ef..ade9d60 100644 --- a/css/turtleps.css +++ b/css/turtleps.css @@ -1,5 +1,8 @@ body { margin: 0; + position: fixed; + overflow-x: hidden; + width: 100%; } .pyscript { @@ -25,6 +28,7 @@ html { line-height: 1.5; } + nav { position: sticky; width: 100%; diff --git a/main.py b/main.py index bd41da9..2b5d2ae 100644 --- a/main.py +++ b/main.py @@ -1,193 +1,31 @@ -import random -#import pyscript.web.dom -from pyscript import when, display -from pyscript.web import page, img - -from js import DOMParser -from js import ( - document, - Element, -) - - -from pyscript import window, document - -from pyodide.http import open_url -from pyodide.ffi.wrappers import set_timeout -from pyodide.ffi.wrappers import add_event_listener - + from turtleps import * -import asyncio - - - -ctx = None -interval=200 # TODO - -_pressedKeys = { -} - - -def _handle_input(e): - """ { - "key": "a", - "keyCode": 65, - "which": 65, - "code": "KeyA", - "location": 0, - "altKey": false, - "ctrlKey": false, - "metaKey": false, - "shiftKey": false, - "repeat": false - } - - { - "key": "ArrowLeft", - "keyCode": 37, - "which": 37, - "code": "ArrowLeft", - "location": 0, - "altKey": false, - "ctrlKey": false, - "metaKey": false, - "shiftKey": false, - "repeat": false - } - """ - global _pressedKeys - if e.type == "keydown": - _pressedKeys[e.key] = True - elif e.type == "keyup": - _pressedKeys[e.key] = False - -def btn(key: str): - if key in _pressedKeys: - return _pressedKeys[key] - else: - return False +screen = Screen() +hideturtle() -def init(sprite, append=True): - global ctx +ada = Sprite() - sprite.url = "./antigravity.svg" - sprite.target = page["body"][0] +ada.forward(100) - doc = DOMParser.new().parseFromString( - open_url(sprite.url).read(), "image/svg+xml" - ) - - #doc = img(src=sprite.url) - #page.append(open_url(sprite.url).read()) - - sprite.node = doc.documentElement - - #sprite.node = doc._dom_element - - - - ctx = sprite.node - - if append: - sprite.target.append(sprite.node) - else: - sprite.target._js.replaceChildren(sprite.node) - - sprite.x, sprite.y = 0, 0 - - #init input - add_event_listener( - document, - "keydown", - _handle_input - ) - - add_event_listener( - document, - "keyup", - _handle_input - ) - - +init_engine() def update(): - global ctx, sprite - if btn("ArrowUp") or btn(" "): - print("DAVID: KEY_UP or KEY_SPACE") - - if btn("q"): - print("DAVID: KEY_Q") - print("DAVID: QUITTING!") - return - - char = sprite.node.getElementsByTagName("g")[1] - - char.setAttribute("transform", f"translate({sprite.x}, {-sprite.y})") - sprite.x += random.normalvariate(0, 1) / 20 - if sprite.y < 50: - sprite.y += 0.1 - sprite.x -= 0.1 - else: - sprite.y += random.normalvariate(0, 1) / 20 - - set_timeout(update, interval) - - - -def check_type(arg, *types): - for t in types: - if type(arg) == t: - return - raise CDTNException(f"Tipo di dato sbagliato per il valore {arg }!\n Atteso: {types} Ottenuto: {type(arg)}") - - - - - -from tests import * - -#sprite = ada -#init(ada, append=True) -#rettangolo = ada.add_rect(30,40,fill='rgb(0,255,255)', sid='pippo') -#rettangolo.setAttribute("fill", "green") -#rettangolo.fill = "green" # non funziona -#update() - -#hideturtle() - -#await test_turtleps() + if pressed("ArrowRight"): + print("KEY_RIGHT") + ada.goto(ada.xcor() + 10, ada.ycor()) + if pressed("ArrowLeft"): + print("KEY_LEFT") + ada.goto(ada.xcor() - 10, ada.ycor()) -#test_stamp() - -#test_big_star() - -#await test_storytelling() - -#test_load_image() - -#await test_layers() - -#await test_shapesize_one() - -#await test_shapesize_many() - -#await test_tilt() - -#await test_quadrato_pieno() - -#await test_fumetti_ciao() - -#await test_fumetti_piu() - -#test_text() -#test_colors() + set_timeout(update, interval) +update() diff --git a/test_main.py b/test_main.py new file mode 100644 index 0000000..fd91d32 --- /dev/null +++ b/test_main.py @@ -0,0 +1,179 @@ +import random +#import pyscript.web.dom +from pyscript import when, display +from pyscript.web import page, img + +from js import DOMParser +from js import ( + document, + Element, +) + +from pyscript import window, document + +from pyodide.http import open_url +from pyodide.ffi.wrappers import set_timeout +from pyodide.ffi.wrappers import add_event_listener + + +from turtleps import * +import asyncio + + +ctx = None +interval=20 # TODO millisecs + +_pressedKeys = { +} + + +def _handle_input(e): + """ { + "key": "a", + "keyCode": 65, + "which": 65, + "code": "KeyA", + "location": 0, + "altKey": false, + "ctrlKey": false, + "metaKey": false, + "shiftKey": false, + "repeat": false + } + + { + "key": "ArrowLeft", + "keyCode": 37, + "which": 37, + "code": "ArrowLeft", + "location": 0, + "altKey": false, + "ctrlKey": false, + "metaKey": false, + "shiftKey": false, + "repeat": false + } + """ + global _pressedKeys + if e.type == "keydown": + _pressedKeys[e.key] = True + elif e.type == "keyup": + _pressedKeys[e.key] = False + +def btn(key: str): + if key in _pressedKeys: + return _pressedKeys[key] + else: + return False + + +def init(sprite, append=True): + global ctx + + sprite.url = "./antigravity.svg" + sprite.target = page["body"][0] + + doc = DOMParser.new().parseFromString( + open_url(sprite.url).read(), "image/svg+xml" + ) + + #doc = img(src=sprite.url) + #page.append(open_url(sprite.url).read()) + + sprite.node = doc.documentElement + + #sprite.node = doc._dom_element + + + + ctx = sprite.node + + if append: + sprite.target.append(sprite.node) + else: + sprite.target._js.replaceChildren(sprite.node) + + sprite.x, sprite.y = 0, 0 + + #init input + add_event_listener( + document, + "keydown", + _handle_input + ) + + add_event_listener( + document, + "keyup", + _handle_input + ) + + + +def update(): + global ctx, sprite + + if btn("ArrowUp") or btn(" "): + print("KEY_UP or KEY_SPACE") + + if btn("q"): + print("KEY_Q") + print("QUITTING!") + return + + char = sprite.node.getElementsByTagName("g")[1] + + char.setAttribute("transform", f"translate({sprite.x}, {-sprite.y})") + sprite.x += random.normalvariate(0, 1) / 20 + if sprite.y < 50: + sprite.y += 0.1 + sprite.x -= 0.1 + else: + sprite.y += random.normalvariate(0, 1) / 20 + + set_timeout(update, interval) + + + +from tests import * + +#sprite = ada +#init(ada, append=True) +#rettangolo = ada.add_rect(30,40,fill='rgb(0,255,255)', sid='pippo') +#rettangolo.setAttribute("fill", "green") +#rettangolo.fill = "green" # non funziona +#update() + +#hideturtle() + +#await test_turtleps() + + +#test_stamp() + +#test_big_star() + +#await test_storytelling() + +#test_load_image() + +#await test_layers() + +#await test_shapesize_one() + +#await test_shapesize_many() + +#await test_tilt() + +#await test_quadrato_pieno() + +#await test_fumetti_ciao() + +#await test_fumetti_piu() + +#test_text() +#test_colors() + + + + diff --git a/tests.py b/tests.py index 040fc49..ea3ba29 100644 --- a/tests.py +++ b/tests.py @@ -473,3 +473,13 @@ def centered_text(text: str, color): print("Fine main.py") """ + +""" +def check_type(arg, *types): + for t in types: + if type(arg) == t: + return + raise CDTNException(f"Tipo di dato sbagliato per il valore {arg }!\n Atteso: {types} Ottenuto: {type(arg)}") + + +""" \ No newline at end of file diff --git a/turtleps.py b/turtleps.py index 42c5dbc..69e5d54 100644 --- a/turtleps.py +++ b/turtleps.py @@ -93,8 +93,8 @@ def _parse_color_args(*args): import math -#_debugging = False -_debugging = True +_debugging = False +#_debugging = True #_tracing = True _tracing = False @@ -1492,3 +1492,88 @@ async def say(self, text, seconds, dx=0, dy=65): screen.colormode(255) # this is Trinket default, see https://github.com/CoderDojoTrento/turtle-storytelling/issues/2 """ + +# VERY DRAFTY EVENT LOOP STUFF + +import random +#import pyscript.web.dom +from pyscript import when, display +from pyscript.web import page, img + +from js import DOMParser +from js import ( + document, + Element, +) + +from pyscript import window, document + +from pyodide.http import open_url +from pyodide.ffi.wrappers import set_timeout +from pyodide.ffi.wrappers import add_event_listener +import asyncio + + + +ctx = None +interval=20 # TODO millisecs + +_pressedKeys = { +} + + +def _handle_input(e): + """ { + "key": "a", + "keyCode": 65, + "which": 65, + "code": "KeyA", + "location": 0, + "altKey": false, + "ctrlKey": false, + "metaKey": false, + "shiftKey": false, + "repeat": false + } + + { + "key": "ArrowLeft", + "keyCode": 37, + "which": 37, + "code": "ArrowLeft", + "location": 0, + "altKey": false, + "ctrlKey": false, + "metaKey": false, + "shiftKey": false, + "repeat": false + } + """ + global _pressedKeys + if e.type == "keydown": + _pressedKeys[e.key] = True + elif e.type == "keyup": + _pressedKeys[e.key] = False + +def pressed(key: str): + if key in _pressedKeys: + return _pressedKeys[key] + else: + return False + + +def init_engine(): + + #init input + add_event_listener( + document, + "keydown", + _handle_input + ) + + add_event_listener( + document, + "keyup", + _handle_input + ) + \ No newline at end of file