{"id":173503,"date":"2024-05-08T18:13:04","date_gmt":"2024-05-08T10:13:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173503.html"},"modified":"2024-05-08T18:13:13","modified_gmt":"2024-05-08T10:13:13","slug":"python-pygame%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173503.html","title":{"rendered":"Python pygame\u5982\u4f55\u5b89\u88c5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27045509\/dafa7d74-40ca-4dc4-9ff0-8f103547b9f4.webp\" alt=\"Python pygame\u5982\u4f55\u5b89\u88c5\" \/><\/p>\n<p><p>Python\u7684pygame\u5e93\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u3001\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u7528\u4e8e\u8bbe\u8ba1\u548c\u7f16\u5199\u89c6\u9891\u6e38\u620f\u3002<strong>\u5b89\u88c5pygame\u5f88\u7b80\u5355\uff1a\u9996\u5148\u786e\u4fdd\u4f60\u5b89\u88c5\u4e86\u6700\u65b0\u7248\u672c\u7684Python\u3001\u7136\u540e\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5668pip\u5b89\u88c5pygame<\/strong>\u3002\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install pygame<\/code>\u5373\u53ef\u3002\u4f46\u662f\u5728\u67d0\u4e9b\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u9996\u5148\u5b89\u88c5Python\u7684\u5f00\u53d1\u7248\u548c\u4e00\u4e9b\u4f9d\u8d56\u9879\uff0c\u8fd9\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fWindows\u7cfb\u7edf\uff0c\u901a\u5e38\u53ea\u9700\u8981\u7b80\u5355\u7684pip\u547d\u4ee4\u3002\u811a\u672c\u4f1a\u81ea\u52a8\u68c0\u6d4b\u7cfb\u7edf\u7684\u4f4d\u6570\uff0c\u5e76\u4e0b\u8f7d\u76f8\u5e94\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3002Windows\u7528\u6237\u53e6\u5916\u4e00\u4e2a\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u662f\u7f3a\u4e4fC\u7f16\u8bd1\u5668\uff0c\u4e0d\u8fc7pygame\u7684\u9884\u7f16\u8bd1\u5305\u5df2\u7ecf\u5305\u542b\u4e86\u6240\u6709\u5fc5\u9700\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6240\u4ee5\u901a\u5e38\u4e0d\u9700\u8981\u989d\u5916\u7684\u7f16\u8bd1\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001WINDOWS\u7cfb\u7edf\u5b89\u88c5PYGAME<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u4f60\u9700\u8981\u786e\u8ba4Python\u5df2\u7ecf\u5b89\u88c5\u5728\u4f60\u7684\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u884c\u754c\u9762\u8f93\u5165 <code>python --version<\/code> \u6216 <code>python3 --version<\/code> \u6765\u9a8c\u8bc1\u3002\u4e00\u65e6\u786e\u8ba4Python\u5df2\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216PowerShell\u3002<\/li>\n<li>\u8f93\u5165\u547d\u4ee4 <code>pip install pygame<\/code>\uff0c\u7136\u540e\u6309\u56de\u8f66\u952e\u3002<\/li>\n<li>\u7b49\u5f85\u5b89\u88c5\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u9047\u5230\u6743\u9650\u9519\u8bef\uff0c\u53ef\u80fd\u9700\u8981\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u4f7f\u7528 <code>pip install --user pygame<\/code> \u6765\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001MACOS\u7cfb\u7edf\u5b89\u88c5PYGAME<\/h3>\n<\/p>\n<p><p>\u5728macOS\u4e0a\uff0c\u5b89\u88c5\u65b9\u6cd5\u4e0eWindows\u7c7b\u4f3c\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b89\u88c5Xcode\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\u3002<\/li>\n<li>\u5982\u679c\u9700\u8981\uff0c\u5b89\u88c5Xcode\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u4f7f\u7528 <code>xcode-select --install<\/code> \u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li>\u8f93\u5165 <code>pip3 install pygame<\/code>\uff08\u6216 <code>pip install pygame<\/code>\uff09\uff0c\u53d6\u51b3\u4e8e\u4f60\u7cfb\u7edf\u4e2d\u7684pip\u7248\u672c\u3002<\/li>\n<li>\u7b49\u5f85\u5b89\u88c5\u8fc7\u7a0b\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u9047\u5230\u4efb\u4f55\u6743\u9650\u95ee\u9898\uff0c\u5c1d\u8bd5\u5728\u547d\u4ee4\u524d\u52a0\u4e0a <code>sudo<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001LINUX\u7cfb\u7edf\u5b89\u88c5PYGAME<\/h3>\n<\/p>\n<p><p>\u5728Linux\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u5b89\u88c5Python\u548cpygame\u7684\u4f9d\u8d56\u9879\uff0c\u7136\u540e\u518d\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\u3002\u8fd9\u4e00\u6b65\u9aa4\u53d6\u51b3\u4e8e\u4f60\u7684\u53d1\u884c\u7248\uff0c\u4f46\u4ee5\u4e0b\u662f\u5927\u81f4\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u7ec8\u7aef\u3002<\/li>\n<li>\u6839\u636e\u4f60\u7684Linux\u53d1\u884c\u7248\uff0c\u4f7f\u7528\u5bf9\u5e94\u7684\u5305\u7ba1\u7406\u5668\uff08\u5982apt-get\u3001yum\u7b49\uff09\u5b89\u88c5Python\u5f00\u53d1\u5de5\u5177\u548cSDL\u5e93\u3002\u4f8b\u5982\uff0c\u5728\u57fa\u4e8eDebian\u7684\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>sudo apt-get install python3 python3-pip python3-dev libsdl1.2debian libportmidi-dev<\/code>\u3002<\/li>\n<li>\u7ee7\u7eed\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165 <code>pip3 install pygame<\/code>\u3002<\/li>\n<li>\u7b49\u5f85\u5b89\u88c5\u5b8c\u6210\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u89e3\u51b3POTENTIAL ISSUES<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5b89\u88c5\u8fc7\u7a0b\u901a\u5e38\u5f88\u987a\u5229\uff0c\u4f46\u6709\u65f6\u4e5f\u4f1a\u9047\u5230\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u5e38\u89c1\u95ee\u9898\u548c\u89e3\u51b3\u529e\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f9d\u8d56\u6027\u95ee\u9898<\/strong>\uff1a\u786e\u4fdd\u6240\u6709pygame\u4f9d\u8d56\u7684\u5e93\u90fd\u5df2\u7ecf\u5b89\u88c5\u3002\u5728Linux\u4e0a\uff0c\u8fd9\u901a\u5e38\u6d89\u53ca\u4f7f\u7528\u7cfb\u7edf\u7684\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u989d\u5916\u7684\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7248\u672c\u517c\u5bb9\u6027\u95ee\u9898<\/strong>\uff1a\u786e\u4fddpip\u3001Python\u548cpygame\u7248\u672c\u4e4b\u95f4\u517c\u5bb9\u3002\u5982\u679c\u9047\u5230\u5177\u4f53\u7684\u9519\u8bef\u6d88\u606f\uff0c\u8bf7\u67e5\u9605\u76f8\u5173\u7684\u6587\u6863\u6216\u793e\u533a\u8bba\u575b\u5bfb\u6c42\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6743\u9650\u95ee\u9898<\/strong>\uff1a\u5728\u5b89\u88c5\u8f6f\u4ef6\u5305\u65f6\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u5728Windows\u4e0a\uff0c\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\u4e3a\u7ba1\u7406\u5458\u3002\u5728macOS\u548cLinux\u4e0a\uff0c\u524d\u9762\u52a0\u4e0a <code>sudo<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f51\u7edc\u95ee\u9898<\/strong>\uff1a\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cpip\u955c\u50cf\u6e90\u53ef\u80fd\u5728\u7279\u5b9a\u5730\u533a\u8bbf\u95ee\u53d7\u9650\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u66f4\u6362pip\u6e90\u5230\u5176\u4ed6\u53ef\u9760\u7684\u6e90\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\u548c\u63d0\u793a\uff0c\u5927\u591a\u6570\u7528\u6237\u5e94\u8be5\u80fd\u591f\u4e0d\u53d7\u963b\u788d\u5730\u5b89\u88c5pygame\uff0c\u8fdb\u800c\u5f00\u59cb\u4ed6\u4eec\u7684\u6e38\u620f\u5f00\u53d1\u4e4b\u65c5\u3002\u5982\u679c\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u9047\u5230\u56f0\u96be\uff0c\u4e0d\u59a8\u53c2\u8003\u5728\u7ebf\u793e\u533a\u6216\u5b98\u65b9\u6587\u6863\uff0c\u90a3\u91cc\u6709\u7740\u4e30\u5bcc\u7684\u6559\u7a0b\u548c\u89e3\u51b3\u65b9\u6848\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u987a\u5229\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q1\uff1a\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5Pygame\u6a21\u5757\uff1f<\/strong><br \/>\nA1\uff1a\u60f3\u8981\u5728Python\u4e2d\u5b89\u88c5Pygame\u6a21\u5757\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u4ece\u5b98\u65b9\u7f51\u7ad9\u4e0a\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/li>\n<li>\u5176\u6b21\uff0c\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u7a97\u53e3\uff0c\u5e76\u901a\u8fc7\u4f7f\u7528pip\u6216conda\u547d\u4ee4\u6765\u5b89\u88c5Pygame\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u4f7f\u7528pip\u5b89\u88c5\u53ef\u4ee5\u8f93\u5165\u547d\u4ee4<code>pip install pygame<\/code>\uff0c\u800c\u4f7f\u7528conda\u5b89\u88c5\u53ef\u4ee5\u8f93\u5165\u547d\u4ee4<code>conda install -c cogsci pygame<\/code>\u3002<\/li>\n<li>\u7a0d\u7b49\u7247\u523b\uff0c\u5b89\u88c5\u8fc7\u7a0b\u4f1a\u81ea\u52a8\u8fdb\u884c\uff0c\u7b49\u5f85\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5f00\u59cb\u4f7f\u7528Pygame\u6a21\u5757\u4e86\u3002<\/li>\n<\/ol>\n<p><strong>Q2\uff1a\u5728Windows\u4e0a\u5b89\u88c5Pygame\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\nA2\uff1a\u5982\u679c\u4f60\u60f3\u5728Windows\u4e0a\u5b89\u88c5Pygame\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u89e3\u91ca\u5668\uff0c\u53ef\u4ee5\u4ece\u5b98\u65b9\u7f51\u7ad9\u4e0a\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/li>\n<li>\u5176\u6b21\uff0c\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u7a97\u53e3\uff0c\u8fdb\u5165\u5230Python\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684Scripts\u6587\u4ef6\u5939\u4e2d\uff0c\u4f8b\u5982\uff1a<code>C:\\PythonXX\\Scripts<\/code>\uff0c\u8fd9\u91cc\u7684<code>XX<\/code>\u4ee3\u8868\u4f60\u5b89\u88c5\u7684Python\u7248\u672c\u53f7\u3002<\/li>\n<li>\u7136\u540e\uff0c\u8f93\u5165\u547d\u4ee4<code>pip install pygame<\/code>\uff0c\u6309\u4e0b\u56de\u8f66\u952e\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<li>\u7a0d\u7b49\u7247\u523b\uff0c\u5b89\u88c5\u8fc7\u7a0b\u4f1a\u81ea\u52a8\u8fdb\u884c\uff0c\u7b49\u5f85\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5f00\u59cb\u4f7f\u7528Pygame\u6a21\u5757\u4e86\u3002<\/li>\n<\/ol>\n<p><strong>Q3\uff1a\u6211\u5e94\u8be5\u5982\u4f55\u9a8c\u8bc1Pygame\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1f<\/strong><br \/>\nA3\uff1a\u5982\u679c\u4f60\u60f3\u9a8c\u8bc1Pygame\u5b89\u88c5\u662f\u5426\u6210\u529f\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u6253\u5f00Python\u89e3\u91ca\u5668\uff0c\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u7a97\u53e3\u4e2d\u8f93\u5165<code>import pygame<\/code>\uff0c\u5e76\u6309\u4e0b\u56de\u8f66\u952e\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709\u51fa\u73b0\u4efb\u4f55\u9519\u8bef\u63d0\u793a\u4fe1\u606f\uff0c\u90a3\u4e48\u8bf4\u660ePygame\u6a21\u5757\u5df2\u7ecf\u6210\u529f\u5b89\u88c5\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u7f16\u5199\u4e00\u4e9bPygame\u4ee3\u7801\uff0c\u5e76\u8fd0\u884c\u770b\u770b\u662f\u5426\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\u5e76\u663e\u793a\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u3002<\/li>\n<\/ol>\n<p>\u5e0c\u671b\u4e0a\u8ff0\u89e3\u7b54\u80fd\u591f\u5e2e\u52a9\u4f60\u987a\u5229\u5b89\u88c5\u548c\u4f7f\u7528Pygame\u6a21\u5757\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7684pygame\u5e93\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u3001\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u7528\u4e8e\u8bbe\u8ba1\u548c\u7f16\u5199\u89c6\u9891\u6e38\u620f\u3002\u5b89\u88c5pygame\u5f88 [&hellip;]","protected":false},"author":3,"featured_media":173514,"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\/173503"}],"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=173503"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173503\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173514"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}