{"id":188,"date":"2026-06-16T19:53:56","date_gmt":"2026-06-16T11:53:56","guid":{"rendered":"https:\/\/weiyu2025.top\/?p=188"},"modified":"2026-06-16T19:54:27","modified_gmt":"2026-06-16T11:54:27","slug":"%e8%b4%aa%e5%90%83%e8%9b%87%ef%bc%88python%ef%bc%89","status":"publish","type":"post","link":"https:\/\/weiyu2025.top\/index.php\/2026\/06\/16\/%e8%b4%aa%e5%90%83%e8%9b%87%ef%bc%88python%ef%bc%89\/","title":{"rendered":"\u8d2a\u5403\u86c7\uff08python\uff09"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4e07\u5e74\u4e0d\u66f4\u65b0\u7684\u6211\u53c8\u6765\u66f4\u65b0\u4e86\uff0c\u4eca\u5929\u95f2\u7684\u65e0\u804a\u7684\u65f6\u5019\u5199\u4e86\u4e00\u6bb5\u5c0f\u6e38\u620f\u2014\u2014\u8d2a\u5403\u86c7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f8\u4fe1\u5927\u5bb6\u4e00\u5b9a\u73a9\u8fc7\uff0c\u6ca1\u73a9\u8fc7\u7684\u4e5f\u4e0d\u8981\u7d27\uff0c\u56e0\u4e3a\u4e5f\u6709\u53ef\u80fd\u542c\u8fc7<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f\u6e38\u620f\u672c\u4f53\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame\nimport random\nfrom collections import deque\n\n#\u521d\u59cb\u5316\npygame.init()\nWIDTH, HEIGHT = 600, 400\nCELL_SUZE = 20\nCOLS = WIDTH \/\/ CELL_SUZE\nROWS = HEIGHT \/\/ CELL_SUZE\n\nscreen = pygame.display.set_mode((WIDTH, HEIGHT))\npygame.display.set_caption(\"SmartSnake - \u6309A\u5207\u6362AI\u6a21\u5f0f\")\nclock = pygame.time.Clock()\n\n#\u989c\u8272\nWHITE = (225, 225, 225)\nGREEN = (0, 200, 0)\nRED = (200, 0, 0)\nBLACK = (0, 0, 0)\nDARK_GREEN = (0, 150, 0)\n\nfont = pygame.font,SysFont(\"simhei,\", 24)\n\nclass SnakeGame:\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.snake = deque(&#91;(5, ROWS\/\/2)])\n        self.direction = (1, 0)\n        self.food = self.spawn_food()\n        self.score = 0\n        self.ai_mode = False\n\n    def spawn_food(self):\n        while True:\n            pos = (random.randint(0, COLS-1), random.randint(0, ROWS-1))\n            if pos not in self.snake:\n                return pos\n\n    def move(self, direction=None):\n        if direction is None:\n            direction = self.direction\n        head = self.snake&#91;0]\n        new_head = (head&#91;0] + direction&#91;0], head&#91;1] + direction&#91;1])\n\n        #\u649e\u5899\u68c0\u6d4b\n        if new_head&#91;0] &lt; 0 or new_head&#91;0] >= COLS or new_head&#91;1] &lt; 0 or new_head&#91;1] >= ROWS:\n            return False\n\n        #\u88c5\u81ea\u5df1\n        if new_head in self.snake:\n            return False\n\n        self.snake.appendleft(new_head)\n        if new_head == self.food:\n            self.score += 1\n            self.food = self.spawn_food()\n        else:\n            self.snake.pop()\n\n        self.direction = direction\n        return True\n\n    def ai_move(self):\n        head = self.snake&#91;0]\n        fx, fy = self.food\n\n        #\u7b80\u5355\u5bfb\u8def\uff1a\u4f18\u5148\u5f80\u98df\u7269\u65b9\u5411\u8d70\uff0c\u907f\u5f00\u969c\u788d\n        possible_dirs = &#91;]\n        for dx, dy in &#91;(1,0), (0,1), (-1,0), (0,-1)]:\n            nx, ny = head&#91;0]+dx, head&#91;1]+dy\n            if 0 &lt;= nx &lt; COLS and 0 &lt;= ny &lt; ROWS and (nx, ny) not in self.snake:\n                #\u8ba1\u7b97\u66fc\u54c8\u987f\u8ddd\u79bb\n                dist = abs(nx-fx) + abs(ny-fy)\n                possible_dirs.append((dist, (dx, dy)))\n\n            if not possible_dirs:\n                #\u65e0\u8def\u53ef\u8d70\uff0c\u968f\u4fbf\u52a8\u4e00\u4e0b\uff08\u4f1a\u6b7b\uff09\n                return self.move(self.direction)\n\n            #\u9009\u6700\u8fd1\u98df\u7269\u7684\u65b9\u5411\n            possible_dirs.sort(key=lambda x: x&#91;0])\n            best_dir = possible_dirs&#91;0]&#91;1]\n            return self.move(best_dir)\n\n    def draw(self):\n        screen.fill(BLACK)\n        #\u753b\u86c7\uff08\u543e\u7684\u8eab\u4f53\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff01\uff09ovo\n        for i, seg in enumerate(self.snake):\n            color = GREEN if i != 0 else DARK_GREEN\n            rect = pygame.Rect(seg&#91;0]*CELL_SUZE, seg&#91;1]*CELL_SIZE, CELL_SIZE, CELL_SIZE)\n            pygame.draw.rect(screen, RED, rect)\n            pygame.draw.rect(screen, WHITE, rect, 1)\n\n        #\u753b\u98df\u7269\uff08\u5c4e\uff09\n        rect = pygame.Rect(self.food&#91;0]*CELL_SIZE, self.food&#91;1]*CELL_SIZE, CELL_SIZE, CELL_SIZE)\n        pygame.draw.rect(screen, RED, rect)\n        pygame.draw.rect(screen, WHITE, rect, 1)\n\n        #\u663e\u793a\u5206\u6570\u548c\u6a21\u5f0f\n        mode_text = \"AI\" if self.ai_mode else \"Manual\"\n        text = font.render(f\"Score: {self.score} Mode: {mode_text}\", Ture, WHITE)\n        screen.blit(text, (10, 10))\n\ndef main():\n    game = SnakeGame()\n    running = True\n    paused = False\n\n    while running:\n        clock.tick(8)   #\u901f\u5ea6\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 event.key == pygame.K_a:\n                    game.ai_move = not game.ai_move\n                elif event.key == pygame.K_r:\n                    game.reset()\n                elif event.key == pygame.K_SPACE:\n                    paused = not paused\n\n                 #\u624b\u52a8\u63a7\u5236\n                if not game.ai_mode and not paused:\n                    if event.key == pygame.K_UP and game.direction != (0, 1):\n                        game.direction = (0, -1)\n                    elif event.key == pygame.K_DOWN and game.direction != (0, -1):\n                        game.direction = (0, 1)\n                    elif event.key == pygame.K_LEFT and game.direction != (1, 0):\n                        game.direction = (-1, 0)\n                    elif event.key == pygame.K_RIGHT and game.direction != (-1, 0):\n                        game.direction = (1, 0)\n\n        if not paused:\n            if game.ai_mode:\n                alive = game.ai_move()\n            else:\n                alive = game.move()\n\n            if not alive:\n                print(f\"Game Over! Score: {game.score}\")\n                game.reset()\n\n        game.draw()\n        pygame.display.flip()\n\n    pygame.quit()\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u590d\u5236\u7c98\u8d34\u5373\u53ef\uff0c\u8981\u6539\u53ef\u4ee5\u81ea\u5df1\u6765\u6539\u54e6~<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b27\u514b\uff0c\u672c\u7bc7\u6587\u7ae0\u7ed3\u675f~<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u5728\u8bc4\u8bba\u533a\u548c\u6211\u7684<a href=\"https:\/\/qm.qq.com\/q\/TAs4J1Wgi6\">QQ\u7fa4<\/a>\u6765\u8ba8\u8bba\u54e6~<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ps\uff1a\u9700\u8981pygame\u8f6f\u4ef6\u5305<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e07\u5e74\u4e0d\u66f4\u65b0\u7684\u6211\u53c8\u6765\u66f4\u65b0\u4e86\uff0c\u4eca\u5929\u95f2\u7684\u65e0\u804a\u7684\u65f6\u5019\u5199\u4e86\u4e00\u6bb5\u5c0f\u6e38\u620f\u2014\u2014\u8d2a\u5403\u86c7 \u76f8\u4fe1\u5927\u5bb6\u4e00\u5b9a\u73a9\u8fc7\uff0c\u6ca1\u73a9\u8fc7\u7684\u4e5f\u4e0d\u8981\u7d27\uff0c\u56e0\u4e3a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":54,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20,22],"class_list":["post-188","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn","tag-python","tag-22"],"_links":{"self":[{"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":1,"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":190,"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/posts\/188\/revisions\/190"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/media\/54"}],"wp:attachment":[{"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weiyu2025.top\/index.php\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}