{"id":6950,"date":"2025-08-31T18:13:11","date_gmt":"2025-08-31T09:13:11","guid":{"rendered":"https:\/\/the-world-star.com\/?p=6950"},"modified":"2026-11-28T14:12:22","modified_gmt":"2026-11-28T05:12:22","slug":"writing-game-code-with-ai-part-2","status":"publish","type":"post","link":"https:\/\/the-world-star.com\/en\/games\/writing-game-code-with-ai-part-2-6950\/","title":{"rendered":"Writing Game Code with AI: Part 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This is based on the original article. The code from the very end of that article is posted here.<\/p>\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\t\t\t<div class=\"blog-card\">\n\t\t\t\t<div class=\"blog-card-body-outer\">\n\t\t\t\t\t<div class=\"blog-card-body\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t<h5 class=\"blog-card-title\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/the-world-star.com\/en\/games\/ai-programming-building-a-game-part-2-6955\/\">AI Programming: Building a Game Part 2<\/a>\n\t\t\t\t\t\t\t<\/h5>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p class=\"blog-card-text\">\n\t\t\t\t\t\t\t\tToday, I&#039;m working on Ubuntu\/Linux. While both Copilot and Gemini have dedicated apps, you can easily use them right in your web browser by searching and loggi\u2026\t\t\t\t\t\t\t<\/p>\n\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"blog-card-site-title\">\n\t\t\t\t\t\t\t<a href=\"http:\/\/the-world-star.com\/en\/\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tThe\u2606World Star\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<div class=\"blog-card-image-outer\">\n\t\t\t\t\t\t<a href=\"https:\/\/the-world-star.com\/en\/games\/ai-programming-building-a-game-part-2-6955\/\" class=\"blog-card-image-frame\">\n\t\t\t\t\t\t\t<img decoding=\"async\" class=\"blog-card-image-src\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2025\/08\/31165710\/image-13.png\" alt=\"\">\n\t\t\t\t\t\t<\/a>\n\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\n\t\t\t<\/div><\/figure>\n\n\n<p class=\"wp-block-paragraph\"><br>You can save this code into a text file as a \".py\" file, install Python, and run it from your terminal using the command: python filename.py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame\nimport sys\nimport random\n\n# ===== \u5b9a\u6570 =====\nWIDTH, HEIGHT = 800, 600\nWHITE = (255, 255, 255)\nBLUE = (0, 150, 255)\nRED = (255, 100, 100)\nBLACK = (0, 0, 0)\nGREEN = (100, 255, 100)\nYELLOW = (255, 255, 100)\n\nMAX_BALLS = 3  # \u753b\u9762\u4e0a\u306e\u30dc\u30fc\u30eb\u4e0a\u9650\n\n# ===== Paddle =====\nclass Paddle:\n    def __init__(self):\n        self.width = 100\n        self.height = 15\n        self.rect = pygame.Rect(WIDTH\/\/2 - self.width\/\/2, HEIGHT - 50, self.width, self.height)\n        self.speed = 7\n        self.expand_time = 0\n\n    def move(self, keys):\n        if keys&#91;pygame.K_LEFT] and self.rect.left &gt; 0:\n            self.rect.x -= self.speed\n        if keys&#91;pygame.K_RIGHT] and self.rect.right &lt; WIDTH:\n            self.rect.x += self.speed\n\n    def expand(self):\n        self.width += 40\n        self.rect.width = self.width\n        self.expand_time = pygame.time.get_ticks()\n\n    def update(self):\n        if self.expand_time and pygame.time.get_ticks() - self.expand_time &gt; 5000:\n            self.width = 100\n            self.rect.width = self.width\n            self.expand_time = 0\n\n    def draw(self, screen):\n        pygame.draw.rect(screen, BLUE, self.rect)\n\n# ===== Ball =====\nclass Ball:\n    def __init__(self, x=None, y=None, speed_x=4, speed_y=-4):\n        self.size = 15\n        self.rect = pygame.Rect(x if x else WIDTH\/\/2, y if y else HEIGHT\/\/2, self.size, self.size)\n        self.speed_x = speed_x\n        self.speed_y = speed_y\n\n    def move(self):\n        self.rect.x += self.speed_x\n        self.rect.y += self.speed_y\n\n        if self.rect.left &lt;= 0 or self.rect.right &gt;= WIDTH:\n            self.speed_x *= -1\n        if self.rect.top &lt;= 0:\n            self.speed_y *= -1\n\n    def bounce_paddle(self, paddle):\n        if self.rect.colliderect(paddle.rect):\n            offset = (self.rect.centerx - paddle.rect.centerx) \/ (paddle.rect.width \/ 2)\n            self.speed_x = offset * 5\n            self.speed_y *= -1\n\n    def bounce_block(self):\n        self.speed_y *= -1\n\n    def reset_position(self):\n        self.rect.x = WIDTH\/\/2\n        self.rect.y = HEIGHT\/\/2\n        self.speed_x = 4\n        self.speed_y = -4\n\n    def draw(self, screen):\n        pygame.draw.ellipse(screen, WHITE, self.rect)\n\n# ===== Block =====\nclass Block:\n    def __init__(self, x, y, w, h, block_type=\"normal\"):\n        self.rect = pygame.Rect(x, y, w, h)\n        self.type = block_type  # \"normal\", \"powerup\", \"multiball\"\n\n    def draw(self, screen):\n        color = RED\n        if self.type == \"powerup\":\n            color = GREEN\n        elif self.type == \"multiball\":\n            color = YELLOW\n        pygame.draw.rect(screen, color, self.rect)\n\n# ===== PowerUp =====\nclass PowerUp:\n    def __init__(self, x, y):\n        self.size = 20\n        self.rect = pygame.Rect(x, y, self.size, self.size)\n        self.speed = 4\n\n    def move(self):\n        self.rect.y += self.speed\n\n    def draw(self, screen):\n        pygame.draw.rect(screen, GREEN, self.rect)\n\n# ===== Game =====\nclass Game:\n    def __init__(self):\n        pygame.init()\n        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))\n        pygame.display.set_caption(\"\u30d6\u30ed\u30c3\u30af\u5d29\u3057 OOP\u7248\")\n        self.clock = pygame.time.Clock()\n        self.font = pygame.font.SysFont(None, 36)\n\n        # \u52b9\u679c\u97f3\u30fbBGM\n        try:\n            pygame.mixer.music.load(\"bgm.mp3\")  # \u9069\u5f53\u306aBGM\u30d5\u30a1\u30a4\u30eb\n            pygame.mixer.music.play(-1)\n            self.sound_block = pygame.mixer.Sound(\"block.wav\")\n            self.sound_powerup = pygame.mixer.Sound(\"powerup.wav\")\n            self.sound_multiball = pygame.mixer.Sound(\"multiball.wav\")\n        except:\n            self.sound_block = self.sound_powerup = self.sound_multiball = None\n\n        self.paddle = Paddle()\n        self.balls = &#91;Ball()]\n        self.blocks = self.create_blocks()\n        self.powerups = &#91;]\n\n        self.score = 0\n        self.lives = 3\n        self.stage = 1\n        self.game_over = False\n\n    def create_blocks(self):\n        blocks = &#91;]\n        rows, cols = 5, 8\n        block_width = WIDTH \/\/ cols\n        block_height = 30\n        for row in range(rows):\n            for col in range(cols):\n                # \u30e9\u30f3\u30c0\u30e0\u3067\u7279\u6b8a\u30d6\u30ed\u30c3\u30af\n                r = random.random()\n                if r &lt; 0.1:\n                    btype = \"powerup\"\n                elif r &lt; 0.2:\n                    btype = \"multiball\"\n                else:\n                    btype = \"normal\"\n                blocks.append(Block(col * block_width, row * block_height + 50, block_width - 2, block_height - 2, btype))\n        return blocks\n\n    def reset_game(self):\n        self.score = 0\n        self.lives = 3\n        self.stage = 1\n        self.blocks = self.create_blocks()\n        self.powerups.clear()\n        self.paddle = Paddle()\n        self.balls = &#91;Ball()]\n        self.game_over = False\n\n    def update(self):\n        keys = pygame.key.get_pressed()\n        self.paddle.move(keys)\n        self.paddle.update()\n\n        for ball in self.balls&#91;:]:\n            ball.move()\n            ball.bounce_paddle(self.paddle)\n\n            # \u30dc\u30fc\u30eb\u843d\u4e0b\n            if ball.rect.bottom &gt;= HEIGHT:\n                self.balls.remove(ball)\n                if not self.balls:\n                    self.lives -= 1\n                    if self.lives &gt; 0:\n                        self.balls = &#91;Ball()]\n                        self.paddle = Paddle()\n                    else:\n                        self.game_over = True\n                continue\n\n            # \u30d6\u30ed\u30c3\u30af\u885d\u7a81\n            hit_index = ball.rect.collidelist(&#91;b.rect for b in self.blocks])\n            if hit_index != -1:\n                hit_block = self.blocks.pop(hit_index)\n                ball.bounce_block()\n                self.score += 100\n                if self.sound_block: self.sound_block.play()\n\n                if hit_block.type == \"powerup\":\n                    self.powerups.append(PowerUp(hit_block.rect.centerx, hit_block.rect.centery))\n                elif hit_block.type == \"multiball\":\n                    if self.sound_multiball:\n                        self.sound_multiball.play()\n\n                    # \u751f\u6210\u5019\u88dc\uff082\u7403\uff09\n                    spawn_candidates = &#91;\n                        Ball(hit_block.rect.centerx, hit_block.rect.centery, speed_x=4, speed_y=-4),\n                        Ball(hit_block.rect.centerx, hit_block.rect.centery, speed_x=-4, speed_y=-4)\n                    ]\n\n                    # \u4e0a\u9650\u3092\u8d85\u3048\u306a\u3044\u5206\u3060\u3051\u8ffd\u52a0\n                    for new_ball in spawn_candidates:\n                        if len(self.balls) &lt; MAX_BALLS:\n                            self.balls.append(new_ball)\n\n\n        # \u30d1\u30ef\u30fc\u30a2\u30c3\u30d7\u51e6\u7406\n        for p in self.powerups&#91;:]:\n            p.move()\n            if p.rect.colliderect(self.paddle.rect):\n                self.paddle.expand()\n                if self.sound_powerup: self.sound_powerup.play()\n                self.powerups.remove(p)\n            elif p.rect.top &gt; HEIGHT:\n                self.powerups.remove(p)\n\n        # \u30b9\u30c6\u30fc\u30b8\u30af\u30ea\u30a2\n        if not self.blocks:\n            self.stage += 1\n            for ball in self.balls:\n                ball.speed_x *= 1.1\n                ball.speed_y *= 1.1\n            self.blocks = self.create_blocks()\n            \n            # \u30af\u30ea\u30a2\u5f8c\u306f\u30dc\u30fc\u30eb\u3092\u30d1\u30c9\u30eb\u4f4d\u7f6e\u306b\u623b\u3059\n            for ball in self.balls:\n                ball.rect.centerx = self.paddle.rect.centerx\n                ball.rect.bottom = self.paddle.rect.top - 1\n                ball.speed_x = 4\n                ball.speed_y = -4\n        \n    def draw(self):\n        self.screen.fill(BLACK)\n        self.paddle.draw(self.screen)\n        for ball in self.balls:\n            ball.draw(self.screen)\n        for block in self.blocks:\n            block.draw(self.screen)\n        for p in self.powerups:\n            p.draw(self.screen)\n\n        score_text = self.font.render(f\"Score: {self.score}  Stage: {self.stage}  Lives: {self.lives}\", True, WHITE)\n        self.screen.blit(score_text, (10, 10))\n\n        if self.game_over:\n            over_text = self.font.render(\"GAME OVER - Press SPACE to Restart\", True, WHITE)\n            self.screen.blit(over_text, (WIDTH\/\/2 - over_text.get_width()\/\/2, HEIGHT\/\/2))\n\n        pygame.display.flip()\n\n    def run(self):\n        while True:\n            # ===== 1. \u30a4\u30d9\u30f3\u30c8\u51e6\u7406 =====\n            for event in pygame.event.get():\n                if event.type == pygame.QUIT:\n                    pygame.quit()\n                    sys.exit()\n                # \u30b2\u30fc\u30e0\u30aa\u30fc\u30d0\u30fc\u6642\u306b\u30b9\u30da\u30fc\u30b9\u3067\u518d\u30b9\u30bf\u30fc\u30c8\n                if self.game_over and event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:\n                    self.reset_game()\n\n            # ===== 2. \u30b2\u30fc\u30e0\u72b6\u614b\u66f4\u65b0 =====\n            if not self.game_over:\n                self.update()\n\n            # ===== 3. \u63cf\u753b =====\n            self.draw()\n\n            # ===== 4. \u30d5\u30ec\u30fc\u30e0\u30ec\u30fc\u30c8\u5236\u5fa1 =====\n            self.clock.tick(60)\n\n\n# ===== \u5b9f\u884c =====\nif __name__ == \"__main__\":\n    Game().run()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is based on the original article. The code from the very end of that article is posted here. You can save this code into a text file as a \".py\" file, install Python, and run it from your terminal using the command: python filename.py<\/p>\n","protected":false},"author":1,"featured_media":5769,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/175.41.248.22\/?p=5773","_lightning_design_setting":{"layout":"default"},"footnotes":""},"categories":[371],"tags":[860],"class_list":["post-6950","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-games","tag-games","en-US"],"_links":{"self":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/comments?post=6950"}],"version-history":[{"count":2,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6950\/revisions"}],"predecessor-version":[{"id":6952,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6950\/revisions\/6952"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media\/5769"}],"wp:attachment":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media?parent=6950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/categories?post=6950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/tags?post=6950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}