Skip to content

Instantly share code, notes, and snippets.

@Akkiesoft
Created October 26, 2021 14:53
Show Gist options
  • Select an option

  • Save Akkiesoft/d0b2e7166329f6dce64224c0465298ac to your computer and use it in GitHub Desktop.

Select an option

Save Akkiesoft/d0b2e7166329f6dce64224c0465298ac to your computer and use it in GitHub Desktop.
村人取引(キー)のCircuitPython版。Raspberry Pi Picoとかで使える
# mc-villager-trading-key for CircuitPython
import board
import digitalio
from time import sleep
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.mouse import Mouse
from adafruit_hid.keycode import Keycode
# 取引アイテム受け取りの枠から、取引アイテム選択リストまでの移動について数値を入れる
# 右から左上に移動と想定されるので基本的にマイナス値
mx = -500
my = -50
keyboard = Keyboard(usb_hid.devices)
mouse = Mouse(usb_hid.devices)
button0 = digitalio.DigitalInOut(board.GP0)
button0.switch_to_input(pull=digitalio.Pull.UP)
led_r = digitalio.DigitalInOut(board.LED_R)
led_r.direction = digitalio.Direction.OUTPUT
led_g = digitalio.DigitalInOut(board.LED_G)
led_g.direction = digitalio.Direction.OUTPUT
led_b = digitalio.DigitalInOut(board.LED_B)
led_b.direction = digitalio.Direction.OUTPUT
# tiny2040 LED is active low
led_r.value = 1
led_g.value = 1
led_b.value = 1
trading = 0
while True:
if not button0.value:
led_r.value = 0
led_g.value = 0
led_b.value = 0
if not trading:
# 取引開始。取引中はShift押しっぱなし
trading = 1
keyboard.press(Keycode.LEFT_SHIFT)
# 取引アイテム受け取り枠<->取引アイテムリストを反復しながらクリック
mouse.move(x = mx * trading, y = my * trading)
mouse.click(Mouse.LEFT_BUTTON)
trading = trading * -1
sleep(0.15)
else:
led_r.value = 1
led_g.value = 1
led_b.value = 1
keyboard.release_all()
if trading < 0:
# 取引アイテム受け取り枠に戻す
mouse.move(x = mx * -1, y = my * -1)
trading = 0
sleep(0.02)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment