EDITED: set methods as a Lock class so to be reused multiple times using just a prefix for the buttons.
Each lock should have a prefix the first has k0 k1 kCANC kSHOW, if you have a second one you should call each part with a different prefix for example l0 l1 lCANC lSHOW and use Lock(‘l’, … ) instead.
You can find a cospace featuring it at CoSpaces Edu :: Electronic lock code,
mainly you find there
- a lockpad with 5678 hardcoded, when you type the code it is visible on the top and pressing the key it will open the door
- a door
The full python code for this small component is attached, and you can just copy and paste the lock in your project.
Project is remixable so you can use modify this proof and better it as you like. It is CC0
from cospaces import *
import math
import random
class Lock:
def __init__(self,prefix,target,animation, code):
print(f"init")
self.x = 0
self.prefix = prefix
self.key_SHOW=scene.get_item(f"{prefix}SHOW")
print(f"self.key_SHOW is {self.key_SHOW}")
self.target: AnimatedItem = scene.get_item(target)
self.animation = animation
self.code = code
self.set_lock(prefix)
self.show()
def show(self):
self.key_SHOW.text = str(self.x)
def press(self,k):
print("press")
self.x = self.x*10 + k
self.show()
def clear(self):
print("clear")
self.x=0
self.show()
def canc(self):
print("canc")
self.x = int(self.x/10)
self.show()
def ok(self):
print("ok")
if self.x == self.code:
self.target.animation.play(self.animation)
else:
self.x = 0
self.show()
def set_key(self,prefix,function = None, key = None):
#print(f"called with {prefix} {function} {key}")
if function:
scene.get_item(prefix).input.on_click(lambda: function())
if key:
scene.get_item(prefix).input.on_click(lambda: self.press(key))
def set_lock(self,prefix):
for k in range(0,10):
prefix1 = f"{prefix}{k}"
#print(f"calling set_key with {prefix1}")
self.set_key(prefix1, key=k)
self.set_key("kOK", function = self.ok)
self.set_key("kCANC", function = self.canc)
self.set_key("kCLEAR", function = self.clear)
self.show()
lock=Lock("k",'door','Open',5678)
Here a yt video in italian explaining how to use this… Cospaces, lucchetto digitale apriporta per escape room - YouTube