{"id":173845,"date":"2024-05-08T18:19:44","date_gmt":"2024-05-08T10:19:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173845.html"},"modified":"2024-05-08T18:19:56","modified_gmt":"2024-05-08T10:19:56","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bc%96%e4%b8%80%e4%b8%aa%e5%b0%8f%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173845.html","title":{"rendered":"\u5982\u4f55\u7528Python\u7f16\u4e00\u4e2a\u5c0f\u6e38\u620f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27045704\/379b0c89-028f-4454-a1fd-a38cec5e64ef.webp\" alt=\"\u5982\u4f55\u7528Python\u7f16\u4e00\u4e2a\u5c0f\u6e38\u620f\" \/><\/p>\n<p><p>\u8981\u7528Python\u7f16\u5199\u4e00\u4e2a\u5c0f\u6e38\u620f\uff0c\u60a8\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u6e38\u620f\u7c7b\u578b\u3001\u7f16\u5199\u6e38\u620f\u903b\u8f91\u4ee3\u7801\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u548c\u4ea4\u4e92\u3001\u4ee5\u53ca\u53ef\u80fd\u7684\u6d4b\u8bd5\u548c\u8c03\u8bd5\u3002<strong>\u6d41\u884c\u7684Python\u5c0f\u6e38\u620f\u7c7b\u578b\u5305\u62ec\u8d2a\u5403\u86c7\u3001\u4fc4\u7f57\u65af\u65b9\u5757\u3001\u6253\u7816\u5757\u3001\u6587\u5b57\u5192\u9669\u6e38\u620f\u7b49<\/strong>\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u4ece\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u5b57\u6e38\u620f\uff08\u5982\u731c\u6570\u5b57\u6e38\u620f\uff09\u5f00\u59cb\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u8fd9\u9700\u8981\u719f\u6089Python\u7684\u57fa\u672c\u8bed\u6cd5\u548c\u63a7\u5236\u7ed3\u6784\uff0c\u5982\u5faa\u73af\u548c\u6761\u4ef6\u5224\u65ad\u3002\u6269\u5c55\u5230\u56fe\u5f62\u6e38\u620f\u5219\u901a\u5e38\u4f1a\u4f7f\u7528\u5982Pygame\u8fd9\u6837\u7684\u5e93\u6765\u5e2e\u52a9\u5904\u7406\u56fe\u5f62\u3001\u97f3\u6548\u548c\u6e38\u620f\u5faa\u73af\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u5b57\u6e38\u620f\uff0c\u7136\u540e\u5728\u6b64\u57fa\u7840\u4e0a\u8bf4\u660e\u5982\u4f55\u5f00\u53d1\u4e00\u4e2a\u57fa\u672c\u7684\u56fe\u5f62\u6e38\u620f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u6e38\u620f\u7c7b\u578b\u548c\u4e3b\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u6e38\u620f\u524d\uff0c\u786e\u5b9a\u6e38\u620f\u7684\u7c7b\u578b\u548c\u4e3b\u9898\u662f\u5fc5\u8981\u7684\u3002<strong>\u5bf9\u4e8e\u521d\u6b21\u5c1d\u8bd5\u7f16\u5199\u6e38\u620f\u7684\u5f00\u53d1\u8005\uff0c\u901a\u5e38\u5efa\u8bae\u4ece\u7b80\u5355\u7684\u6e38\u620f\u9879\u76ee\u5f00\u59cb<\/strong>\uff0c\u5982\u731c\u6570\u5b57\u6e38\u620f\u3001\u7eb8\u724c\u6e38\u620f\u6216\u8005\u6587\u5b57\u5192\u9669\u6e38\u620f\u3002\u9009\u62e9\u4e00\u79cd\u7c7b\u578b\u540e\uff0c\u63a5\u4e0b\u6765\u662f\u786e\u5b9a\u6e38\u620f\u7684\u5177\u4f53\u89c4\u5219\u548c\u671f\u671b\u7684\u73a9\u5bb6\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7acb\u6e38\u620f\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u786e\u5b9a\u6e38\u620f\u7c7b\u578b\u540e\uff0c\u9700\u8981\u8bbe\u7f6ePython\u5f00\u53d1\u73af\u5883\u3002<strong>\u5b89\u88c5Python\u5e76\u786e\u4fdd\u5b89\u88c5\u4e86\u6240\u6709\u5fc5\u8981\u7684\u5e93<\/strong>\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u731c\u6570\u5b57\u6e38\u620f<\/strong>\u7684\u8bdd\uff0c\u53ef\u80fd\u65e0\u9700\u989d\u5916\u7684\u5e93\uff1b<\/li>\n<li><strong>\u56fe\u5f62\u6e38\u620f<\/strong>\u5219\u53ef\u80fd\u9700\u8981Pygame\u5e93\u3002<\/li>\n<\/ul>\n<p><p>\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Pygame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u8ba1\u6e38\u620f\u903b\u8f91\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u6e38\u620f\u7684\u6838\u5fc3\u662f\u5176\u903b\u8f91\u3002\u903b\u8f91\u4ee3\u7801\u51b3\u5b9a\u4e86\u6e38\u620f\u5982\u4f55\u54cd\u5e94\u73a9\u5bb6\u7684\u8f93\u5165\u4ee5\u53ca\u6e38\u620f\u672c\u8eab\u5982\u4f55\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u6570\u5b57\u731c\u8c1c\u6e38\u620f\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u731c\u6570\u5b57\u6e38\u620f\u901a\u5e38\u6709\u4e0b\u9762\u7684\u903b\u8f91\uff1a\u7a0b\u5e8f\u751f\u6210\u4e00\u4e2a\u79d8\u5bc6\u6570\u5b57\uff0c\u7528\u6237\u5c1d\u8bd5\u731c\u8fd9\u4e2a\u6570\u5b57\uff0c\u6e38\u620f\u4f1a\u6839\u636e\u7528\u6237\u7684\u731c\u6d4b\u63d0\u4f9b\u63d0\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def guess_number_game():<\/p>\n<p>    secret_number = random.randint(1, 100)<\/p>\n<p>    user_guess = None<\/p>\n<p>    attempts = 0<\/p>\n<p>    while user_guess != secret_number:<\/p>\n<p>        user_guess = int(input(&#039;Guess the number between 1 and 100: &#039;))<\/p>\n<p>        attempts += 1<\/p>\n<p>        if user_guess &lt; secret_number:<\/p>\n<p>            print(&#039;Too low!&#039;)<\/p>\n<p>        elif user_guess &gt; secret_number:<\/p>\n<p>            print(&#039;Too high!&#039;)<\/p>\n<p>    print(f&#039;Congratulations! You guessed the right number {secret_number} in {attempts} attempts.&#039;)<\/p>\n<h2><strong>Call the game function<\/strong><\/h2>\n<p>guess_number_game()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u56fe\u5f62\u6e38\u620f\u7684\u903b\u8f91<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u5f62\u6e38\u620f\u4e2d\uff0c\u7c7b\u4f3c\u7684\u903b\u8f91\u4f1a\u88ab\u5d4c\u5165\u5230\u6e38\u620f\u5faa\u73af\u4e2d\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u4e8b\u4ef6\u76d1\u542c\uff08\u6bd4\u5982\u73a9\u5bb6\u7684\u70b9\u51fb\u6216\u952e\u76d8\u8f93\u5165\uff09\u3001\u6e38\u620f\u72b6\u6001\u7684\u66f4\u65b0\uff0c\u4ee5\u53ca\u6bcf\u4e00\u5e27\u7684\u56fe\u5f62\u6e32\u67d3\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u548c\u4ea4\u4e92<\/h3>\n<\/p>\n<p><p>\u5728\u731c\u6570\u5b57\u6e38\u620f\u4e2d\uff0c\u7528\u6237\u754c\u9762\u53ef\u80fd\u975e\u5e38\u7b80\u5355\uff0c\u5c31\u662f\u547d\u4ee4\u884c\u8f93\u5165\u548c\u8f93\u51fa\u3002\u4f46\u5728\u56fe\u5f62\u6e38\u620f\u4e2d\uff0c\u754c\u9762\u548c\u4ea4\u4e92\u5c06\u6d89\u53ca\u56fe\u5f62\u3001\u6587\u672c\u548c\u53ef\u80fd\u7684\u97f3\u9891\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>\u547d\u4ee4\u884c\u754c\u9762<\/h4>\n<\/p>\n<p><p>\u731c\u6570\u5b57\u6e38\u620f\u901a\u5e38\u4f7f\u7528\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u6765\u4e0e\u73a9\u5bb6\u4e92\u52a8\u3002\u5b83\u53ef\u4ee5<strong>\u901a\u8fc7\u7b80\u5355\u7684print\u8bed\u53e5\u548cinput\u51fd\u6570\u5b9e\u73b0<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u56fe\u5f62\u7528\u6237\u754c\u9762<\/h4>\n<\/p>\n<p><p>\u56fe\u5f62\u6e38\u620f\u9700\u8981\u4e00\u4e2a\u89c6\u89c9\u4e0a\u5438\u5f15\u4eba\u7684\u754c\u9762\u3002\u4f7f\u7528Pygame\u5e93\u53ef\u4ee5\u5904\u7406\u6e38\u620f\u7a97\u53e3\u3001\u56fe\u5f62\u3001\u52a8\u753b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import sys<\/p>\n<h2><strong>\u521d\u59cb\u5316Pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3<\/strong><\/h2>\n<p>width, height = 640, 480<\/p>\n<p>screen = pygame.display.set_mode((width, height))<\/p>\n<p>pygame.display.set_caption(&#039;Game Title&#039;)<\/p>\n<h2><strong>\u6e38\u620f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>running = True<\/p>\n<p>while running:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            running = False<\/p>\n<p>    # \u6e38\u620f\u903b\u8f91\u5904\u7406<\/p>\n<p>    # ...<\/p>\n<p>    # \u753b\u9762\u66f4\u65b0<\/p>\n<p>    pygame.display.flip()<\/p>\n<h2><strong>\u9000\u51fa\u6e38\u620f<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p>sys.exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/h3>\n<\/p>\n<p><p>\u6d4b\u8bd5\u662f\u6e38\u620f\u5f00\u53d1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u90e8\u5206\u3002<strong>\u786e\u4fdd\u6e38\u620f\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u90fd\u80fd\u987a\u5229\u8fd0\u884c\uff0c\u6ca1\u6709\u9519\u8bef\u6216bug<\/strong>\u3002\u8fdb\u884c\u6e38\u620f\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u8981\u6c42\u670b\u53cb\u6216\u793e\u533a\u6210\u5458\u53c2\u4e0e\uff0c\u83b7\u53d6\u53cd\u9988\u5e76\u4f18\u5316\u6e38\u620f\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u53d1\u5e03<\/h3>\n<\/p>\n<p><p>\u5f00\u53d1\u5b8c\u6bd5\u540e\uff0c\u53d1\u5e03\u6e38\u620f\u4f9b\u5176\u4ed6\u4eba\u73a9\u800d\u3002\u53ef\u4ee5\u9009\u62e9\u4e0a\u4f20\u5230GitHub\uff0c\u6216\u8005\u53d1\u5e03\u5728\u6e38\u620f\u8bba\u575b\u4e0a\u3002\u5bf9\u4e8e\u56fe\u5f62\u6e38\u620f\uff0c\u53ef\u80fd\u8fd8\u6d89\u53ca\u6253\u5305\u548c\u5206\u53d1\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7528Python\u7f16\u5199\u5c0f\u6e38\u620f\u662f\u4e00\u4e2a\u65e2\u6709\u8da3\u53c8\u5bcc\u6709\u6559\u80b2\u610f\u4e49\u7684\u8fc7\u7a0b\u3002<strong>\u9002\u7528\u4e8e\u521d\u5b66\u8005\u7684Python\u5c0f\u6e38\u620f\u9879\u76ee\u65e2\u80fd\u5de9\u56fa\u7f16\u7a0b\u57fa\u672c\u6982\u5ff5\uff0c\u53c8\u80fd\u63d0\u9ad8\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u7684\u80fd\u529b<\/strong>\u3002\u65e0\u8bba\u662f\u6784\u5efa\u731c\u6570\u5b57\u6e38\u620f\u8fd9\u6837\u7684\u7b80\u5355\u6587\u5b57\u6e38\u620f\u8fd8\u662f\u8bbe\u8ba1\u66f4\u590d\u6742\u7684\u56fe\u5f62\u6e38\u620f\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5fc5\u8981\u7684\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u60a8\u7684\u521b\u610f\u3002\u968f\u7740\u6280\u80fd\u7684\u63d0\u5347\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u52a0\u5165\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u8bf8\u5982\u7f51\u7edc\u73a9\u5bb6\u5bf9\u6218\u3001\u5b58\u50a8\u73a9\u5bb6\u5206\u6570\u3001\u52a8\u753b\u548c\u97f3\u6548\u6548\u679c\u7b49\uff0c\u8ba9\u6e38\u620f\u66f4\u52a0\u5b8c\u5584\u548c\u5438\u5f15\u4eba\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1: Python\u5982\u4f55\u7528\u6765\u7f16\u5199\u5c0f\u6e38\u620f\uff1f<\/strong><br \/>Python\u4f5c\u4e3a\u4e00\u79cd\u7b80\u5355\u6613\u5b66\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u7f16\u5199\u5c0f\u6e38\u620f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5404\u79cd\u6e38\u620f\u5f00\u53d1\u6846\u67b6\uff0c\u4f8b\u5982Pygame\u6216\u8005Pyglet\u3002\u8fd9\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6e38\u620f\u5f00\u53d1\u5de5\u5177\u548c\u51fd\u6570\u5e93\uff0c\u8ba9\u60a8\u80fd\u591f\u8f7b\u677e\u5730\u521b\u5efa\u4e00\u4e2a\u5c0f\u6e38\u620f\u3002<\/p>\n<p><strong>Q2: \u6211\u53ef\u4ee5\u7528Python\u7f16\u5199\u4ec0\u4e48\u6837\u7684\u5c0f\u6e38\u620f\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u7528\u4e8e\u7f16\u5199\u5404\u79cd\u7c7b\u578b\u7684\u5c0f\u6e38\u620f\uff0c\u4f8b\u5982\u8ff7\u5bab\u6e38\u620f\u3001\u5c04\u51fb\u6e38\u620f\u3001\u5361\u724c\u6e38\u620f\u7b49\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u5174\u8da3\u548c\u521b\u610f\u8bbe\u8ba1\u4e00\u4e2a\u6709\u8da3\u7684\u6e38\u620f\u73a9\u6cd5\uff0c\u7136\u540e\u4f7f\u7528Python\u6765\u5b9e\u73b0\u6e38\u620f\u903b\u8f91\u548c\u56fe\u5f62\u754c\u9762\u3002<\/p>\n<p><strong>Q3: \u6211\u9700\u8981\u54ea\u4e9b\u57fa\u7840\u77e5\u8bc6\u6765\u7528Python\u7f16\u5199\u5c0f\u6e38\u620f\uff1f<\/strong><br \/>\u8981\u7528Python\u7f16\u5199\u5c0f\u6e38\u620f\uff0c\u60a8\u9700\u8981\u638c\u63e1\u57fa\u672c\u7684Python\u7f16\u7a0b\u8bed\u6cd5\u548c\u6982\u5ff5\uff0c\u4f8b\u5982\u53d8\u91cf\u3001\u6761\u4ef6\u8bed\u53e5\u3001\u5faa\u73af\u7b49\u3002\u6b64\u5916\uff0c\u4e86\u89e3\u4e00\u4e9b\u57fa\u672c\u7684\u6e38\u620f\u5f00\u53d1\u6982\u5ff5\uff0c\u4f8b\u5982\u6e38\u620f\u5faa\u73af\u3001\u78b0\u649e\u68c0\u6d4b\u7b49\u4e5f\u4f1a\u5bf9\u60a8\u7684\u6e38\u620f\u5f00\u53d1\u6709\u6240\u5e2e\u52a9\u3002\u5982\u679c\u60a8\u662f\u521d\u5b66\u8005\uff0c\u5efa\u8bae\u5148\u5b66\u4e60Python\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u7136\u540e\u518d\u9010\u6e10\u6df1\u5165\u5b66\u4e60\u6e38\u620f\u5f00\u53d1\u76f8\u5173\u7684\u77e5\u8bc6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u7f16\u5199\u4e00\u4e2a\u5c0f\u6e38\u620f\uff0c\u60a8\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u6e38\u620f\u7c7b\u578b\u3001\u7f16\u5199\u6e38\u620f\u903b\u8f91\u4ee3\u7801\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u548c\u4ea4\u4e92\u3001\u4ee5\u53ca\u53ef\u80fd\u7684\u6d4b\u8bd5\u548c [&hellip;]","protected":false},"author":3,"featured_media":173860,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173845"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=173845"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173845\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173860"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173845"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173845"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173845"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}