{"id":30,"date":"2024-04-30T10:52:30","date_gmt":"2024-04-30T02:52:30","guid":{"rendered":"https:\/\/www.djlcx.cn\/?p=30"},"modified":"2024-04-30T10:54:48","modified_gmt":"2024-04-30T02:54:48","slug":"pygame","status":"publish","type":"post","link":"https:\/\/www.djlcx.cn\/index.php\/2024\/04\/30\/pygame\/","title":{"rendered":"\u4f7f\u7528 Pygame \u7f16\u5199\u7b80\u5355\u6e38\u620f"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Pygame \u662f\u4e00\u4e2a\u6d41\u884c\u7684 Python \u6a21\u5757\uff0c\u7528\u4e8e\u591a\u5a92\u4f53\u5e94\u7528\uff08\u5982\u7535\u5b50\u6e38\u620f\uff09\u7684\u5f00\u53d1\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u5f62\u3001\u58f0\u97f3\u3001\u4e8b\u4ef6\u5904\u7406\u7b49\u529f\u80fd\uff0c\u4f7f\u5f97 Python \u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u5730\u521b\u5efa\u5404\u79cd\u6e38\u620f\u548c\u4ea4\u4e92\u5f0f\u5e94\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecd Pygame \u7684\u57fa\u672c\u6982\u5ff5\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u6e38\u620f\u793a\u4f8b\u6765\u5c55\u793a\u5176\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e00\u3001\u5b89\u88c5 Pygame<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684 Python \u73af\u5883\u4e2d\u5b89\u88c5 Pygame\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 pip\uff08Python \u7684\u5305\u7ba1\u7406\u5de5\u5177\uff09\u6765\u5b89\u88c5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pygame<\/code><\/pre>\n\n\n\n<p>\u4e8c\u3001Pygame \u7684\u57fa\u672c\u6982\u5ff5<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Surface<\/strong>\uff1aSurface \u662f Pygame \u7684\u57fa\u672c\u56fe\u5f62\u5355\u5143\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u7406\u89e3\u4e3a\u56fe\u50cf\u6216\u753b\u5e03\u3002\u4f8b\u5982\uff0c\u5c4f\u5e55\u662f\u4e00\u4e2a Surface\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0a\u9762\u7ed8\u5236\u56fe\u5f62\u548c\u6587\u672c\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6\uff08Event\uff09<\/strong>\uff1a\u4e8b\u4ef6\u662f\u7528\u6237\u4e0e\u7a0b\u5e8f\u4ea4\u4e92\u7684\u65b9\u5f0f\uff0c\u5982\u952e\u76d8\u8f93\u5165\u3001\u9f20\u6807\u79fb\u52a8\u548c\u70b9\u51fb\u7b49\u3002Pygame \u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e8b\u4ef6\u961f\u5217\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e2d\u83b7\u53d6\u548c\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li><strong>Rect<\/strong>\uff1aRect \u662f Pygame \u4e2d\u7684\u4e00\u4e2a\u77e9\u5f62\u7c7b\uff0c\u7528\u4e8e\u8868\u793a\u548c\u64cd\u4f5c\u77e9\u5f62\u533a\u57df\u3002\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0cRect \u5e38\u5e38\u7528\u4e8e\u8868\u793a\u89d2\u8272\u7684\u4f4d\u7f6e\u3001\u78b0\u649e\u68c0\u6d4b\u7b49\u3002<\/li>\n\n\n\n<li><strong>Sprite<\/strong>\uff1aSprite \u662f Pygame \u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u7528\u4e8e\u8868\u793a\u6e38\u620f\u4e2d\u7684\u53ef\u79fb\u52a8\u5bf9\u8c61\uff08\u5982\u89d2\u8272\u3001\u654c\u4eba\u3001\u5b50\u5f39\u7b49\uff09\u3002Sprite \u7ee7\u627f\u81ea Surface\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u50cf\u64cd\u4f5c Surface \u4e00\u6837\u64cd\u4f5c Sprite\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4e09\u3001\u7b80\u5355\u6e38\u620f\u793a\u4f8b<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 Pygame \u7f16\u5199\u7684\u7b80\u5355\u6e38\u620f\u793a\u4f8b\u2014\u2014\u4e00\u4e2a\u53ef\u4ee5\u79fb\u52a8\u7684\u5c0f\u7403\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5bfc\u5165 Pygame \u6a21\u5757\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame  \nimport sys<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li>\u521d\u59cb\u5316 Pygame\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>pygame.init()<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>\u8bbe\u7f6e\u5c4f\u5e55\u5927\u5c0f\u548c\u6807\u9898\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>screen = pygame.display.set_mode((800, 600))  pygame.display.set_caption(\"\u6e38\u620f\u793a\u4f8b\")<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>\u52a0\u8f7d\u5c0f\u7403\u56fe\u50cf\uff08\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a &#8220;ball.png&#8221; \u7684\u5c0f\u7403\u56fe\u50cf\u6587\u4ef6\uff09\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ball = pygame.image.load(\"ball.png\").convert_alpha()  ball_rect = ball.get_rect(center=(400, 300))  # \u8bbe\u7f6e\u5c0f\u7403\u7684\u521d\u59cb\u4f4d\u7f6e<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"5\">\n<li>\u8bbe\u7f6e\u6e38\u620f\u5faa\u73af\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>while True:  \n    for event in pygame.event.get():  # \u5904\u7406\u4e8b\u4ef6\u961f\u5217  \n        if event.type == pygame.QUIT:  # \u5982\u679c\u7528\u6237\u70b9\u51fb\u4e86\u5173\u95ed\u6309\u94ae  \n            pygame.quit()  # \u9000\u51fa Pygame  \n            sys.exit()  # \u9000\u51fa\u7a0b\u5e8f  \n  \n    keys = pygame.key.get_pressed()  # \u83b7\u53d6\u6309\u952e\u72b6\u6001  \n    if keys&#91;pygame.K_LEFT] and ball_rect.left > 0:  # \u5982\u679c\u6309\u4e0b\u5de6\u952e\u4e14\u5c0f\u7403\u6ca1\u6709\u78b0\u5230\u5de6\u8fb9\u754c  \n        ball_rect.move_ip(-5, 0)  # \u5c0f\u7403\u5411\u5de6\u79fb\u52a8  \n    if keys&#91;pygame.K_RIGHT] and ball_rect.right &lt; 800:  # \u5982\u679c\u6309\u4e0b\u53f3\u952e\u4e14\u5c0f\u7403\u6ca1\u6709\u78b0\u5230\u53f3\u8fb9\u754c  \n        ball_rect.move_ip(5, 0)  # \u5c0f\u7403\u5411\u53f3\u79fb\u52a8  \n    if keys&#91;pygame.K_UP] and ball_rect.top > 0:  # \u5982\u679c\u6309\u4e0b\u4e0a\u952e\u4e14\u5c0f\u7403\u6ca1\u6709\u78b0\u5230\u4e0a\u8fb9\u754c  \n        ball_rect.move_ip(0, -5)  # \u5c0f\u7403\u5411\u4e0a\u79fb\u52a8  \n    if keys&#91;pygame.K_DOWN] and ball_rect.bottom &lt; 600:  # \u5982\u679c\u6309\u4e0b\u4e0b\u952e\u4e14\u5c0f\u7403\u6ca1\u6709\u78b0\u5230\u4e0b\u8fb9\u754c  \n        ball_rect.move_ip(0, 5)  # \u5c0f\u7403\u5411\u4e0b\u79fb\u52a8  \n  \n    screen.fill((255, 255, 255))  # \u586b\u5145\u5c4f\u5e55\u4e3a\u767d\u8272  \n    screen.blit(ball, ball_rect)  # \u5728\u5c4f\u5e55\u4e0a\u7ed8\u5236\u5c0f\u7403  \n    pygame.display.flip()  # \u66f4\u65b0\u5c4f\u5e55\u663e\u793a  \n  \n    pygame.time.Clock().tick(60)  # \u63a7\u5236\u6e38\u620f\u5faa\u73af\u7684\u901f\u5ea6\u4e3a\u6bcf\u79d2 60 \u5e27<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"6\">\n<li>\u8fd0\u884c\u7a0b\u5e8f\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u56db\u3001\u603b\u7ed3<\/p>\n\n\n\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86 Pygame \u7684\u57fa\u672c\u6982\u5ff5\u548c\u7b80\u5355\u4f7f\u7528\u6559\u7a0b\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u53ef\u4ee5\u79fb\u52a8\u7684\u5c0f\u7403\u6e38\u620f\u793a\u4f8b\u5c55\u793a\u4e86 Pygame \u7684\u5f3a\u5927\u529f\u80fd\u3002\u5f53\u7136\uff0cPygame \u7684\u529f\u80fd\u8fdc\u4e0d\u6b62\u4e8e\u6b64\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u66f4\u590d\u6742\u7684\u6e38\u620f\u548c\u4ea4\u4e92\u5f0f\u5e94\u7528\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u4f60\u5bf9 Pygame \u6709\u4efb\u4f55\u7591\u95ee\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u4e0e\u6211\u8054\u7cfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pygame \u662f\u4e00\u4e2a\u6d41\u884c\u7684 Python \u6a21\u5757\uff0c\u7528\u4e8e\u591a\u5a92\u4f53\u5e94\u7528\uff08\u5982\u7535\u5b50\u6e38\u620f\uff09\u7684\u5f00\u53d1\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u5f62\u3001\u58f0\u97f3\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":1,"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/posts\/30\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.djlcx.cn\/index.php\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}