{"id":6473,"date":"2025-08-24T22:06:01","date_gmt":"2025-08-24T13:06:01","guid":{"rendered":"https:\/\/the-world-star.com\/?p=6473"},"modified":"2026-11-14T17:58:45","modified_gmt":"2026-11-14T08:58:45","slug":"the-tetris-code-an-ai-wrote-for-me","status":"publish","type":"post","link":"https:\/\/the-world-star.com\/en\/games\/the-tetris-code-an-ai-wrote-for-me-6473\/","title":{"rendered":"The Tetris Code an AI Wrote for Me"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import pygame\nimport random\n\n# \u521d\u671f\u5316\npygame.init()\nWIDTH, HEIGHT = 300, 600\nBLOCK_SIZE = 30\nCOLUMNS = WIDTH \/\/ BLOCK_SIZE\nROWS = HEIGHT \/\/ BLOCK_SIZE\nscreen = pygame.display.set_mode((WIDTH, HEIGHT))\npygame.display.set_caption(\"\u30c6\u30c8\u30ea\u30b9\u98a8\u30b2\u30fc\u30e0\")\nclock = pygame.time.Clock()\nfont = pygame.font.SysFont(\"Arial\", 24)\n\n# \u8272\ncolors = &#91;\n    (0, 0, 0),       # \u7a7a\n    (255, 0, 0),     # \u8d64\n    (0, 255, 0),     # \u7dd1\n    (0, 0, 255),     # \u9752\n    (255, 255, 0),   # \u9ec4\n    (255, 165, 0),   # \u30aa\u30ec\u30f3\u30b8\n    (128, 0, 128),   # \u7d2b\n    (0, 255, 255)    # \u30b7\u30a2\u30f3\n]\n\n# \u30d6\u30ed\u30c3\u30af\u5f62\u72b6\nshapes = &#91;\n    &#91;&#91;1, 1, 1, 1]],  # I\n    &#91;&#91;2, 2], &#91;2, 2]],  # O\n    &#91;&#91;0, 3, 0], &#91;3, 3, 3]],  # T\n    &#91;&#91;4, 0, 0], &#91;4, 4, 4]],  # L\n    &#91;&#91;0, 0, 5], &#91;5, 5, 5]],  # J\n    &#91;&#91;6, 6, 0], &#91;0, 6, 6]],  # S\n    &#91;&#91;0, 7, 7], &#91;7, 7, 0]]   # Z\n]\n\nclass Block:\n    def __init__(self):\n        self.shape = random.choice(shapes)\n        self.color = max(max(row) for row in self.shape)\n        self.x = COLUMNS \/\/ 2 - len(self.shape&#91;0]) \/\/ 2\n        self.y = 0\n\n    def rotate(self):\n        self.shape = &#91;list(row) for row in zip(*self.shape&#91;::-1])]\n\nclass Grid:\n    def __init__(self):\n        self.grid = &#91;&#91;0 for _ in range(COLUMNS)] for _ in range(ROWS)]\n\n    def is_valid(self, block):\n        for i, row in enumerate(block.shape):\n            for j, cell in enumerate(row):\n                if cell:\n                    x = block.x + j\n                    y = block.y + i\n                    if x &lt; 0 or x &gt;= COLUMNS or y &gt;= ROWS or self.grid&#91;y]&#91;x]:\n                        return False\n        return True\n\n    def place(self, block):\n        for i, row in enumerate(block.shape):\n            for j, cell in enumerate(row):\n                if cell:\n                    self.grid&#91;block.y + i]&#91;block.x + j] = block.color\n\n    def clear_lines(self):\n        cleared = 0\n        new_grid = &#91;]\n        for row in self.grid:\n            if all(cell != 0 for cell in row):\n                cleared += 1\n            else:\n                new_grid.append(row)\n        while len(new_grid) &lt; ROWS:\n            new_grid.insert(0, &#91;0 for _ in range(COLUMNS)])\n        self.grid = new_grid\n        return cleared\n\n    def draw(self):\n        for y in range(ROWS):\n            for x in range(COLUMNS):\n                color = colors&#91;self.grid&#91;y]&#91;x]]\n                pygame.draw.rect(screen, color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)\n                pygame.draw.rect(screen, (50, 50, 50), (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)\n\nclass Game:\n    def __init__(self, high_score=0):\n        self.grid = Grid()\n        self.block = Block()\n        self.score = 0\n        self.lines_cleared = 0\n        self.fall_timer = 0\n        self.fall_delay = 500\n        self.key_timer = 0\n        self.key_delay = 50\n        self.game_over = False\n        self.high_score = high_score\n\n    def update(self, dt):\n        if self.game_over:\n            return\n\n        self.fall_timer += dt\n        self.key_timer += dt\n\n        if self.fall_timer &gt; self.fall_delay:\n            self.block.y += 1\n            if not self.grid.is_valid(self.block):\n                self.block.y -= 1\n                self.grid.place(self.block)\n                cleared = self.grid.clear_lines()\n                self.score += cleared * 100\n                self.lines_cleared += cleared\n                self.block = Block()\n                if not self.grid.is_valid(self.block):\n                    self.game_over = True\n                    if self.score &gt; self.high_score:\n                        self.high_score = self.score\n                self.adjust_speed()\n            self.fall_timer = 0\n\n        keys = pygame.key.get_pressed()\n        if self.key_timer &gt; self.key_delay:\n            if keys&#91;pygame.K_DOWN]:\n                self.block.y += 1\n                if not self.grid.is_valid(self.block):\n                    self.block.y -= 1\n            if keys&#91;pygame.K_LEFT]:\n                self.block.x -= 1\n                if not self.grid.is_valid(self.block):\n                    self.block.x += 1\n            if keys&#91;pygame.K_RIGHT]:\n                self.block.x += 1\n                if not self.grid.is_valid(self.block):\n                    self.block.x -= 1\n            self.key_timer = 0\n\n    def adjust_speed(self):\n        self.fall_delay = max(100, 500 - self.lines_cleared * 10)\n\n    def draw(self):\n        self.grid.draw()\n        for i, row in enumerate(self.block.shape):\n            for j, cell in enumerate(row):\n                if cell:\n                    x = self.block.x + j\n                    y = self.block.y + i\n                    pygame.draw.rect(screen, colors&#91;self.block.color], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)\n                    pygame.draw.rect(screen, (255, 255, 255), (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)\n\n        score_text = font.render(f\"Score: {self.score}\", True, (255, 255, 255))\n        screen.blit(score_text, (10, 10))\n        hs_text = font.render(f\"High Score: {self.high_score}\", True, (255, 255, 0))\n        screen.blit(hs_text, (10, 40))\n\n        if self.game_over:\n            over_text = font.render(\"Game Over - Press Any Key\", True, (255, 0, 0))\n            screen.blit(over_text, (WIDTH \/\/ 2 - over_text.get_width() \/\/ 2, HEIGHT \/\/ 2))\n\ndef main():\n    game = Game()\n    running = True\n    while running:\n        dt = clock.tick(60)\n        screen.fill((0, 0, 0))\n\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                running = False\n            elif event.type == pygame.KEYDOWN:\n                if game.game_over:\n                    game = Game(high_score=game.high_score)\n                else:\n                    if event.key == pygame.K_UP:\n                        game.block.rotate()\n                        if not game.grid.is_valid(game.block):\n                            game.block.rotate()\n                            game.block.rotate()\n                            game.block.rotate()\n\n        game.update(dt)\n        game.draw()\n        pygame.display.flip()\n\n    pygame.quit()\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":5712,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/175.41.248.22\/?p=5726","_lightning_design_setting":{"layout":"default"},"footnotes":""},"categories":[371],"tags":[835],"class_list":["post-6473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-games","tag-gaming","en-US"],"_links":{"self":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6473","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=6473"}],"version-history":[{"count":1,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6473\/revisions"}],"predecessor-version":[{"id":6474,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6473\/revisions\/6474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media\/5712"}],"wp:attachment":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media?parent=6473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/categories?post=6473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/tags?post=6473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}