{"id":2488,"date":"2023-08-03T21:16:59","date_gmt":"2023-08-03T13:16:59","guid":{"rendered":"https:\/\/104.es\/?p=2488"},"modified":"2023-08-03T21:16:59","modified_gmt":"2023-08-03T13:16:59","slug":"microbitmicropython-random-game","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/08\/03\/microbitmicropython-random-game\/","title":{"rendered":"\u3010micro:bit\u3011MicroPython\u4e82\u6578\u7684\u61c9\u7528"},"content":{"rendered":"\n<p>\u5728micro:bit\u7684\u7a0b\u5f0f\u61c9\u7528\u4e2d\u96a8\u6a5f\u4e82\u6578\u662f\u975e\u5e38\u6709\u8da3\u7684\u4e00\u500b\u51fd\u6578\uff0c\u9019\u539f\u672c\u662f\u5728Python\u4e2d\u5c31\u6709\u7684\u51fd\u6578\uff0c\u5728microPython\u4e2d\u4e5f\u662f\u4e00\u6a23\u53ef\u4ee5\u4f7f\u7528\u3002\u5148\u4f86\u770b\u770b\u4ee5\u4e0b\u9019\u500b\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\nimport random \r\n\r\nwhile True:\r\n    display.show(random.randint(0, 9))\r\n    sleep(1000)<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u9019\u500b\u7a0b\u5f0f\u6703\u6bcf\u9694\u4e00\u79d2\u63db\u4e00\u500b\u6578\u5b57\uff0c\u9019\u500b\u6578\u5b57\u7531random.randint()\u51fd\u6578\u7522\u751f\uff0c\u6bcf\u4e00\u6b21\u90fd\u4e0d\u4e00\u6a23\u3002\u61c9\u7528\u9019\u500b\u51fd\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u8a2d\u8a08\u4e00\u500b\u731c\u6578\u5b57\u7684\u904a\u6232\u7a0b\u5f0f\u3002\u5148\u4f86\u770b\u770b\u5982\u4f55\u8b93\u4f7f\u7528\u8005\u900f\u904e\u6309\u9215\u9078\u64c7\u4e00\u500b\u6578\u5b57\u7684\u7a0b\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\nimport random \r\n\r\nguess = 5\r\nwhile True:\r\n    if button_a.is_pressed() and button_b.is_pressed():\r\n        break;\r\n    if button_a.was_pressed():\r\n        guess += 1\r\n        if guess > 9: \r\n            guess = 9\r\n    elif button_b.was_pressed():\r\n        guess -= 1\r\n        if guess &lt; 0:\r\n            guess = 0\r\n    display.show(guess)<\/code><\/pre>\n\n\n\n<p>\u9019\u500b\u7a0b\u5f0f\u4e00\u958b\u59cb\u7684\u6642\u5019\u5148\u8b93\u8b8a\u6578guess\u8a2d\u5b9a\u70ba5\uff0c\u63a5\u4e0b\u4f86\u5728\u8ff4\u5708\u4e2d\u5075\u6e2c\u662f\u5426A\u548cB\u6309\u9215\u540c\u6642\u88ab\u6309\u4e0b\uff0c\u5982\u679c\u662f\u7684\u8a71\uff0c\u5c31\u96e2\u958b\u8ff4\u5708\uff0c\u9019\u6642\u5019guess\u7684\u5167\u5bb9\u5c31\u662f\u6211\u5011\u8981\u731c\u7684\u6578\u5b57\u3002\u5982\u679c\u53ea\u662f\u6309\u4e0b\u5176\u4e2d\u4e00\u500b\u6309\u9215\u7684\u8a71\uff0c\u5c31\u4f9d\u7167\u6309\u9215\u662f\u54ea\u4e00\u500b\u4f86\u6c7a\u5b9a\u8981\u628aguess\u52a01\u6216\u662f\u6e1b1\u3002\u540c\u6a23\u7684\uff0c\u5728\u52a0\u6e1b\u6578\u5b57\u7684\u904e\u7a0b\u4e2d\uff0c\u53ea\u8981\u540c\u6642\u6309\u4e0b\u5169\u500b\u6309\u9215\u4e4b\u5f8c\u5c31\u6703\u96e2\u958b\u8ff4\u5708\uff0c\u4e5f\u5c31\u662f\u78ba\u5b9a\u4e86\u73a9\u5bb6\u8981\u9078\u64c7\u7684\u6578\u5b57\uff0c\u800c\u4e14\u662f\u653e\u5728guess\u4e2d\u3002<\/p>\n\n\n\n<p>\u70ba\u4e86\u589e\u52a0\u8da3\u5473\u6027\uff0c\u63a5\u4e0b\u4f86\u5229\u7528\u4e00\u500b\u8ff4\u5708\u4f86\u986f\u793a100\u500b\u4e0d\u76f8\u95dc\u7684\u6578\u5b57\uff0c\u800c\u4e14\u6bcf\u986f\u793a\u4e00\u500b\u6578\u5b57\u4e4b\u5f8c\u5c31\u589e\u52a0\u66ab\u505c\u7684\u6642\u9593\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(100):\r\n    display.show(random.randint(0, 9))\r\n    sleep(20+i*2)<\/code><\/pre>\n\n\n\n<p>\u5728\u986f\u793a\u4e86\u9019\u4e9b\u6578\u5b57\u4e4b\u5f8c\uff0c\u518d\u53d6\u5f97\u6b63\u78ba\u7684\u7b54\u6848\u653e\u5728answer\u4e2d\uff0c\u518d\u4f5c\u4e00\u4e9b\u5c0f\u5c0f\u7684\u9583\u720d\u52d5\u756b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>answer = random.randint(0, 9)\r\nfor i in range(5):\r\n    display.clear()\r\n    sleep(500)\r\n    display.show(answer)\r\n    sleep(500)<\/code><\/pre>\n\n\n\n<p>\u6700\u5f8c\u5247\u662f\u6bd4\u5c0d\u73a9\u5bb6\u731c\u7684\u6578\u5b57guess\u548c\u6b63\u78ba\u7b54\u6848\uff0c\u518d\u4f9d\u76f8\u7b49\u8207\u5426\u986f\u793a\u4e0d\u540c\u7684\u5716\u6848\uff0c\u5b8c\u6574\u7684\u7a0b\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from microbit import *\r\nimport random \r\n\r\nguess = 5\r\nwhile True:\r\n    if button_a.is_pressed() and button_b.is_pressed():\r\n        break;\r\n    if button_a.was_pressed():\r\n        guess += 1\r\n        if guess > 9: \r\n            guess = 9\r\n    elif button_b.was_pressed():\r\n        guess -= 1\r\n        if guess &lt; 0:\r\n            guess = 0\r\n    display.show(guess)\r\n    \r\nfor i in range(100):\r\n    display.show(random.randint(0, 9))\r\n    sleep(20+i*2)\r\n    \r\nanswer = random.randint(0, 9)\r\nfor i in range(5):\r\n    display.clear()\r\n    sleep(500)\r\n    display.show(answer)\r\n    sleep(500)\r\n    \r\nif answer == guess:\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\uff0c\u53ea\u8981\u6309\u4e0b\u91cd\u7f6e\u6309\u9215\u5c31\u53ef\u4ee5\u56c9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728micro:bit\u7684\u7a0b\u5f0f\u61c9\u7528\u4e2d\u96a8\u6a5f\u4e82\u6578\u662f\u975e\u5e38\u6709\u8da3\u7684\u4e00\u500b\u51fd\u6578\uff0c\u9019\u539f\u672c\u662f\u5728Python\u4e2d\u5c31\u6709\u7684\u51fd\u6578\uff0c\u5728microPython\u4e2d\u4e5f\u662f\u4e00\u6a23\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":2480,"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-2488","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\/oqtafyt5ktw.jpg","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2488","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=2488"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2488\/revisions"}],"predecessor-version":[{"id":2489,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2488\/revisions\/2489"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2480"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}