{"id":928,"date":"2025-01-12T10:34:44","date_gmt":"2025-01-12T02:34:44","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=928"},"modified":"2025-01-12T10:34:44","modified_gmt":"2025-01-12T02:34:44","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e5%88%b6%e4%bd%9c%e6%96%b0%e5%b9%b4%e7%83%9f%e8%8a%b1%e6%95%88%e6%9e%9c%ef%bc%9a%e5%ae%8c%e6%95%b4%e6%95%99%e7%a8%8b%e4%b8%8e%e4%bb%a3%e7%a0%81%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/12\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e5%88%b6%e4%bd%9c%e6%96%b0%e5%b9%b4%e7%83%9f%e8%8a%b1%e6%95%88%e6%9e%9c%ef%bc%9a%e5%ae%8c%e6%95%b4%e6%95%99%e7%a8%8b%e4%b8%8e%e4%bb%a3%e7%a0%81%e7%a4%ba\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u5236\u4f5c\u65b0\u5e74\u70df\u82b1\u6548\u679c\uff1a\u5b8c\u6574\u6559\u7a0b\u4e0e\u4ee3\u7801\u793a\u4f8b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4f7f\u7528Python\u5236\u4f5c\u65b0\u5e74\u70df\u82b1\u6548\u679c\u53ef\u4ee5\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u8da3\u7684\u9879\u76ee\uff0c\u7279\u522b\u9002\u5408\u5c55\u793a\u8282\u65e5\u6c14\u6c1b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>pygame<\/code> \u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u6548\u679c\u3002\u4e0b\u9762\u662f\u5b8c\u6574\u7684\u6559\u7a0b\u4e0e\u4ee3\u7801\u793a\u4f8b\uff0c\u5e2e\u52a9\u4f60\u8f7b\u677e\u521b\u5efa\u65b0\u5e74\u70df\u82b1\u6548\u679c\u3002<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u73af\u5883\u51c6\u5907<\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86 <code>pygame<\/code> \u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pygame<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame\nimport random\nimport math\n\n# \u521d\u59cb\u5316pygame\npygame.init()\n\n# \u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f\nWIDTH, HEIGHT = 800, 600\nscreen = pygame.display.set_mode((WIDTH, HEIGHT))\npygame.display.set_caption(\"\u65b0\u5e74\u70df\u82b1\u6548\u679c\")\n\n# \u5b9a\u4e49\u989c\u8272\nBLACK = (0, 0, 0)\nWHITE = (255, 255, 255)\n\n# \u70df\u82b1\u7c7b\nclass Firework:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n        self.particles = &#91;]\n        self.num_particles = 100\n        self.exploded = False\n        self.color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))\n        self.angle = random.uniform(0, 2 * math.pi)\n        self.speed = random.uniform(2, 6)\n        self.velocity = (math.cos(self.angle) * self.speed, math.sin(self.angle) * self.speed)\n\n    def update(self):\n        if not self.exploded:\n            self.x += self.velocity&#91;0]\n            self.y += self.velocity&#91;1]\n            if random.random() &lt; 0.05:\n                self.exploded = True\n                for _ in range(self.num_particles):\n                    self.particles.append(Particle(self.x, self.y, self.color))\n        else:\n            for particle in self.particles:\n                particle.update()\n\n    def draw(self):\n        if not self.exploded:\n            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 3)\n        else:\n            for particle in self.particles:\n                particle.draw()\n\n# \u70df\u82b1\u7c92\u5b50\u7c7b\nclass Particle:\n    def __init__(self, x, y, color):\n        self.x = x\n        self.y = y\n        self.color = color\n        self.lifetime = random.randint(30, 50)\n        self.size = random.randint(2, 4)\n        angle = random.uniform(0, 2 * math.pi)\n        speed = random.uniform(1, 3)\n        self.velocity = (math.cos(angle) * speed, math.sin(angle) * speed)\n\n    def update(self):\n        self.x += self.velocity&#91;0]\n        self.y += self.velocity&#91;1]\n        self.lifetime -= 1\n\n    def draw(self):\n        if self.lifetime &gt; 0:\n            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size)\n\n# \u4e3b\u51fd\u6570\ndef main():\n    clock = pygame.time.Clock()\n    fireworks = &#91;]\n    running = True\n\n    while running:\n        screen.fill(BLACK)\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                running = False\n\n        if random.random() &lt; 0.05:\n            fireworks.append(Firework(random.randint(100, WIDTH - 100), HEIGHT))\n\n        for firework in fireworks:\n            firework.update()\n            firework.draw()\n\n        pygame.display.flip()\n        clock.tick(60)\n\n    pygame.quit()\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u89e3\u91ca<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521d\u59cb\u5316\u8bbe\u7f6e<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <code>pygame<\/code> \u6765\u521d\u59cb\u5316\u7a97\u53e3\u548c\u8bbe\u7f6e\u663e\u793a\u6a21\u5f0f\uff0c\u7a97\u53e3\u5927\u5c0f\u4e3a 800&#215;600 \u50cf\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u70df\u82b1\u7c7b (<code>Firework<\/code>)<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u4e2a\u70df\u82b1\u7684\u542f\u52a8\u4f4d\u7f6e\u3001\u989c\u8272\u3001\u901f\u5ea6\u548c\u65b9\u5411\u662f\u968f\u673a\u7684\u3002<\/li>\n\n\n\n<li>\u70df\u82b1\u901a\u8fc7 <code>particles<\/code> \u5c5e\u6027\u751f\u6210\u591a\u4e2a\u7c92\u5b50\u6a21\u62df\u7206\u70b8\u6548\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7c92\u5b50\u7c7b (<code>Particle<\/code>)<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7c92\u5b50\u901a\u8fc7\u968f\u673a\u89d2\u5ea6\u548c\u901f\u5ea6\u53d1\u5c04\uff0c\u6a21\u62df\u70df\u82b1\u7684\u6269\u6563\u6548\u679c\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u7c92\u5b50\u6709\u4e00\u5b9a\u7684\u751f\u547d\u5468\u671f\uff0c\u5f53\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6\uff0c\u7c92\u5b50\u4f1a\u6d88\u5931\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4e3b\u7a0b\u5e8f<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7a0b\u5e8f\u5faa\u73af\u4e2d\u968f\u673a\u751f\u6210\u70df\u82b1\uff0c\u5e76\u6a21\u62df\u70df\u82b1\u5347\u7a7a\u4e0e\u7206\u70b8\u8fc7\u7a0b\u3002<\/li>\n\n\n\n<li>\u6bcf\u79d2\u949f\u4f1a\u66f4\u65b0\u5e76\u7ed8\u5236\u6240\u6709\u7684\u70df\u82b1\u548c\u7c92\u5b50\u6548\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd0\u884c\u6548\u679c<\/h3>\n\n\n\n<p>\u5f53\u8fd0\u884c\u8be5\u7a0b\u5e8f\u65f6\uff0c\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u9ed1\u8272\u80cc\u666f\u4e0b\uff0c\u5076\u5c14\u6709\u70df\u82b1\u5347\u7a7a\u5e76\u7206\u70b8\uff0c\u6563\u53d1\u51fa\u4e94\u5149\u5341\u8272\u7684\u7c92\u5b50\uff0c\u6a21\u62df\u65b0\u5e74\u7684\u70df\u82b1\u6548\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5c0f\u7ed3<\/h3>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e2a\u9879\u76ee\uff0c\u4f60\u4e0d\u4ec5\u53ef\u4ee5\u638c\u63e1\u5982\u4f55\u4f7f\u7528 <code>pygame<\/code> \u521b\u5efa\u52a8\u753b\u6548\u679c\uff0c\u8fd8\u80fd\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u7269\u7406\u5b66\u539f\u7406\u6a21\u62df\u70df\u82b1\u7684\u8fd0\u52a8\u4e0e\u7206\u70b8\u3002\u5982\u679c\u4f60\u5e0c\u671b\u8fdb\u884c\u66f4\u590d\u6742\u7684\u70df\u82b1\u6548\u679c\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6539\u8fdb\u7c92\u5b50\u7684\u8fd0\u52a8\u8f68\u8ff9\u3001\u751f\u547d\u5468\u671f\u6216\u52a0\u5165\u97f3\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Python\u5236\u4f5c\u65b0\u5e74\u70df\u82b1\u6548\u679c\u53ef\u4ee5\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u8da3\u7684\u9879\u76ee\uff0c\u7279\u522b\u9002\u5408\u5c55\u793a\u8282\u65e5\u6c14\u6c1b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 pygame \u5e93 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":929,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=928"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/928\/revisions"}],"predecessor-version":[{"id":930,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/928\/revisions\/930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/929"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}