{"id":921439,"date":"2024-12-26T14:12:50","date_gmt":"2024-12-26T06:12:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/921439.html"},"modified":"2024-12-26T14:12:51","modified_gmt":"2024-12-26T06:12:51","slug":"python%e5%a6%82%e4%bd%95%e5%ae%89%e8%a3%85pygame","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/921439.html","title":{"rendered":"python\u5982\u4f55\u5b89\u88c5pygame"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210410\/dc9b751f-0b7a-48fb-ac39-2cb3ef08090f.webp\" alt=\"python\u5982\u4f55\u5b89\u88c5pygame\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5b89\u88c5Pygame\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\u3001\u786e\u8ba4Python\u548cpip\u7684\u6b63\u786e\u5b89\u88c5\u3001\u68c0\u67e5Pygame\u7684\u5b89\u88c5\u6210\u529f\u4e0e\u5426<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u8fdb\u884c\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5<\/p>\n<\/p>\n<p><p>\u5b89\u88c5Pygame\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7Python\u81ea\u5e26\u7684\u5305\u7ba1\u7406\u5668pip\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u4e86Python\u4ee5\u53capip\u3002\u7136\u540e\uff0c\u6253\u5f00\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u547d\u4ee4\u5c06\u81ea\u52a8\u4ecePython\u7684\u5b98\u65b9\u5305\u7d22\u5f15PyPI\u4e2d\u4e0b\u8f7d\u5e76\u5b89\u88c5Pygame\u3002\u5982\u679c\u5b89\u88c5\u6210\u529f\uff0c\u4f60\u5c06\u4f1a\u770b\u5230\u4e00\u6761\u6210\u529f\u5b89\u88c5\u7684\u6d88\u606f\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u7ba1\u7406\u5458\u6743\u9650\u6765\u8fd0\u884c\u6b64\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u786e\u8ba4Python\u548cpip\u7684\u6b63\u786e\u5b89\u88c5<\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5Pygame\u4e4b\u524d\uff0c\u786e\u4fddPython\u548cpip\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\uff0c\u5e76\u4e14\u53ef\u4ee5\u88ab\u7cfb\u7edf\u8bc6\u522b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u68c0\u67e5Python\u5b89\u88c5\uff1a\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\uff0c\u4ee5\u786e\u4fddPython\u5df2\u5b89\u88c5\u4e14\u7248\u672c\u7b26\u5408Pygame\u7684\u8981\u6c42\uff08\u901a\u5e38\u662fPython 3.x\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5pip\u5b89\u88c5\uff1a\u8f93\u5165<code>pip --version<\/code>\u6765\u786e\u8ba4pip\u5df2\u7ecf\u5b89\u88c5\uff0c\u5e76\u4e14\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u3002pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u5305\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u6ca1\u6709\u5b89\u88c5Python\u6216pip\uff0c\u4f60\u53ef\u4ee5\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3002\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u5c06\u5e2e\u52a9\u4f60\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528Python\u548cpip\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u68c0\u67e5Pygame\u7684\u5b89\u88c5\u6210\u529f\u4e0e\u5426<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fddPygame\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u5728Python\u73af\u5883\u4e2d\u5bfc\u5165Pygame\u5e76\u8fd0\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00Python\u89e3\u91ca\u5668\uff1a\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\u4ee5\u542f\u52a8Python\u89e3\u91ca\u5668\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\u4ee5\u6d4b\u8bd5Pygame\u7684\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>pygame.init()<\/p>\n<p>print(&quot;Pygame installed successfully!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u4e14\u8f93\u51fa\u201cPygame installed successfully!\u201d\u5b57\u6837\uff0c\u8bf4\u660ePygame\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u7684\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u529e\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5Pygame\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u53ef\u80fd\u7684\u89e3\u51b3\u529e\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f51\u7edc\u95ee\u9898<\/strong>\uff1a\u5982\u679c\u7f51\u7edc\u8fde\u63a5\u4e0d\u7a33\u5b9a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b89\u88c5\u5931\u8d25\u3002\u53ef\u4ee5\u5c1d\u8bd5\u591a\u6b21\u5b89\u88c5\u6216\u8005\u4f7f\u7528\u5176\u4ed6\u7f51\u7edc\u73af\u5883\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6743\u9650\u95ee\u9898<\/strong>\uff1a\u6709\u65f6\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u624d\u80fd\u5b89\u88c5\u8f6f\u4ef6\u5305\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u547d\u4ee4\u524d\u52a0\u4e0a<code>sudo<\/code>\uff08\u5728Linux\u548cmacOS\u4e2d\uff09\u6216\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\uff08\u5728Windows\u4e2d\uff09\u6765\u89e3\u51b3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u517c\u5bb9\u6027\u95ee\u9898<\/strong>\uff1a\u786e\u4fddPython\u7248\u672c\u4e0ePygame\u7248\u672c\u517c\u5bb9\uff0c\u901a\u5e38\u4f7f\u7528\u6700\u65b0\u7684Python 3\u7248\u672c\u6765\u5b89\u88c5Pygame\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u73af\u5883\u53d8\u91cf\u95ee\u9898<\/strong>\uff1a\u5982\u679cPython\u6216pip\u7684\u8def\u5f84\u6ca1\u6709\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u4ee4\u65e0\u6cd5\u8bc6\u522b\u3002\u53ef\u4ee5\u624b\u52a8\u5c06Python\u548cpip\u7684\u8def\u5f84\u6dfb\u52a0\u5230\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4e3a\u4e0d\u540c\u5e73\u53f0\u5b89\u88c5Pygame<\/p>\n<\/p>\n<p><p>Pygame\u7684\u5b89\u88c5\u8fc7\u7a0b\u5728\u4e0d\u540c\u7684\u5e73\u53f0\u4e0a\u53ef\u80fd\u6709\u6240\u4e0d\u540c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows\u5e73\u53f0<\/strong>\uff1a\u76f4\u63a5\u901a\u8fc7\u547d\u4ee4\u63d0\u793a\u7b26\u4f7f\u7528<code>pip install pygame<\/code>\u5373\u53ef\u3002\u5982\u679c\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4e0b\u8f7dPygame\u7684Windows\u5b89\u88c5\u5305\uff08.whl\u6587\u4ef6\uff09\u8fdb\u884c\u624b\u52a8\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>macOS\u5e73\u53f0<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528Homebrew\u6765\u5b89\u88c5Python\uff0c\u7136\u540e\u4f7f\u7528pip\u5b89\u88c5Pygame\u3002\u786e\u4fddXcode\u547d\u4ee4\u884c\u5de5\u5177\u5df2\u5b89\u88c5\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u80fd\u662f\u5fc5\u8981\u7684\u4f9d\u8d56\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Linux\u5e73\u53f0<\/strong>\uff1a\u901a\u5e38\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edf\u81ea\u5e26\u7684\u5305\u7ba1\u7406\u5668\uff08\u5982apt\u3001yum\u7b49\uff09\u6765\u5b89\u88c5Python\u548cpip\uff0c\u7136\u540e\u901a\u8fc7pip\u5b89\u88c5Pygame\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u66f4\u65b0\u548c\u5378\u8f7dPygame<\/p>\n<\/p>\n<ol>\n<li><strong>\u66f4\u65b0Pygame<\/strong>\uff1a\u4e3a\u4e86\u4fdd\u6301Pygame\u7684\u6700\u65b0\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u66f4\u65b0\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-shell\">pip install --upgrade pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5378\u8f7dPygame<\/strong>\uff1a\u5982\u679c\u9700\u8981\u5378\u8f7dPygame\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-shell\">pip uninstall pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u4f1a\u4ece\u4f60\u7684\u7cfb\u7edf\u4e2d\u79fb\u9664Pygame\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528Pygame\u521b\u5efa\u7b80\u5355\u6e38\u620f<\/p>\n<\/p>\n<p><p>\u5b89\u88c5Pygame\u540e\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6e38\u620f\u6765\u719f\u6089\u5b83\u7684\u57fa\u672c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/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\u5927\u5c0f<\/strong><\/h2>\n<p>screen = pygame.display.set_mode((400, 300))<\/p>\n<p>pygame.display.set_caption(&#39;Simple Game&#39;)<\/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>    # \u586b\u5145\u80cc\u666f<\/p>\n<p>    screen.fill((255, 255, 255))<\/p>\n<p>    # \u66f4\u65b0\u5c4f\u5e55<\/p>\n<p>    pygame.display.flip()<\/p>\n<h2><strong>\u9000\u51faPygame<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p>sys.exit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u521b\u5efa\u4e86\u4e00\u4e2a\u7a97\u53e3\uff0c\u5e76\u5728\u5176\u4e2d\u8fd0\u884c\u4e00\u4e2a\u4e3b\u5faa\u73af\uff0c\u76f4\u5230\u7528\u6237\u5173\u95ed\u7a97\u53e3\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5f00\u59cb\u63a2\u7d22Pygame\u7684\u66f4\u591a\u529f\u80fd\uff0c\u4f8b\u5982\u52a0\u8f7d\u56fe\u50cf\u3001\u5904\u7406\u7528\u6237\u8f93\u5165\u3001\u5b9e\u73b0\u7b80\u5355\u7684\u52a8\u753b\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u68c0\u67e5\u6211\u7684Python\u7248\u672c\u4ee5\u786e\u4fdd\u4e0epygame\u517c\u5bb9\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u60a8\u7684Python\u7248\u672c\uff0c\u60a8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\uff08Windows\uff09\u6216\u7ec8\u7aef\uff08macOS\/Linux\uff09\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>python --version<\/code> \u6216 <code>python3 --version<\/code>\u3002pygame\u901a\u5e38\u4e0ePython 3.x\u517c\u5bb9\uff0c\u786e\u4fdd\u60a8\u4f7f\u7528\u7684\u662f\u6700\u65b0\u7248\u672c\u7684Python\u4ee5\u83b7\u5f97\u6700\u4f73\u6027\u80fd\u548c\u529f\u80fd\u3002<\/p>\n<p><strong>\u5b89\u88c5pygame\u65f6\u9047\u5230\u9519\u8bef\uff0c\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u5728\u5b89\u88c5pygame\u65f6\u9047\u5230\u9519\u8bef\uff0c\u9996\u5148\u68c0\u67e5\u60a8\u7684\u7f51\u7edc\u8fde\u63a5\u662f\u5426\u7a33\u5b9a\uff0c\u5e76\u786e\u4fdd\u60a8\u4f7f\u7528\u7684pip\u7248\u672c\u662f\u6700\u65b0\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install --upgrade pip<\/code>\u6765\u66f4\u65b0pip\u3002\u6b64\u5916\uff0c\u67e5\u770b\u9519\u8bef\u4fe1\u606f\u4e2d\u7684\u63d0\u793a\uff0c\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684\u4f9d\u8d56\u9879\u6216\u5de5\u5177\u94fe\uff0c\u5982Microsoft Visual C++ Build Tools\uff08\u5bf9\u4e8eWindows\u7528\u6237\uff09\u6216\u76f8\u5173\u7684\u7f16\u8bd1\u5668\uff08\u5bf9\u4e8eLinux\u7528\u6237\uff09\u3002<\/p>\n<p><strong>\u5728\u5b89\u88c5pygame\u540e\uff0c\u5982\u4f55\u9a8c\u8bc1\u5b83\u662f\u5426\u6210\u529f\u5b89\u88c5\uff1f<\/strong><br \/>\u5b89\u88c5pygame\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00Python\u89e3\u91ca\u5668\u5e76\u8f93\u5165<code>import pygame<\/code>\u6765\u9a8c\u8bc1\u5b89\u88c5\u3002\u5982\u679c\u6ca1\u6709\u9519\u8bef\u63d0\u793a\uff0c\u5219\u8868\u793apygame\u5b89\u88c5\u6210\u529f\u3002\u60a8\u8fd8\u53ef\u4ee5\u8fd0\u884c<code>pygame.version.ver<\/code>\u6765\u67e5\u770bpygame\u7684\u7248\u672c\u4fe1\u606f\uff0c\u786e\u4fdd\u5b83\u6b63\u786e\u5b89\u88c5\u5e76\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5b89\u88c5Pygame\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\u3001\u786e\u8ba4Python\u548cpip\u7684\u6b63\u786e\u5b89\u88c5\u3001 [&hellip;]","protected":false},"author":3,"featured_media":921441,"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\/921439"}],"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=921439"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/921439\/revisions"}],"predecessor-version":[{"id":921442,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/921439\/revisions\/921442"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/921441"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=921439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=921439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=921439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}