TL;DR scroll to bottom
PyScript is a framework that allows for Python in HTML. Technically, this should also be tagged as HTML/JS/CSS.
The function
move
in the below class p
serves the purpose of incrementing x/y values, when key inputs from JS(held in an object known as keys
) match the object’s registered input values from the parameter keys
. I now realize how confusing that is, but please bear with me.
Python Code:
class p:
def __init__(self, x, y, id, imgs, keys):
self.x = x
self.y = y
self.id = id
self.imgs = imgs
self.framenum = 0
self.score = 0
self.alive = True
self.motion = False
self.keys = keys
def cld(self, ob):
if self.x == ob.x and self.y == ob.y:
return True
else:
return False
def move(self,inputsrc):
if inputsrc[self.keys[0]]:
self.y -= 1
if inputsrc[self.keys[1]]:
self.y += 1
if inputsrc[self.keys[2]]:
self.x -= 1
if inputsrc[self.keys[3]]:
self.x += 1
The object p1
is created in Python:
p1 = p(0,0,"1",[0,1,2,3,4],["keyUp","keyDown","keyLeft","keyRight"])
JS for keys
(the object):
let keys = {};
document.addEventListener('keydown', (event) => {
keys[event.key] = true;
}, false);
document.addEventListener('keyup', (event) => {
keys[event.key] = false;
}, false);
//assume p1 object has already been imported from python
p1.move(keys)
When ran, the console returns the following:
File “”, line 66, in move
TypeError: ‘pyodide.JsProxy’ object is not subscriptable
Either the Python parameter keys
is not being read as an array, or the JS keys
object is being read as an array. Another possibility is that the JS object keys
is empty, yet is still being read.
Here’s a link, 'cause I explained that poorly:
https://replit.com/@arn5891/idk-man-its-something