{"id":2490,"date":"2023-08-03T23:07:03","date_gmt":"2023-08-03T15:07:03","guid":{"rendered":"https:\/\/104.es\/?p=2490"},"modified":"2023-08-03T23:07:03","modified_gmt":"2023-08-03T15:07:03","slug":"microbitmicropython-roulette","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/08\/03\/microbitmicropython-roulette\/","title":{"rendered":"\u3010micro:bit\u3011MicroPython\u96fb\u5b50\u8f2a\u76e4\u904a\u6232\u5be6\u4f5c"},"content":{"rendered":"\n<p>\u4f7f\u7528MicroPython\u4f86\u8a2d\u8a08\u96fb\u5b50\u8f2a\u76e4\u904a\u6232\u96d6\u7136\u770b\u8d77\u4f86\u7a0b\u5f0f\u78bc\u8f03\u591a\uff0c\u4f46\u5728\u908f\u8f2f\u4e0a\u6bd4\u8d77\u7a4d\u6728\u4f86\u5f97\u5bb9\u6613\u7406\u89e3\uff0c\u800c\u4e14\u6587\u5b57\u5f0f\u7684\u7a0b\u5f0f\u78bc\u4e5f\u6bd4\u8f03\u597d\u8907\u88fd\u4fee\u6539\uff0c\u6240\u4ee5\u9019\u7bc7\u6587\u7ae0\u6211\u5011\u5c31\u4f86\u770b\u770b\u96fb\u5b50\u8f2a\u76e4\u904a\u6232\u7684\u5be6\u4f5c\u6280\u5de7\u3002<\/p>\n\n\n\n<p>\u5148\u4f86\u770b\u770b\u5982\u4f55\u8b93\u96fb\u5b50\u8f2a\u76e4\u53ef\u4ee5\u4f9d\u4e82\u6578\u7684\u65b9\u5f0f\u4e0d\u5b9a\u6642\u505c\u6b62\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\nimport random \r\n\r\ndef setLED(number, value):\r\n    x = number % 5\r\n    y = number \/\/ 5\r\n    display.set_pixel(x, y, value)\r\n\r\nseq = &#91;0, 1, 2, 3, 4, 9, 14, 19, 24, 23, 22, 21, 20, 15, 10, 5]\r\ncounter, old = 0, 0\r\ndelay = 1\r\nwhile True:\r\n    setLED(seq&#91;old], 0)\r\n    setLED(seq&#91;counter], 9)\r\n    delay += random.randint(1,10) \r\n    sleep(delay)\r\n    if delay > 500: break\r\n    old = counter\r\n    counter = counter + 1\r\n    if counter >= len(seq): \r\n        counter = 0\r\nsleep(2000)\r\ndisplay.show(Image.HEART)<\/code><\/pre>\n\n\n\n<p>\u9019\u500b\u7a0b\u5f0f\u4e00\u958b\u59cb\u57f7\u884c\u4e4b\u5f8c\uff0c\u5c31\u6703\u6709\u4e00\u500bLED\u7531\u5feb\u800c\u6162\u5730\u8f2a\u6d41\u5728LED\u77e9\u9663\u5916\u570d\u65cb\u8f49\u986f\u793a\uff0c\u81f3\u65bc\u6703\u505c\u5728\u54ea\u4e00\u500b\u4f4d\u7f6e\u4e0a\u4e26\u4e0d\u6703\u56fa\u5b9a\uff0c\u56e0\u70ba\u4e3b\u8981\u5728\u8f2a\u6d41\u9ede\u4eaeLED\u4f4d\u7f6e\u7684\u8ff4\u5708\u662f\u7b2c12\u884c\uff0c\u5b83\u6c92\u6709\u9810\u5b9a\u7684\u91cd\u8907\u6b21\u6578\uff0c\u4e00\u958b\u59cb\u7684delay\u8b8a\u6578\u5f9e1\u958b\u59cb\uff0c\u6bcf\u4e00\u6b21\u5728\u7b2c15\u884c\u57f7\u884c\u7684\u6642\u5019\u5c31\u6703\u57281~10\u4e4b\u9593\u96a8\u6a5f\u53d6\u4e00\u500b\u6578\u5b57\u52a0\u5230delay\u4e2d\uff0c\u4e00\u76f4\u5230\u7b2c17\u884c\u7684\u5730\u65b9\u6aa2\u67e5delay\u662f\u5426\u5927\u65bc500\uff0c\u5982\u679c\u5927\u65bc500\u5c31\u96e2\u958b\u8ff4\u5708\uff0c\u4e5f\u5c31\u662f\u5b83\u7684\u505c\u6b62\u689d\u4ef6 \u3002<\/p>\n\n\n\n<p>\u7576\u96e2\u958b\u8ff4\u5708\u7684\u6642\u5019\uff0c\u5148\u5728\u7b2c22\u884c\u66ab\u505c2\u79d2\u9418\uff0c\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u770b\u6e05\u695aLED\u505c\u7559\u7684\u4f4d\u7f6e\uff0c\u6700\u5f8c\u518d\u4ee5\u7b2c21\u884c\u986f\u793a\u4e00\u500b\u611b\u5fc3\u5716\u6848\u70ba\u7a0b\u5f0f\u7684\u7d50\u675f\u8a18\u865f\u3002<\/p>\n\n\n\n<p>\u70ba\u4e86\u8981\u8b93\u904a\u6232\u53ef\u4ee5\u9032\u884c\uff0c\u5728\u958b\u59cb\u57f7\u884c\u65cb\u8f49LED\u4f5c\u696d\u4e4b\u524d\u9084\u9700\u8981\u8b93\u73a9\u5bb6\u6709\u8a2d\u5b9a\u731c\u6e2c\u4f4d\u7f6e\u7684\u6a5f\u6703\uff0c\u7a0b\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counter, old = 0, 0\r\nsetLED(seq&#91;counter], 9)\r\nwhile True:\r\n    if old != counter:\r\n        setLED(seq&#91;old], 0)\r\n        setLED(seq&#91;counter], 9)\r\n    if button_a.is_pressed() and button_b.is_pressed():\r\n        break\r\n    elif button_a.was_pressed():\r\n        old = counter\r\n        counter -= 1\r\n        if counter &lt; 0:\r\n            counter = len(seq)-1\r\n        sleep(200)\r\n    elif button_b.was_pressed():\r\n        old = counter\r\n        counter += 1\r\n        if counter >= len(seq):\r\n            counter = 0\r\n        sleep(200)<\/code><\/pre>\n\n\n\n<p>\u4e0d\u540c\u65bc\u524d\u9762\u7684\u7a0b\u5f0f\uff0c\u9019\u500b\u7a0b\u5f0f\u662f\u7531\u73a9\u5bb6\u4f86\u6c7a\u5b9a\u8981\u986f\u793a\u7684LED\u4f4d\u7f6e\uff0c\u4e00\u958b\u59cb\u628acounter\u8a2d\u5b9a\u5728\u4e32\u5217seq\u7684\u7b2c0\u500b\u4f4d\u7f6e\uff0c\u4e26\u5728\u7b2c2\u884c\u7684\u6642\u5019\u9ede\u4eae\u8a72\u4f4d\u7f6e\u7684LED\u3002\u63a5\u8457\u9032\u5165\u8ff4\u5708\u4e2d\uff0c\u9019\u500b\u8ff4\u5708\u4e5f\u662f\u4e0d\u5b9a\u91cd\u8907\u6b21\u6578\uff0c\u5b83\u662f\u7531\u7b2c7\u884c\u7684\u5075\u6e2c\u52d5\u4f5c\u4ee5\u6c7a\u5b9a\u662f\u5426\u8981\u96e2\u958b\u9019\u500b\u8ff4\u5708\uff0c\u4e5f\u5c31\u662f\u7576\u73a9\u5bb6\u540c\u6642\u6309\u4e0bA\u6309\u9215\u53caB\u6309\u9215\u7684\u6642\u5019\uff0c\u5c31\u6703\u96e2\u958b\u9019\u500b\u8ff4\u5708\u4ee5\u7e7c\u7e8c\u5f8c\u7e8c\u7684\u4f5c\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u73a9\u5bb6\u6309\u4e0bA\u6309\u9215\u7684\u8a71\uff0c\u5c31\u8b93LED\u9006\u6642\u91dd\u79fb\u52d5\uff0c\u82e5\u662f\u6309\u4e0bB\u6309\u9215\uff0c\u5247\u8b93LED\u9806\u6642\u91dd\u8f49\u52d5\u3002\u5982\u6b64\uff0c\u9019\u500b\u7a0b\u5f0f\u7247\u6bb5\u5c31\u53ef\u4ee5\u8b93\u73a9\u5bb6\u8a2d\u5b9a\u8981\u731c\u6e2c\u7684\u4f4d\u7f6e\uff0c\u4e5f\u5c31\u662f\u7576\u73a9\u5bb6\u540c\u6642\u6309\u4e0bA\u548cB\u6309\u9215\u96e2\u958b\u9019\u500b\u8ff4\u5708\u7684\u6642\u5019\uff0c\u6b64\u6642\u7684counter\u8b8a\u6578\u7684\u5167\u5bb9\u5c31\u662f\u73a9\u5bb6\u8981\u731c\u6e2c\u7684\u7de8\u865f\uff0c\u5728\u96e2\u958b\u8ff4\u5708\u4e4b\u5f8c\uff0c\u8981\u7528\u53e6\u5916\u4e00\u500b\u8b8a\u6578guess\u4f86\u8a18\u9304\u6b64\u6578\u503c\uff0c\u4ee5\u4fbf\u548c\u5f8c\u4f86\u7684\u96a8\u6a5f\u6578\u505a\u6bd4\u8f03\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\nimport random \r\n\r\ndef setLED(number, value):\r\n    x = number % 5\r\n    y = number \/\/ 5\r\n    display.set_pixel(x, y, value)\r\n\r\nseq = &#91;0, 1, 2, 3, 4, 9, 14, 19, 24, 23, 22, 21, 20, 15, 10, 5]\r\n\r\ncounter, old = 0, 0\r\nsetLED(seq&#91;counter], 9)\r\nwhile True:\r\n    if old != counter:\r\n        setLED(seq&#91;old], 0)\r\n        setLED(seq&#91;counter], 9)\r\n    if button_a.is_pressed() and button_b.is_pressed():\r\n        break\r\n    elif button_a.was_pressed():\r\n        old = counter\r\n        counter -= 1\r\n        if counter &lt; 0:\r\n            counter = len(seq)-1\r\n        sleep(200)\r\n    elif button_b.was_pressed():\r\n        old = counter\r\n        counter += 1\r\n        if counter >= len(seq):\r\n            counter = 0\r\n        sleep(200)\r\n\r\nanswer = counter \r\ncounter, old = 0, 0\r\ndelay = 1\r\nwhile True:\r\n    setLED(seq&#91;old], 0)\r\n    setLED(seq&#91;counter], 9)\r\n    delay += random.randint(1,10) \r\n    sleep(delay)\r\n    if delay > 500: break\r\n    old = counter\r\n    counter = counter + 1\r\n    if counter >= len(seq): \r\n        counter = 0\r\nsleep(2000)\r\nif counter == answer:\r\n    display.show(Image.HEART)\r\nelse:\r\n    display.show(Image.SAD)<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u60f3\u8981\u518d\u73a9\u4e00\u6b21\u7684\u8a71\uff0c\u53ef\u4ee5\u6309\u4e0b\u91cd\u7f6e\u6309\u9215\u3002\u4e0d\u904e\uff0c\u4e5f\u8a31\u540c\u5b78\u5011\u53ef\u4ee5\u60f3\u4e00\u4e0b\uff0c\u5982\u4f55\u5728\u7a0b\u5f0f\u4e2d\u8a62\u554f\u4f7f\u7528\u8005\u662f\u5426\u91cd\u73a9\u4e00\u904d\uff0c\u4e26\u5229\u7528\u6309\u9215A\u6216\u6309\u9215B\u4f86\u6c7a\u5b9a\u91cd\u73a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528MicroPython\u4f86\u8a2d\u8a08\u96fb\u5b50\u8f2a\u76e4\u904a\u6232\u96d6\u7136\u770b\u8d77\u4f86\u7a0b\u5f0f\u78bc\u8f03\u591a\uff0c\u4f46\u5728\u908f\u8f2f\u4e0a\u6bd4\u8d77\u7a4d\u6728\u4f86\u5f97\u5bb9\u6613\u7406\u89e3\uff0c\u800c\u4e14\u6587\u5b57\u5f0f\u7684\u7a0b\u5f0f\u78bc\u4e5f\u6bd4\u8f03\u597d\u8907\u88fd\u4fee\u6539\uff0c\u6240\u4ee5\u9019\u7bc7\u6587\u7ae0\u6211\u5011\u5c31\u4f86\u770b\u770b\u96fb\u5b50\u8f2a\u76e4\u904a\u6232\u7684\u5be6\u4f5c\u6280\u5de7\u3002<\/p>\n","protected":false},"author":1,"featured_media":2492,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11,15],"tags":[],"class_list":["post-2490","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microbit","category-python"],"jetpack_featured_media_url":"https:\/\/104.es\/wp-content\/uploads\/2023\/08\/64lnxlb2lwo.jpg","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2490"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2490\/revisions"}],"predecessor-version":[{"id":2491,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2490\/revisions\/2491"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2492"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}